🧠 核心模块¶
evoagentx.core ¶
BaseConfig ¶
Bases: BaseModule
Base configuration class that serves as parent for all configuration classes.
A config should inherit BaseConfig and specify the attributes and their types. Otherwise this will be an empty config.
Source code in evoagentx/core/module.py
save ¶
Save configuration to the specified path.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
path
|
str
|
The file path to save the configuration |
required |
**kwargs
|
Any
|
Additional keyword arguments passed to save_module method |
{}
|
Returns:
Name | Type | Description |
---|---|---|
str |
str
|
The path where the file was saved |
Source code in evoagentx/core/base_config.py
get_config_params ¶
Get a list of configuration parameters.
Returns:
Type | Description |
---|---|
List[str]
|
List[str]: List of configuration parameter names, excluding 'class_name' |
Source code in evoagentx/core/base_config.py
get_set_params ¶
Get a dictionary of explicitly set parameters.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
ignore
|
List[str]
|
List of parameter names to ignore |
[]
|
Returns:
Name | Type | Description |
---|---|---|
dict |
dict
|
Dictionary of explicitly set parameters, excluding 'class_name' and ignored parameters |
Source code in evoagentx/core/base_config.py
Message ¶
Bases: BaseModule
the base class for message.
Attributes:
Name | Type | Description |
---|---|---|
content |
Any
|
the content of the message, need to implement str() function. |
agent |
str
|
the sender of the message, normally set as the agent name. |
action |
str
|
the trigger of the message, normally set as the action name. |
prompt |
str
|
the prompt used to obtain the generated text. |
next_actions |
List[str]
|
the following actions. |
msg_type |
str
|
the type of the message, such as "request", "response", "command" etc. |
wf_goal |
str
|
the goal of the whole workflow. |
wf_task |
str
|
the name of a task in the workflow, i.e., the |
wf_task_desc |
str
|
the description of a task in the workflow, i.e., the |
message_id |
str
|
the unique identifier of the message. |
timestamp |
str
|
the timestame of the message. |
Source code in evoagentx/core/module.py
sort_by_timestamp
classmethod
¶
sort the messages based on the timestamp.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
messages
|
List[Message]
|
the messages to be sorted. |
required |
reverse
|
bool
|
If True, sort the messages in descending order. Otherwise, sort the messages in ascending order. |
False
|
Source code in evoagentx/core/message.py
sort
classmethod
¶
sort(messages: List[Message], key: Optional[Callable[[Message], Any]] = None, reverse: bool = False) -> List[Message]
sort the messages using key or timestamp (by default).
Parameters:
Name | Type | Description | Default |
---|---|---|---|
messages
|
List[Message]
|
the messages to be sorted. |
required |
key
|
Optional[Callable[[Message], Any]]
|
the function used to sort messages. |
None
|
reverse
|
bool
|
If True, sort the messages in descending order. Otherwise, sort the messages in ascending order. |
False
|
Source code in evoagentx/core/message.py
merge
classmethod
¶
merge(messages: List[List[Message]], sort: bool = False, key: Optional[Callable[[Message], Any]] = None, reverse: bool = False) -> List[Message]
merge different message list.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
messages
|
List[List[Message]]
|
the message lists to be merged. |
required |
sort
|
bool
|
whether to sort the merged messages. |
False
|
key
|
Optional[Callable[[Message], Any]]
|
the function used to sort messages. |
None
|
reverse
|
bool
|
If True, sort the messages in descending order. Otherwise, sort the messages in ascending order. |
False
|
Source code in evoagentx/core/message.py
Parser ¶
Bases: BaseModule
Source code in evoagentx/core/module.py
parse
classmethod
¶
the method used to parse text into a Parser object. Use Parser.from_str to parse input by default. Args: content: The content to parse **kwargs: Additional keyword arguments Returns: Parser: The parsed Parser object
Source code in evoagentx/core/parser.py
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
ParseFunctionRegistry ¶
register ¶
Register a function with a given name.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
func_name
|
str
|
The name to register the function under |
required |
func
|
Callable
|
The function to register |
required |
Raises:
Type | Description |
---|---|
ValueError
|
If a function with the same name is already registered |
Source code in evoagentx/core/registry.py
get_function ¶
Get a registered function by name.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
func_name
|
str
|
The name of the function to retrieve |
required |
Returns:
Name | Type | Description |
---|---|---|
Callable |
callable
|
The registered function |
Raises:
Type | Description |
---|---|
KeyError
|
If no function with the given name is registered |
Source code in evoagentx/core/registry.py
has_function ¶
Check if a function name is registered.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
func_name
|
str
|
The name to check |
required |
Returns:
Type | Description |
---|---|
bool
|
True if the function name is registered, False otherwise |