-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathrun.py
56 lines (43 loc) · 1.61 KB
/
run.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
from flask import Flask, request, Response, stream_with_context, json
import requests
import sseclient
app = Flask(__name__)
from flask_cors import CORS
OPEN_AI_KEY = 'YOUR_API_KEY'
# handle cors
CORS(app)
@app.route('/')
def index():
return 'Hello World!'
@app.route('/api/prompt', methods=['GET', 'POST'])
def prompt():
if request.method == 'POST':
prompt = request.json['prompt']
def generate():
url = 'https://api.openai.com/v1/chat/completions'
headers = {
'content-type': 'application/json; charset=utf-8',
'Authorization': f"Bearer {OPEN_AI_KEY}"
}
data = {
'model': 'gpt-3.5-turbo',
'messages': [
{'role': 'system', 'content': 'You are an AI assistant that answers questions about anything.'},
{'role': 'user', 'content': prompt}
],
'temperature': 1,
'max_tokens': 1000,
'stream': True,
}
response = requests.post(url, headers=headers, data=json.dumps(data), stream=True)
client = sseclient.SSEClient(response)
for event in client.events():
if event.data != '[DONE]':
try:
text = json.loads(event.data)['choices'][0]['delta']['content']
yield(text)
except:
yield('')
return Response(stream_with_context(generate()))
if __name__ == '__main__':
app.run(port=4444, debug=True)