Spaces:
Running
Running
handle_conversation_by_open_ai_assistant
Browse files
app.py
CHANGED
|
@@ -2237,94 +2237,38 @@ def chat_with_opan_ai_assistant(password, youtube_id, user_data, thread_id, tras
|
|
| 2237 |
|
| 2238 |
verify_chat_limit(chat_history, CHAT_LIMIT)
|
| 2239 |
|
| 2240 |
-
|
| 2241 |
-
|
| 2242 |
-
|
| 2243 |
-
|
| 2244 |
-
|
| 2245 |
-
|
| 2246 |
-
|
| 2247 |
-
|
| 2248 |
-
|
| 2249 |
-
|
| 2250 |
-
|
| 2251 |
-
|
| 2252 |
-
|
| 2253 |
-
|
| 2254 |
-
|
| 2255 |
-
|
| 2256 |
-
|
| 2257 |
-
|
| 2258 |
-
|
| 2259 |
-
|
| 2260 |
-
|
| 2261 |
-
|
| 2262 |
-
|
| 2263 |
-
|
| 2264 |
-
|
| 2265 |
-
|
| 2266 |
-
|
| 2267 |
-
print("=== instructions ===")
|
| 2268 |
-
print(instructions)
|
| 2269 |
-
|
| 2270 |
-
# 创建线程
|
| 2271 |
-
if not thread_id:
|
| 2272 |
-
thread = client.beta.threads.create(
|
| 2273 |
-
|
| 2274 |
-
)
|
| 2275 |
-
thread_id = thread.id
|
| 2276 |
-
else:
|
| 2277 |
-
thread = client.beta.threads.retrieve(thread_id)
|
| 2278 |
-
|
| 2279 |
-
# add meta data to thread
|
| 2280 |
-
client.beta.threads.update(
|
| 2281 |
-
thread_id=thread_id,
|
| 2282 |
-
metadata={
|
| 2283 |
-
"youtube_id": youtube_id,
|
| 2284 |
-
"user_data": user_data,
|
| 2285 |
-
"content_subject": content_subject,
|
| 2286 |
-
"content_grade": content_grade,
|
| 2287 |
-
"socratic_mode": str(socratic_mode),
|
| 2288 |
-
"assistant_id": assistant_id,
|
| 2289 |
-
"is_streaming": "false",
|
| 2290 |
-
}
|
| 2291 |
-
)
|
| 2292 |
-
|
| 2293 |
-
# 向线程添加用户的消息
|
| 2294 |
-
client.beta.threads.messages.create(
|
| 2295 |
-
thread_id=thread.id,
|
| 2296 |
-
role="user",
|
| 2297 |
-
content=user_message + "/n 請嚴格遵循instructions,擔任一位蘇格拉底家教,絕對不要重複 user 的問句,請用引導的方式指引方向,請一定要用繁體中文回答 zh-TW,並用台灣人的禮貌口語表達,回答時不要特別說明這是台灣人的語氣,請在回答的最後標註【參考:(時):(分):(秒)】,(如果是反問學生,就只問一個問題,請幫助學生更好的理解資料,字數在100字以內,回答時如果講到數學專有名詞,請用數學符號代替文字(Latex 用 $ 字號 render, ex: $x^2$)"
|
| 2298 |
-
)
|
| 2299 |
-
|
| 2300 |
-
# 运行助手,生成响应
|
| 2301 |
-
run = client.beta.threads.runs.create(
|
| 2302 |
-
thread_id=thread.id,
|
| 2303 |
-
assistant_id=assistant_id,
|
| 2304 |
-
instructions=instructions,
|
| 2305 |
-
)
|
| 2306 |
-
|
| 2307 |
-
# 等待助手响应,设定最大等待时间为 30 秒
|
| 2308 |
-
run_status = poll_run_status(run.id, thread.id, timeout=30)
|
| 2309 |
-
# 获取助手的响应消息
|
| 2310 |
-
if run_status == "completed":
|
| 2311 |
-
messages = client.beta.threads.messages.list(thread_id=thread.id)
|
| 2312 |
-
# [MessageContentText(text=Text(annotations=[], value='您好!有什麼我可以幫助您的嗎?如果有任何問題或需要指導,請隨時告訴我!'), type='text')]
|
| 2313 |
-
response_text = messages.data[0].content[0].text.value
|
| 2314 |
-
else:
|
| 2315 |
-
response_text = "學習精靈有點累,請稍後再試!"
|
| 2316 |
-
|
| 2317 |
-
except Exception as e:
|
| 2318 |
-
print(f"Error: {e}")
|
| 2319 |
-
raise gr.Error(f"Error: {e}")
|
| 2320 |
-
|
| 2321 |
# 更新聊天历史
|
| 2322 |
chat_history = update_chat_history(user_message, response_text, chat_history)
|
| 2323 |
send_btn_update, send_feedback_btn_update = update_send_and_feedback_buttons(chat_history, CHAT_LIMIT)
|
| 2324 |
|
| 2325 |
-
|
| 2326 |
-
# 返回聊天历史和空字符串清空输入框
|
| 2327 |
-
return "", chat_history, thread.id, send_btn_update, send_feedback_btn_update
|
| 2328 |
|
| 2329 |
def feedback_with_opan_ai_assistant(thread_id, chat_history):
|
| 2330 |
# prompt: 請依據以上的對話(chat_history),總結我的「提問力」,並給予我是否有「問對問題」的回饋和建議
|
|
@@ -2363,53 +2307,11 @@ def feedback_with_opan_ai_assistant(thread_id, chat_history):
|
|
| 2363 |
"""
|
| 2364 |
|
| 2365 |
|
| 2366 |
-
|
| 2367 |
-
|
| 2368 |
-
|
| 2369 |
-
|
| 2370 |
-
|
| 2371 |
-
# 创建线程
|
| 2372 |
-
if not thread_id:
|
| 2373 |
-
thread = client.beta.threads.create(
|
| 2374 |
-
|
| 2375 |
-
)
|
| 2376 |
-
thread_id = thread.id
|
| 2377 |
-
else:
|
| 2378 |
-
thread = client.beta.threads.retrieve(thread_id)
|
| 2379 |
-
|
| 2380 |
-
# 向线程添加用户的消息
|
| 2381 |
-
client.beta.threads.messages.create(
|
| 2382 |
-
thread_id=thread.id,
|
| 2383 |
-
role="user",
|
| 2384 |
-
content=user_content
|
| 2385 |
-
)
|
| 2386 |
-
|
| 2387 |
-
# 运行助手,生成响应
|
| 2388 |
-
run = client.beta.threads.runs.create(
|
| 2389 |
-
thread_id=thread.id,
|
| 2390 |
-
assistant_id=assistant_id,
|
| 2391 |
-
instructions=system_content,
|
| 2392 |
-
)
|
| 2393 |
-
|
| 2394 |
-
# 等待助手响应,设定最大等待时间为 30 秒
|
| 2395 |
-
run_status = poll_run_status(run.id, thread.id, timeout=30)
|
| 2396 |
-
# 获取助手的响应消息
|
| 2397 |
-
if run_status == "completed":
|
| 2398 |
-
messages = client.beta.threads.messages.list(thread_id=thread.id)
|
| 2399 |
-
# [MessageContentText(text=Text(annotations=[], value='您好!有什麼我可以幫助您的嗎?如果有任何問題或需要指導,請隨時告訴我!'), type='text')]
|
| 2400 |
-
response_text = messages.data[0].content[0].text.value
|
| 2401 |
-
else:
|
| 2402 |
-
response_text = "學習精靈有點累,請稍後再試!"
|
| 2403 |
-
except Exception as e:
|
| 2404 |
-
response_text = OPEN_AI_CLIENT.chat.completions.create(
|
| 2405 |
-
model="gpt-4-turbo",
|
| 2406 |
-
messages=[
|
| 2407 |
-
{"role": "system", "content": system_content},
|
| 2408 |
-
{"role": "user", "content": user_content}
|
| 2409 |
-
],
|
| 2410 |
-
max_tokens=4000,
|
| 2411 |
-
).choices[0].message.content.strip()
|
| 2412 |
-
|
| 2413 |
chat_history = update_chat_history(feedback_request_message, response_text, chat_history)
|
| 2414 |
feedback_btn_update = gr.update(value="已回饋", interactive=False, variant="secondary")
|
| 2415 |
|
|
@@ -2447,6 +2349,60 @@ def verify_chat_limit(chat_history, chat_limit):
|
|
| 2447 |
error_msg = "此次對話超過上限(對話一輪10次)"
|
| 2448 |
raise gr.Error(error_msg)
|
| 2449 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2450 |
def update_chat_history(user_message, response, chat_history):
|
| 2451 |
# 更新聊天歷史的邏輯
|
| 2452 |
new_chat_history = (user_message, response)
|
|
|
|
| 2237 |
|
| 2238 |
verify_chat_limit(chat_history, CHAT_LIMIT)
|
| 2239 |
|
| 2240 |
+
client = OPEN_AI_CLIENT
|
| 2241 |
+
assistant_id = OPEN_AI_ASSISTANT_ID_GPT4 #GPT 4 turbo
|
| 2242 |
+
if isinstance(key_moments, str):
|
| 2243 |
+
key_moments_json = json.loads(key_moments)
|
| 2244 |
+
else:
|
| 2245 |
+
key_moments_json = key_moments
|
| 2246 |
+
# key_moments_json remove images
|
| 2247 |
+
for moment in key_moments_json:
|
| 2248 |
+
moment.pop('images', None)
|
| 2249 |
+
moment.pop('end', None)
|
| 2250 |
+
moment.pop('transcript', None)
|
| 2251 |
+
key_moments_text = json.dumps(key_moments_json, ensure_ascii=False)
|
| 2252 |
+
instructions = get_instructions(content_subject, content_grade, key_moments_text)
|
| 2253 |
+
print(f"=== instructions:{instructions} ===")
|
| 2254 |
+
metadata={
|
| 2255 |
+
"video_id": youtube_id,
|
| 2256 |
+
"user_data": user_data,
|
| 2257 |
+
"content_subject": content_subject,
|
| 2258 |
+
"content_grade": content_grade,
|
| 2259 |
+
"socratic_mode": str(socratic_mode),
|
| 2260 |
+
"assistant_id": assistant_id,
|
| 2261 |
+
"is_streaming": "false",
|
| 2262 |
+
}
|
| 2263 |
+
user_message_note = "/n 請嚴格遵循instructions,擔任一位蘇格拉底家教,絕對不要重複 user 的問句,請用引導的方式指引方向,請一定要用繁體中文回答 zh-TW,並用台灣人的禮貌口語表達,回答時不要特別說明這是台灣人的語氣,請在回答的最後標註【參考:(時):(分):(秒)】,(如果是反問學生,就只問一個問題,請幫助學生更好的理解資料,字數在100字以內,回答時如果講到數學專有名詞,請用數學符號代替文字(Latex 用 $ 字號 render, ex: $x^2$)"
|
| 2264 |
+
user_content = user_message + user_message_note
|
| 2265 |
+
response_text, thread_id = handle_conversation_by_open_ai_assistant(client, user_content, instructions, assistant_id, thread_id, metadata, fallback=True)
|
| 2266 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2267 |
# 更新聊天历史
|
| 2268 |
chat_history = update_chat_history(user_message, response_text, chat_history)
|
| 2269 |
send_btn_update, send_feedback_btn_update = update_send_and_feedback_buttons(chat_history, CHAT_LIMIT)
|
| 2270 |
|
| 2271 |
+
return "", chat_history, thread_id, send_btn_update, send_feedback_btn_update
|
|
|
|
|
|
|
| 2272 |
|
| 2273 |
def feedback_with_opan_ai_assistant(thread_id, chat_history):
|
| 2274 |
# prompt: 請依據以上的對話(chat_history),總結我的「提問力」,並給予我是否有「問對問題」的回饋和建議
|
|
|
|
| 2307 |
"""
|
| 2308 |
|
| 2309 |
|
| 2310 |
+
client = OPEN_AI_CLIENT
|
| 2311 |
+
assistant_id = OPEN_AI_ASSISTANT_ID_GPT4 #GPT 4 turbo
|
| 2312 |
+
# assistant_id = OPEN_AI_ASSISTANT_ID_GPT3 #GPT 3.5 turbo
|
| 2313 |
+
instructions = system_content
|
| 2314 |
+
response_text, thread_id = handle_conversation_by_open_ai_assistant(client, user_content, instructions, assistant_id, thread_id, metadata=None, fallback=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2315 |
chat_history = update_chat_history(feedback_request_message, response_text, chat_history)
|
| 2316 |
feedback_btn_update = gr.update(value="已回饋", interactive=False, variant="secondary")
|
| 2317 |
|
|
|
|
| 2349 |
error_msg = "此次對話超過上限(對話一輪10次)"
|
| 2350 |
raise gr.Error(error_msg)
|
| 2351 |
|
| 2352 |
+
def handle_conversation_by_open_ai_assistant(client, user_message, instructions, assistant_id, thread_id=None, metadata=None, fallback=False):
|
| 2353 |
+
"""
|
| 2354 |
+
Handles the creation and management of a conversation thread.
|
| 2355 |
+
:param client: The OpenAI client object.
|
| 2356 |
+
:param thread_id: The existing thread ID, if any.
|
| 2357 |
+
:param user_message: The message from the user.
|
| 2358 |
+
:param instructions: System instructions for the assistant.
|
| 2359 |
+
:param assistant_id: ID of the assistant to use.
|
| 2360 |
+
:param metadata: Additional metadata to add to the thread.
|
| 2361 |
+
:param fallback: Whether to use a fallback method in case of failure.
|
| 2362 |
+
:return: A string with the response text or an error message.
|
| 2363 |
+
"""
|
| 2364 |
+
try:
|
| 2365 |
+
if not thread_id:
|
| 2366 |
+
thread = client.beta.threads.create()
|
| 2367 |
+
thread_id = thread.id
|
| 2368 |
+
else:
|
| 2369 |
+
thread = client.beta.threads.retrieve(thread_id)
|
| 2370 |
+
|
| 2371 |
+
if metadata:
|
| 2372 |
+
client.beta.threads.update(thread_id=thread_id, metadata=metadata)
|
| 2373 |
+
|
| 2374 |
+
# Send the user message to the thread
|
| 2375 |
+
client.beta.threads.messages.create(thread_id=thread.id, role="user", content=user_message)
|
| 2376 |
+
|
| 2377 |
+
# Run the assistant
|
| 2378 |
+
run = client.beta.threads.runs.create(thread_id=thread.id, assistant_id=assistant_id, instructions=instructions)
|
| 2379 |
+
|
| 2380 |
+
# Wait for the response
|
| 2381 |
+
run_status = poll_run_status(run.id, thread.id, timeout=30)
|
| 2382 |
+
|
| 2383 |
+
if run_status == "completed":
|
| 2384 |
+
messages = client.beta.threads.messages.list(thread_id=thread.id)
|
| 2385 |
+
response_text = messages.data[0].content[0].text.value
|
| 2386 |
+
else:
|
| 2387 |
+
response_text = "學習精靈有點累,請稍後再試!"
|
| 2388 |
+
|
| 2389 |
+
except Exception as e:
|
| 2390 |
+
if fallback:
|
| 2391 |
+
response = client.chat.completions.create(
|
| 2392 |
+
model="gpt-4-turbo",
|
| 2393 |
+
messages=[
|
| 2394 |
+
{"role": "system", "content": instructions},
|
| 2395 |
+
{"role": "user", "content": user_message}
|
| 2396 |
+
],
|
| 2397 |
+
max_tokens=4000,
|
| 2398 |
+
)
|
| 2399 |
+
response_text = response.choices[0].message.content.strip()
|
| 2400 |
+
else:
|
| 2401 |
+
print(f"Error: {e}")
|
| 2402 |
+
raise gr.Error(f"Error: {e}")
|
| 2403 |
+
|
| 2404 |
+
return response_text, thread_id
|
| 2405 |
+
|
| 2406 |
def update_chat_history(user_message, response, chat_history):
|
| 2407 |
# 更新聊天歷史的邏輯
|
| 2408 |
new_chat_history = (user_message, response)
|