Amity123 commited on
Commit
4f5e4fa
·
verified ·
1 Parent(s): 97ef8d5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +11 -7
app.py CHANGED
@@ -82,15 +82,15 @@ def professional_agent(user_input, state):
82
  question = re.sub(r"我是.*?(,|,)", "", user_input)
83
  if not question.strip():
84
  answer = f"✅ 已設定你的專業領域:\n{profession_prompt}\n請提出問題。"
85
- state["chat_history"].append([user_input, answer])
 
86
  return state["chat_history"], state
87
  else:
88
  user_input = question
89
 
90
  messages = [{"role": "system", "content": state["profession_prompt"]}]
91
- for h in state["chat_history"]:
92
- messages.append({"role": "user", "content": h[0]})
93
- messages.append({"role": "assistant", "content": h[1]})
94
  messages.append({"role": "user", "content": user_input})
95
 
96
  try:
@@ -105,9 +105,13 @@ def professional_agent(user_input, state):
105
  print(tb) # 後端完整 log
106
  answer = f"⚠️ 發生錯誤,請查看後端 logs: {str(e)}"
107
 
108
- state["chat_history"].append([user_input, answer])
109
- if len(state["chat_history"]) > 10:
110
- state["chat_history"] = state["chat_history"][-10:]
 
 
 
 
111
 
112
  return state["chat_history"], state
113
 
 
82
  question = re.sub(r"我是.*?(,|,)", "", user_input)
83
  if not question.strip():
84
  answer = f"✅ 已設定你的專業領域:\n{profession_prompt}\n請提出問題。"
85
+ state["chat_history"].append({"role":"user","content":user_input})
86
+ state["chat_history"].append({"role":"assistant","content":answer})
87
  return state["chat_history"], state
88
  else:
89
  user_input = question
90
 
91
  messages = [{"role": "system", "content": state["profession_prompt"]}]
92
+ for msg in state["chat_history"]:
93
+ messages.append({"role": msg["role"], "content": msg["content"]})
 
94
  messages.append({"role": "user", "content": user_input})
95
 
96
  try:
 
105
  print(tb) # 後端完整 log
106
  answer = f"⚠️ 發生錯誤,請查看後端 logs: {str(e)}"
107
 
108
+ # Append using dict 格式
109
+ state["chat_history"].append({"role":"user","content":user_input})
110
+ state["chat_history"].append({"role":"assistant","content":answer})
111
+
112
+ # 保留最近 10 條訊息
113
+ if len(state["chat_history"]) > 20:
114
+ state["chat_history"] = state["chat_history"][-20:]
115
 
116
  return state["chat_history"], state
117