forked from alexdredmon/phone-gpt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain_serverless.py
83 lines (74 loc) · 1.86 KB
/
main_serverless.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
import base64
import json
import openai
import re
import requests
import time
openai.api_key = ''
def chat(request):
start_x = time.time()
params = request.get_json(silent=True)
key = params.get('key')
if key != '':
return 'Unauthorized'
text = params['text']
history = params.get('history') or 'W10='
phone = params.get('phone') or False
max_execution = params.get('max_execution') or 4
raw = params.get('raw') or False
if not raw:
text = text + " Please keep your response brief."
history = json.loads(
base64.b64decode(
history
).decode('utf-8')
)
messages = history + [
{
'role': 'user',
'content': text,
},
]
params = {
'model': 'gpt-3.5-turbo',
'messages': messages,
}
streams = []
message = ""
for completion in openai.ChatCompletion.create(
model="gpt-3.5-turbo",
messages=messages,
stream=True,
):
content = completion['choices'][0]['delta'].get('content', '')
if content:
message += content
streams.append(completion)
elapsed = time.time() - start_x
if phone and elapsed > max_execution:
break
if phone:
trunc_match = re.search(r'(.*)[\.!\?]', message)
if trunc_match is not None:
message = trunc_match[0]
history = history + [
{
'role': 'user',
'content': text,
},
{
'role': 'assistant',
'content': message,
},
]
return {
'elapsed': time.time() - start_x,
'message': message,
'phone': phone,
'history': str(base64.b64encode(
bytes(
json.dumps(history),
'utf-8',
)
).decode('ascii')),
}