Spaces:
Running
Running
feedback_with_opan_ai_assistant
Browse files
app.py
CHANGED
@@ -83,7 +83,7 @@ else:
|
|
83 |
|
84 |
TRANSCRIPTS = []
|
85 |
CURRENT_INDEX = 0
|
86 |
-
CHAT_LIMIT =
|
87 |
|
88 |
# CLIENTS CONFIG
|
89 |
GROQ_CLIENT = Groq(api_key=GROQ_API_KEY)
|
@@ -2220,7 +2220,7 @@ def chat_with_ai(ai_name, password, video_id, user_data, trascript_state, key_mo
|
|
2220 |
print(f"Error: {e}")
|
2221 |
return "请求失败,请稍后再试!", chat_history
|
2222 |
|
2223 |
-
def chat_with_opan_ai_assistant(password, youtube_id, user_data, thread_id, trascript_state, key_moments, user_message, chat_history, content_subject, content_grade, questions_answers_json, send_btn, socratic_mode=False):
|
2224 |
verify_password(password)
|
2225 |
|
2226 |
print("=====user_data=====")
|
@@ -2241,6 +2241,8 @@ def chat_with_opan_ai_assistant(password, youtube_id, user_data, thread_id, tras
|
|
2241 |
print(f"answer: {answer}")
|
2242 |
print(f"thread_id: {thread_id}")
|
2243 |
# 更新聊天历史
|
|
|
|
|
2244 |
new_chat_history = (user_message, answer)
|
2245 |
if chat_history is None:
|
2246 |
chat_history = [new_chat_history]
|
@@ -2253,11 +2255,13 @@ def chat_with_opan_ai_assistant(password, youtube_id, user_data, thread_id, tras
|
|
2253 |
if len(chat_history) > CHAT_LIMIT:
|
2254 |
send_btn_value = f"對話已達上限 ({send_count}/{CHAT_LIMIT})"
|
2255 |
send_btn = gr.update(value=send_btn_value, interactive=False)
|
|
|
2256 |
else:
|
2257 |
send_btn_value = f"發送 ({send_count}/{CHAT_LIMIT})"
|
2258 |
send_btn = gr.update(value=send_btn_value, interactive=True)
|
|
|
2259 |
|
2260 |
-
return "", chat_history, thread_id, send_btn
|
2261 |
|
2262 |
|
2263 |
# 如果 chat_history 超過 10 則訊息,直接 return "對話超過上限"
|
@@ -2356,13 +2360,109 @@ def chat_with_opan_ai_assistant(password, youtube_id, user_data, thread_id, tras
|
|
2356 |
if len(chat_history) > CHAT_LIMIT:
|
2357 |
send_btn_value = f"對話已達上限 ({send_count}/{CHAT_LIMIT})"
|
2358 |
send_btn = gr.update(value=send_btn_value, interactive=False)
|
|
|
2359 |
else:
|
2360 |
send_btn_value = f"發送 ({send_count}/{CHAT_LIMIT})"
|
2361 |
send_btn = gr.update(value=send_btn_value, interactive=True)
|
|
|
2362 |
|
2363 |
|
2364 |
# 返回聊天历史和空字符串清空输入框
|
2365 |
-
return "", chat_history, thread.id, send_btn
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2366 |
|
2367 |
def process_open_ai_audio_to_chatbot(password, audio_url):
|
2368 |
verify_password(password)
|
@@ -2816,6 +2916,7 @@ with gr.Blocks(theme=gr.themes.Base(primary_hue=gr.themes.colors.orange, seconda
|
|
2816 |
with gr.Row():
|
2817 |
msg = gr.Textbox(label="訊息",scale=3)
|
2818 |
send_button = gr.Button("送出", variant="primary", scale=1)
|
|
|
2819 |
with gr.Row("飛特音速") as chatbot_open_ai_streaming:
|
2820 |
with gr.Column():
|
2821 |
streaming_chat_greeting = """
|
@@ -3108,8 +3209,14 @@ with gr.Blocks(theme=gr.themes.Base(primary_hue=gr.themes.colors.orange, seconda
|
|
3108 |
# OPENAI ASSISTANT CHATBOT 模式
|
3109 |
send_button.click(
|
3110 |
chat_with_opan_ai_assistant,
|
3111 |
-
inputs=[password, video_id, user_data, thread_id, trascript_state, key_moments, msg, chatbot, content_subject, content_grade, questions_answers_json, send_button, socratic_mode_btn],
|
3112 |
-
outputs=[msg, chatbot, thread_id, send_button],
|
|
|
|
|
|
|
|
|
|
|
|
|
3113 |
scroll_to_output=True
|
3114 |
)
|
3115 |
openai_chatbot_audio_input.change(
|
@@ -3127,8 +3234,8 @@ with gr.Blocks(theme=gr.themes.Base(primary_hue=gr.themes.colors.orange, seconda
|
|
3127 |
)
|
3128 |
question_buttons = [btn_1, btn_2, btn_3]
|
3129 |
for question_btn in question_buttons:
|
3130 |
-
inputs_list = [password, video_id, user_data, thread_id, trascript_state, key_moments, question_btn, chatbot, content_subject, content_grade, questions_answers_json, send_button, socratic_mode_btn]
|
3131 |
-
outputs_list = [msg, chatbot, thread_id, send_button]
|
3132 |
setup_question_button_click(question_btn, inputs_list, outputs_list, chat_with_opan_ai_assistant)
|
3133 |
|
3134 |
# 為生成問題按鈕設定特殊的點擊事件
|
@@ -3145,12 +3252,12 @@ with gr.Blocks(theme=gr.themes.Base(primary_hue=gr.themes.colors.orange, seconda
|
|
3145 |
outputs=[ai_msg, ai_chatbot, ai_send_button],
|
3146 |
scroll_to_output=True
|
3147 |
)
|
3148 |
-
# 其他精靈 ai_chatbot
|
3149 |
-
|
3150 |
-
for
|
3151 |
-
inputs_list = [ai_name, password, video_id, user_data, trascript_state, key_moments,
|
3152 |
outputs_list = [ai_msg, ai_chatbot, ai_send_button]
|
3153 |
-
setup_question_button_click(
|
3154 |
|
3155 |
# file_upload.change(process_file, inputs=file_upload, outputs=df_string_output)
|
3156 |
# file_upload.change(process_file, inputs=file_upload, outputs=[btn_1, btn_2, btn_3, df_summarise, df_string_output])
|
|
|
83 |
|
84 |
TRANSCRIPTS = []
|
85 |
CURRENT_INDEX = 0
|
86 |
+
CHAT_LIMIT = 3
|
87 |
|
88 |
# CLIENTS CONFIG
|
89 |
GROQ_CLIENT = Groq(api_key=GROQ_API_KEY)
|
|
|
2220 |
print(f"Error: {e}")
|
2221 |
return "请求失败,请稍后再试!", chat_history
|
2222 |
|
2223 |
+
def chat_with_opan_ai_assistant(password, youtube_id, user_data, thread_id, trascript_state, key_moments, user_message, chat_history, content_subject, content_grade, questions_answers_json, send_btn, send_feedback_btn, socratic_mode=False):
|
2224 |
verify_password(password)
|
2225 |
|
2226 |
print("=====user_data=====")
|
|
|
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]
|
|
|
2255 |
if len(chat_history) > CHAT_LIMIT:
|
2256 |
send_btn_value = f"對話已達上限 ({send_count}/{CHAT_LIMIT})"
|
2257 |
send_btn = gr.update(value=send_btn_value, interactive=False)
|
2258 |
+
send_feedback_btn_update = gr.update(visible=True)
|
2259 |
else:
|
2260 |
send_btn_value = f"發送 ({send_count}/{CHAT_LIMIT})"
|
2261 |
send_btn = gr.update(value=send_btn_value, interactive=True)
|
2262 |
+
send_feedback_btn_update = send_feedback_btn
|
2263 |
|
2264 |
+
return "", chat_history, thread_id, send_btn, send_feedback_btn_update
|
2265 |
|
2266 |
|
2267 |
# 如果 chat_history 超過 10 則訊息,直接 return "對話超過上限"
|
|
|
2360 |
if len(chat_history) > CHAT_LIMIT:
|
2361 |
send_btn_value = f"對話已達上限 ({send_count}/{CHAT_LIMIT})"
|
2362 |
send_btn = gr.update(value=send_btn_value, interactive=False)
|
2363 |
+
send_feedback_btn_update = gr.update(visible=True)
|
2364 |
else:
|
2365 |
send_btn_value = f"發送 ({send_count}/{CHAT_LIMIT})"
|
2366 |
send_btn = gr.update(value=send_btn_value, interactive=True)
|
2367 |
+
send_feedback_btn_update = send_feedback_btn
|
2368 |
|
2369 |
|
2370 |
# 返回聊天历史和空字符串清空输入框
|
2371 |
+
return "", chat_history, thread.id, send_btn, send_feedback_btn_update
|
2372 |
+
|
2373 |
+
def feedback_with_opan_ai_assistant(thread_id, chat_history):
|
2374 |
+
# prompt: 請依據以上的對話(chat_history),總結我的「提問力」,並給予我是否有「問對問題」的回饋和建議
|
2375 |
+
system_content = """
|
2376 |
+
你是一個擅長引導問答素養的老師,user 為學生的提問跟回答,請精讀對話過程,針對 user 給予回饋就好,根據以下 Rule:
|
2377 |
+
- 請使用繁體中文 zh-TW 總結 user 的提問力,並給予是否有問對問題的回饋和建議
|
2378 |
+
- 不採計【預設提問】的問題,如果 user 的提問都來自【預設提問】,表達用戶善於使用系統,請給予回饋並鼓勵 user 親自提問更具體的問題
|
2379 |
+
- 如果用戶提問都相當簡短,甚至就是一個字,請給予回饋並建議 user 提問更具體的問題
|
2380 |
+
- 如果用戶提問內容只有符號,像是?、!、...等,請給予回饋並建議 user 提問更具體的問題
|
2381 |
+
- 如果用戶提問內容有色情、暴力、仇恨、不當言論等,請給予嚴厲的回饋並建議 user 提問更具體的問題
|
2382 |
+
- 並用第二人稱「你」來代表 user
|
2383 |
+
- 請禮貌,並給予鼓勵
|
2384 |
+
"""
|
2385 |
+
chat_history_conversation = ""
|
2386 |
+
# 標註 user and assistant as string
|
2387 |
+
# chat_history 第一組不採計
|
2388 |
+
for chat in chat_history[1:]:
|
2389 |
+
user_message = chat[0]
|
2390 |
+
assistant_message = chat[1]
|
2391 |
+
chat_history_conversation += f"User: {user_message}\nAssistant: {assistant_message}\n"
|
2392 |
+
feedback_request_message = "請依據以上的對話,總結我的「提問力」,並給予我是否有「問對問題」的回饋和建議"
|
2393 |
+
user_content = f"""conversation: {chat_history_conversation}
|
2394 |
+
{feedback_request_message}
|
2395 |
+
最後根據提問力表現,給予提問建議、提問表現,並用 emoji 來表示評分:
|
2396 |
+
🟢:(表現很好的回饋,給予正向肯定)
|
2397 |
+
🟡:(還可以加油的的回饋,給予明確的建議)
|
2398 |
+
🔴:(非常不懂提問的回饋,給予鼓勵並給出明確示範)
|
2399 |
+
|
2400 |
+
example:
|
2401 |
+
���一方面,你表達「我不想學了」這個情感,其實也是一種重要的反饋。這顯示你可能感到挫折或疲倦。在這種情況下,表達出你的感受是好的,但如果能具體說明是什麼讓你感到這樣,或是有什麼具體的學習障礙,會更有助於找到解決方案。
|
2402 |
+
給予你的建議是,嘗試在提問時更明確一些,這樣不僅能幫助你獲得更好的學習支持,也能提高你的問題解決技巧。
|
2403 |
+
......
|
2404 |
+
提問建議:在提問時,試著具體並清晰地表達你的需求和疑惑,這樣能更有效地得到幫助。
|
2405 |
+
提問表現:【🟡】加油,持續練習,你的提問力會越來越好!
|
2406 |
+
"""
|
2407 |
+
|
2408 |
+
|
2409 |
+
try:
|
2410 |
+
client = OPEN_AI_CLIENT
|
2411 |
+
assistant_id = OPEN_AI_ASSISTANT_ID_GPT4 #GPT 4 turbo
|
2412 |
+
# assistant_id = OPEN_AI_ASSISTANT_ID_GPT3 #GPT 3.5 turbo
|
2413 |
+
|
2414 |
+
# 创建线程
|
2415 |
+
if not thread_id:
|
2416 |
+
thread = client.beta.threads.create(
|
2417 |
+
|
2418 |
+
)
|
2419 |
+
thread_id = thread.id
|
2420 |
+
else:
|
2421 |
+
thread = client.beta.threads.retrieve(thread_id)
|
2422 |
+
|
2423 |
+
# 向线程添加用户的消息
|
2424 |
+
client.beta.threads.messages.create(
|
2425 |
+
thread_id=thread.id,
|
2426 |
+
role="user",
|
2427 |
+
content=user_content
|
2428 |
+
)
|
2429 |
+
|
2430 |
+
# 运行助手,生成响应
|
2431 |
+
run = client.beta.threads.runs.create(
|
2432 |
+
thread_id=thread.id,
|
2433 |
+
assistant_id=assistant_id,
|
2434 |
+
instructions=system_content,
|
2435 |
+
)
|
2436 |
+
|
2437 |
+
# 等待助手响应,设定最大等待时间为 30 秒
|
2438 |
+
run_status = poll_run_status(run.id, thread.id, timeout=30)
|
2439 |
+
# 获取助手的响应消息
|
2440 |
+
if run_status == "completed":
|
2441 |
+
messages = client.beta.threads.messages.list(thread_id=thread.id)
|
2442 |
+
# [MessageContentText(text=Text(annotations=[], value='您好!有什麼我可以幫助您的嗎?如果有任何問題或需要指導,請隨時告訴我!'), type='text')]
|
2443 |
+
response_text = messages.data[0].content[0].text.value
|
2444 |
+
else:
|
2445 |
+
response_text = "學習精靈有點累,請稍後再試!"
|
2446 |
+
|
2447 |
+
except Exception as e:
|
2448 |
+
response_text = OPEN_AI_CLIENT.chat.completions.create(
|
2449 |
+
model="gpt-4-turbo",
|
2450 |
+
messages=[
|
2451 |
+
{"role": "system", "content": system_content},
|
2452 |
+
{"role": "user", "content": user_content}
|
2453 |
+
],
|
2454 |
+
max_tokens=4000,
|
2455 |
+
).choices[0].message.content.strip()
|
2456 |
+
|
2457 |
+
new_chat_history = (feedback_request_message, response_text)
|
2458 |
+
if chat_history is None:
|
2459 |
+
chat_history = [new_chat_history]
|
2460 |
+
else:
|
2461 |
+
chat_history.append(new_chat_history)
|
2462 |
+
|
2463 |
+
feedback_btn_update = gr.update(value="已回饋", interactive=False, variant="secondary")
|
2464 |
+
|
2465 |
+
return chat_history, feedback_btn_update
|
2466 |
|
2467 |
def process_open_ai_audio_to_chatbot(password, audio_url):
|
2468 |
verify_password(password)
|
|
|
2916 |
with gr.Row():
|
2917 |
msg = gr.Textbox(label="訊息",scale=3)
|
2918 |
send_button = gr.Button("送出", variant="primary", scale=1)
|
2919 |
+
send_feedback_btn = gr.Button("提問力回饋", variant="primary", scale=1, visible=False)
|
2920 |
with gr.Row("飛特音速") as chatbot_open_ai_streaming:
|
2921 |
with gr.Column():
|
2922 |
streaming_chat_greeting = """
|
|
|
3209 |
# OPENAI ASSISTANT CHATBOT 模式
|
3210 |
send_button.click(
|
3211 |
chat_with_opan_ai_assistant,
|
3212 |
+
inputs=[password, video_id, user_data, thread_id, trascript_state, key_moments, msg, chatbot, content_subject, content_grade, questions_answers_json, send_button, send_feedback_btn, socratic_mode_btn],
|
3213 |
+
outputs=[msg, chatbot, thread_id, send_button, send_feedback_btn],
|
3214 |
+
scroll_to_output=True
|
3215 |
+
)
|
3216 |
+
send_feedback_btn.click(
|
3217 |
+
feedback_with_opan_ai_assistant,
|
3218 |
+
inputs=[thread_id, chatbot],
|
3219 |
+
outputs=[chatbot, send_feedback_btn],
|
3220 |
scroll_to_output=True
|
3221 |
)
|
3222 |
openai_chatbot_audio_input.change(
|
|
|
3234 |
)
|
3235 |
question_buttons = [btn_1, btn_2, btn_3]
|
3236 |
for question_btn in question_buttons:
|
3237 |
+
inputs_list = [password, video_id, user_data, thread_id, trascript_state, key_moments, question_btn, chatbot, content_subject, content_grade, questions_answers_json, send_button, send_feedback_btn, socratic_mode_btn]
|
3238 |
+
outputs_list = [msg, chatbot, thread_id, send_button, send_feedback_btn]
|
3239 |
setup_question_button_click(question_btn, inputs_list, outputs_list, chat_with_opan_ai_assistant)
|
3240 |
|
3241 |
# 為生成問題按鈕設定特殊的點擊事件
|
|
|
3252 |
outputs=[ai_msg, ai_chatbot, ai_send_button],
|
3253 |
scroll_to_output=True
|
3254 |
)
|
3255 |
+
# 其他精靈 ai_chatbot 连接 QA 按钮点击事件
|
3256 |
+
ai_chatbot_question_buttons = [ai_chatbot_question_1, ai_chatbot_question_2, ai_chatbot_question_3]
|
3257 |
+
for question_btn in ai_chatbot_question_buttons:
|
3258 |
+
inputs_list = [ai_name, password, video_id, user_data, trascript_state, key_moments, question_btn, ai_chatbot, content_subject, content_grade, questions_answers_json, ai_send_button, ai_chatbot_socratic_mode_btn]
|
3259 |
outputs_list = [ai_msg, ai_chatbot, ai_send_button]
|
3260 |
+
setup_question_button_click(question_btn, inputs_list, outputs_list, chat_with_ai)
|
3261 |
|
3262 |
# file_upload.change(process_file, inputs=file_upload, outputs=df_string_output)
|
3263 |
# file_upload.change(process_file, inputs=file_upload, outputs=[btn_1, btn_2, btn_3, df_summarise, df_string_output])
|