💾 存储模块¶
evoagentx.storages ¶
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
DBStoreFactory ¶
Factory class for creating database store instances based on provider and configuration. Maps provider names to specific database store classes.
create
classmethod
¶
Create a database store instance for the specified provider.
Attributes:
Name | Type | Description |
---|---|---|
provider_name |
str
|
Name of the database provider (e.g., 'sqlite', 'posgre_sql'). |
config |
DBConfig
|
Configuration for the database store. |
Returns:
Name | Type | Description |
---|---|---|
DBStoreBase |
An instance of the database store. |
Raises:
Type | Description |
---|---|
ValueError
|
If the provider is not supported. |
Source code in evoagentx/utils/factory.py
VectorStoreFactory ¶
Factory class for creating vector store instances based on configuration. Maps provider names to specific vector store classes.
create ¶
Create a vector store instance based on the provided configuration.
Attributes:
Name | Type | Description |
---|---|---|
config |
VectorStoreConfig
|
Configuration for the vector store. |
Returns:
Name | Type | Description |
---|---|---|
VectorStoreBase |
An instance of the vector store. |
Source code in evoagentx/utils/factory.py
GraphStoreFactory ¶
Factory class for creating graph store instances based on configuration. Maps provider names to specific graph store classes.
create
classmethod
¶
Create a graph store instance based on the provided configuration.
Attributes:
Name | Type | Description |
---|---|---|
config |
VectorStoreConfig
|
Configuration for the graph store. |
Returns:
Name | Type | Description |
---|---|---|
GraphStoreBase |
An instance of the graph store. |
Source code in evoagentx/utils/factory.py
StoreConfig ¶
Bases: BaseConfig
Aggregates database, vector, file, and graph store configurations.
Source code in evoagentx/core/module.py
TableType ¶
Bases: str
, Enum
Enum representing the default table types for the database. Each value corresponds to a specific table name used for storing different types of data.
AgentStore ¶
Bases: BaseModel
Stores agent metadata with a unique name and content dictionary.
WorkflowStore ¶
Bases: BaseModel
Stores workflow metadata with a unique name and content dictionary.
MemoryStore ¶
Bases: BaseModel
Stores memory-related metadata with optional fields for keywords, entities, and embeddings.
HistoryStore ¶
Bases: BaseModel
Stores changes to memory with event details and timestamps.
StorageHandler ¶
Bases: BaseModule
Implementation of a storage handler for managing various storage backends.
StorageHandler provides an abstraction for reading and writing data (e.g., memory, agents, workflows). It supports multiple storage types, including database, vector, and graph storage, initialized via factories.
Source code in evoagentx/core/module.py
init_module ¶
Initialize all storage backends based on the provided configuration. Calls individual initialization methods for database, vector, and graph stores.
Source code in evoagentx/storages/base.py
load ¶
Load all data from the database storage.
Attributes:
Name | Type | Description |
---|---|---|
tables |
Optional[List[str]]
|
List of table names to load; if None, loads all tables. |
Returns:
Type | Description |
---|---|
Dict[str, Any]
|
Dict[str, Dict[str, str]]: A dictionary with table names as keys and lists of records as values. You should parse the values by yourself. |
Source code in evoagentx/storages/base.py
save ¶
Save all provided data to the database storage.
Attributes:
Name | Type | Description |
---|---|---|
data |
Dict[str, Any]
|
Dictionary with table names as keys and lists of records to save. |
Raises:
Type | Description |
---|---|
ValueError
|
If an unknown table name is provided. |
Source code in evoagentx/storages/base.py
parse_result ¶
parse_result(results: Dict[str, str], store: Union[AgentStore, WorkflowStore, MemoryStore, HistoryStore]) -> Dict[str, Any]
Parse database results, converting JSON strings to Python objects where applicable.
Attributes:
Name | Type | Description |
---|---|---|
results |
Dict[str, str]
|
Raw database results with column names as keys. |
store |
Union[AgentStore, WorkflowStore, MemoryStore, HistoryStore]
|
Pydantic model for validation. |
Returns:
Type | Description |
---|---|
Dict[str, Any]
|
Dict[str, Any]: Parsed results with JSON strings deserialized to Python objects. |
Source code in evoagentx/storages/base.py
load_memory ¶
Load a single long-term memory data.
Attributes:
Name | Type | Description |
---|---|---|
memory_id |
str
|
The ID of the long-term memory. |
table |
Optional[str]
|
The table name; defaults to 'memory' if None. |
Returns:
Type | Description |
---|---|
Dict[str, Any]
|
Dict[str, Any]: The data that can be used to create a LongTermMemory instance. |
Source code in evoagentx/storages/base.py
save_memory ¶
Save or update a single memory.
Attributes:
Name | Type | Description |
---|---|---|
memory_data |
Dict[str, Any]
|
The long-term memory's data. |
table |
Optional[str]
|
The table name; defaults to 'memory' if None. |
Source code in evoagentx/storages/base.py
load_agent ¶
Load a single agent's data.
Attributes:
Name | Type | Description |
---|---|---|
agent_name |
str
|
The unique name of the agent to retrieve. |
table |
Optional[str]
|
The table name; defaults to 'agent' if None. |
Returns:
Type | Description |
---|---|
Dict[str, Any]
|
Dict[str, Any]: The data that can be used to create an Agent instance, or None if not found. |
Source code in evoagentx/storages/base.py
remove_agent ¶
Remove an agent from storage if the agent exists.
Attributes:
Name | Type | Description |
---|---|---|
agent_name |
str
|
The name of the agent to be deleted. |
table |
Optional[str]
|
The table name; defaults to 'agent' if None. |
Raises:
Type | Description |
---|---|
ValueError
|
If the agent does not exist in the specified table. |
Source code in evoagentx/storages/base.py
save_agent ¶
Save or update a single agent's data.
Attributes:
Name | Type | Description |
---|---|---|
agent_data |
Dict[str, Any]
|
The agent's data, must include 'name' and 'content' keys. |
table |
Optional[str]
|
The table name; defaults to 'agent' if None. |
Raises:
Type | Description |
---|---|
ValueError
|
If 'name' field is missing or if Pydantic validation fails. |
Source code in evoagentx/storages/base.py
load_workflow ¶
Load a single workflow's data.
Attributes:
Name | Type | Description |
---|---|---|
workflow_id |
str
|
The ID of the workflow. |
table |
Optional[str]
|
The table name; defaults to 'workflow' if None. |
Returns:
Type | Description |
---|---|
Dict[str, Any]
|
Dict[str, Any]: The data that can be used to create a WorkFlow instance, or None if not found. |
Source code in evoagentx/storages/base.py
save_workflow ¶
Save or update a workflow's data.
Attributes:
Name | Type | Description |
---|---|---|
workflow_data |
Dict[str, Any]
|
The workflow's data, must include 'name' field. |
table |
Optional[str]
|
The table name; defaults to 'workflow' if None. |
Raises:
Type | Description |
---|---|
ValueError
|
If 'name' field is missing or if Pydantic validation fails. |
Source code in evoagentx/storages/base.py
load_history ¶
Load a single history entry.
Attributes:
Name | Type | Description |
---|---|---|
memory_id |
str
|
The ID of the memory associated with the history entry. |
table |
Optional[str]
|
The table name; defaults to 'history' if None. |
Returns:
Type | Description |
---|---|
Dict[str, Any]
|
Dict[str, Any]: The history data, or None if not found. |
Source code in evoagentx/storages/base.py
save_history ¶
Save or update a single history entry.
Attributes:
Name | Type | Description |
---|---|---|
history_data |
Dict[str, Any]
|
The history data, must include 'memory_id' field. |
table |
Optional[str]
|
The table name; defaults to 'history' if None. |
Raises:
Type | Description |
---|---|
ValueError
|
If 'memory_id' field is missing or if Pydantic validation fails. |