@@ -75,6 +75,11 @@ def convert_code_format(text):
7575 return re .sub (pattern , replacement , text ).replace ("```<" , "```" )
7676
7777
78+ class FinalAnswerError (Exception ):
79+ """Raised when agent output directly."""
80+ pass
81+
82+
7883class CoreAgent (CodeAgent ):
7984 def __init__ (self , observer : MessageObserver , prompt_templates : Dict [str , Any ] | None = None , * args , ** kwargs ):
8085 super ().__init__ (prompt_templates = prompt_templates , * args , ** kwargs )
@@ -101,20 +106,22 @@ def _step_stream(self, memory_step: ActionStep) -> Generator[Any]:
101106 memory_step .model_output_message = chat_message
102107 model_output = chat_message .content
103108 memory_step .model_output = model_output
109+
110+ self .logger .log_markdown (content = model_output , title = "MODEL OUTPUT" ,level = LogLevel .INFO )
104111 except Exception as e :
105112 raise AgentGenerationError (f"Error in generating model output:\n { e } " , self .logger ) from e
106113
107- self .logger .log_markdown (content = model_output , title = "Output message of the LLM:" , level = LogLevel .DEBUG , )
114+ self .logger .log_markdown (content = model_output , title = "Output message of the LLM:" , level = LogLevel .DEBUG )
108115
109116 # Parse
110117 try :
111118 code_action = fix_final_answer_code (parse_code_blobs (model_output ))
112119 # Record parsing results
113120 self .observer .add_message (self .agent_name , ProcessType .PARSE , code_action )
114121
115- except Exception as e :
116- error_msg = f"Error in code parsing: \n { e } \n Make sure to provide correct code blobs."
117- raise AgentParsingError ( error_msg , self . logger )
122+ except Exception :
123+ self . logger . log_markdown ( content = model_output , title = "AGENT FINAL ANSWER" , level = LogLevel . INFO )
124+ raise FinalAnswerError ( )
118125
119126 memory_step .tool_calls = [
120127 ToolCall (name = "python_interpreter" , arguments = code_action , id = f"call_{ len (self .memory .steps )} " , )]
@@ -254,16 +261,14 @@ def _run_stream(
254261 for el in self ._execute_step (action_step ):
255262 yield el
256263 final_answer = el
264+ except FinalAnswerError :
265+ # When the model does not output code, directly treat the large model content as the final answer
266+ final_answer = action_step .model_output
267+ if isinstance (final_answer , str ):
268+ final_answer = convert_code_format (final_answer )
257269
258270 except AgentError as e :
259- except_parse_error_pattern = """Make sure to include code with the correct pattern, for instance"""
260- if except_parse_error_pattern in e .message :
261- # When the model does not output code, directly treat the large model content as the final answer
262- final_answer = action_step .model_output
263- if isinstance (final_answer , str ):
264- final_answer = convert_code_format (final_answer )
265- else :
266- action_step .error = e
271+ action_step .error = e
267272
268273 finally :
269274 self ._finalize_step (action_step , step_start_time )
0 commit comments