EduTechTeam commited on
Commit
533f7a7
·
verified ·
1 Parent(s): 0398e13

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +7 -5
app.py CHANGED
@@ -9,17 +9,18 @@ client = openai.OpenAI(api_key=os.environ['OPENAI_API_KEY'])
9
  def boyfriend_chatbot(user_input, history):
10
  history = history or []
11
 
 
12
  messages = [{
13
  "role": "system",
14
  "content": "你是一個溫柔、貼心的男友,總是關心對方的感受,給予支持和鼓勵,用輕鬆、暖心的語氣回應。"
15
  }]
16
 
17
- # 加入歷史對話,跳過尚未回應的 bot_msg(通常是 None)
18
- for user_msg, bot_msg in history:
19
- messages.append({"role": "user", "content": user_msg})
20
- if bot_msg is not None:
21
- messages.append({"role": "assistant", "content": bot_msg})
22
 
 
23
  messages.append({"role": "user", "content": user_input})
24
 
25
  try:
@@ -40,6 +41,7 @@ def boyfriend_chatbot(user_input, history):
40
  except Exception as e:
41
  yield f"⚠️ 顯示訊息時發生錯誤:{str(e)}"
42
 
 
43
 
44
  # 建立 Gradio 介面
45
  chat_interface = gr.ChatInterface(
 
9
  def boyfriend_chatbot(user_input, history):
10
  history = history or []
11
 
12
+ # 加入系統提示,設定角色
13
  messages = [{
14
  "role": "system",
15
  "content": "你是一個溫柔、貼心的男友,總是關心對方的感受,給予支持和鼓勵,用輕鬆、暖心的語氣回應。"
16
  }]
17
 
18
+ # history 是一串角色訊息(dict),可直接 append
19
+ for msg in history:
20
+ if msg.get("role") in ["user", "assistant"]:
21
+ messages.append(msg)
 
22
 
23
+ # 加入當前使用者訊息
24
  messages.append({"role": "user", "content": user_input})
25
 
26
  try:
 
41
  except Exception as e:
42
  yield f"⚠️ 顯示訊息時發生錯誤:{str(e)}"
43
 
44
+
45
 
46
  # 建立 Gradio 介面
47
  chat_interface = gr.ChatInterface(