Skip to content

Commit 1030dc8

Browse files
committed
Merge branch 'feat/agent_team' into dev
2 parents 85edd49 + a6c4e30 commit 1030dc8

File tree

9 files changed

+366
-315
lines changed

9 files changed

+366
-315
lines changed

docs/tutorials/agent/agent.md

Lines changed: 28 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,10 @@ agent = Agent(llm=llm,
4646

4747
```python
4848
from course_graph.agent import Controller
49-
controller = Controller(max_turns=10)
49+
controller = Controller()
5050
```
5151

52-
`Controller` 负责启动 `Agent` 并为 `Agent` 提供上下文、具体执行外部工具等功能, `max_turns` 参数代表运行最大轮数
52+
`Controller` 负责启动 `Agent` 并为 `Agent` 提供上下文、具体执行外部工具等功能。
5353

5454
## 启动智能体
5555

@@ -59,7 +59,7 @@ resp = controller.run_sync(agent=translator, message="请帮我翻译蛋白质
5959

6060
其中 `message` 参数代表用户的具体指令或者是用户与智能体对话的开始。
6161

62-
`controller.run_sync` 方法返回一个 `ControllerResponse` 对象, 其中 `agent` 代表最后响应的 `Agent` 对象 (暂时还用不到), `message` 代表智能体最后的相应内容, `turns` 代表智能体运行的轮数
62+
`controller.run_sync` 方法返回一个 `ControllerResponse` 对象, 其中 `agent` 代表最后响应的 `Agent` 对象, `message` 代表智能体最后的相应内容。
6363

6464
## 使用外部工具
6565

@@ -374,24 +374,23 @@ controller = Controller(trace_callback=pprint)
374374
该回调函数需要接受一个 `TraceEvent` 类型的参数, 其中事件类型包括:
375375

376376
- `USER_MESSAGE`: 用户消息
377+
- `AGENT_THINK`: 智能体思考过程
377378
- `AGENT_MESSAGE`: 智能体消息
378379
- `AGENT_SWITCH`: 智能体切换
379380
- `TOOL_CALL`: 工具调用
380381
- `TOOL_RESULT`: 工具调用结果
381382
- `CONTEXT_UPDATE`: 上下文变量更新
382383
- `MCP_TOOL_CALL`: MCP 工具调用
383384

384-
## 多智能协作
385+
## 多智能体协作
385386

386-
除了让智能体使用 <a href='#transfer-function'>转移函数</a> 自行决定接下来被激活的智能体外,也提供了一些固定的协作范式,称之为团队。
387+
除了手动编写协作流程或让智能体使用 <a href='#transfer-function'>转移函数</a> 自行决定切换外,也提供了一些固定的协作范式,称之为团队。
387388

388389
### 团队
389390

390-
`RoundTeam` 为例,团队中每个 `Agent` 对象会依次被激活,所有 `Agent` 以广播的方式共享相同的上下文。
391-
392391
#### 创建团队
393392

394-
直接使用 `RoundTeam` 类创建一个团队:
393+
`RoundTeam` 为例,直接使用 `RoundTeam` 类创建一个团队:
395394

396395
```python
397396
team = RoundTeam([agent1, agent2, agent3])
@@ -403,44 +402,49 @@ team = RoundTeam([agent1, agent2, agent3])
403402
team: RoundTeam = agent1 | agent2 | agent3
404403
```
405404

406-
#### 添加任务
407-
408-
团队中所有 `Agent` 都将围绕这个任务进行协作:
409-
410-
```python
411-
team.add_task("请创作一首关于春天的七律诗。")
412-
```
413-
414405
#### 添加终止器
415406

416407
团队中可以设置一个终止器,当满足终止条件时,团队将停止运行:
417408

418409
```python
419-
team.terminator = MaxTurnsTerminator(max_turns=10)
410+
team.termination = TextMentionTermination(text="APPROVED")
420411
```
421412

422413
终止器包含以下类型:
423414

424-
- `MaxTurnsTerminator`: 最大轮数终止, 包含每个 `Agent` 调用工具的次数。
425-
- `MaxActiveTerminator`: 最大激活次数终止, 表示团队中能够激活 `Agent` 的最大次数。
426-
- `TextTerminator`: 当团队中任意一个 `Agent` 的响应中包含指定的文本时,团队将停止运行。
427-
- `TimeOutTerminator`: 超时终止,表示整个团队运行的最长时长。
415+
- `TextMentionTermination`: 响应中包含指定的文本时,团队将停止运行。
428416

429417
所有终止器都支持 `&``|` 操作符来组合使用。
430418

431419
#### 运行团队
432420

433-
使用 `run_sync``run` 方法运行团队:
421+
使用 `run_sync``run` 方法运行团队,团队中所有成员都将围绕这个任务进行协作
434422

435423
```python
436-
team.run_sync()
424+
team.run_sync(task="请创作一首关于春天的七律诗。")
437425
```
438426

439-
不提供返回值,你可以使用 `team.global_messages` 获取团队中所有 `Agent` 的对话历史或者 `add_trace_callback` 方法添加一个回调函数来获取内部流程
427+
该方法提供一个 `TeamResponse` 类型的返回值。如果想知道团队的内部流程, 可以使用 `trace` 属性或通过 `set_trace_callback` 方法设置一个回调函数
440428

441429
### 团队类型
442430

431+
- `RoundTeam`: 轮询团队,团队中每个成员会依次被激活并循环该过程,直到满足终止条件。所有成员以广播的方式共享相同的上下文。
432+
433+
```python
434+
team: RoundTeam = agent1 | agent2 | agent3
435+
```
436+
437+
- `LinearTeam`: 线性团队,团队中每个成员会依次被激活,每个成员仅接收前一个成员的运行结果。团队在最后一个成员运行结束后自动结束。
443438

439+
```python
440+
team: LinearTeam = agent1 => agent2 => agent3
441+
```
444442

443+
- `LeaderTeam`: 领导团队,团队中有一个领导和一个或多个下属,领导负责判断用户意图并切换到相应的下属上执行任务, 每个下属仅接收领导的指令。
445444

445+
```python
446+
team: LeaderTeam = agent1 <= [agent2, agent3] <= ["负责...", "负责..."]
447+
```
446448

449+
> [!TIP]
450+
> 可以通过第二个 `<=` 操作符设置下属 `Agent` 的描述, 也可以省略使用默认描述

src/course_graph/agent/__init__.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,5 @@
88
from .controller import Controller
99
from .types import Result, ContextVariables, Tool
1010
from .mcp import MCPServer, STDIO, SSE
11-
from .trace import TraceEvent
12-
from .utils import trace_callback
13-
from .teams import Team, RoundTeam, Terminator, TextTerminator, MaxTurnsTerminator
11+
from .trace import TraceEvent, trace_callback
12+
from .teams import Team, RoundTeam, Termination, TextMentionTermination, LinearTeam, LeaderTeam

src/course_graph/agent/agent.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ def add_assistant_message(self, message: str, name: str = None) -> None:
136136
message = {'content': message, 'role': 'assistant', 'name': name or self.name}
137137
self.messages.append(message)
138138

139-
def add_tool_call_message(self, tool_content: str, tool_call_id: str) -> None:
139+
def add_tool_call_result_message(self, tool_content: str, tool_call_id: str) -> None:
140140
""" 添加工具调用记录
141141
142142
Args:
@@ -171,9 +171,7 @@ def add_tools(self, *tools: 'Tool') -> 'Agent':
171171
for tool in tools:
172172
self.tools.append(tool["tool"])
173173
function = tool["function"]
174-
function_name = tool.get('function_name', function.__name__)
175-
if function_name == '<lambda>':
176-
continue
174+
function_name = tool['tool']['function']['name']
177175
self.tool_functions[function_name] = function
178176
if (r := tool.get('context_variables_parameter_name')) is not None:
179177
self.use_context_variables[function_name] = r
@@ -270,7 +268,7 @@ def add_tool_functions(self, *functions: Callable | Awaitable) -> 'Agent':
270268
'type': 'object',
271269
'properties': properties,
272270
'required': required
273-
} if len(properties) != 0 else {}
271+
}
274272
},
275273
}
276274
})

0 commit comments

Comments
 (0)