Run the agent for the given task. Args: task (`str`): Task to perform. stream (`bool`): Whether to run in streaming mode. If `True`, returns a generator that yields each step as it is executed. You must iterate over this generator to process
(
self,
task: str,
stream: bool = False,
reset: bool = True,
images: list["PIL.Image.Image"] | None = None,
additional_args: dict | None = None,
max_steps: int | None = None,
return_full_result: bool | None = None,
)
| 434 | self.step_callbacks.register(ActionStep, self.monitor.update_metrics) |
| 435 | |
| 436 | def run( |
| 437 | self, |
| 438 | task: str, |
| 439 | stream: bool = False, |
| 440 | reset: bool = True, |
| 441 | images: list["PIL.Image.Image"] | None = None, |
| 442 | additional_args: dict | None = None, |
| 443 | max_steps: int | None = None, |
| 444 | return_full_result: bool | None = None, |
| 445 | ) -> Any | RunResult: |
| 446 | """ |
| 447 | Run the agent for the given task. |
| 448 | |
| 449 | Args: |
| 450 | task (`str`): Task to perform. |
| 451 | stream (`bool`): Whether to run in streaming mode. |
| 452 | If `True`, returns a generator that yields each step as it is executed. You must iterate over this generator to process the individual steps (e.g., using a for loop or `next()`). |
| 453 | If `False`, executes all steps internally and returns only the final answer after completion. |
| 454 | reset (`bool`): Whether to reset the conversation or keep it going from previous run. |
| 455 | images (`list[PIL.Image.Image]`, *optional*): Image(s) objects. |
| 456 | additional_args (`dict`, *optional*): Any other variables that you want to pass to the agent run, for instance images or dataframes. Give them clear names! |
| 457 | max_steps (`int`, *optional*): Maximum number of steps the agent can take to solve the task. if not provided, will use the agent's default value. |
| 458 | return_full_result (`bool`, *optional*): Whether to return the full [`RunResult`] object or just the final answer output. |
| 459 | If `None` (default), the agent's `self.return_full_result` setting is used. |
| 460 | |
| 461 | Example: |
| 462 | ```py |
| 463 | from smolagents import CodeAgent |
| 464 | agent = CodeAgent(tools=[]) |
| 465 | agent.run("What is the result of 2 power 3.7384?") |
| 466 | ``` |
| 467 | """ |
| 468 | max_steps = max_steps or self.max_steps |
| 469 | self.task = task |
| 470 | self.interrupt_switch = False |
| 471 | if additional_args: |
| 472 | self.state.update(additional_args) |
| 473 | self.task += f""" |
| 474 | You have been provided with these additional arguments, that you can access directly using the keys as variables: |
| 475 | {str(additional_args)}.""" |
| 476 | |
| 477 | self.memory.system_prompt = SystemPromptStep(system_prompt=self.system_prompt) |
| 478 | if reset: |
| 479 | self.memory.reset() |
| 480 | self.monitor.reset() |
| 481 | |
| 482 | self.logger.log_task( |
| 483 | content=self.task.strip(), |
| 484 | subtitle=f"{type(self.model).__name__} - {(self.model.model_id if hasattr(self.model, 'model_id') else '')}", |
| 485 | level=LogLevel.INFO, |
| 486 | title=self.name if hasattr(self, "name") else None, |
| 487 | ) |
| 488 | self.memory.steps.append(TaskStep(task=self.task, task_images=images)) |
| 489 | |
| 490 | if getattr(self, "python_executor", None): |
| 491 | self.python_executor.send_variables(variables=self.state) |
| 492 | self.python_executor.send_tools({**self.tools, **self.managed_agents}) |
| 493 |