Miemie123 commited on
Commit
99b260b
·
verified ·
1 Parent(s): 866a06b

Update app_shared.py

Browse files
Files changed (1) hide show
  1. app_shared.py +69 -21
app_shared.py CHANGED
@@ -421,27 +421,53 @@ class ChatManager:
421
  st.session_state.chat_step += 1
422
  st.session_state.last_update_time = now
423
 
424
- elif next_msg["role"] == "assistant":
425
- # 调用API前,确认上一条是用户消息,且内容不为空
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
426
  if step == 0 or history[step - 1]["role"] != "user" or not history[step - 1]["content"]:
427
  st.warning("⚠️ 无法生成 AI 回答:找不到上一条用户消息")
428
  return
429
-
430
- user_prompt = history[step - 1]["content"]
431
-
 
 
432
  app_id = "c968f91131ac432787f5ef81f51922ba"
433
  api_key = os.getenv("DASHSCOPE_API_KEY")
434
- reply = self.generate_response(user_prompt, app_id, api_key)
435
-
 
 
 
 
436
  # 写入 assistant 回复
437
  if len(history) > step:
438
  history[step]["content"] = reply
439
  else:
440
  history.append({"role": "assistant", "content": reply})
441
-
442
  st.session_state.chat_step += 1
443
  st.session_state.last_update_time = now
444
-
 
445
 
446
 
447
  def render_message(self, role: str, content: str) -> str:
@@ -516,18 +542,40 @@ class ChatManager:
516
  # return response
517
 
518
  # return "Thank you for your feedback. I will conduct an analysis based on this information. Please continue to describe your symptoms."
519
- def generate_response(self, user_input: str, app_id: str, api_key: str) -> str:
520
- if not api_key:
521
- return "❌ 请在环境变量中配置 DASHSCOPE_API_KEY。"
522
-
523
- try:
524
- st.write(f"🚀 调用 Qwen,输入:{user_input}")
525
- response = call_qwen_agent(user_input, app_id, api_key)
526
- st.write(f"✅ Qwen 返回前200字:{response[:200]}")
527
- return response
528
- except Exception as e:
529
- st.write(f"❌ Qwen 调用失败:{e}")
530
- return "调用 Qwen API 出错,请稍后再试。"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
531
 
532
 
533
  # =============================================================================
 
421
  st.session_state.chat_step += 1
422
  st.session_state.last_update_time = now
423
 
424
+ # elif next_msg["role"] == "assistant":
425
+ # # 调用API前,确认上一条是用户消息,且内容不为空
426
+ # if step == 0 or history[step - 1]["role"] != "user" or not history[step - 1]["content"]:
427
+ # st.warning("⚠️ 无法生成 AI 回答:找不到上一条用户消息")
428
+ # return
429
+
430
+ # user_prompt = history[step - 1]["content"]
431
+
432
+ # app_id = "c968f91131ac432787f5ef81f51922ba"
433
+ # api_key = os.getenv("DASHSCOPE_API_KEY")
434
+ # reply = self.generate_response(user_prompt, app_id, api_key)
435
+
436
+ # # 写入 assistant 回复
437
+ # if len(history) > step:
438
+ # history[step]["content"] = reply
439
+ # else:
440
+ # history.append({"role": "assistant", "content": reply})
441
+
442
+ # st.session_state.chat_step += 1
443
+ # st.session_state.last_update_time = now
444
+ elif next_msg["role"] == "assistant": #multi
445
  if step == 0 or history[step - 1]["role"] != "user" or not history[step - 1]["content"]:
446
  st.warning("⚠️ 无法生成 AI 回答:找不到上一条用户消息")
447
  return
448
+
449
+ # 取当前上下文(含用户与助手历史)
450
+ messages = history[:step] # 所有已发生的轮次
451
+ messages.append(history[step - 1]) # 确保当前用户输入在最后
452
+
453
  app_id = "c968f91131ac432787f5ef81f51922ba"
454
  api_key = os.getenv("DASHSCOPE_API_KEY")
455
+ reply = self.generate_response(messages, app_id, api_key)
456
+
457
+ if not reply:
458
+ st.warning("⚠️ AI 未返回内容,请稍后重试")
459
+ return
460
+
461
  # 写入 assistant 回复
462
  if len(history) > step:
463
  history[step]["content"] = reply
464
  else:
465
  history.append({"role": "assistant", "content": reply})
466
+
467
  st.session_state.chat_step += 1
468
  st.session_state.last_update_time = now
469
+
470
+
471
 
472
 
473
  def render_message(self, role: str, content: str) -> str:
 
542
  # return response
543
 
544
  # return "Thank you for your feedback. I will conduct an analysis based on this information. Please continue to describe your symptoms."
545
+
546
+ # def generate_response(self, user_input: str, app_id: str, api_key: str) -> str:
547
+ # if not api_key:
548
+ # return "❌ 请在环境变量中配置 DASHSCOPE_API_KEY。"
549
+
550
+ # try:
551
+ # st.write(f"🚀 调用 Qwen,输入:{user_input}")
552
+ # response = call_qwen_agent(user_input, app_id, api_key)
553
+ # st.write(f"✅ Qwen 返回前200字:{response[:200]}")
554
+ # return response
555
+ # except Exception as e:
556
+ # st.write(f" Qwen 调用失败:{e}")
557
+ # return "调用 Qwen API 出错,请稍后再试。"
558
+
559
+ def generate_response(self, messages: list, app_id: str, api_key: str):
560
+ client = OpenAI(
561
+ api_key=api_key,
562
+ base_url="https://dashscope.aliyuncs.com/compatible-mode/v1"
563
+ )
564
+
565
+ completion = client.chat.completions.create(
566
+ model="qwen-plus-2025-04-28",
567
+ messages=messages,
568
+ extra_body={"enable_thinking": True}
569
+ )
570
+
571
+ full_reply = ""
572
+ for chunk in completion:
573
+ if chunk.choices:
574
+ delta = chunk.choices[0].delta
575
+ if hasattr(delta, "content") and delta.content:
576
+ full_reply += delta.content
577
+ return full_reply
578
+
579
 
580
 
581
  # =============================================================================