1515from agents .matmaster_agent .locales import i18n
1616from agents .matmaster_agent .model import UserContent
1717from agents .matmaster_agent .prompt import get_user_content_lang
18- from agents .matmaster_agent .style import get_job_complete_card
18+ from agents .matmaster_agent .style import get_job_complete_card , hallucination_card
1919from agents .matmaster_agent .utils .job_utils import (
2020 get_job_status ,
2121 get_running_jobs_detail ,
@@ -77,6 +77,12 @@ async def matmaster_prepare_state(
7777 callback_context .state ['new_query_job_status' ] = callback_context .state .get (
7878 'new_query_job_status' , {}
7979 )
80+ callback_context .state ['hallucination' ] = callback_context .state .get (
81+ 'hallucination' , False
82+ )
83+ callback_context .state ['hallucination_agent' ] = callback_context .state .get (
84+ 'hallucination_agent' , None
85+ )
8086
8187
8288async def matmaster_set_lang (
@@ -93,6 +99,15 @@ async def matmaster_set_lang(
9399 logger .info (f"[{ inspect .currentframe ().f_code .co_name } ] result = { result } " )
94100 language = str (result .get ('language' , 'zh' ))
95101 callback_context .state ['target_language' ] = language
102+ if callback_context .state ['target_language' ] in [
103+ 'Chinese' ,
104+ 'zh-CN' ,
105+ '简体中文' ,
106+ 'Chinese (Simplified)' ,
107+ ]:
108+ i18n .language = 'zh'
109+ else :
110+ i18n .language = 'en'
96111
97112
98113# after_model_callback
@@ -113,16 +128,6 @@ async def matmaster_check_job_status(
113128 jobs_dict
114129 ): # 确认当前有在运行中的任务
115130 running_job_ids = get_running_jobs_detail (jobs_dict ) # 从 state 里面拿
116- if callback_context .state ['target_language' ] in [
117- 'Chinese' ,
118- 'zh-CN' ,
119- '简体中文' ,
120- 'Chinese (Simplified)' ,
121- ]:
122- i18n .language = 'zh'
123- else :
124- i18n .language = 'en'
125-
126131 reset = False
127132 for origin_job_id , job_id , agent_name in running_job_ids :
128133 if not callback_context .state ['last_llm_response_partial' ]:
@@ -149,9 +154,8 @@ async def matmaster_check_job_status(
149154 llm_response .content = None
150155 break
151156 if not reset :
152- callback_context .state ['special_llm_response' ] = (
153- True # 标记开始处理原来消息的非流式版本
154- )
157+ # 标记开始处理原来消息的非流式版本
158+ callback_context .state ['special_llm_response' ] = True
155159 llm_response .content .parts = []
156160 reset = True
157161 function_call_id = f"call_{ str (uuid .uuid4 ()).replace ('-' , '' )[:24 ]} "
@@ -169,7 +173,42 @@ async def matmaster_check_job_status(
169173 )
170174 )
171175 callback_context .state ['last_llm_response_partial' ] = llm_response .partial
172- return llm_response
176+ # return llm_response
173177
174178 callback_context .state ['last_llm_response_partial' ] = llm_response .partial
175- return None
179+ return
180+
181+
182+ async def matmaster_hallucination_retry (
183+ callback_context : CallbackContext , llm_response : LlmResponse
184+ ) -> Optional [LlmResponse ]:
185+ hallucination_flag = callback_context .state ['hallucination' ]
186+ hallucination_agent = callback_context .state ['hallucination_agent' ]
187+ logger .info (
188+ f'[matmaster_hallucination_retry] hallucination_flag={ hallucination_flag } , hallucination_agent={ hallucination_agent } , i18n.language = { i18n .language } '
189+ )
190+ if not callback_context .state ['hallucination' ]:
191+ return
192+
193+ if llm_response .partial : # 原来消息的流式版本置空 None
194+ llm_response .content = None
195+ return
196+
197+ # 标记开始处理原来消息的非流式版本
198+ callback_context .state ['special_llm_response' ] = True
199+ llm_response .content .parts = []
200+
201+ llm_response .content .parts .append (Part (text = hallucination_card (i18n = i18n )))
202+ function_call_id = f"added_{ str (uuid .uuid4 ()).replace ('-' , '' )[:24 ]} "
203+ llm_response .content .parts .append (
204+ Part (
205+ function_call = FunctionCall (
206+ id = function_call_id ,
207+ name = 'transfer_to_agent' ,
208+ args = {'agent_name' : callback_context .state ['hallucination_agent' ]},
209+ )
210+ )
211+ )
212+ callback_context .state ['hallucination' ] = False
213+
214+ return llm_response
0 commit comments