Spaces:
Running
Running
refactor
Browse files
app.py
CHANGED
@@ -2226,40 +2226,16 @@ def chat_with_opan_ai_assistant(password, youtube_id, user_data, thread_id, tras
|
|
2226 |
print("=====user_data=====")
|
2227 |
print(f"user_data: {user_data}")
|
2228 |
|
2229 |
-
|
2230 |
-
|
2231 |
-
|
2232 |
-
|
2233 |
-
|
2234 |
-
|
2235 |
-
|
2236 |
-
|
2237 |
-
answer = qa["answer"]
|
2238 |
-
if user_message == question and answer != "":
|
2239 |
-
print("=== in questions_answers_json==")
|
2240 |
-
print(f"question: {question}")
|
2241 |
-
print(f"answer: {answer}")
|
2242 |
-
print(f"thread_id: {thread_id}")
|
2243 |
-
# 更新聊天历史
|
2244 |
-
# user_message 前方加上【預設問題】
|
2245 |
-
user_message = f"【預設問題】{user_message}"
|
2246 |
-
new_chat_history = (user_message, answer)
|
2247 |
-
if chat_history is None:
|
2248 |
-
chat_history = [new_chat_history]
|
2249 |
-
else:
|
2250 |
-
chat_history.append(new_chat_history)
|
2251 |
-
|
2252 |
-
# 等待 3 秒
|
2253 |
-
time.sleep(3)
|
2254 |
-
send_btn_update, send_feedback_btn_update = update_send_and_feedback_buttons(chat_history, CHAT_LIMIT)
|
2255 |
-
|
2256 |
-
return "", chat_history, thread_id, send_btn_update, send_feedback_btn_update
|
2257 |
-
|
2258 |
|
2259 |
-
|
2260 |
-
if chat_history is not None and len(chat_history) > CHAT_LIMIT:
|
2261 |
-
error_msg = "此次對話超過上限(對話一輪10次)"
|
2262 |
-
raise gr.Error(error_msg)
|
2263 |
|
2264 |
try:
|
2265 |
assistant_id = OPEN_AI_ASSISTANT_ID_GPT4 #GPT 4 turbo
|
@@ -2338,16 +2314,12 @@ def chat_with_opan_ai_assistant(password, youtube_id, user_data, thread_id, tras
|
|
2338 |
else:
|
2339 |
response_text = "學習精靈有點累,請稍後再試!"
|
2340 |
|
2341 |
-
# 更新聊天历史
|
2342 |
-
new_chat_history = (user_message, response_text)
|
2343 |
-
if chat_history is None:
|
2344 |
-
chat_history = [new_chat_history]
|
2345 |
-
else:
|
2346 |
-
chat_history.append(new_chat_history)
|
2347 |
except Exception as e:
|
2348 |
print(f"Error: {e}")
|
2349 |
raise gr.Error(f"Error: {e}")
|
2350 |
|
|
|
|
|
2351 |
send_btn_update, send_feedback_btn_update = update_send_and_feedback_buttons(chat_history, CHAT_LIMIT)
|
2352 |
|
2353 |
|
@@ -2373,6 +2345,7 @@ def feedback_with_opan_ai_assistant(thread_id, chat_history):
|
|
2373 |
user_message = chat[0]
|
2374 |
assistant_message = chat[1]
|
2375 |
chat_history_conversation += f"User: {user_message}\nAssistant: {assistant_message}\n"
|
|
|
2376 |
feedback_request_message = "請依據以上的對話,總結我的「提問力」,並給予我是否有「問對問題」的回饋和建議"
|
2377 |
user_content = f"""conversation: {chat_history_conversation}
|
2378 |
{feedback_request_message}
|
@@ -2427,7 +2400,6 @@ def feedback_with_opan_ai_assistant(thread_id, chat_history):
|
|
2427 |
response_text = messages.data[0].content[0].text.value
|
2428 |
else:
|
2429 |
response_text = "學習精靈有點累,請稍後再試!"
|
2430 |
-
|
2431 |
except Exception as e:
|
2432 |
response_text = OPEN_AI_CLIENT.chat.completions.create(
|
2433 |
model="gpt-4-turbo",
|
@@ -2437,17 +2409,54 @@ def feedback_with_opan_ai_assistant(thread_id, chat_history):
|
|
2437 |
],
|
2438 |
max_tokens=4000,
|
2439 |
).choices[0].message.content.strip()
|
2440 |
-
|
2441 |
-
new_chat_history = (feedback_request_message, response_text)
|
2442 |
-
if chat_history is None:
|
2443 |
-
chat_history = [new_chat_history]
|
2444 |
-
else:
|
2445 |
-
chat_history.append(new_chat_history)
|
2446 |
|
|
|
2447 |
feedback_btn_update = gr.update(value="已回饋", interactive=False, variant="secondary")
|
2448 |
|
2449 |
return chat_history, feedback_btn_update
|
2450 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2451 |
def update_send_and_feedback_buttons(chat_history, chat_limit):
|
2452 |
# 计算发送次数
|
2453 |
send_count = len(chat_history) - 1
|
|
|
2226 |
print("=====user_data=====")
|
2227 |
print(f"user_data: {user_data}")
|
2228 |
|
2229 |
+
verify_message_length(user_message, max_length=1500)
|
2230 |
+
|
2231 |
+
is_questions_answers_exists, question_message, answer_message = check_questions_answers(user_message, questions_answers_json)
|
2232 |
+
if is_questions_answers_exists:
|
2233 |
+
chat_history = update_chat_history(question_message, answer_message, chat_history)
|
2234 |
+
send_btn_update, send_feedback_btn_update = update_send_and_feedback_buttons(chat_history, CHAT_LIMIT)
|
2235 |
+
time.sleep(3)
|
2236 |
+
return "", chat_history, thread_id, send_btn_update, send_feedback_btn_update
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2237 |
|
2238 |
+
verify_chat_limit(chat_history, CHAT_LIMIT)
|
|
|
|
|
|
|
2239 |
|
2240 |
try:
|
2241 |
assistant_id = OPEN_AI_ASSISTANT_ID_GPT4 #GPT 4 turbo
|
|
|
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 |
|
|
|
2345 |
user_message = chat[0]
|
2346 |
assistant_message = chat[1]
|
2347 |
chat_history_conversation += f"User: {user_message}\nAssistant: {assistant_message}\n"
|
2348 |
+
|
2349 |
feedback_request_message = "請依據以上的對話,總結我的「提問力」,並給予我是否有「問對問題」的回饋和建議"
|
2350 |
user_content = f"""conversation: {chat_history_conversation}
|
2351 |
{feedback_request_message}
|
|
|
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",
|
|
|
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 |
|
2416 |
return chat_history, feedback_btn_update
|
2417 |
|
2418 |
+
def verify_message_length(user_message, max_length=500):
|
2419 |
+
# 驗證用戶消息的長度
|
2420 |
+
if len(user_message) > max_length:
|
2421 |
+
error_msg = "你的訊息太長了,請縮短訊息長度至五百字以內"
|
2422 |
+
raise gr.Error(error_msg)
|
2423 |
+
|
2424 |
+
def check_questions_answers(user_message, questions_answers_json):
|
2425 |
+
"""檢查問答是否存在,並處理相關邏輯"""
|
2426 |
+
is_questions_answers_exists = False
|
2427 |
+
answer = ""
|
2428 |
+
# 解析問答數據
|
2429 |
+
if isinstance(questions_answers_json, str):
|
2430 |
+
qa_data = json.loads(questions_answers_json)
|
2431 |
+
else:
|
2432 |
+
qa_data = questions_answers_json
|
2433 |
+
for qa in qa_data:
|
2434 |
+
if user_message == qa["question"] and qa["answer"]:
|
2435 |
+
is_questions_answers_exists = True
|
2436 |
+
question_message = f"【預設問題】{user_message}"
|
2437 |
+
answer_message = qa["answer"]
|
2438 |
+
print("=== in questions_answers_json==")
|
2439 |
+
print(f"question: {qa['question']}")
|
2440 |
+
print(f"answer: {answer_message}")
|
2441 |
+
break # 匹配到答案後退出循環
|
2442 |
+
|
2443 |
+
return is_questions_answers_exists, question_message, answer_message
|
2444 |
+
|
2445 |
+
def verify_chat_limit(chat_history, chat_limit):
|
2446 |
+
if chat_history is not None and len(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)
|
2453 |
+
if chat_history is None:
|
2454 |
+
chat_history = [new_chat_history]
|
2455 |
+
else:
|
2456 |
+
chat_history.append(new_chat_history)
|
2457 |
+
|
2458 |
+
return chat_history
|
2459 |
+
|
2460 |
def update_send_and_feedback_buttons(chat_history, chat_limit):
|
2461 |
# 计算发送次数
|
2462 |
send_count = len(chat_history) - 1
|