-
Notifications
You must be signed in to change notification settings - Fork 1
/
sidechat.py
741 lines (519 loc) · 21.8 KB
/
sidechat.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
# 5 dec key manager integration
# 24 oct dynamodb call
# 5 nov azure switch
# 5 nov+ base64 switch
### ### ### ### ### ### ### ### ### ### ### ###
### ### ### ### ### ### ### ### ### ### ### ###
### ### ### ### ### ### ### ### ### ### ### ###
# for message handling:
import json
import base64
# for python stunts:
from typing import List
# for dynamodb functionality:
import boto3
from botocore.exceptions import ClientError
from datetime import datetime # possibly depricated!!!!
import time
# for all the kids in africa:
from lyra_plugins import *
from lyra import *
plugin_system_messages = macro_instructions # compatibility wrapper
master_system_prompt = macro_instructions["<<disclaimer>>"]
### ### ### ### ### ### ### ### ### ### ### ###
### ### ### ### ### ### ### ### ### ### ### ###
### ### ### ### ### ### ### ### ### ### ### ###
AZURE = True
import openai
def get_secret(secret_name = "openaikey"):
region_name = "us-east-2"
# Create a Secrets Manager client
session = boto3.session.Session()
client = session.client(
service_name='secretsmanager',
region_name=region_name
)
try:
get_secret_value_response = client.get_secret_value(
SecretId=secret_name
)
except ClientError as e:
# For a list of exceptions thrown, see
# https://docs.aws.amazon.com/secretsmanager/latest/apireference/API_GetSecretValue.html
raise e
# Decrypts secret using the associated KMS key.
secret = get_secret_value_response['SecretString']
return secret
if not AZURE:
openai.api_key = get_secret()
else:
openai.api_type = "azure"
openai.api_base = "https://q-ai-east-2.openai.azure.com/"
openai.api_version = "2023-07-01-preview"
openai.api_key = get_secret("azureopenaikey2")
### ### ### ### ### ### ### ### ### ### ### ###
### ### ### ### ### ### ### ### ### ### ### ###
### ### ### ### ### ### ### ### ### ### ### ###
# Initialize DynamoDB client
dynamodb = boto3.resource('dynamodb')
table = dynamodb.Table('test_user_visits')
### ### ### ### ### ### ### ### ### ### ### ###
### ### ### ### ### ### ### ### ### ### ### ###
### ### ### ### ### ### ### ### ### ### ### ###
# FROM MAIN CHAT (12 dec 23 version):
##### ##### ##### ##### ##### ##### ##### ##### ##### #####
#
class ChatBot:
def __init__(self, system=""):# add in endpoint tag for conditional and model choice
self.system = system
self.messages = []
# getter setters for catching the last X message(s)
# stream handler code
if self.system:
self.messages.append({"role": "system", "content": system})
def __call__(self, message):
#print("calling chatbot object")
#print(f"chat history: {str(self.messages)}")
# alter function parameters and insert previous history here
self.messages.append({"role": "user", "content": message})
print("messages appended")
try:
result = self.execute()
except:
print("execution failed!!")
print("execution results:")
print(result)
self.messages.append({"role": "assistant", "content": result})
return result
def execute(self):
#print("inside execute -- attempting now")
try:
print(f"completion for: {self.messages}")
completion = openai.ChatCompletion.create(
model=global_model_choice, # this is changed if using azure endpoint
messages=self.messages,
#temperature=0.2,
temperature=1,
stream=False
)
return ("completion for: {self.messages}", completion)
#print(completion)
except openai.error.Timeout as e:
#Handle timeout error, e.g. retry or log
print(f"OpenAI API request timed out: {e}")
pass
except openai.error.APIError as e:
#Handle API error, e.g. retry or log
print(f"OpenAI API returned an API Error: {e}")
pass
except openai.error.APIConnectionError as e:
#Handle connection error, e.g. check network or log
print(f"OpenAI API request failed to connect: {e}")
pass
except openai.error.InvalidRequestError as e:
#Handle invalid request error, e.g. validate parameters or log
print(f"OpenAI API request was invalid: {e}")
pass
except openai.error.AuthenticationError as e:
#Handle authentication error, e.g. check credentials or log
print(f"OpenAI API request was not authorized: {e}")
pass
except openai.error.PermissionError as e:
#Handle permission error, e.g. check scope or log
print(f"OpenAI API request was not permitted: {e}")
pass
except openai.error.RateLimitError as e:
#Handle rate limit error, e.g. wait or log
print(f"OpenAI API request exceeded rate limit: {e}")
pass
except:
print("unknown error with OpenAI endpoint")
# Uncomment this to print out token usage each time, e.g.
# {"completion_tokens": 86, "prompt_tokens": 26, "total_tokens": 112}
# info: https://platform.openai.com/docs/guides/gpt/completions-response-format
print(completion.usage) # need to broadcast this or have it passed back as a part of the response below to track usage
#time.sleep(1) #deactivate or lambda hangs
return completion.choices[0].message.content
##### ##### ##### ##### ##### ##### ##### ##### ##### #####
#
class Message:
def __init__(self, id: int, version_id: int, text: str, timestamp: str, sender: str,
conversationTopic: str, tool: List[str], reference: List[str],
token_cost: int, rating: int) -> None:
self._id = id
self._version_id = version_id
self._text = text
self._timestamp = timestamp
self._sender = sender
self._conversationTopic = conversationTopic
# metadata section
self._tool = tool
self._reference = reference
self._token_cost = token_cost
self._rating = rating
@classmethod
def from_json(cls, json_string: str) -> 'Message':
data = json.loads(json_string)
metadata = data['metadata']
return cls(data['id'], data['version_id'], data['text'], data['timestamp'], data['sender'],
metadata['conversationTopic'], metadata['tool'], metadata['reference'],
metadata['token_cost'], metadata['rating'])
def to_json(self) -> str:
return json.dumps({
'id': self._id,
'version_id': self._version_id,
'text': self._text,
'timestamp': self._timestamp,
'sender': self._sender,
'metadata': {
'conversationTopic': self._conversationTopic,
'tool': self._tool,
'reference': self._reference,
'token_cost': self._token_cost,
'rating': self._rating
}
})
# Getter and Setter for tool with type annotations
@property
def tool(self) -> List[str]:
return self._tool
@tool.setter
def tool(self, tool: List[str]) -> None:
self._tool = tool
# Getter and Setter for reference with type annotations
@property
def reference(self) -> List[str]:
return self._reference
@reference.setter
def reference(self, reference: List[str]) -> None:
self._reference = reference
# Getter and Setter for id
@property
def id(self) -> int:
return self._id
@id.setter
def id(self, id: int) -> None:
self._id = id
# Getter and Setter for version_id
@property
def version_id(self) -> int:
return self._version_id
@version_id.setter
def version_id(self, version_id: int) -> None:
self._version_id = version_id
# Getter and Setter for text
@property
def text(self) -> str:
return self._text
@text.setter
def text(self, text: str) -> None:
self._text = text
# Getter and Setter for timestamp
@property
def timestamp(self) -> str:
return self._timestamp
@timestamp.setter
def timestamp(self, timestamp: str) -> None:
self._timestamp = timestamp
# Getter and Setter for sender
@property
def sender(self) -> str:
return self._sender
@sender.setter
def sender(self, sender: str) -> None:
self._sender = sender
# Getter and Setter for conversationTopic
@property
def conversationTopic(self) -> str:
return self._conversationTopic
@conversationTopic.setter
def conversationTopic(self, conversationTopic: str) -> None:
self._conversationTopic = conversationTopic
# Getter and Setter for token_cost
@property
def token_cost(self) -> int:
return self._token_cost
@token_cost.setter
def token_cost(self, token_cost: int) -> None:
self._token_cost = token_cost
# Getter and Setter for rating
@property
def rating(self) -> int:
return self._rating
@rating.setter
def rating(self, rating: int) -> None:
self._rating = rating
# end of Message class definition
##### ##### ##### ##### ##### ##### ##### ##### ##### #####
# this replaces the single function below
class UserVisitManager:
def __init__(self, table_name='test_user_visits'):
self.dynamodb = boto3.resource('dynamodb')
self.table = self.dynamodb.Table(table_name)
def update_user_visit(self, user):
"""
Update the last_visit timestamp and increment the visits count for a user.
If the user doesn't exist, create a new entry with initial state.
"""
# UPDATE: add token count and whatever other KPIs
response = self.table.update_item(
Key={'user_id': user},
UpdateExpression="SET last_visit = :last_visit ADD plugin_calls :increment",
#UpdateExpression="SET last_visit = if_not_exists(last_visit, :initial_date) ADD visits :increment",
ExpressionAttributeValues={
#':last_visit': datetime.utcnow().isoformat(),
':last_visit': int(time.time()),
':increment': 1
},
ReturnValues="ALL_NEW"
)
return response['Attributes']
##### ##### ##### ##### ##### ##### ##### ##### ##### #####
### ### ### ### ### ### ### ### ### ### ### ###
### ### ### ### ### ### ### ### ### ### ### ###
### ### ### ### ### ### ### ### ### ### ### ###
# deprecate this asap:
# function: dynamodb integration
def update_user_visit(user):
"""
Update the last_visit timestamp and increment the visits count for a user.
"""
response = table.update_item(
Key={'user_id': user},
UpdateExpression="SET last_visit = :last_visit ADD visits :increment",
ExpressionAttributeValues={
#':last_visit': datetime.utcnow().isoformat(),
':last_visit': int(time.time()),
':increment': 1
},
ReturnValues="ALL_NEW"
)
return response['Attributes']
### ### ### ### ### ### ### ### ### ### ### ###
### ### ### ### ### ### ### ### ### ### ### ###
### ### ### ### ### ### ### ### ### ### ### ###
def lambda_handler(event, context):
###############################################################
# LLM specifics
history_engram = ""
llm_response = ""
system_prompt = ""
final_token_count = 0
flag_gen_subject = False
flag_lockout_copywriter = False
ADMIN_MODE = False
VERBOSE_MODE = False
history_engram = ""
engine_choice = "gpt3516k"
# Billing Specifics
billable_action = True # changes if in admin
allowable_action = False
# DB Ops
request_context = event.get('requestContext', {})
authorizer = request_context.get('authorizer', {})
claims = authorizer.get('claims', {})
user_id = claims.get('principalId') # This will be the user's unique identifier (sub)
# Extract user name
user_name = claims.get('cognito:username')
manager = UserVisitManager() # this doesnt work unless the auth layer is applied to API gateway
# CHECK DB HERE
# Extract user group information
user_groups = claims.get('cognito:groups', [])
if user_groups == "ADMIN":
print("Admin user mode [LOGGING ENABLED]")
VERBOSE_MODE = True
ADMIN_MODE = True
billable_action = False # changes if in admin
allowable_action = True
# this is a temp fix:
def s_print(message):
# suppressive print / use to retrofit old stuff
if VERBOSE_MODE:
print(message)
else:
print("PROTECTED DATA -- OMIT FROM LOGS")
###############################################################
# Extract the request body from the event
body = event.get('body', '')
headers = event.get('headers', '')
request_context = event.get('requestContext', {})
decoded_body = base64.b64decode(body).decode('utf-8')
# Extract substring
#payload = decoded_body[10:-2]
user_message = Message.from_json(decoded_body[10:-2])
s_print("------------------------")
s_print(f"EVENT: {event}")
s_print(type(event))
s_print(f"EVENT: {event['body']}")
s_print(type(event['body']))
s_print(f"EVENT: {event['body']}")
s_print(type(event['body']))
s_print(f"PATH: {event['path']}")
s_print("------------------------")
s_print("------- DATA OPS -------")
#user = "U12347" #update to use dynamodb for userbase
# Update the DynamoDB item
#responseupdated_attributes = update_user_visit(user)
s_print("------- END DATA -------")
#tool = event.get('tool', '')
#body = event.get('body', '')
#print(f"BODY: {body}")
#print(type(body))
#tools = user_message.tool
#references = user_message.reference
tools = [base64.b64decode(t).decode('utf-8') for t in user_message.tool]
references = [base64.b64decode(r).decode('utf-8') for r in user_message.reference]
try:
new_conversation_topic = base64.b64decode(user_message.conversationTopic).decode('utf-8')
except:
new_conversation_topic = user_message.conversationTopic
tool_reference_pairs = [(t, r) for t, r in zip(tools, references)]
s_print(f"Tools and variables: {tool_reference_pairs}")
#tool = d_body["tool"]
#inputs = d_body['inputs']
#print(f"tool: {tool}, inputs: {inputs}") # inputs is a dict
#d_inputs = json.loads(inputs)
# Tool messages: verified / updated 15 Sep 2023
# deck_flow 👺
# deck_inconsistencies 👺
# action_title 👺
# formatting_inconsistencies👺
# ideate_layouts👺
# academic_citation_formatting👺
# executive_summary👺
# grammar_check👺
# feedback_sentiment👺
# feedback_clarity👺
# feedback_suggestions👺
# questions_to_ask👺
# academic_abstract👺
# academic_conclusion👺
# academic_ideate_discussion👺
# academic_insert_toc👺
# condense_text👺
# enrich_text👺
# transform_to_bullets👺
# transform_to_paragraphs👺
# rewrite_content👺
# fix_grammar👺
# text_transformation👺*
decoded_user_content = base64.b64decode(user_message.text).decode('utf-8')
user_content = f'Text input: """{decoded_user_content}"""'
s_print(f"user_content: {user_content}")
system_content = ""
for tool, reference in tool_reference_pairs:
s_print(f"attempting tool: {tool}" )
s_print(f"w/ variable: {reference}" )
if tool == "chatbot":
s_print(f"TOOL OVERRIDE: MODEL CHOICE: {reference}")
engine_choice = reference # needs a flag from the DB check
elif tool == "<<quantify_metric>>" or tool == "<<excel_quantify_metric>>":
print("TOOL OVERRIDE: switching to gpt4")
engine_choice = "gpt4"
elif tool == "chat_history":
s_print("Handling chat_history..." + reference)
history_engram = reference
s_print("<HISTORY NOT IMPLEMENTED YET>")
# this is inactive
elif tool in plugin_system_messages:
system_content += plugin_system_messages.get(tool)
try:
system_content += plugin_system_messages[tool].format(variable=reference)
except:
system_content += system_content + " " + reference
else:
system_content += tool + " " + reference
"""try:
system_content += plugin_system_messages.get(tool)
s_print(f"getting tool: {system_content}" )
try:
# all of this code is a huge work-around when the variable could just be AFTER the string / prompt. leaving here for now until the standard is formalised more
except Exception as e:
except Exception as e:
print("error getting tool")
return {
'statusCode': 501,
'message': "Tool value mangled or does not exist."
}"""
# this adds the disclaimer to the system prompt
system_content = system_content + master_system_prompt
s_print(f"Sending to LLM [SYSTEM]: {system_content}")
s_print(f"Sending to LLM [USER]: {user_content}")
# if keep manager.update_user_visit(user_name) BEFORE the LLM call so we charge even if the call fails -- but then we wont know the token count in advance!!!
# ACTIVE CHANGES TO MAKE:
# disable and replace with OOP version in order to use chat history:
completion = openai.ChatCompletion.create(
engine=engine_choice,
messages=[
{"role": "system", "content": system_content},
{"role": "user", "content": user_content}
],
temperature=0.2, # note this is not the same as chat or OOP
#max_tokens=350,
#top_p=0.95,
frequency_penalty=0,
presence_penalty=0,
stop=None)
s_print(f"Completion from LLM: {completion}")
llm_full_response = {
"id": user_message.id,
"version_id": user_message.version_id,
"text": completion.choices[0].message.content,
"timestamp": time.time(),#str(datetime.now()),#update to unix time stamp
"sender": "q",
#"conversationTopic": user_message.conversationTopic,#
"conversationTopic": new_conversation_topic,
"tool": user_message.tool,
"reference": user_message.reference,
"token_cost": completion.usage.total_tokens, #update this later
"rating": 0
}
manager.update_user_visit(user_name) # add model cjo
api_response = Message(**llm_full_response).to_json()
response_body = {
'message': completion["choices"][0]["message"]["content"]
}
return {
'statusCode': 200,
'headers': {
'Content-Type': 'application/json'
},
'body': json.dumps(response_body)
#'response': completion["choices"][0]["message"]["content"]
}
### ### ### ### ### ### ### ### ### ### ### ###
### ### ### ### ### ### ### ### ### ### ### ###
### ### ### ### ### ### ### ### ### ### ### ###
### ### ### ### ### ### ### ### ### ### ### ###
### ### ### ### ### ### ### ### ### ### ### ###
### ### ### ### ### ### ### ### ### ### ### ###
### ### ### ### ### ### ### ### ### ### ### ###
### ### ### ### ### ### ### ### ### ### ### ###
### ### ### ### ### ### ### ### ### ### ### ###
### ### ### ### ### ### ### ### ### ### ### ###
### ### ### ### ### ### ### ### ### ### ### ###
### ### ### ### ### ### ### ### ### ### ### ###
### ### ### ### ### ### ### ### ### ### ### ###
### ### ### ### ### ### ### ### ### ### ### ###
### ### ### ### ### ### ### ### ### ### ### ###
### ### ### ### ### ### ### ### ### ### ### ###
### ### ### ### ### ### ### ### ### ### ### ###
### ### ### ### ### ### ### ### ### ### ### ###
### ### ### ### ### ### ### ### ### ### ### ###
### ### ### ### ### ### ### ### ### ### ### ###
### ### ### ### ### ### ### ### ### ### ### ###
### ### ### ### ### ### ### ### ### ### ### ###
### ### ### ### ### ### ### ### ### ### ### ###
### ### ### ### ### ### ### ### ### ### ### ###
### ### ### ### ### ### ### ### ### ### ### ###
### ### ### ### ### ### ### ### ### ### ### ###
### ### ### ### ### ### ### ### ### ### ### ###
### ### ### ### ### ### ### ### ### ### ### ###
### ### ### ### ### ### ### ### ### ### ### ###
### ### ### ### ### ### ### ### ### ### ### ###
### ### ### ### ### ### ### ### ### ### ### ###
### ### ### ### ### ### ### ### ### ### ### ###
### ### ### ### ### ### ### ### ### ### ### ###
### ### ### ### ### ### ### ### ### ### ### ###
### ### ### ### ### ### ### ### ### ### ### ###
### ### ### ### ### ### ### ### ### ### ### ###