Miemie123 commited on
Commit
149844d
·
verified ·
1 Parent(s): 7073fec

Update app_shared.py

Browse files
Files changed (1) hide show
  1. app_shared.py +30 -11
app_shared.py CHANGED
@@ -15,7 +15,8 @@ import io
15
  import markdown
16
  import os
17
  from openai import OpenAI
18
- from dashscope.agents import Agent
 
19
 
20
 
21
  # =============================================================================
@@ -524,7 +525,11 @@ class ChatManager:
524
  st.session_state.chat_history.append({"role": "user", "content": user_input})
525
  app_id = "c968f91131ac432787f5ef81f51922ba"
526
  api_key = os.getenv("DASHSCOPE_API_KEY")
527
- response = self.generate_response(user_input, app_id, api_key)
 
 
 
 
528
  st.session_state.chat_history.append({"role": "assistant", "content": response})
529
 
530
  st.session_state.chat_step = len(st.session_state.chat_history)
@@ -544,16 +549,30 @@ class ChatManager:
544
  # return response
545
 
546
  # return "Thank you for your feedback. I will conduct an analysis based on this information. Please continue to describe your symptoms."
 
547
 
548
- def generate_response(messages: list) -> str:
549
- response = AgentService.call(
550
- agent_id=st.secrets["DASHSCOPE_AGENT_ID"],
551
- messages=messages,
552
- api_key=st.secrets["DASHSCOPE_API_KEY"]
553
- )
554
- if response.status_code != 200:
555
- return "⚠️ 智能体调用失败"
556
- return response.output.choices[0].message.content
 
 
 
 
 
 
 
 
 
 
 
 
 
557
 
558
 
559
 
 
15
  import markdown
16
  import os
17
  from openai import OpenAI
18
+ from http import HTTPStatus
19
+ import dashscope
20
 
21
 
22
  # =============================================================================
 
525
  st.session_state.chat_history.append({"role": "user", "content": user_input})
526
  app_id = "c968f91131ac432787f5ef81f51922ba"
527
  api_key = os.getenv("DASHSCOPE_API_KEY")
528
+
529
+ # response = self.generate_response(user_input, app_id, api_key)
530
+ messages = [{"role": "user", "content": user_input}]
531
+ response = self.generate_response(messages, app_id, api_key)
532
+
533
  st.session_state.chat_history.append({"role": "assistant", "content": response})
534
 
535
  st.session_state.chat_step = len(st.session_state.chat_history)
 
549
  # return response
550
 
551
  # return "Thank you for your feedback. I will conduct an analysis based on this information. Please continue to describe your symptoms."
552
+
553
 
554
+ def generate_response(self, messages: list, app_id: str, api_key: str) -> str:
555
+ dashscope.api_key = api_key
556
+ dashscope.base_http_api_url = 'https://dashscope.aliyuncs.com/api/v1'
557
+
558
+ # 拼接历史对话为 prompt(只支持单轮,不建议传 messages 列表)
559
+ prompt = "\n".join([
560
+ f"{msg['role'].capitalize()}: {msg['content']}" for msg in messages if msg["content"]
561
+ ])
562
+
563
+ try:
564
+ response = dashscope.Application.call(
565
+ app_id=app_id,
566
+ prompt=prompt
567
+ )
568
+ except Exception as e:
569
+ return f"⚠️ 智能体调用异常: {e}"
570
+
571
+ if response.status_code != HTTPStatus.OK:
572
+ return f"⚠️ 智能体调用失败: {response.message}"
573
+
574
+ return response.output.get("text", "⚠️ 没有返回内容")
575
+
576
 
577
 
578