Action parsers
Action parsers interpret the output of the LM and return actions (tool calls/commands to execute) and thoughts (any other output of the LM, e.g., justifying the tool calls).
sweagent.tools.parsing
Our parsers parse output from the LM into thoughts and actions.
For example, our most basic parser is the ThoughtActionParser.
It expects the model response to be a discussion followed by a command wrapped in backticks like so:
Let's look at the files in the current directory.
Action:
```
ls -l
```
For models that support function calling, we instead recommend using the FunctionCallingParser.
To use a specific parser, set the parse_function key in your tool config to the type field of the parser.
agent:
tools:
...
parse_function:
type: "thought_action"
Or from the command line: --agent.tools.parse_function.type=thought_action.
Describing available tools
If you do not use the FunctionCallingParser, you need to include documentation about the available tools
in your system prompt. You can use the {{command_docs}} variable to include the automatically generated
documentation or explicitly describe the available tools.
Also see #1130.
FN_REGEX_PATTERN
module-attribute
FN_REGEX_PATTERN = '<function=([^>]+)>\\n(.*?)</function>'
FN_PARAM_REGEX_PATTERN
module-attribute
FN_PARAM_REGEX_PATTERN = '<parameter=([^>]+)>(.*?)</parameter>'
ParseFunction
module-attribute
ParseFunction = ActionParser | ThoughtActionParser | ActionOnlyParser | XMLThoughtActionParser | XMLFunctionCallingParser | FunctionCallingParser | EditFormat | Identity | JsonParser | BashCodeBlockParser | SingleBashCodeBlockParser
AbstractParseFunction
Bases: ABC
Abstract class for parsing functions. We use get to generate the right parser based on the name of the parser.
error_message
instance-attribute
error_message: str
format_error_template
property
format_error_template
ActionParser
pydantic-model
Bases: AbstractParseFunction, BaseModel
Expects the model response to be a single command. Example: "ls -l"
Fields:
-
error_message(str) -
type(Literal['action'])
error_message
pydantic-field
error_message: str = ' The command you provided was not recognized. Please specify one of the commands (+ any necessary arguments) from the following list in your response. Do not include any other text.\n\n COMMANDS:\n {command_docs}\n '
type
pydantic-field
type: Literal['action'] = 'action'
Type for (de)serialization. Do not change.
ActionOnlyParser
pydantic-model
Bases: AbstractParseFunction, BaseModel
Expects the model response to be a single command.
Fields:
-
error_message(str) -
type(Literal['action_only'])
error_message
pydantic-field
error_message: str = 'No message found in model response.'
type
pydantic-field
type: Literal['action_only'] = 'action_only'
Type for (de)serialization. Do not change.
ThoughtActionParser
pydantic-model
Bases: AbstractParseFunction, BaseModel
Expects the model response to be a discussion followed by a command wrapped in backticks. Example: Let's look at the files in the current directory.
ls -l
Fields:
-
error_message(str) -
type(Literal['thought_action'])
error_message
pydantic-field
error_message: str
type
pydantic-field
type: Literal['thought_action'] = 'thought_action'
Type for (de)serialization. Do not change.
XMLThoughtActionParser
pydantic-model
Bases: AbstractParseFunction, BaseModel
Expects the model response to be a discussion followed by a command wrapped in XML tags.
Example:
Let's look at the files in the current directory.
Fields:
-
error_message(str) -
type(Literal['xml_thought_action'])
error_message
pydantic-field
error_message: str
type
pydantic-field
type: Literal['xml_thought_action'] = 'xml_thought_action'
Type for (de)serialization. Do not change.
XMLFunctionCallingParser
pydantic-model
Bases: AbstractParseFunction, BaseModel
Expects the model response to be a tool calling format, where the command and parameters are specified
in XML tags.
Example:
Let's look at the files in the current directory.
Fields:
-
error_message(str) -
type(Literal['xml_function_calling'])
error_message
pydantic-field
error_message: str
type
pydantic-field
type: Literal['xml_function_calling'] = 'xml_function_calling'
EditFormat
pydantic-model
Bases: ThoughtActionParser, BaseModel
Expects the model response to be a discussion followed by a command wrapped in backticks. Example: We'll replace the contents of the current window with the following:
import os
os.listdir()
Fields:
-
error_message(str) -
type(Literal['edit_format'])
error_message
pydantic-field
error_message: str
type
pydantic-field
type: Literal['edit_format'] = 'edit_format'
Type for (de)serialization. Do not change.
Identity
pydantic-model
Bases: AbstractParseFunction, BaseModel
This parser does not do any parsing. It just returns the model response as both the thought and action.
Fields:
-
error_message(str) -
type(Literal['identity'])
error_message
pydantic-field
error_message: str = ' It seems like something went wrong with your output. Please try again.\n '
type
pydantic-field
type: Literal['identity'] = 'identity'
Type for (de)serialization. Do not change.
FunctionCallingParser
pydantic-model
Bases: AbstractParseFunction, BaseModel
Expects the model response to be a LiteLLM tool call.
Fields:
-
error_message(str) -
type(Literal['function_calling'])
error_message
pydantic-field
error_message: str
type
pydantic-field
type: Literal['function_calling'] = 'function_calling'
Type for (de)serialization. Do not change.
JsonParser
pydantic-model
Bases: AbstractParseFunction, BaseModel
Expects the model response to be a JSON object.
Fields:
-
error_message(str) -
type(Literal['json'])
error_message
pydantic-field
error_message: str
type
pydantic-field
type: Literal['json'] = 'json'
Type for (de)serialization. Do not change.
BashCodeBlockParser
pydantic-model
Bases: AbstractParseFunction, BaseModel
Executes all commands in ```bash code blocks.
Fields:
-
error_message(str) -
type(Literal['all_bash_code_blocks'])
error_message
pydantic-field
error_message: str
type
pydantic-field
type: Literal['all_bash_code_blocks'] = 'all_bash_code_blocks'
SingleBashCodeBlockParser
pydantic-model
Bases: AbstractParseFunction, BaseModel
Executes all commands in ```bash code blocks.
Fields:
-
error_message(str) -
type(Literal['single_bash_code_block'])
error_message
pydantic-field
error_message: str
type
pydantic-field
type: Literal['single_bash_code_block'] = 'single_bash_code_block'