tool
¶
Tool
¶
Tool(function: Callable)
Bases: BaseModel
A wrapper class for functions that allows them to be both directly callable and serializable to JSON for use with an LLM.
Attributes:
| Name | Type | Description |
|---|---|---|
function |
Callable
|
The function to be wrapped. |
description |
FunctionJSONSchema
|
A TypedDict that describes the function according to JSON schema standards. |
name |
str
|
The name of the function, extracted from the FunctionJSONSchema. |
See also
- [
Agent][silverlingua.core.templates.agent.Agent] - [
Model][silverlingua.core.templates.model.Model]
Example
def my_function(x, y):
return x + y
# Create a Tool instance
tool_instance = Tool(my_function)
# Directly call the wrapped function
result = tool_instance(1, 2) # Output will be 3
# Serialize to JSON
serialized = str(tool_instance)
####
# Alternatively, you can use the decorator to turn a function into a Tool.
####
@tool
def my_function(x, y):
return x + y
# Now the function itself is already a Tool.
result = my_function(1, 2) # Output will be 3
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
function
|
Callable
|
The function to be turned into a Tool. |
required |
Source code in src/SilverLingua/core/atoms/tool/tool.py
description
class-attribute
instance-attribute
¶
description: FunctionJSONSchema = Field(validate_default=True)
use_function_call
¶
use_function_call(function_call: ToolCallFunction)
Uses a FunctionCall to call the function.
Source code in src/SilverLingua/core/atoms/tool/tool.py
FunctionJSONSchema
¶
Bases: BaseModel
A function according to JSON schema standards.
This is also passed in to OpenAI ChatCompletion API functions list so the AI understands how to call a function.
Attributes:
| Name | Type | Description |
|---|---|---|
name |
str
|
The name of the function |
description |
str
|
A description of the function |
parameters |
Parameters
|
A dictionary of parameters and their types (Optional) |
Example:
{
"name": "roll_dice",
"description": "Rolls a number of dice with a given number of sides, optionally
with a modifier and/or advantage/disadvantage.
Returns `{result: int, rolls: int[]}`",
"parameters": {
"type": "object",
"properties": {
"sides": {
"description": "The number of sides on each die",
"type": "integer"
},
"dice": {
"description": "The number of dice to roll (default 1)",
"type": "integer"
},
"modifier": {
"description": "The modifier to add to the roll total (default 0)",
"type": "integer"
},
"advantage": {
"description": "Whether to roll with advantage (default False)",
"type": "boolean"
},
"disadvantage": {
"description": "Whether to roll with disadvantage (default False)",
"type": "boolean"
}
}
}
}
ToolCall
¶
Bases: BaseModel
model_config
class-attribute
instance-attribute
¶
model_config = ConfigDict(extra='allow', ignored_types=(type(None)))
string_if_none
¶
concat
¶
Concatenates two tool calls and returns the result.
If the IDs are different, prioritize the ID of 'self'. For 'function', merge the 'name' and 'arguments' fields.
We will prefer the id of self over other for 2 reasons:
1. We assume that self is the older of the two
2. The newer may be stream chunked, in which case
the id of other may have been None and generated
using UUID, but the older ID likely was generated
by an API and thus this newer ID is not the true ID.
Source code in src/SilverLingua/core/atoms/tool/util.py
ToolCallFunction
¶
Bases: BaseModel
ToolCallResponse
¶
Bases: BaseModel
The response property of a tool call.
from_tool_call
classmethod
¶
from_tool_call(tool_call: ToolCall, response: str) -> ToolCallResponse
ToolCalls
¶
Bases: BaseModel
A list of tool calls.
list
class-attribute
instance-attribute
¶
concat
¶
Concatenates two tool calls lists and returns the result.