Skip to content

Agent Definitions

Agent

Bases: AgentMeta

The Agent class is a concrete implementation of an AI agent with tool-calling capabilities, inheriting from AgentMeta. It integrates a language model, tools, memory, and flow management to process queries, execute tools, and maintain conversational context.

Methods:

Name Description
ainvoke

Answer the user query asynchronously with continuous tool calling capability.

invoke

Answer the user query synchronously with continuous tool calling capability.

stream

Answer the user query by streaming with continuous tool calling capability. Yields streamed responses or the final tool execution result.

save_memory

Save the tool message to the memory

register_tools

Register a list of tools

ainvoke async

ainvoke(query: str, is_save_memory: bool = False, user_id: str = 'unknown_user', max_iterations: int = 10, is_tool_formatted: bool = True, max_history: int = None, **kwargs) -> Any

Answer the user query asynchronously with continuous tool calling capability.

Parameters:

Name Type Description Default
query str

The input query or task description provided by the user.

required
is_save_memory bool

Flag to determine if the conversation should be saved to memory. Defaults to False.

False
user_id str

Identifier for the user making the request. Defaults to "unknown_user".

'unknown_user'
max_iterations int

Maximum number of tool call iterations to prevent infinite loops. Defaults to 10.

10
is_tool_formatted bool

Modifying the output Tool to become more human-preferred. If True, it needs one next llm invoke to format answer, else directly return tool_message. Defaults to True.

True
max_history (int, None)

Number of maximum history messages. Defaults to None.

None
**kwargs

Additional keyword arguments, including an optional config dictionary for graph execution.

{}

Returns:

Name Type Description
Any Any

The result of the tool execution, LLM response, or None if an error occurs during tool execution.

Raises:

Type Description
JSONDecodeError

If the tool data cannot be parsed as valid JSON.

KeyError

If required keys are missing in the tool data.

ValueError

If the tool data is invalid or cannot be processed.

invoke

invoke(query: str, is_save_memory: bool = False, user_id: str = 'unknown_user', max_iterations: int = 10, is_tool_formatted: bool = True, max_history: int = None, **kwargs) -> Any

Answer the user query synchronously with continuous tool calling capability.

Parameters:

Name Type Description Default
query str

The input query or task description provided by the user.

required
is_save_memory bool

Flag to determine if the conversation should be saved to memory. Defaults to False.

False
user_id str

Identifier for the user making the request. Defaults to "unknown_user".

'unknown_user'
max_iterations int

Maximum number of tool call iterations to prevent infinite loops. Defaults to 10.

10
is_tool_formatted bool

Modifying the output Tool to become more human-preferred. If True, it needs one next llm invoke to format answer, else directly return tool_message. Defaults to True.

True
max_history int

The maximum number of messages. Defaults to None.

None
**kwargs

Additional keyword arguments, including an optional config dictionary for graph execution.

{}

Returns:

Name Type Description
Any Any

The result of the tool execution, LLM response, or None if an error occurs during tool execution.

Raises:

Type Description
JSONDecodeError

If the tool data cannot be parsed as valid JSON.

KeyError

If required keys are missing in the tool data.

ValueError

If the tool data is invalid or cannot be processed.

stream

stream(query: str, is_save_memory: bool = False, user_id: str = 'unknown_user', max_iterations: int = 10, is_tool_formatted: bool = True, max_history: int = None, **kwargs) -> AsyncGenerator[Any, None]

Answer the user query by streaming with continuous tool calling capability. Yields streamed responses or the final tool execution result.

Parameters:

Name Type Description Default
query str

The input query or task description provided by the user.

required
is_save_memory bool

Flag to determine if the conversation should be saved to memory. Defaults to False.

False
user_id str

Identifier for the user making the request. Defaults to "unknown_user".

'unknown_user'
max_iterations int

Maximum number of tool call iterations to prevent infinite loops. Defaults to 10.

10
is_tool_formatted bool

Modifying the output Tool to become more human-preferred. If True, it needs one next llm invoke to format answer, else directly return tool_message. Defaults to True.

True
max_history int

Number of last messages in the history. Defaults to None.

None
**kwargs

Additional keyword arguments, including an optional config dictionary for graph execution.

{}

Returns:

Name Type Description
Any AsyncGenerator[Any, None]

The result of the tool execution, LLM response, or None if an error occurs during tool execution.

Raises:

Type Description
JSONDecodeError

If the tool data cannot be parsed as valid JSON.

KeyError

If required keys are missing in the tool data.

ValueError

If the tool data is invalid or cannot be processed.

save_memory

save_memory(message: Union[ToolMessage, AIMessage], user_id: str = 'unknown_user') -> None

Save the tool message to the memory

register_tools

register_tools(tools: List[str]) -> Any

Register a list of tools