The agent class
This page documents the Agent
class, which runs the main loop of the agent.
To learn about the configuration objects used to specify the behavior of an agent,
see the agent configuration reference page.
sweagent.agent.agents.Agent
Agent(*, templates: TemplateConfig, tools: ToolHandler, history_processors: list[HistoryProcessor], model: AbstractModel, max_requeries: int = 3, name: str = 'main', _catch_errors: bool = True, _always_require_zero_exit_code: bool = False)
The agent handles the behaviour of the model and how it interacts with the environment.
To run the agent, either call self.run
or self.setup
and then self.step
in a loop.
Source code in sweagent/agent/agents.py
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 |
|
history
property
writable
history: History
History that is passed on to the model.
Use _append_history
to modify.
info
property
writable
info: AgentInfo
Information about the agent's run
logger
instance-attribute
logger = get_logger('swea-agent', emoji='🤠')
messages
property
messages: list[dict[str, str]]
Return the history of the agent since the last reset, processed through all history processors.
replay_config
property
writable
replay_config: BaseModel | None
traj_path
instance-attribute
traj_path: Path | None = None
trajectory
property
writable
trajectory: Trajectory
Trajectory of the agent for the current instance. In contrast to history
,
this is mostly for the informational value of how the agent interacted with
the environment and is also what is being used when replaying the trajectory
add_demonstrations_to_history
add_demonstrations_to_history() -> None
Add demonstrations to history
Source code in sweagent/agent/agents.py
329 330 331 332 |
|
add_hook
add_hook(hook: AbstractAgentHook) -> None
Add hook to agent
Source code in sweagent/agent/agents.py
200 201 202 203 |
|
add_instance_template_to_history
add_instance_template_to_history(state: dict[str, str]) -> None
Add observation to history, as well as the instance template or demonstrations if we're at the start of a new attempt.
Source code in sweagent/agent/agents.py
447 448 449 450 451 452 453 454 455 456 457 458 459 |
|
add_step_to_history
add_step_to_history(step: StepOutput) -> None
Adds a step (command that was run and output) to the model history
Source code in sweagent/agent/agents.py
420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 |
|
add_step_to_trajectory
add_step_to_trajectory(step: StepOutput) -> None
Source code in sweagent/agent/agents.py
809 810 811 812 813 814 815 816 817 818 819 820 821 |
|
add_system_message_to_history
add_system_message_to_history() -> None
Add system message to history
Source code in sweagent/agent/agents.py
320 321 322 323 324 325 326 327 |
|
attempt_autosubmission_after_error
attempt_autosubmission_after_error(step: StepOutput) -> StepOutput
For most exceptions, we attempt to still extract the patch and submit that.
This means we send the submit
command to the runtime and parse the output.
Source code in sweagent/agent/agents.py
527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 |
|
forward
forward(history: list[dict[str, str]]) -> StepOutput
Forward the model without handling errors.
All exceptions raised will contain the StepOutput
object
with some of the attributes set.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
history
|
list[dict[str, str]]
|
history to query the model with |
required |
Returns:
Name | Type | Description |
---|---|---|
step_output |
StepOutput
|
step output |
Source code in sweagent/agent/agents.py
643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 |
|
forward_with_handling
forward_with_handling(history: list[dict[str, str]]) -> StepOutput
Forward the model and handle errors, requerying the model if we can. For example, if the model outputs a bash command that has syntax errors, we will not execute it but requery the model for a corrected command.
Note: This will update the trajectory, but not the history.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
history
|
list[dict[str, str]]
|
history to forward |
required |
Returns:
Name | Type | Description |
---|---|---|
step_output |
StepOutput
|
step output |
Source code in sweagent/agent/agents.py
684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 |
|
from_config
classmethod
from_config(config: AgentConfig) -> Self
Source code in sweagent/agent/agents.py
189 190 191 192 193 194 195 196 197 198 |
|
get_model_requery_history
get_model_requery_history(error_template: str, *, output: str, **kwargs: str | int | float | bool | None) -> list[dict[str, str]]
Ask the model to correct after a hitting one of the following errors:
- Malformatted output (could not parse action)
- Blocked action (command is on the blocklist)
- Bash command syntax error
At the time this function is called, the proposed action and observation are not part of the history yet.
This function adds temporary history based on the error template and queries the model. If the model is able to correct itself, the records of the mistakes will not be part of the history (but they are saved in the trajectory).
Parameters:
Name | Type | Description | Default |
---|---|---|---|
error_template
|
str
|
error template |
required |
output
|
str
|
model output |
required |
**kwargs
|
str | int | float | bool | None
|
keyword arguments to be passed to the error template |
{}
|
Returns:
Type | Description |
---|---|
list[dict[str, str]]
|
model output after requery |
Source code in sweagent/agent/agents.py
493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 |
|
handle_action
handle_action(step: StepOutput) -> StepOutput
Runs an action proposed by the agent in the environment and returns the corresponding output.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
action
|
command to run in bash shell |
required | |
output
|
output from model (only used for error handling) |
required |
Returns:
Name | Type | Description |
---|---|---|
action_execution_output |
StepOutput
|
action execution output |
Source code in sweagent/agent/agents.py
585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 |
|
handle_submission
handle_submission(step: StepOutput, *, observation='') -> StepOutput
Check if there was a submission in the observation and handle it.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
step
|
StepOutput
|
|
required |
observation
|
If specified, will use this rather than stepobservation |
''
|
Source code in sweagent/agent/agents.py
549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 |
|
run
run(env: SWEEnv, problem_statement: ProblemStatement | ProblemStatementConfig, output_dir: Path = Path('.')) -> AgentRunResult
Run the agent on a problem instance. This method contains the
main loop that repeatedly calls self._step
until the problem is solved.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
setup_args
|
Arguments to pass to the agent's setup method. |
required | |
env
|
SWEEnv
|
The environment to run the agent on. |
required |
traj_dir
|
Directory to save the trajectory to |
required |
Source code in sweagent/agent/agents.py
854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 |
|
save_trajectory
save_trajectory() -> None
Save the trajectory to disk. This includes the history, the environment state, and the model stats.
Source code in sweagent/agent/agents.py
461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 |
|
setup
setup(env: SWEEnv, problem_statement: ProblemStatement | ProblemStatementConfig, output_dir: Path = Path('.')) -> None
Setup the agent for a new instance. This includes formatting the system message and adding demonstrations to the history.
This method is called by self.run
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
instance_args
|
Arguments for the instance |
required |
Source code in sweagent/agent/agents.py
271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 |
|
setup_attempt
setup_attempt() -> None
Setup the agent for a new attempt. This includes resetting the model stats.
Source code in sweagent/agent/agents.py
310 311 312 313 314 315 316 317 318 |
|
step
step() -> StepOutput
Run a step of the agent. This is a wrapper around self.forward_with_handling
with additional bookkeeping:
- Update message history with performed action and observation
- Update trajectory with the final executed result
- Update the info dictionary
Returns:
Name | Type | Description |
---|---|---|
step_output |
StepOutput
|
step output (same as the output of |
Source code in sweagent/agent/agents.py
823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 |
|