-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPythonAWSBot.py
More file actions
77 lines (71 loc) · 2.42 KB
/
PythonAWSBot.py
File metadata and controls
77 lines (71 loc) · 2.42 KB
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
import json
import time
import cohere
import uuid
from urllib.parse import urlparse
from gql import Client, gql
from gql.transport.aiohttp import AIOHTTPTransport
from gql.transport.appsync_auth import AppSyncApiKeyAuthentication
def handler(event, context):
print(event)
params = json.loads(event["body"])
print(params)
background = f'This is a conversation between {params["user"]["name"]} and {params["bot"]["name"]}.\n\n'
prompt = background
userFirstName = params['user']['name'].split(' ')[0]
botFirstName = params['bot']['name'].split(' ')[0]
newInput = f'{userFirstName}: {params["content"]}\n{botFirstName}:'
stopString = f'{userFirstName}:'
messagesArray = []
for message in params['lastMessages']:
if message['userId']:
name = userFirstName
else:
name = botFirstName
messagesArray.append(f'{name}: {message["content"]}\n')
newInput = ''.join(messagesArray) + newInput
prompt = prompt + newInput
co = cohere.Client('INSERT-KEY')
response = co.generate(
prompt=prompt,
model='command-xlarge-nightly-2',
max_tokens=100,
temperature=0,
stop_sequences=[stopString],
)
content = response.generations[0].text.replace(stopString, "").strip()
url = 'https://INSERTURL.appsync-api.us-west-2.amazonaws.com/graphql'
api_key = 'INSERT-KEY'
host = str(urlparse(url).netloc)
auth = AppSyncApiKeyAuthentication(host=host, api_key=api_key)
transport = AIOHTTPTransport(url=url, auth=auth)
client = Client(transport=transport,fetch_schema_from_transport=False)
mutation = gql("""
mutation CreateMessage(
$input: CreateMessageInput!
$condition: ModelMessageConditionInput
) {
createMessage(input: $input, condition: $condition) {
id
content
createdAt
type
chatId
userId
botId
updatedAt
owner
}
}
""")
variables = {'input': {'id': str(uuid.uuid4()),
'type': 'Message',
'chatId': params['chatId'],
'botId': params['bot']['id'],
'content': content,
'createdAt': int(time.time())}}
client.execute(mutation, variable_values=json.dumps(variables))
return {
'statusCode': 200,
'body': 'Done'
}