🛠️ Tools¶
evoagentx.tools ¶
BaseModule ¶
Bases: BaseModel
Base module class that serves as the foundation for all modules in the EvoAgentX framework.
This class provides serialization/deserialization capabilities, supports creating instances from dictionaries, JSON, or files, and exporting instances to these formats.
Attributes:
Name | Type | Description |
---|---|---|
class_name |
str
|
The class name, defaults to None but is automatically set during subclass initialization |
model_config |
Pydantic model configuration that controls type matching and behavior |
Initializes a BaseModule instance.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
**kwargs
|
Any
|
Keyword arguments used to initialize the instance |
{}
|
Raises:
Type | Description |
---|---|
ValidationError
|
When parameter validation fails |
Exception
|
When other errors occur during initialization |
Source code in evoagentx/core/module.py
kwargs
property
¶
Returns the extra fields of the model.
Returns:
Name | Type | Description |
---|---|---|
dict |
dict
|
Dictionary containing all extra keyword arguments |
__init_subclass__ ¶
Subclass initialization method that automatically sets the class_name attribute.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
cls
|
Type
|
The subclass being initialized |
required |
**kwargs
|
Any
|
Additional keyword arguments |
{}
|
Source code in evoagentx/core/module.py
__str__ ¶
Returns a string representation of the object.
Returns:
Name | Type | Description |
---|---|---|
str |
str
|
String representation of the object |
from_dict
classmethod
¶
from_dict(data: Dict[str, Any], **kwargs) -> BaseModule
Instantiate the BaseModule from a dictionary.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
data
|
Dict[str, Any]
|
Dictionary containing instance data |
required |
**kwargs
|
Any
|
Additional keyword arguments, can include log to control logging output |
{}
|
Returns:
Name | Type | Description |
---|---|---|
BaseModule |
BaseModule
|
The created module instance |
Raises:
Type | Description |
---|---|
Exception
|
When errors occur during initialization |
Source code in evoagentx/core/module.py
from_json
classmethod
¶
from_json(content: str, **kwargs) -> BaseModule
Construct the BaseModule from a JSON string.
This method uses yaml.safe_load to parse the JSON string into a Python object, which supports more flexible parsing than standard json.loads (including handling single quotes, trailing commas, etc). The parsed data is then passed to from_dict to create the instance.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
content
|
str
|
JSON string |
required |
**kwargs
|
Any
|
Additional keyword arguments, can include |
{}
|
Returns:
Name | Type | Description |
---|---|---|
BaseModule |
BaseModule
|
The created module instance |
Raises:
Type | Description |
---|---|
ValueError
|
When the input is not a valid JSON string |
Source code in evoagentx/core/module.py
from_str
classmethod
¶
from_str(content: str, **kwargs) -> BaseModule
Construct the BaseModule from a string that may contain JSON.
This method is more forgiving than from_json
as it can extract valid JSON
objects embedded within larger text. It uses parse_json_from_text
to extract
all potential JSON strings from the input text, then tries to create an instance
from each extracted JSON string until successful.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
content
|
str
|
Text that may contain JSON strings |
required |
**kwargs
|
Any
|
Additional keyword arguments, can include |
{}
|
Returns:
Name | Type | Description |
---|---|---|
BaseModule |
BaseModule
|
The created module instance |
Raises:
Type | Description |
---|---|
ValueError
|
When the input does not contain valid JSON strings or the JSON is incompatible with the class |
Source code in evoagentx/core/module.py
load_module
classmethod
¶
Load the values for a module from a file.
By default, it opens the specified file and uses yaml.safe_load
to parse its contents
into a Python object (typically a dictionary).
Parameters:
Name | Type | Description | Default |
---|---|---|---|
path
|
str
|
The path of the file |
required |
**kwargs
|
Any
|
Additional keyword arguments |
{}
|
Returns:
Name | Type | Description |
---|---|---|
dict |
dict
|
The JSON object instantiated from the file |
Source code in evoagentx/core/module.py
from_file
classmethod
¶
from_file(path: str, load_function: Callable = None, **kwargs) -> BaseModule
Construct the BaseModule from a file.
This method reads and parses a file into a data structure, then creates
a module instance from that data. It first verifies that the file exists,
then uses either the provided load_function
or the default load_module
method to read and parse the file content, and finally calls from_dict
to create the instance.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
path
|
str
|
The path of the file |
required |
load_function
|
Callable
|
The function used to load the data, takes a file path as input and returns a JSON object |
None
|
**kwargs
|
Any
|
Additional keyword arguments, can include |
{}
|
Returns:
Name | Type | Description |
---|---|---|
BaseModule |
BaseModule
|
The created module instance |
Raises:
Type | Description |
---|---|
ValueError
|
When the file does not exist |
Source code in evoagentx/core/module.py
to_dict ¶
Convert the BaseModule to a dictionary.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
exclude_none
|
bool
|
Whether to exclude fields with None values |
True
|
ignore
|
List[str]
|
List of field names to ignore |
[]
|
**kwargs
|
Any
|
Additional keyword arguments |
{}
|
Returns:
Name | Type | Description |
---|---|---|
dict |
dict
|
Dictionary containing the object data |
Source code in evoagentx/core/module.py
to_json ¶
Convert the BaseModule to a JSON string.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
use_indent
|
bool
|
Whether to use indentation |
False
|
ignore
|
List[str]
|
List of field names to ignore |
[]
|
**kwargs
|
Any
|
Additional keyword arguments |
{}
|
Returns:
Name | Type | Description |
---|---|---|
str |
str
|
The JSON string |
Source code in evoagentx/core/module.py
to_str ¶
Convert the BaseModule to a string. Use .to_json to output JSON string by default.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
**kwargs
|
Any
|
Additional keyword arguments |
{}
|
Returns:
Name | Type | Description |
---|---|---|
str |
str
|
The string |
Source code in evoagentx/core/module.py
save_module ¶
Save the BaseModule to a file.
This method will set non-serializable objects to None by default.
If you want to save non-serializable objects, override this method.
Remember to also override the load_module
function to ensure the loaded
object can be correctly parsed by cls.from_dict
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
path
|
str
|
The path to save the file |
required |
ignore
|
List[str]
|
List of field names to ignore |
[]
|
**kwargs
|
Any
|
Additional keyword arguments |
{}
|
Returns:
Name | Type | Description |
---|---|---|
str |
str
|
The path where the file is saved, same as the input path |
Source code in evoagentx/core/module.py
deepcopy ¶
Deep copy the module.
This is a tweak to the default python deepcopy that only deep copies self.parameters()
, and for other
attributes, we just do the shallow copy.
Source code in evoagentx/core/module.py
Tool ¶
Bases: BaseModule
An interface for all the tools.