1717sys .path .insert (0 , os .path .abspath (os .path .join (os .path .dirname (__file__ ), '..' )))
1818
1919from pydify import AgentClient
20+ from pydify .common import DifyAPIError
2021
2122# 从环境变量或直接设置 API 密钥
2223API_KEY = os .environ .get ("DIFY_API_KEY_AGENT" , "app-JH2PWol59GDhOfLpB1Qwvts3" )
@@ -73,7 +74,7 @@ def example_send_message_streaming():
7374
7475 # 定义处理函数
7576 def on_agent_message (chunk ):
76- print (f"收到文本块: { chunk .get ('answer' , '' )} " , end = "" , flush = True )
77+ print (f"{ chunk .get ('answer' , '' )} " , end = "" , flush = True )
7778
7879 def on_agent_thought (chunk ):
7980 print (f"\n \n [Agent思考] { chunk .get ('position' )} :" )
@@ -96,29 +97,47 @@ def on_message_end(chunk):
9697 def on_error (chunk ):
9798 print (f"\n 错误: { chunk .get ('message' , '未知错误' )} " )
9899
99- # 发送消息(流式模式,Agent只支持流式模式)
100- stream = client .send_message (
101- query = "帮我搜索最近一周的股市行情,并分析趋势" ,
102- user = USER_ID
103- )
100+ # 请求参数,包括加入重试和超时设置
101+ request_kwargs = get_request_kwargs ()
104102
105- # 处理流式响应
106- result = client .process_streaming_response (
107- stream ,
108- handle_agent_message = on_agent_message ,
109- handle_agent_thought = on_agent_thought ,
110- handle_message_file = on_message_file ,
111- handle_message_end = on_message_end ,
112- handle_error = on_error
113- )
114-
115- print ("\n \n 处理结果摘要:" )
116- print (f"消息ID: { result .get ('message_id' )} " )
117- print (f"会话ID: { result .get ('conversation_id' )} " )
118- print (f"Agent思考步骤数: { len (result .get ('agent_thoughts' , []))} " )
103+ try :
104+ # 发送消息(流式模式,Agent只支持流式模式)
105+ print ("发送查询: '帮我搜索最近一周的股市行情,并分析趋势'" )
106+ stream = client .send_message (
107+ query = "帮我搜索最近一周的股市行情,并分析趋势" ,
108+ user = USER_ID ,
109+ inputs = {}, # 添加空的inputs参数
110+ ** request_kwargs # 传递请求参数
111+ )
112+
113+ # 处理流式响应
114+ result = client .process_streaming_response (
115+ stream ,
116+ handle_agent_message = on_agent_message ,
117+ handle_agent_thought = on_agent_thought ,
118+ handle_message_file = on_message_file ,
119+ handle_message_end = on_message_end ,
120+ handle_error = on_error
121+ )
122+
123+ print ("\n \n 处理结果摘要:" )
124+ print (f"消息ID: { result .get ('message_id' )} " )
125+ print (f"会话ID: { result .get ('conversation_id' )} " )
126+ print (f"Agent思考步骤数: { len (result .get ('agent_thoughts' , []))} " )
127+
128+ # 返回会话ID,可用于后续对话
129+ return result .get ("conversation_id" )
119130
120- # 返回会话ID,可用于后续对话
121- return result .get ("conversation_id" )
131+ except DifyAPIError as e :
132+ print (f"API错误: { str (e )} " )
133+ print (f"状态码: { e .status_code } " )
134+ print (f"错误数据: { e .error_data } " )
135+ return None
136+ except Exception as e :
137+ print (f"发生异常: { str (e )} " )
138+ import traceback
139+ traceback .print_exc ()
140+ return None
122141
123142def example_continuation_conversation ():
124143 """多轮对话示例"""
@@ -133,7 +152,7 @@ def example_continuation_conversation():
133152
134153 # 继续对话函数
135154 def on_agent_message (chunk ):
136- print (f"收到文本块: { chunk .get ('answer' , '' )} " , end = "" , flush = True )
155+ print (f"{ chunk .get ('answer' , '' )} " , end = "" , flush = True )
137156
138157 def on_agent_thought (chunk ):
139158 print (f"\n \n [Agent思考] { chunk .get ('position' )} :" )
@@ -409,7 +428,7 @@ def example_send_message_with_image():
409428
410429 # 定义处理函数
411430 def on_agent_message (chunk ):
412- print (f"收到文本块: { chunk .get ('answer' , '' )} " , end = "" , flush = True )
431+ print (f"{ chunk .get ('answer' , '' )} " , end = "" , flush = True )
413432
414433 def on_agent_thought (chunk ):
415434 print (f"\n \n [Agent思考] { chunk .get ('position' )} :" )
0 commit comments