Agent Module¶
erniebot_agent.agents
¶
Agent
¶
The base class for agents.
Typically, this is the class that a custom agent class should inherit from. A class inheriting from this class must implement how the agent orchestates the components to complete tasks.
Attributes:
| Name | Type | Description |
|---|---|---|
llm |
BaseERNIEBot
|
The LLM that the agent uses. |
memory |
Memory
|
The message storage that keeps the chat history. |
Source code in erniebot-agent/src/erniebot_agent/agents/agent.py
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 | |
__init__(llm, tools, *, memory=None, system_message=None, callbacks=None, file_manager=None, plugins=None)
¶
Initialize an agent.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
llm |
BaseERNIEBot
|
An LLM for the agent to use. |
required |
tools |
Union[ToolManager, List[BaseTool]]
|
A list of tools for the agent to use. |
required |
memory |
Optional[Memory]
|
A memory object that equips the agent to remember chat history. If not specified, a new WholeMemory object will be instantiated. |
None
|
system_message |
Optional[SystemMessage]
|
A message that tells the LLM how to interpret the
conversations. If |
None
|
callbacks |
Optional[Union[CallbackManager, List[CallbackHandler]]]
|
A list of callback handlers for the agent to use. If
|
None
|
file_manager |
Optional[FileManager]
|
A file manager for the agent to interact with files.
If |
None
|
plugins |
Optional[List[str]]
|
A list of names of the plugins for the agent to use. If
|
None
|
Source code in erniebot-agent/src/erniebot_agent/agents/agent.py
get_tool(tool_name)
¶
Get the tool by its name.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tool_name |
str
|
the tool name of the tool to get. |
required |
get_tools()
¶
load_tool(tool)
¶
Load a tool into the agent.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tool |
BaseTool
|
The tool to load. |
required |
reset_memory()
¶
run(prompt, files=None)
async
¶
Run the agent asynchronously.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
prompt |
str
|
A natural language text describing the task that the agent should perform. |
required |
files |
Optional[List[File]]
|
A list of files that the agent can use to perform the task. |
None
|
Returns:
| Type | Description |
|---|---|
AgentResponse
|
Response from the agent. |
Source code in erniebot-agent/src/erniebot_agent/agents/agent.py
run_llm(messages, **opts)
async
¶
Run the LLM asynchronously.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
messages |
List[Message]
|
The input messages. |
required |
**opts |
Any
|
Options to pass to the LLM. |
{}
|
Returns:
| Type | Description |
|---|---|
LLMResponse
|
Response from the LLM. |
Source code in erniebot-agent/src/erniebot_agent/agents/agent.py
run_tool(tool_name, tool_args)
async
¶
Run the specified tool asynchronously.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tool_name |
str
|
The name of the tool to run. |
required |
tool_args |
str
|
The tool arguments in JSON format. |
required |
Returns:
| Type | Description |
|---|---|
ToolResponse
|
Response from the tool. |
Source code in erniebot-agent/src/erniebot_agent/agents/agent.py
FunctionAgent
¶
An agent driven by function calling.
The orchestration capabilities of a function agent are powered by the function calling ability of LLMs. Typically, a function agent asks the LLM to generate a response that can be parsed into an action (e.g., calling a tool with given arguments), and then the agent takes that action, which forms an agent step. The agent repeats this process until the maximum number of steps is reached or the LLM considers the task finished.
Attributes:
| Name | Type | Description |
|---|---|---|
llm |
BaseERNIEBot
|
The LLM that the agent uses. |
memory |
Memory
|
The message storage that keeps the chat history. |
max_steps |
int
|
The maximum number of steps in each agent run. |
Source code in erniebot-agent/src/erniebot_agent/agents/function_agent.py
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 | |
__init__(llm, tools, *, memory=None, system_message=None, callbacks=None, file_manager=None, plugins=None, max_steps=None)
¶
Initialize a function agent.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
llm |
BaseERNIEBot
|
An LLM for the agent to use. |
required |
tools |
Union[ToolManager, List[BaseTool]]
|
A list of tools for the agent to use. |
required |
memory |
Optional[Memory]
|
A memory object that equips the agent to remember chat history. |
None
|
system_message |
Optional[SystemMessage]
|
A message that tells the LLM how to interpret the
conversations. If |
None
|
callbacks |
Optional[Union[CallbackManager, List[CallbackHandler]]]
|
A list of callback handlers for the agent to use. If
|
None
|
file_manager |
Optional[FileManager]
|
A file manager for the agent to interact with files.
If |
None
|
plugins |
Optional[List[str]]
|
A list of names of the plugins for the agent to use. If
|
None
|
max_steps |
Optional[int]
|
The maximum number of steps in each agent run. If |
None
|
Source code in erniebot-agent/src/erniebot_agent/agents/function_agent.py
erniebot_agent.agents.callback
¶
CallbackManager
¶
The manager for callback handlers.
Source code in erniebot-agent/src/erniebot_agent/agents/callback/callback_manager.py
handlers: List[CallbackHandler]
property
¶
The list of callback handlers.
__init__(handlers)
¶
Initialize a callback manager.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
handlers |
List[CallbackHandler]
|
A list of callback handlers. |
required |
Source code in erniebot-agent/src/erniebot_agent/agents/callback/callback_manager.py
add_handler(handler)
¶
remove_all_handlers()
¶
remove_handler(handler)
¶
CallbackHandler
¶
The base class for callback handlers.
Source code in erniebot-agent/src/erniebot_agent/agents/callback/handlers/base.py
on_llm_end(agent, llm, response)
async
¶
Called when the LLM ends running.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
agent |
BaseAgent
|
The agent that is running. |
required |
llm |
ChatModel
|
The LLM that is running. |
required |
response |
LLMResponse
|
The response that the LLM returns. |
required |
Source code in erniebot-agent/src/erniebot_agent/agents/callback/handlers/base.py
on_llm_error(agent, llm, error)
async
¶
Called when the LLM errors.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
agent |
BaseAgent
|
The agent that is running. |
required |
llm |
ChatModel
|
The LLM that is running. |
required |
error |
Union[Exception, KeyboardInterrupt]
|
The error that occured. |
required |
Source code in erniebot-agent/src/erniebot_agent/agents/callback/handlers/base.py
on_llm_start(agent, llm, messages)
async
¶
Called when the LLM starts running.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
agent |
BaseAgent
|
The agent that is running. |
required |
llm |
ChatModel
|
The LLM that is running. |
required |
messages |
List[Message]
|
The messages that the LLM uses as input. |
required |
Source code in erniebot-agent/src/erniebot_agent/agents/callback/handlers/base.py
on_run_end(agent, response)
async
¶
Called when the agent ends running.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
agent |
BaseAgent
|
The agent that is running. |
required |
response |
AgentResponse
|
The response that the agent returns. |
required |
Source code in erniebot-agent/src/erniebot_agent/agents/callback/handlers/base.py
on_run_start(agent, prompt)
async
¶
Called when the agent starts running.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
agent |
BaseAgent
|
The agent that is running. |
required |
prompt |
str
|
The prompt that the agent uses as input. |
required |
Source code in erniebot-agent/src/erniebot_agent/agents/callback/handlers/base.py
on_tool_end(agent, tool, response)
async
¶
Called when a tool ends running.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
agent |
BaseAgent
|
The agent that is running. |
required |
tool |
BaseTool
|
The tool that is running. |
required |
response |
ToolResponse
|
The response that the tool returns. |
required |
Source code in erniebot-agent/src/erniebot_agent/agents/callback/handlers/base.py
on_tool_error(agent, tool, error)
async
¶
Called when a tool errors.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
agent |
BaseAgent
|
The agent that is running. |
required |
tool |
BaseTool
|
The tool that is running. |
required |
error |
Union[Exception, KeyboardInterrupt]
|
The error that occured. |
required |
Source code in erniebot-agent/src/erniebot_agent/agents/callback/handlers/base.py
on_tool_start(agent, tool, input_args)
async
¶
Called when a tool starts running.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
agent |
BaseAgent
|
The agent that is running. |
required |
tool |
BaseTool
|
The tool that is running. |
required |
input_args |
str
|
The input arguments that the tool uses. |
required |
Source code in erniebot-agent/src/erniebot_agent/agents/callback/handlers/base.py
LoggingHandler
¶
A callback handler for logging.
Source code in erniebot-agent/src/erniebot_agent/agents/callback/handlers/logging_handler.py
__init__(logger=None)
¶
Initialize a logging handler.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
logger |
Optional[Logger]
|
The logger to use. If |
None
|
Source code in erniebot-agent/src/erniebot_agent/agents/callback/handlers/logging_handler.py
on_llm_end(agent, llm, response)
async
¶
Called to log when the LLM ends running.
Source code in erniebot-agent/src/erniebot_agent/agents/callback/handlers/logging_handler.py
on_llm_start(agent, llm, messages)
async
¶
Called to log when the LLM starts running.
Source code in erniebot-agent/src/erniebot_agent/agents/callback/handlers/logging_handler.py
on_run_end(agent, response)
async
¶
Called to log when the agent ends running.
Source code in erniebot-agent/src/erniebot_agent/agents/callback/handlers/logging_handler.py
on_run_start(agent, prompt)
async
¶
Called to log when the agent starts running.
Source code in erniebot-agent/src/erniebot_agent/agents/callback/handlers/logging_handler.py
on_tool_end(agent, tool, response)
async
¶
Called to log when a tool ends running.
Source code in erniebot-agent/src/erniebot_agent/agents/callback/handlers/logging_handler.py
on_tool_start(agent, tool, input_args)
async
¶
Called to log when a tool starts running.
Source code in erniebot-agent/src/erniebot_agent/agents/callback/handlers/logging_handler.py
erniebot_agent.agents.schema
¶
LLMResponse
dataclass
¶
ToolResponse
dataclass
¶
AgentResponse
dataclass
¶
The final response from an agent.