Spaces:
Sleeping
Sleeping
respond_with_jutor_chat
Browse files
app.py
CHANGED
@@ -989,6 +989,109 @@ def respond(password, user_message, data, chat_history, socratic_mode=False):
|
|
989 |
# 返回聊天历史和空字符串清空输入框
|
990 |
return "", chat_history
|
991 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
992 |
def chat_with_groq(password, user_message, data, chat_history, socratic_mode=False):
|
993 |
verify_password(password)
|
994 |
|
|
|
989 |
# 返回聊天历史和空字符串清空输入框
|
990 |
return "", chat_history
|
991 |
|
992 |
+
def respond_with_jutor_chat(password, user_message, data, chat_history, socratic_mode=False):
|
993 |
+
verify_password(password)
|
994 |
+
|
995 |
+
data_json = json.loads(data)
|
996 |
+
for entry in data_json:
|
997 |
+
entry.pop('embed_url', None) # Remove 'embed_url' if it exists
|
998 |
+
entry.pop('screenshot_path', None)
|
999 |
+
|
1000 |
+
if socratic_mode:
|
1001 |
+
sys_content = f"""
|
1002 |
+
你是一個擅長資料分析跟影片教學的老師,user 為學生
|
1003 |
+
請用 {data} 為資料文本,自行判斷資料的種類,
|
1004 |
+
並進行對話,使用 台灣人的口與表達,及繁體中文zh-TW
|
1005 |
+
|
1006 |
+
如果是影片類型,不用解釋逐字稿格式,直接回答學生問題
|
1007 |
+
請你用蘇格拉底式的提問方式,引導學生思考,並且給予學生一些提示
|
1008 |
+
不要直接給予答案,讓學生自己思考
|
1009 |
+
但可以給予一些提示跟引導,例如給予影片的時間軸,讓學生自己去找答案
|
1010 |
+
|
1011 |
+
如果學生問了一些問題你無法判斷,請告訴學生你無法判斷,並建議學生可以問其他問題
|
1012 |
+
或者你可以問學生一些問題,幫助學生更好的理解資料
|
1013 |
+
|
1014 |
+
如果學生的問題與資料文本無關,請告訴學生你無法回答超出範圍的問題
|
1015 |
+
|
1016 |
+
最後,在你回答的開頭標註【蘇格拉底助教】
|
1017 |
+
"""
|
1018 |
+
else:
|
1019 |
+
sys_content = f"""
|
1020 |
+
你是一個擅長資料分析跟影片教學的老師,user 為學生
|
1021 |
+
請用 {data} 為資料文本,自行判斷資料的種類,
|
1022 |
+
並進行對話,使用 zh-TW
|
1023 |
+
|
1024 |
+
如果是影片類型,不用解釋逐字稿格式,直接回答學生問題
|
1025 |
+
但可以給予一些提示跟引導,例如給予影片的時間軸,讓學生可以找到相對應的時間點
|
1026 |
+
|
1027 |
+
如果學生問了一些問題你無法判斷,請告訴學生你無法判斷,並建議學生可以問其他問題
|
1028 |
+
或者你可以問學生一些問題,幫助學生更好的理解資料
|
1029 |
+
|
1030 |
+
如果學生的問題與資料文本無關,請告訴學生你無法回答超出範圍的問題
|
1031 |
+
"""
|
1032 |
+
|
1033 |
+
messages = [
|
1034 |
+
{"role": "system", "content": sys_content}
|
1035 |
+
]
|
1036 |
+
|
1037 |
+
# if chat_history is not none, append role, content to messages
|
1038 |
+
# chat_history = [(user, assistant), (user, assistant), ...]
|
1039 |
+
# In the list, first one is user, then assistant
|
1040 |
+
if chat_history is not None:
|
1041 |
+
# 如果超過10則訊息,只保留最後10則訊息
|
1042 |
+
if len(chat_history) > 10:
|
1043 |
+
chat_history = chat_history[-10:]
|
1044 |
+
|
1045 |
+
for chat in chat_history:
|
1046 |
+
old_messages = [
|
1047 |
+
{"role": "user", "content": chat[0]},
|
1048 |
+
{"role": "assistant", "content": chat[1]}
|
1049 |
+
]
|
1050 |
+
messages += old_messages
|
1051 |
+
else:
|
1052 |
+
pass
|
1053 |
+
|
1054 |
+
# 构造请求体
|
1055 |
+
request_payload = {
|
1056 |
+
"endpoint": "https://api.openai.com/v1/chat/completions",
|
1057 |
+
"http_method": "POST",
|
1058 |
+
"data": {
|
1059 |
+
"model": "gpt-4-1106-preview", # 或其他模型
|
1060 |
+
"messages": messages,
|
1061 |
+
"max_tokens": 4000 # 設定一個較大的值,可根據需要調整
|
1062 |
+
}
|
1063 |
+
}
|
1064 |
+
|
1065 |
+
# 发送请求到远程API
|
1066 |
+
response = requests.post(
|
1067 |
+
"https://www.junyiacademy.com/api/v2/jutor/chat", # 你的远程API URL
|
1068 |
+
headers={
|
1069 |
+
"Content-Type": "application/json",
|
1070 |
+
# 如果API需要XSRF token或其他认证信息,请在这里添加
|
1071 |
+
# 'X-KA-FKey': xsrfToken
|
1072 |
+
},
|
1073 |
+
json=request_payload # 使用json参数直接发送JSON格式的数据
|
1074 |
+
)
|
1075 |
+
|
1076 |
+
if response.status_code == 200:
|
1077 |
+
# 处理响应数据
|
1078 |
+
response_data = response.json()
|
1079 |
+
prompt = response_data['data']['choices'][0]['message']['content'].strip()
|
1080 |
+
# 更新聊天历史
|
1081 |
+
new_chat_history = (user_message, prompt)
|
1082 |
+
if chat_history is None:
|
1083 |
+
chat_history = [new_chat_history]
|
1084 |
+
else:
|
1085 |
+
chat_history.append(new_chat_history)
|
1086 |
+
|
1087 |
+
# 返回聊天历史和空字符串清空输入框
|
1088 |
+
return "", chat_history
|
1089 |
+
else:
|
1090 |
+
# 处理错误情况
|
1091 |
+
print(f"Error: {response.status_code}")
|
1092 |
+
return "请求失败,请稍后再试!", chat_history
|
1093 |
+
|
1094 |
+
|
1095 |
def chat_with_groq(password, user_message, data, chat_history, socratic_mode=False):
|
1096 |
verify_password(password)
|
1097 |
|