agent
¶
Agent
¶
Agent(model: Model, idearium: Optional[Idearium] = None, tools: Optional[List[Tool]] = None, auto_append_response: bool = True)
Bases: BaseModel
A wrapper around a [Model][silverlingua.core.templates.model.Model] that utilizes an [Idearium][silverlingua.core.organisms.idearium.Idearium] and a set of [Tool][silverlingua.core.atoms.tool.tool.Tool]s.
This is a base class not meant to be used directly. It is meant to be subclassed by specific model implementations.
However, there is limited boilerplate. The only thing that needs to be
redefined in subclasses is the _bind_tools method.
Additionally, the _use_tools method is a common method to redefine.
Initializes the agent.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
Model
|
The model to use. |
required |
idearium
|
Idearium
|
The idearium to use. If None, a new one will be created. |
None
|
tools
|
List[Tool]
|
The tools to use. |
None
|
Source code in src/SilverLingua/core/templates/agent.py
tools
instance-attribute
¶
The tools used by the agent.
Warning
Do not modify this list directly. Use add_tool, add_tools,
and remove_tool instead.
auto_append_response
class-attribute
instance-attribute
¶
auto_append_response: bool = True
Whether to automatically append the response to the idearium after generating a response.
model_post_init
¶
_find_tool
¶
_use_tools
¶
Uses Tools based on the given ToolCalls, returning Notions containing ToolCallResponses.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tool_calls
|
ToolCalls
|
The ToolCalls to use. |
required |
Returns:
| Type | Description |
|---|---|
List[Notion]
|
List[Notion]: The Notions containing ToolCallResponses. Each Notion will have a role of ChatRole.TOOL_RESPONSE. |
Source code in src/SilverLingua/core/templates/agent.py
_bind_tools
¶
Called at the end of init to bind the tools to the model.
This MUST be redefined in subclasses to dictate how the tools are bound to the model.
Example:
# From OpenAIChatAgent
def _bind_tools(self) -> None:
m_tools: List[ChatCompletionToolParam] = [
{"type": "function", "function": tool.description}
for tool in self.tools
]
if len(m_tools) > 0:
self.model.tools = m_tools
Source code in src/SilverLingua/core/templates/agent.py
add_tools
¶
_process_messages
¶
Convert various message types into a list of Notions.
Source code in src/SilverLingua/core/templates/agent.py
_process_generation
¶
Wrapper around shared logic between generate and agenerate.
Source code in src/SilverLingua/core/templates/agent.py
generate
¶
Generates a response to the given messages by calling the underlying model's generate method and checking/actualizing tool usage.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
messages
|
Union[str, Notion, Idearium, List[Union[str, Notion]]]
|
|
required |
Returns:
| Type | Description |
|---|---|
List[Notion]
|
List[Notion]: A list of responses to the given messages. (Many times there will only be one response.) |
Source code in src/SilverLingua/core/templates/agent.py
agenerate
async
¶
Asynchronously generates a response to the given messages by calling the underlying model's agenerate method and checking/actualizing tool usage.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
messages
|
Union[str, Notion, Idearium, List[Union[str, Notion]]]
|
|
required |
Returns:
| Type | Description |
|---|---|
List[Notion]
|
List[Notion]: A list of responses to the given messages. (Many times there will only be one response.) |
Source code in src/SilverLingua/core/templates/agent.py
_process_tool_calls
¶
_process_tool_calls(tool_calls: ToolCalls)
Processes tool calls and returns the tool response.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tool_calls
|
ToolCalls
|
The tool calls to process. |
required |
Returns:
| Type | Description |
|---|---|
|
Optional[ToolCalls]: The tool response. If None, no tool calls were found. |
Source code in src/SilverLingua/core/templates/agent.py
stream
¶
stream(messages: Messages, **kwargs)
Streams a response to the given prompt by calling the underlying model's stream method and checking/actualizing tool usage.
NOTE: Will raise an exception if the underlying model does not support streaming.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
messages
|
Union[str, Notion, Idearium, List[Union[str, Notion]]]
|
|
required |
Returns:
| Type | Description |
|---|---|
|
Generator[Notion, Any, None]: A generator of responses to the given messages. |
Source code in src/SilverLingua/core/templates/agent.py
astream
async
¶
astream(messages: Messages, **kwargs)
Asynchronously streams a response to the given prompt by calling the underlying model's astream method and checking/actualizing tool usage.
NOTE: Will raise an exception if the underlying model does not support streaming.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
messages
|
Union[str, Notion, Idearium, List[Union[str, Notion]]]
|
|
required |
Returns:
| Type | Description |
|---|---|
|
Generator[Notion, Any, None]: A generator of responses to the given messages. |