LiteLLM
LiteLLMBase
LiteLLMBase(model_name: str, system_instruction: str | None = None, tools: list[dict[str, Any]] | None = None, **kwargs)
Bases: CodeActModel
Base class for all code action models in freeact
.
It uses LiteLLM for model access and cost tracking. It tracks
conversation state in the history
attribute. Subclasses must implement the
extract_code method for extracting
code from a LiteLLMResponse.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
model_name
|
str
|
The LiteLLM-specific name of the model. |
required |
system_instruction
|
str | None
|
A system instruction that guides the model to generate code actions. |
None
|
tools
|
list[dict[str, Any]] | None
|
A list of tool definitions. Some implementation classes use tools for passing code actions as argument while others include code actions directly in their response text. |
None
|
**kwargs
|
{}
|
Attributes:
Name | Type | Description |
---|---|---|
history |
list[dict[str, Any]]
|
List of conversation messages. User messages are either actual user queries
sent via the |
Source code in freeact/model/litellm/model.py
extract_code
abstractmethod
extract_code(response: LiteLLMResponse) -> str | None
feedback
feedback(feedback: str, is_error: bool, tool_use_id: str | None, tool_use_name: str | None, **kwargs) -> LiteLLMTurn
Constructs a new message with role tool
if tool_use_id
is defined,
or with role user
otherwise. Message content is feedback
. It returns
a LiteLLMTurn. After the turn
is consumed, the input message and assistant message are added to the model's
conversation history
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
feedback
|
str
|
The feedback text from code execution. |
required |
is_error
|
bool
|
Whether the feedback represents an error condition. |
required |
tool_use_id
|
str | None
|
Identifier for the specific tool use instance. |
required |
tool_use_name
|
str | None
|
Name of the tool that was used. |
required |
**kwargs
|
Completion kwargs supported by LiteLLM. |
{}
|
Returns:
Name | Type | Description |
---|---|---|
LiteLLMTurn |
LiteLLMTurn
|
Represents a feedback interaction with the model where |
Source code in freeact/model/litellm/model.py
request
request(user_query: str, **kwargs) -> LiteLLMTurn
Constructs a new message with role user
and content user_query
and returns a LiteLLMTurn.
After the turn is consumed, the user message and assistant message are
added to the model's conversation history
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
user_query
|
str
|
The user's input query or request. |
required |
**kwargs
|
Completion kwargs supported by LiteLLM. |
{}
|
Returns:
Name | Type | Description |
---|---|---|
LiteLLMTurn |
LiteLLMTurn
|
Represents a single user interaction. |
Source code in freeact/model/litellm/model.py
LiteLLM
LiteLLM(model_name: str, execution_output_template: str, execution_error_template: str, system_instruction: str | None = None, tools: list[dict[str, Any]] | None = None, **kwargs)
Bases: LiteLLMBase
A default implementation of LiteLLMBase
that
- formats code execution feedback based on provided output and error templates.
- implements extract_code by extracting the first Python code block from the response text.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
model_name
|
str
|
The LiteLLM-specific name of the model. |
required |
execution_output_template
|
str
|
A template for formatting successful code execution output.
Must define a |
required |
execution_error_template
|
str
|
A template for formatting code execution errors.
Must define a |
required |
system_instruction
|
str | None
|
A system instruction that guides the model to generate code actions. |
None
|
tools
|
list[dict[str, Any]] | None
|
A list of tool definitions. Some implementation classes use tools for passing code actions as argument while others include code actions directly in their response text. |
None
|
**kwargs
|
{}
|
Source code in freeact/model/litellm/model.py
extract_code
extract_code(response: LiteLLMResponse) -> str | None
Extracts the first Python code block from response.text
.
Override this method to customize extraction logic.
LiteLLMTurn
LiteLLMTurn(iter: AsyncIterator[str | LiteLLMResponse])
code_block
Finds the index
-th block matching pattern
in text
.
code_blocks
Finds all blocks matching pattern
in text
.
sanitize_tool_name
Sanitizes a tool name by replacing non-alphanumeric characters with underscores.
tool_name
Extracts the tool name from a tool definition.