Spaces:
Running
Running
refactor
Browse files
app.py
CHANGED
@@ -1161,92 +1161,6 @@ def download_exam_result(content):
|
|
1161 |
return word_path
|
1162 |
|
1163 |
# ---- Chatbot ----
|
1164 |
-
def respond(password, user_message, data, chat_history, socratic_mode=False):
|
1165 |
-
verify_password(password)
|
1166 |
-
|
1167 |
-
print("=== 變數:user_message ===")
|
1168 |
-
print(user_message)
|
1169 |
-
print("=== 變數:chat_history ===")
|
1170 |
-
print(chat_history)
|
1171 |
-
|
1172 |
-
data_json = json.loads(data)
|
1173 |
-
for entry in data_json:
|
1174 |
-
entry.pop('embed_url', None) # Remove 'embed_url' if it exists
|
1175 |
-
entry.pop('screenshot_path', None)
|
1176 |
-
|
1177 |
-
if socratic_mode:
|
1178 |
-
sys_content = f"""
|
1179 |
-
你是一個擅長資料分析跟影片教學的老師,user 為學生
|
1180 |
-
請用 {data} 為資料文本,自行判斷資料的種類,
|
1181 |
-
並進行對話,使用 台灣人的口與表達,及繁體中文zh-TW
|
1182 |
-
|
1183 |
-
如果是影片類型,不用解釋逐字稿格式,直接回答學生問題
|
1184 |
-
請你用蘇格拉底式的提問方式,引導學生思考,並且給予學生一些提示
|
1185 |
-
不要直接給予答案,讓學生自己思考
|
1186 |
-
但可以給予一些提示跟引導,例如給予影片的時間軸,讓學生自己去找答案
|
1187 |
-
|
1188 |
-
如果學生問了一些問題你無法判斷,請告訴學生你無法判斷,並建議學生可以問其他問題
|
1189 |
-
或者你可以問學生一些問題,幫助學生更好的理解資料
|
1190 |
-
|
1191 |
-
如果學生的問題與資料文本無關,請告訴學生你無法回答超出範圍的問題
|
1192 |
-
|
1193 |
-
最後,在你回答的開頭標註【蘇格拉底助教】
|
1194 |
-
"""
|
1195 |
-
else:
|
1196 |
-
sys_content = f"""
|
1197 |
-
你是一個擅長資料分析跟影片教學的老師,user 為學生
|
1198 |
-
請用 {data} 為資料文本,自行判斷資料的種類,
|
1199 |
-
並進行對話,使用 zh-TW
|
1200 |
-
|
1201 |
-
如果是影片類型,不用解釋逐字稿格式,直接回答學生問題
|
1202 |
-
但可以給予一些提示跟引導,例如給予影片的時間軸,讓學生可以找到相對應的時間點
|
1203 |
-
|
1204 |
-
如果學生問了一些問題你無法判斷,請告訴學生你無法判斷,並建議學生可以問其他問題
|
1205 |
-
或者你可以問學生一些問題,幫助學生更好的理解資料
|
1206 |
-
|
1207 |
-
如果學生的問題與資料文本無關,請告訴學生你無法回答超出範圍的問題
|
1208 |
-
"""
|
1209 |
-
|
1210 |
-
messages = [
|
1211 |
-
{"role": "system", "content": sys_content}
|
1212 |
-
]
|
1213 |
-
|
1214 |
-
# if chat_history is not none, append role, content to messages
|
1215 |
-
# chat_history = [(user, assistant), (user, assistant), ...]
|
1216 |
-
# In the list, first one is user, then assistant
|
1217 |
-
if chat_history is not None:
|
1218 |
-
# 如果超過10則訊息,只保留最後10則訊息
|
1219 |
-
if len(chat_history) > 10:
|
1220 |
-
chat_history = chat_history[-10:]
|
1221 |
-
|
1222 |
-
for chat in chat_history:
|
1223 |
-
old_messages = [
|
1224 |
-
{"role": "user", "content": chat[0]},
|
1225 |
-
{"role": "assistant", "content": chat[1]}
|
1226 |
-
]
|
1227 |
-
messages += old_messages
|
1228 |
-
else:
|
1229 |
-
pass
|
1230 |
-
|
1231 |
-
messages.append({"role": "user", "content": user_message})
|
1232 |
-
request_payload = {
|
1233 |
-
"model": "gpt-4-1106-preview",
|
1234 |
-
"messages": messages,
|
1235 |
-
"max_tokens": 4000 # 設定一個較大的值,可根據需要調整
|
1236 |
-
}
|
1237 |
-
response = OPEN_AI_CLIENT.chat.completions.create(**request_payload)
|
1238 |
-
response_text = response.choices[0].message.content.strip()
|
1239 |
-
|
1240 |
-
# 更新聊天历史
|
1241 |
-
new_chat_history = (user_message, response_text)
|
1242 |
-
if chat_history is None:
|
1243 |
-
chat_history = [new_chat_history]
|
1244 |
-
else:
|
1245 |
-
chat_history.append(new_chat_history)
|
1246 |
-
|
1247 |
-
# 返回聊天历史和空字符串清空输入框
|
1248 |
-
return "", chat_history
|
1249 |
-
|
1250 |
def chat_with_jutor(password, user_message, data, chat_history, socratic_mode=False):
|
1251 |
verify_password(password)
|
1252 |
|
|
|
1161 |
return word_path
|
1162 |
|
1163 |
# ---- Chatbot ----
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1164 |
def chat_with_jutor(password, user_message, data, chat_history, socratic_mode=False):
|
1165 |
verify_password(password)
|
1166 |
|