-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.py
123 lines (106 loc) · 4.27 KB
/
server.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
from flask import Flask, jsonify, request
from http import HTTPStatus
from dotenv import load_dotenv
from openai import OpenAI
import os
app = Flask(__name__)
load_dotenv()
key = os.getenv("OPENAI_KEY")
print(key)
client = OpenAI(api_key=key)
assistantId = os.getenv("ASSISTANT_ID")
@app.route('/api/stt/threads', methods=['POST'])
def createSttThread():
print("createSttThread 호출")
data = request.json
if not data or 'situation' not in data:
return jsonify({"error": "Invalid request data"}), HTTPStatus.BAD_REQUEST
try:
thread = client.beta.threads.create()
message = client.beta.threads.messages.create(
thread_id=thread.id,
role="user",
content=f"서비스 : '{data['situation']}'"
)
run = client.beta.threads.runs.create(
thread_id=thread.id,
assistant_id=assistantId
)
run = client.beta.threads.runs.retrieve(thread_id=thread.id, run_id=run.id)
while run.status == 'in_progress':
run = client.beta.threads.runs.retrieve(thread_id=thread.id, run_id=run.id)
if run.status == 'completed':
messages = client.beta.threads.messages.list(
thread_id=thread.id
)
bf = messages.data[0].content[0].text.value
print(bf)
return jsonify({"threadId": thread.id, "botFirstMessage": bf}), HTTPStatus.CREATED
else:
print(run.status)
return jsonify({"error": f"Run failed with status: {run.status}"}), HTTPStatus.INTERNAL_SERVER_ERROR
except Exception as e:
print(f"Error: {str(e)}")
return jsonify({"error": str(e)}), HTTPStatus.INTERNAL_SERVER_ERROR
@app.route('/api/stt/threads/<threadId>', methods=['POST'])
def chatSttThread(threadId):
data = request.json
if not data or 'userMessage' not in data:
return jsonify({"error": "Invalid request data"}), HTTPStatus.BAD_REQUEST
try:
message = client.beta.threads.messages.create(
thread_id=threadId,
role="user",
content=data['userMessage']
)
run = client.beta.threads.runs.create(
thread_id=threadId,
assistant_id=assistantId
)
run = client.beta.threads.runs.retrieve(thread_id=threadId, run_id=run.id)
while run.status == 'in_progress':
run = client.beta.threads.runs.retrieve(thread_id=threadId, run_id=run.id)
if run.status == 'completed':
messages = client.beta.threads.messages.list(
thread_id=threadId
)
bm = messages.data[0].content[0].text.value
print(bm)
return jsonify({"botMessage": bm}), HTTPStatus.OK
else:
print(run.status)
return jsonify({"error": f"Run failed with status: {run.status}"}), HTTPStatus.INTERNAL_SERVER_ERROR
except Exception as e:
print(f"Error: {str(e)}")
return jsonify({"error": str(e)}), HTTPStatus.INTERNAL_SERVER_ERROR
@app.route('/api/stt/threads/<threadId>', methods=['DELETE'])
def finishSttThread(threadId):
try:
message = client.beta.threads.messages.create(
thread_id=threadId,
role="user",
content="서비스 : '상황 학습 종료, 피드백 요청'"
)
run = client.beta.threads.runs.create(
thread_id=threadId,
assistant_id=assistantId
)
run = client.beta.threads.runs.retrieve(thread_id=threadId, run_id=run.id)
while run.status == 'in_progress':
run = client.beta.threads.runs.retrieve(thread_id=threadId, run_id=run.id)
if run.status == 'completed':
messages = client.beta.threads.messages.list(
thread_id=threadId
)
fb = messages.data[0].content[0].text.value
print(fb)
return jsonify({"feedBack": fb}), HTTPStatus.OK
else:
print(run.status)
return jsonify({"error": f"Run failed with status: {run.status}"}), HTTPStatus.INTERNAL_SERVER_ERROR
except Exception as e:
print(f"Error: {str(e)}")
return jsonify({"error": str(e)}), HTTPStatus.INTERNAL_SERVER_ERROR
if __name__ == '__main__':
app.debug = True
app.run(host='0.0.0.0')