|
1 | 1 | ---
|
2 |
| -title: LLM Agent |
3 |
| -description: 基于 LLM 的自主决策 Agent |
| 2 | +title: Agent |
| 3 | +description: VeADK 中提供了多种类型的智能体来承载不同的任务需求 |
4 | 4 | navigation:
|
5 |
| - icon: i-lucide-heading-1 |
| 5 | + icon: i-lucide-lightbulb |
6 | 6 | ---
|
7 | 7 |
|
8 |
| -## 定义方法 |
| 8 | +VeADK 中提供了多种类型的智能体来满足应用场景需要,提供可控要求下的自主决策。其中: |
| 9 | + |
| 10 | +- **自主决策 Agent:** 可以根据不同场景,由 LLM 自主决定调用哪些子智能体或工具 |
| 11 | +- **工作流 Agent:** 根据不同智能体类别,采用硬编码模式来提供确定性的子智能体执行顺序 |
| 12 | + |
| 13 | +## 自主决策 Agent |
| 14 | + |
| 15 | +### 使用示例 |
9 | 16 |
|
10 | 17 | ```python [agent.py]
|
| 18 | +import asyncio |
| 19 | + |
11 | 20 | from veadk import Agent
|
| 21 | +from veadk.tools.demo_tools import get_city_weather |
| 22 | + |
| 23 | +agent = Agent(tools=[get_city_weather]) |
12 | 24 |
|
13 |
| -agent = Agent() |
| 25 | +response = asyncio.run(agent.run("北京天气怎么样?")) |
| 26 | + |
| 27 | +print(response) # 北京天气晴朗,气温25°C。 |
14 | 28 | ```
|
15 | 29 |
|
16 |
| -## 选项 |
| 30 | +### 选项 |
17 | 31 |
|
18 | 32 | ::field-group
|
19 | 33 | ::field{name="name" type="string"}
|
@@ -48,19 +62,162 @@ agent = Agent()
|
48 | 62 | 模型请求的额外配置
|
49 | 63 | ::
|
50 | 64 |
|
51 |
| - ::field{name="tools" type="list[ToolUnion]"} |
| 65 | + ::field{name="tools" type="list[ToolUnion] | None"} |
52 | 66 | 提供给智能体的工具列表
|
53 | 67 | ::
|
54 | 68 |
|
55 |
| - ::field{name="sub_agents" type="list[BaseAgent]"} |
| 69 | + ::field{name="sub_agents" type="list[BaseAgent] | None"} |
56 | 70 | 提供给该智能体的子智能体列表
|
57 | 71 | ::
|
58 | 72 |
|
59 |
| - ::field{name="knowledgebase" type="KnowledgeBase | null"} |
| 73 | + ::field{name="knowledgebase" type="KnowledgeBase | None"} |
60 | 74 | 提供给智能体的知识库
|
61 | 75 | ::
|
62 | 76 |
|
63 |
| - ::field{name="long_term_memory" type="LongTermMemory | null"} |
| 77 | + ::field{name="short_term_memory" type="ShortTermMemory | None"} |
| 78 | + 智能体的单会话上下文 |
| 79 | + :: |
| 80 | + |
| 81 | + ::field{name="long_term_memory" type="LongTermMemory | None"} |
64 | 82 | 智能体的跨会话长期记忆(同一用户范围内)
|
65 | 83 | ::
|
66 | 84 | ::
|
| 85 | + |
| 86 | +::note |
| 87 | +更多兼容字段请参考 [Google ADK LLM Agent 定义](https://github.com/google/adk-python/blob/main/src/google/adk/agents/llm_agent.py)。 |
| 88 | +:: |
| 89 | + |
| 90 | +## 工作流 Agent |
| 91 | + |
| 92 | +### 定义方法 |
| 93 | + |
| 94 | +下面提供了不同类型工作流智能体的定义方法: |
| 95 | + |
| 96 | +#### 顺序类 `SequentialAgent` |
| 97 | + |
| 98 | +#### 循环类 `LoopAgent` |
| 99 | + |
| 100 | +```python [loop_agent.py] |
| 101 | + |
| 102 | +``` |
| 103 | + |
| 104 | +#### 并行类 `ParallelAgent` |
| 105 | + |
| 106 | +```python [parallel_agent.py] |
| 107 | + |
| 108 | +``` |
| 109 | + |
| 110 | +### 选项 |
| 111 | + |
| 112 | +工作流 Agent 采用统一的参数: |
| 113 | + |
| 114 | +::field-group |
| 115 | + ::field{name="name" type="string"} |
| 116 | + 智能体的名称 |
| 117 | + :: |
| 118 | + |
| 119 | + ::field{name="description" type="string"} |
| 120 | + 默认为 `DEFAULT_DESCRIPTION` - 智能体的描述,在 A2A 场景下会有帮助 |
| 121 | + :: |
| 122 | + |
| 123 | + ::field{name="instruction" type="string"} |
| 124 | + 默认为 `DEFAULT_INSTRUCTION` - 智能体的指令,例如函数调用的原则 |
| 125 | + :: |
| 126 | + |
| 127 | + ::field{name="sub_agents" type="list[BaseAgent]"} |
| 128 | + 默认为 `[]` - 提供给该智能体的子智能体列表 |
| 129 | + :: |
| 130 | + |
| 131 | + ::field{name="tracers" type="list[BaseTracer]"} |
| 132 | + 默认为 `[]` - 提供给该智能体的 tracer |
| 133 | + :: |
| 134 | +:: |
| 135 | + |
| 136 | +::note |
| 137 | +更多兼容字段请参考 [Google ADK Agents 定义](https://github.com/google/adk-python/blob/main/src/google/adk/agents/)。 |
| 138 | +:: |
| 139 | + |
| 140 | +## 多智能体协作 |
| 141 | + |
| 142 | +使用 VeADK 可以构建多 Agent 协作, 主 Agent 通过 `sub_agents` 机制协调多个子 Agent 完成复杂任务。 |
| 143 | + |
| 144 | +### 自主决策 Agent |
| 145 | + |
| 146 | +利用能够自主决策的 Agent 来定义一个生活提醒智能体,分别定义了三个智能体: |
| 147 | + |
| 148 | +- `weather_reporter`:负责通过 `get_city_weather` 工具来获取指定城市的天气信息 |
| 149 | +- `suggester`:根据天气情况给出穿衣建议 |
| 150 | +- `planner`:作为“调度员”,先调用 `weather_reporter` 获取天气,再调用 `suggester` 获取建议,最后将结果整合返回给用户。 |
| 151 | + |
| 152 | +```python [agent.py] |
| 153 | +import asyncio |
| 154 | + |
| 155 | +from veadk import Agent, Runner |
| 156 | +from veadk.tools.demo_tools import get_city_weather |
| 157 | + |
| 158 | +weather_reporter = Agent( |
| 159 | + name="weather_reporter", |
| 160 | + description="A weather reporter agent to report the weather.", |
| 161 | + tools=[get_city_weather], |
| 162 | +) |
| 163 | + |
| 164 | +suggester = Agent( |
| 165 | + name="suggester", |
| 166 | + description="A suggester agent that can give some clothing suggestions according to a city's weather.", |
| 167 | +) |
| 168 | + |
| 169 | +planner_agent = Agent( |
| 170 | + name="planner", |
| 171 | + description="A planner that can generate a suggestion according to a city's weather.", |
| 172 | + instruction="Invoke weather reporter agent first to get the weather, then invoke suggester agent to get the suggestion. Return the final response to user.", |
| 173 | + sub_agents=[weather_reporter, suggester], |
| 174 | +) |
| 175 | + |
| 176 | +runner = Runner(planner_agent) |
| 177 | +response = asyncio.run(runner.run("北京穿衣建议")) |
| 178 | + |
| 179 | +print(response) |
| 180 | +# Based on the weather in Beijing today - Sunny with a temperature of 25°C, here are some clothing suggestions for you: |
| 181 | + |
| 182 | +# It's a comfortable and warm day. You can choose light and breathable clothes. For example, a short - sleeved T - shirt or a thin shirt paired with casual pants or a skirt would be great. Since it's sunny, don't forget to wear a hat and sunglasses to protect yourself from the sun. Also, you can carry a light jacket in case the temperature drops in the evening, but it might not be necessary. Enjoy your day in Beijing! |
| 183 | +``` |
| 184 | + |
| 185 | +### `SequentialAgent` |
| 186 | + |
| 187 | +```python [sequential_agent.py] |
| 188 | +import asyncio |
| 189 | + |
| 190 | +from veadk import Agent, Runner |
| 191 | +from veadk.agents.sequential_agent import SequentialAgent |
| 192 | + |
| 193 | +greeting_agent = Agent( |
| 194 | + name="greeting_agent", |
| 195 | + description="A friendly agent that greets the user.", |
| 196 | + instruction="Greet the user warmly.", |
| 197 | +) |
| 198 | + |
| 199 | +goodbye_agent = Agent( |
| 200 | + name="goodbye_agent", |
| 201 | + description="A polite agent that says goodbye to the user.", |
| 202 | + instruction="Say goodbye to the user politely.", |
| 203 | +) |
| 204 | + |
| 205 | +root_agent = SequentialAgent(sub_agents=[greeting_agent, goodbye_agent]) |
| 206 | + |
| 207 | +runner = Runner(root_agent) |
| 208 | +response = asyncio.run(runner.run("你好")) |
| 209 | + |
| 210 | +print(response) |
| 211 | +``` |
| 212 | + |
| 213 | +### `LoopAgent` |
| 214 | + |
| 215 | +```python [loop_agent.py] |
| 216 | + |
| 217 | +``` |
| 218 | + |
| 219 | +### `ParallelAgent` |
| 220 | + |
| 221 | +```python [parallel_agent.py] |
| 222 | + |
| 223 | +``` |
0 commit comments