-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathcallGPT.py
36 lines (26 loc) · 813 Bytes
/
callGPT.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
from flask import Flask, request
import json
import requests
import time
app = Flask(__name__)
@app.route('/chatgpt', methods=['POST'])
def callCHATGPT():
headers = {
"Content-Type": "application/json",
"Authorization": "Bearer {key}"
}
data = request.json
message = data["message"]
data = {
"model": "gpt-3.5-turbo",
"messages": [{ "role": "user", "content": message }]
}
url = "https://api.openai.com/v1/chat/completions"
response = requests.post(url, headers=headers, data=json.dumps(data))
if response.status_code == 200:
json_data = json.loads(response.text)
response = json_data["choices"][0]["message"]["content"]
print(response)
return response
if __name__ == '__main__':
app.run(port=8080)