(
self,
agent: Agent,
messages: List,
context_variables: dict = {},
model_override: str = None,
stream: bool = False,
debug: bool = True,
max_turns: int = float("inf"),
execute_tools: bool = True,
)
| 265 | return partial_response |
| 266 | |
| 267 | def run( |
| 268 | self, |
| 269 | agent: Agent, |
| 270 | messages: List, |
| 271 | context_variables: dict = {}, |
| 272 | model_override: str = None, |
| 273 | stream: bool = False, |
| 274 | debug: bool = True, |
| 275 | max_turns: int = float("inf"), |
| 276 | execute_tools: bool = True, |
| 277 | ) -> Response: |
| 278 | if stream: |
| 279 | return self.run_and_stream( |
| 280 | agent=agent, |
| 281 | messages=messages, |
| 282 | context_variables=context_variables, |
| 283 | model_override=model_override, |
| 284 | debug=debug, |
| 285 | max_turns=max_turns, |
| 286 | execute_tools=execute_tools, |
| 287 | ) |
| 288 | active_agent = agent |
| 289 | context_variables = copy.deepcopy(context_variables) |
| 290 | history = copy.deepcopy(messages) |
| 291 | init_len = len(messages) |
| 292 | |
| 293 | self.logger.info("Receiveing the task:", history[-1]['content'], title="Receive Task", color="green") |
| 294 | |
| 295 | while len(history) - init_len < max_turns and active_agent: |
| 296 | |
| 297 | # get completion with current history, agent |
| 298 | completion = self.get_chat_completion( |
| 299 | agent=active_agent, |
| 300 | history=history, |
| 301 | context_variables=context_variables, |
| 302 | model_override=model_override, |
| 303 | stream=stream, |
| 304 | debug=debug, |
| 305 | ) |
| 306 | message: Message = completion.choices[0].message |
| 307 | message.sender = active_agent.name |
| 308 | # debug_print(debug, "Received completion:", message.model_dump_json(indent=4), log_path=log_path, title="Received Completion", color="blue") |
| 309 | self.logger.pretty_print_messages(message) |
| 310 | history.append( |
| 311 | json.loads(message.model_dump_json()) |
| 312 | ) # to avoid OpenAI types (?) |
| 313 | |
| 314 | if not message.tool_calls or not execute_tools: |
| 315 | self.logger.info("Ending turn.", title="End Turn", color="red") |
| 316 | break |
| 317 | # if (message.tool_calls and message.tool_calls[0].function.name == "case_resolved") or not execute_tools: |
| 318 | # debug_print(debug, "Ending turn.", log_path=log_path, title="End Turn", color="red") |
| 319 | # break |
| 320 | |
| 321 | # handle function calls, updating context_variables, and switching agents |
| 322 | if message.tool_calls: |
| 323 | partial_response = self.handle_tool_calls( |
| 324 | message.tool_calls, active_agent.functions, context_variables, debug, handle_mm_func=active_agent.handle_mm_func |
no test coverage detected