11import json
2+ from app .utils .common_utils import transform_link
23from openai import OpenAI
34from app .utils .log_util import logger
45import time
@@ -33,7 +34,10 @@ async def chat(
3334 retry_delay : float = 1.0 , # 添加重试延迟
3435 top_p : float | None = None , # 添加top_p参数,
3536 agent_name : str = "NO_NAME" , # CoderAgent or WriterAgent
37+ sub_title : str | None = None ,
3638 ) -> str :
39+ logger .info (f"subtitle是:{ sub_title } " )
40+
3741 kwargs = {
3842 "model" : self .model ,
3943 "messages" : history ,
@@ -52,10 +56,11 @@ async def chat(
5256 for attempt in range (max_retries ):
5357 try :
5458 completion = self .client .chat .completions .create (** kwargs )
59+ logger .info (f"API返回: { completion } " )
5560 if not completion or not hasattr (completion , "choices" ):
5661 raise ValueError ("无效的API响应" )
5762 self .chat_count += 1
58- await self .analyse_completion (completion , agent_name )
63+ await self .analyse_completion (completion , agent_name , sub_title )
5964 return completion
6065 except json .JSONDecodeError :
6166 logger .error (f"第{ attempt + 1 } 次重试: API返回无效JSON" )
@@ -65,7 +70,9 @@ async def chat(
6570 logger .debug (f"请求参数: { kwargs } " )
6671 raise # 如果所有重试都失败,则抛出异常
6772
68- async def analyse_completion (self , completion , agent_name ):
73+ async def analyse_completion (self , completion , agent_name , sub_title ):
74+ logger .info (f"subtitle是:{ sub_title } " )
75+
6976 code = ""
7077 if (
7178 hasattr (completion .choices [0 ].message , "tool_calls" )
@@ -74,20 +81,22 @@ async def analyse_completion(self, completion, agent_name):
7481 tool_call = completion .choices [0 ].message .tool_calls [0 ]
7582 if tool_call .function .name == "execute_code" :
7683 code = json .loads (tool_call .function .arguments )["code" ]
77- await self .send_message (agent_name , completion .choices [0 ].message .content , code )
84+ (
85+ await self .send_message (
86+ agent_name , completion .choices [0 ].message .content , code , sub_title
87+ )
88+ )
7889
79- async def send_message (self , agent_name , content , code = "" ):
90+ async def send_message (self , agent_name , content , code = "" , sub_title = None ):
91+ logger .info (f"subtitle是:{ sub_title } " )
8092 if agent_name == "CoderAgent" :
8193 agent_msg : CoderMessage = CoderMessage (content = content , code = code )
8294 elif agent_name == "WriterAgent" :
83- # 判断content是否包含图片 xx.png,对其处理为 http://localhost:8000/static/20250428-200915-ebc154d4/512.jpg
84- if re .search (r"\.(png|jpg|jpeg|gif|bmp|webp)$" , content ):
85- content = re .sub (
86- r"\.(png|jpg|jpeg|gif|bmp|webp)$" ,
87- lambda match : f"http://localhost:8000/static/{ self .task_id } /{ match .group (0 )} " ,
88- content ,
89- )
90- agent_msg : WriterMessage = WriterMessage (content = content )
95+ # 处理 Markdown 格式的图片语法
96+ content = transform_link (self .task_id , content )
97+ agent_msg : WriterMessage = WriterMessage (
98+ content = content , sub_title = sub_title
99+ )
91100 else :
92101 raise ValueError (f"无效的agent_name: { agent_name } " )
93102
0 commit comments