Agent Module¶
erniebot_agent.agents ¶
Agent ¶
The base class for agents.
Typically, this class should be the base class for custom agent classes. A class derived 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
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 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 | |
__init__ ¶
__init__(llm: BaseERNIEBot, tools: Union[ToolManager, Iterable[BaseTool]], *, memory: Optional[Memory] = None, system: Optional[str] = None, callbacks: Optional[Union[CallbackManager, Iterable[CallbackHandler]]] = None, file_manager: Optional[FileManager] = None, plugins: Optional[List[str]] = None) -> None
Initialize an agent.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
llm |
BaseERNIEBot
|
An LLM for the agent to use. |
required |
tools |
Union[ToolManager, Iterable[BaseTool]]
|
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 |
Optional[str]
|
A message that tells the LLM how to interpret the conversations. |
None
|
callbacks |
Optional[Union[CallbackManager, Iterable[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 ¶
get_tools ¶
load_tool ¶
Load a tool into the agent.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tool |
BaseTool
|
The tool to load. |
required |
reset_memory ¶
run
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[Sequence[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
async
¶
Run the LLM asynchronously, returning final response.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
messages |
List[Message]
|
The input messages. |
required |
llm_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_llm_stream
async
¶
Run the LLM asynchronously, returning an async iterator of responses
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
messages |
List[Message]
|
The input messages. |
required |
llm_opts |
Any
|
Options to pass to the LLM. |
{}
|
Returns:
| Type | Description |
|---|---|
AsyncIterator[LLMResponse]
|
Iterator of responses from the LLM. |
Source code in erniebot-agent/src/erniebot_agent/agents/agent.py
run_stream
async
¶
run_stream(prompt: str, files: Optional[Sequence[File]] = None) -> AsyncIterator[Tuple[AgentStep, List[Message]]]
Run the agent asynchronously, returning an async iterator of responses.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
prompt |
str
|
A natural language text describing the task that the agent should perform. |
required |
files |
Optional[Sequence[File]]
|
A list of files that the agent can use to perform the task. |
None
|
Returns: Iterator of responses from the agent.
Source code in erniebot-agent/src/erniebot_agent/agents/agent.py
run_tool
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
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 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 | |
__init__ ¶
__init__(llm: BaseERNIEBot, tools: Union[ToolManager, Iterable[BaseTool]], *, memory: Optional[Memory] = None, system: Optional[str] = None, callbacks: Optional[Union[CallbackManager, Iterable[CallbackHandler]]] = None, file_manager: Optional[FileManager] = None, plugins: Optional[List[str]] = None, max_steps: Optional[int] = None, first_tools: Optional[Sequence[BaseTool]] = []) -> None
Initialize a function agent.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
llm |
BaseERNIEBot
|
An LLM for the agent to use. |
required |
tools |
Union[ToolManager, Iterable[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 |
None
|
system |
Optional[str]
|
A message that tells the LLM how to interpret the
conversations. If |
None
|
callbacks |
Optional[Union[CallbackManager, Iterable[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
|
first_tools |
Optional[Sequence[BaseTool]]
|
Tools scheduled to be called sequentially at the beginning of each agent run. |
[]
|
Raises:
| Type | Description |
|---|---|
ValueError
|
if |
RuntimeError
|
if tools in first_tools but not in tools list. |
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
__init__ ¶
Initialize a callback manager.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
handlers |
Iterable[CallbackHandler]
|
An iterable of callback handlers. |
required |
Source code in erniebot-agent/src/erniebot_agent/agents/callback/callback_manager.py
add_handler ¶
remove_all_handlers ¶
remove_handler ¶
set_handlers ¶
CallbackHandler ¶
The base class for callback handlers.
Source code in erniebot-agent/src/erniebot_agent/agents/callback/handlers/base.py
on_llm_end
async
¶
Called when the LLM successfully 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
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 |
BaseException
|
The error that occured. |
required |
Source code in erniebot-agent/src/erniebot_agent/agents/callback/handlers/base.py
on_llm_start
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
async
¶
Called when the agent successfully 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_error
async
¶
Called when the agent errors.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
agent |
BaseAgent
|
The agent that is running. |
required |
error |
BaseException
|
The error that occured. |
required |
on_run_start
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
async
¶
Called when a tool successfully 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
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 |
BaseException
|
The error that occured. |
required |
Source code in erniebot-agent/src/erniebot_agent/agents/callback/handlers/base.py
on_tool_start
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__ ¶
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
async
¶
Called to log when the LLM successfully ends running.
Source code in erniebot-agent/src/erniebot_agent/agents/callback/handlers/logging_handler.py
on_llm_start
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
async
¶
Called to log when the agent successfully ends running.
Source code in erniebot-agent/src/erniebot_agent/agents/callback/handlers/logging_handler.py
on_run_start
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
async
¶
Called to log when a tool successfully ends running.
Source code in erniebot-agent/src/erniebot_agent/agents/callback/handlers/logging_handler.py
on_tool_start
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.