|
| 1 | +--- |
| 2 | +group: |
| 3 | + title: Agent |
| 4 | + order: 5 |
| 5 | +title: BaseAgent |
| 6 | +order: -1 |
| 7 | +toc: content |
| 8 | +--- |
| 9 | + |
| 10 | +Here is the English translation of the usage documentation for the BaseAgent class, including the code content description, functionality overview, and SDK interface documentation. |
| 11 | + |
| 12 | +# `BaseAgent` Documentation |
| 13 | + |
| 14 | +## Code Content Description |
| 15 | + |
| 16 | +The `BaseAgent` class is a foundational class for implementing agents, providing initialization and interaction methods. Users can customize the agent as needed to handle specific tasks and inquiries. |
| 17 | + |
| 18 | +### Functionality Overview |
| 19 | + |
| 20 | +The `BaseAgent` class primarily serves as a foundational building block for agents. It supports interaction with large language models, prompt management, memory management, and response generation. Below are the main functionalities provided by this class: |
| 21 | + |
| 22 | +- **Task Execution**: Provides `step` and `step_stream` methods for processing user requests and returning agent responses. |
| 23 | +- **Memory Management**: Implements management of historical messages, allowing for optional clearing or restoration of historical records. |
| 24 | +- **Prompt Management**: Generates dynamic prompts based on input queries and session contexts. |
| 25 | + |
| 26 | +**Basic Attributes**: |
| 27 | + |
| 28 | +- `agent_type`: String that defines the type of the agent (default is `BaseAgent`). |
| 29 | +- `agent_name`: String representing the agent's name. |
| 30 | +- `system_prompt`: String containing the system's prompt message. |
| 31 | +- `input_template`: Input template, which can be a string or a base model. |
| 32 | +- `output_template`: Output template, which can be a string or a base model. |
| 33 | +- `prompt`: Optional string for prompt information. |
| 34 | +- `agents`: List of agents for multi-agent interaction. |
| 35 | +- `tools`: List of tools available for external use. |
| 36 | +- `agent_desc`: Description of the agent. |
| 37 | +- `agent_config`: Optional agent configuration. |
| 38 | +- `model_config`: Optional model configuration. |
| 39 | +- `prompt_config`: Prompt configuration (default is `PromptConfig()`). |
| 40 | +- `project_config`: Optional project configuration. |
| 41 | + |
| 42 | +**Class Methods**: |
| 43 | + |
| 44 | +- `init_from_project_config`: Creates a new instance of the agent based on project configuration. |
| 45 | +- `get_wrapper`: Retrieves the appropriate class wrapper based on the agent type. |
| 46 | + |
| 47 | +**Instance Methods**: |
| 48 | + |
| 49 | +- `step`: Processes a query and returns the agent's response. |
| 50 | +- `step_stream`: Streams the agent's responses. |
| 51 | +- `pre_print`: Generates and prints the agent's prompt format. |
| 52 | +- `copy_config`: Creates a copy of the agent's configuration. |
| 53 | +- `clear_history`: Clears the agent's memory history. |
| 54 | +- `get_memory`: Retrieves memory for a specified session index. |
| 55 | +- `append_history`: Appends new messages to the agent's history. |
| 56 | +- `update_memory_manager`: Updates the memory manager to store the latest messages. |
| 57 | +- `memory_to_format_messages`: Formats stored memory into specific message formats. |
| 58 | + |
| 59 | +### SDK Interface Documentation |
| 60 | + |
| 61 | +#### `__init__(agent_name: str = "codefuse_baser", system_prompt: str = "you are a helpful assistant!\n", input_template: Union[str, BaseModel] = "", output_template: Union[str, BaseModel] = "", prompt: Optional[str] = None, agents: List[str] = [], tools: List[str] = [], agent_desc: str = "", *, agent_config: Optional[AgentConfig] = None, model_config: Optional[ModelConfig] = None, prompt_config: Optional[PromptConfig] = PromptConfig(), project_config: Optional[ProjectConfig] = None, log_verbose: str = "0")` |
| 62 | + |
| 63 | +- **Description**: Initializes the basic properties of the agent. |
| 64 | +- **Parameters**: |
| 65 | + - `agent_name`: The name of the agent (default is "codefuse_baser"). |
| 66 | + - `system_prompt`: The system's prompt message (default is "you are a helpful assistant!\n"). |
| 67 | + - `input_template`: Input template, supporting string or base model types. |
| 68 | + - `output_template`: Output template, supporting string or base model types. |
| 69 | + - `prompt`: Optional prompt information. |
| 70 | + - `agents`: List of agents. |
| 71 | + - `tools`: List of tools. |
| 72 | + - `agent_desc`: Description of the agent. |
| 73 | + - `agent_config`: Optional agent configuration. |
| 74 | + - `model_config`: Optional model configuration. |
| 75 | + - `prompt_config`: Prompt configuration, default is `PromptConfig()`. |
| 76 | + - `project_config`: Optional project configuration. |
| 77 | + - `log_verbose`: Verbosity of logs (default is "0"). |
| 78 | + |
| 79 | +#### `step(query: Message, memory_manager: Optional[BaseMemoryManager]=None, session_index: str = "default", **kwargs) -> Optional[Message]` |
| 80 | + |
| 81 | +- **Description**: Responds to user input and returns the agent's response. |
| 82 | +- **Parameters**: |
| 83 | + - `query`: A user input message. |
| 84 | + - `memory_manager`: An optional memory manager instance for managing message history. |
| 85 | + - `session_index`: A string representing the session index (default is "default"). |
| 86 | + - `kwargs`: Additional keyword arguments for extended functionality. |
| 87 | +- **Returns**: Returns the message instance processed by the agent, or `None` if there is no response. |
| 88 | + |
| 89 | +#### `step_stream(query: Message, memory_manager: Optional[BaseMemoryManager]=None, session_index: str = "default") -> Generator[Message, None, None]` |
| 90 | + |
| 91 | +- **Description**: Responds to user input and returns the agent's streaming response. |
| 92 | +- **Parameters**: |
| 93 | + - `query`: A user input message. |
| 94 | + - `memory_manager`: An optional memory manager instance for managing message history. |
| 95 | + - `session_index`: A string representing the session index (default is "default"). |
| 96 | +- **Returns**: Returns a generator for outputting messages one by one. |
| 97 | + |
| 98 | +#### `pre_print(query: Message, memory_manager: Optional[BaseMemoryManager]=None, session_index: str = "default", **kwargs) -> None` |
| 99 | + |
| 100 | +- **Description**: Generates and prints the agent's prompt format and performs pre-processing. |
| 101 | +- **Parameters**: |
| 102 | + - `query`: A user input message. |
| 103 | + - `memory_manager`: An optional memory manager instance for managing message history. |
| 104 | + - `session_index`: A string representing the session index (default is "default"). |
| 105 | + - `kwargs`: Additional keyword arguments for extended functionality. |
| 106 | +- **Returns**: No return value. |
| 107 | + |
| 108 | +#### `clear_history()` |
| 109 | + |
| 110 | +- **Description**: Clears the agent's memory. |
| 111 | +- **Returns**: No return value. |
| 112 | + |
| 113 | +#### `get_memory(session_index: str, memory_manager: Optional[BaseMemoryManager] = None) -> Memory` |
| 114 | + |
| 115 | +- **Description**: Retrieves memory for the specified session index. |
| 116 | +- **Parameters**: |
| 117 | + - `session_index`: A string representing the session index. |
| 118 | + - `memory_manager`: An optional memory manager instance. |
| 119 | +- **Returns**: Returns the `Memory` instance corresponding to the specified index. |
0 commit comments