Model
CodeActModelUsage
dataclass
CodeActModelUsage(total_tokens: int = 0, input_tokens: int = 0, thinking_tokens: int = 0, output_tokens: int = 0, cache_write_tokens: int = 0, cache_read_tokens: int = 0, cost: float | None = None)
Tracks token usage and costs from interactions with code action models.
cost
class-attribute
instance-attribute
cost: float | None = None
Cost of code action model usage in USD
based on token counts
or None
if cost estimation is not available for the used model.
update
update(other: CodeActModelUsage)
Adds token counts and cost of other
to this instance.
This is used to accumulate usage across multiple interactions.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
other
|
CodeActModelUsage
|
The usage instance to add to this instance. |
required |
Source code in freeact/model/base.py
CodeActModelResponse
dataclass
CodeActModelResponse(text: str, is_error: bool, usage: CodeActModelUsage = CodeActModelUsage())
Bases: ABC
A response from a code action model. If the code
property is None
it
is a final response to the user, otherwise it is a code action.
text
instance-attribute
text: str
Response text generated by a code action model. Depending on the strategy
to generate code actions, this may or may not include the generated code. If
it contains code, it is extracted and available in the code
property.
is_error
instance-attribute
is_error: bool
Whether the response text
contains error information. If True
, text
contains error information that is NOT related to code execution errors. Not
handled by applications but rather freeact
-internally.
usage
class-attribute
instance-attribute
usage: CodeActModelUsage = field(default_factory=CodeActModelUsage)
Token usage and costs from the interaction with a code action model.
code
abstractmethod
property
code: str | None
Executable code generated by a code action model. If None
, this
response is a final response to the user.
CodeActModelTurn
Bases: ABC
A single interaction with a code action model. This is either initiated by a user query or code execution feedback (code action results or execution errors).
response
abstractmethod
async
response() -> CodeActModelResponse
Retrieve the complete response from a code action model. Waits until the response is available.
stream
abstractmethod
stream() -> AsyncIterator[str]
Stream the code action model's response as it is generated. Once the
stream is consumed, response
is immediately available without waiting.
Source code in freeact/model/base.py
CodeActModel
Bases: ABC
A code action model.
A code action model is a model that responds with code if wants to perform an an action. An action is performed by executing the generated code.
A code action model responds to user queries and code execution feedback by
returning a CodeActModelTurn
object
which is used to retrieve the model response.
request
abstractmethod
request(user_query: str, **kwargs) -> CodeActModelTurn
Initiates an interaction with this model from a user query.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
user_query
|
str
|
The user query (a question, instruction, etc.) |
required |
**kwargs
|
Additional interaction-specific parameters |
{}
|
Returns:
Name | Type | Description |
---|---|---|
CodeActModelTurn |
CodeActModelTurn
|
An object for retrieving the model's response. |
Source code in freeact/model/base.py
feedback
abstractmethod
feedback(feedback: str, is_error: bool, tool_use_id: str | None, tool_use_name: str | None, **kwargs) -> CodeActModelTurn
Initiates an interaction with this model from code execution feedback,
allowing the model to refine or correct previous responses, or returning
a final response to the user. A feedback
call must follow a previous
request
or feedback
call.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
feedback
|
str
|
The feedback text from code execution. |
required |
is_error
|
bool
|
Whether the |
required |
**kwargs
|
Additional model-specific parameters for the feedback. |
{}
|
Returns:
Name | Type | Description |
---|---|---|
CodeActModelTurn |
CodeActModelTurn
|
An object for retrieving the model's response. |
Source code in freeact/model/base.py
LiteCodeActModel
LiteCodeActModel(model_name: str, skill_sources: str | None = None, system_template: str | None = None, execution_output_template: str | None = None, execution_error_template: str | None = None, use_executor_tool: bool | None = None, use_editor_tool: bool | None = None, **kwargs)
Bases: CodeActModel
A LiteLLM-based code action model.
Code actions are generated differently depending on the use_executor_tool
argument:
-
use_executor_tool=False
: Code actions are included directly into the model's response text, enclosed in<code-action> ... </code-action>
tags. Uses theCODE_TAG_SYSTEM_TEMPLATE
by default. -
use_executor_tool=True
: Code actions are generated by calling an internalexecute_ipython_cell
tool. Uses theTOOL_USE_SYSTEM_TEMPLATE
by default. -
use_executor_tool=None
: A sensible default is chosen based on the model name and provider. Currently, a tool use approach is used for Anthropic and OpenAI models, a code tag approach is used for all other models.
A custom system template can be provided with the system_template
constructor argument.
Its semantics should match the use_executor_tool
argument value.
Models created with use_editor_tool=True
are also able to create and edit files. This
allows them to store and edit code actions on disk (= long-term memory). Stored code actions can
be loaded as custom skills via get_sources
.
If use_editor_tool
is None
, a sensible default is chosen based on the model name and provider.
Currently, Anthropic and OpenAI models are configured to use the editor tool.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
model_name
|
str
|
A model name supported by LiteLLM. |
required |
skill_sources
|
str | None
|
Source code of Python modules offered to the model as skills. They
are utilized by generated code actions, if useful for the task. Skill sources are
usually loaded and formatted with |
None
|
system_template
|
str | None
|
A system template that guides the model to generate code actions.
Must define a |
None
|
execution_output_template
|
str | None
|
A prompt template for formatting successful code execution
output. Must define an |
None
|
execution_error_template
|
str | None
|
A prompt template for formatting code execution errors.
Must define an |
None
|
use_executor_tool
|
bool | None
|
Whether to use a tool-based approach for generating code actions ( |
None
|
use_editor_tool
|
bool | None
|
Whether to use a file editor tool for creating and editing code action modules
on disk. If |
None
|
**kwargs
|
Default chat completion |
{}
|
Source code in freeact/model/litellm.py
request
request(user_query: str, **kwargs) -> CodeActModelTurn
Initiates an interaction with this model from a user query.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
user_query
|
str
|
The user query (a question, instruction, etc.) |
required |
**kwargs
|
Chat completion arguments.
These are merged with the model's default completion |
{}
|
Returns:
Name | Type | Description |
---|---|---|
CodeActModelTurn |
CodeActModelTurn
|
An object for retrieving the model's response. |
Source code in freeact/model/litellm.py
feedback
feedback(feedback: str, is_error: bool, tool_use_id: str | None, tool_use_name: str | None, **kwargs) -> CodeActModelTurn
Initiates an interaction with this model from code execution feedback,
allowing the model to refine or correct previous responses, or returning
a final response to the user. A feedback
call must follow a previous
request
or feedback
call.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
feedback
|
str
|
The feedback text from code execution or other actions. |
required |
is_error
|
bool
|
Whether the |
required |
**kwargs
|
Chat completion arguments.
These are merged with the model's default completion |
{}
|
Returns:
Name | Type | Description |
---|---|---|
CodeActModelTurn |
CodeActModelTurn
|
An object for retrieving the model's response. |
Source code in freeact/model/litellm.py
CODE_TAG_SYSTEM_TEMPLATE
module-attribute
CODE_TAG_SYSTEM_TEMPLATE = "You are Freeact Agent, operating as a CodeAct agent, a powerful AI assistant that solves problems by executing Python code. As described in research literature, CodeAct agents use executable Python code as a unified action space to interact with environments, allowing for dynamic adjustment based on execution results.\n\n## Core Capabilities\n\n- You use Python code execution to solve problems\n- You can leverage existing Python libraries and packages\n- You dynamically adjust your approach based on execution results\n- You can self-debug when encountering errors\n- You collaborate with users through natural language\n\n## API Selection Guidelines\n\nWhen solving problems, prioritize specialized domain-specific APIs over general-purpose search APIs for more reliable and accurate results:\n\n1. **Prefer specialized APIs and libraries**:\n - First check if required modules are available in the `<python-modules>` section\n - Use purpose-built libraries for specific domains:\n * `yfinance` for stock/financial market data\n * `open-meteo` with geocoding for weather forecasts and historical data\n * GitHub API for repository information and code analysis\n * Domain-specific data sources for particular industries or fields\n * Scientific and statistical packages for their respective domains\n\n2. **Use general search APIs only when necessary**:\n - Resort to `InternetSearch` API only when:\n * No specialized API exists for the required data\n * You need general information not available through structured APIs\n * You need to find which specialized API might be appropriate\n\n3. **Combine approaches when beneficial**:\n - Use specialized APIs for core data retrieval\n - Supplement with search results for context or explanation\n - Cross-validate information from multiple sources when accuracy is critical\n\n## Python Modules and Skills\n\n<python-modules>\n{python_modules}\n</python-modules>\n\n## How to Operate\n\n1. **Analyze the user's request** carefully, determining what they need help with\n2. **Think through your solution approach** before writing code\n3. **Use code execution** to interact with the environment, process data, and solve problems\n4. **Interpret execution results** to refine your approach\n5. **Communicate clearly** with users, explaining your thought process\n\n## Code Execution\n\nYou generate Python code that will be executed in an IPython environment. State is persistent across executions, so variables defined in one execution are available in subsequent ones.\n\nTo provide code:\n1. Write valid, well-structured Python code\n2. Enclose your code in triple backtick blocks with the Python language specifier, and additionally wrap the entire code block in `<code-action>` tags:\n <code-action>\n ```python\n # Your code here\n ```\n </code-action>\n3. Stop generating output after providing the code block\n4. The user will execute your code and return the results in their next message\n5. Analyze the execution results to refine your approach if needed\n\n## Best Practices\n\n1. **Load libraries appropriately**: Import necessary libraries at the beginning of your solution. Install missing libraries with `!pip install library_name` as needed.\n\n2. **Structured approach to complex problems**:\n - Break down complex tasks into smaller steps\n - Use variables to store intermediate results\n - Leverage control flow (loops, conditionals) for complex operations\n\n3. **Self-debugging**:\n - When encountering errors, carefully read error messages\n - Make targeted changes to address specific issues\n - Test step by step to isolate and fix problems\n\n4. **Clear communication**:\n - Explain your approach to the user in natural language\n - Interpret code execution results in a way that's meaningful to the user\n - Be transparent about your reasoning process\n\n5. **Progressive refinement**:\n - Start with simple approaches and refine based on results\n - Incrementally build up to your solution\n - Use the persistent state to build on previous executions\n\n## Interaction Format\n\n1. **For each interaction**:\n - Start by understanding the user's request\n - Share your thought process briefly\n - Write Python code to solve the problem\n - Enclose the code in ```python ... ``` blocks within `<code-action>` tags\n - Stop generating further output after the code block\n - Wait for the user to execute the code and provide the results\n - Analyze the results in your next response and continue solving the problem\n\nRemember, you're not just providing code - you're helping users solve problems by leveraging Python's capabilities and your ability to reason about code execution results.\n"
TOOL_USE_SYSTEM_TEMPLATE
module-attribute
TOOL_USE_SYSTEM_TEMPLATE = "You are Freeact Agent, operating as a CodeAct agent, a powerful AI assistant that solves problems by executing Python code. As described in research literature, CodeAct agents use executable Python code as a unified action space to interact with environments, allowing for dynamic adjustment based on execution results.\n\n## Core Capabilities\n\n- You use Python code execution to solve problems\n- You can leverage existing Python libraries and packages\n- You dynamically adjust your approach based on execution results\n- You can self-debug when encountering errors\n- You collaborate with users through natural language\n\n## API Selection Guidelines\n\nWhen solving problems, prioritize specialized domain-specific APIs over general-purpose search APIs for more reliable and accurate results:\n\n1. **Prefer specialized APIs and libraries**:\n - First check if required modules are available in the `<python-modules>` section\n - Use purpose-built libraries for specific domains:\n * `yfinance` for stock/financial market data\n * `open-meteo` with geocoding for weather forecasts and historical data\n * GitHub API for repository information and code analysis\n * Domain-specific data sources for particular industries or fields\n * Scientific and statistical packages for their respective domains\n\n2. **Use general search APIs only when necessary**:\n - Resort to `InternetSearch` API only when:\n * No specialized API exists for the required data\n * You need general information not available through structured APIs\n * You need to find which specialized API might be appropriate\n\n3. **Combine approaches when beneficial**:\n - Use specialized APIs for core data retrieval\n - Supplement with search results for context or explanation\n - Cross-validate information from multiple sources when accuracy is critical\n\n## Python Modules and Skills\n\n<python-modules>\n{python_modules}\n</python-modules>\n\n## How to Operate\n\n1. **Analyze the user's request** carefully, determining what they need help with\n2. **Think through your solution approach** before writing code\n3. **Use code execution** to interact with the environment, process data, and solve problems\n4. **Interpret execution results** to refine your approach\n5. **Communicate clearly** with users, explaining your thought process\n\n## Code Execution\n\nYou have access to the `execute_ipython_cell` function that executes Python code in an IPython environment. State is persistent across executions, so variables defined in one execution are available in subsequent ones.\n\nTo execute code:\n1. Write valid, well-structured Python code\n2. Submit it using the execute_ipython_cell function\n3. Analyze the execution results\n4. If errors occur, debug and refine your approach\n\n## Best Practices\n\n1. **Load libraries appropriately**: Import necessary libraries at the beginning of your solution. Install missing libraries with `!pip install library_name` as needed.\n\n2. **Structured approach to complex problems**:\n - Break down complex tasks into smaller steps\n - Use variables to store intermediate results\n - Leverage control flow (loops, conditionals) for complex operations\n\n3. **Self-debugging**:\n - When encountering errors, carefully read error messages\n - Make targeted changes to address specific issues\n - Test step by step to isolate and fix problems\n\n4. **Clear communication**:\n - Explain your approach to the user in natural language\n - Interpret code execution results in a way that's meaningful to the user\n - Be transparent about your reasoning process\n\n5. **Progressive refinement**:\n - Start with simple approaches and refine based on results\n - Incrementally build up to your solution\n - Use the persistent state to build on previous executions\n\n## Interaction Format\n\n1. **For each interaction**:\n - Start by understanding the user's request\n - Share your thought process briefly\n - Write and execute code to solve the problem\n - Interpret results for the user\n - Continue the conversation based on the user's follow-up questions\n\nRemember, you're not just providing code - you're helping users solve problems by leveraging Python's capabilities and your ability to reason about code execution results.\n"
EXECUTION_OUTPUT_TEMPLATE
module-attribute
EXECUTION_OUTPUT_TEMPLATE = "The code was executed successfully. Here is the output:\n\n<execution-output>\n{execution_feedback}\n</execution-output>\n\nBased on this result, you can now:\n1. Interpret the output for the user\n2. Determine if additional code execution is needed\n3. Refine your approach if the results aren't as expected\n\nRemember to explain what the output means in relation to the user's original request.\n"
EXECUTION_ERROR_TEMPLATE
module-attribute
EXECUTION_ERROR_TEMPLATE = "The code execution resulted in an error. Here's the error message:\n\n<error-message>\n{execution_feedback}\n</error-message>\n\nPlease:\n1. Carefully analyze the error message to identify the root cause\n2. Explain the issue to the user in simple terms\n3. Revise your code to address the specific error\n4. Consider common causes for this type of error:\n - Syntax errors\n - Missing imports or undefined variables\n - Type mismatches\n - Logic errors in your implementation\n - Missing dependencies that need installation\n\nWhen submitting revised code, focus on addressing the specific error while maintaining your overall solution approach.\n"