目录
-
- 整体流程
-
- [1. 基础组件:](#1. 基础组件:)
- [2. SIMPLE_THINK_NODE 和 SIMPLE_CHECK_NODE:](#2. SIMPLE_THINK_NODE 和 SIMPLE_CHECK_NODE:)
- [3. THINK_NODES 类:](#3. THINK_NODES 类:)
- [4. ThinkAction 类:](#4. ThinkAction 类:)
- [5. SimplePrint 类:](#5. SimplePrint 类:)
- [6. Printer 类:](#6. Printer 类:)
- [7. main 函数:](#7. main 函数:)
- 总结:
- 主要执行流程:
- 代码
- 参考链接:
整体流程
实现一个基于节点 (Node) 结构和思维推理的系统,用于生成和打印数字列表。它使用了 ActionNode 和 Action 类来定义任务和节点,并且通过异步任务执行逻辑来实现工作流。
1. 基础组件:
ActionNode 和 Action 类:
- ActionNode 是一个继承自 ActionNode 类的对象,负责执行特定任务的节点。每个节点可以有子节点,形成树形结构。
- Action 是执行动作的基础类,ThinkAction 和 SimplePrint 继承自 Action。
2. SIMPLE_THINK_NODE 和 SIMPLE_CHECK_NODE:
这两个节点负责"思考"和"检查"数字列表的生成:
- SIMPLE_THINK_NODE:这个节点的任务是思考生成一个数字列表的过程,传入的 instruction 说明需要生成什么样的数字列表(例如 Fibonacci 数列的前 10 个数字)。
- SIMPLE_CHECK_NODE:这个节点负责接收数字列表,并确保返回的格式严格符合要求,返回的格式必须是像 [1,2,3,4] 这样的数组。
3. THINK_NODES 类:
THINK_NODES 类继承自 ActionNode,代表一个包含子节点的节点(SIMPLE_THINK_NODE 和 SIMPLE_CHECK_NODE)。
它有一个 simple_fill 方法,负责根据给定的 context 生成思考内容,并用 LLM(Large Language Model)生成数字列表。子节点的返回内容会被依次传递,最终父节点返回的内容是最后一个子节点的输出。
4. ThinkAction 类:
该类继承自 Action,用于"思考"任务,目的是根据传入的 instruction(例如,提供一个斐波那契数列的任务),生成一个数字列表。
它使用 THINK_NODES 类来生成数字列表,并且通过正则表达式(find_in_brackets 方法)从返回的字符串中提取数字列表。
如果提取到有效的数字列表,会将其返回,否则返回空列表。
5. SimplePrint 类:
该类继承自 Action,其任务是简单地打印数字。它接受一个 input_num 参数,并在 run 方法中打印这个数字。
6. Printer 类:
Printer 类代表一个角色(Role),它将多个动作(如 ThinkAction 和 SimplePrint)组合成一个完整的工作流。
它有多个方法来执行这些动作:
- _think:确定下一个要执行的动作(基于角色的当前状态)。
- _prepare_print:准备打印数字列表。
- _act:执行实际的动作(例如,运行 ThinkAction,生成数字列表,然后调用 SimplePrint 打印数字)。
- _react:在运行过程中根据状态决定执行顺序,直到所有任务完成。
7. main 函数:
main 函数是程序的入口点。在此函数中,首先定义了一个任务(例如,生成斐波那契数列的前 10 个数字),然后创建了一个 Printer 角色,运行任务并记录日志。
总结:
使用面向对象的设计,创建不同的 Action 和 ActionNode 来管理工作流。
ThinkAction 负责生成数字列表,SimplePrint 负责打印结果。
Printer 类作为角色,协调执行 ThinkAction 和 SimplePrint。
通过异步方法(async/await)实现了非阻塞的执行流程,使得每个任务都能并发执行。
主要执行流程:
Printer 类 调用 ThinkAction,根据指令生成数字列表。
生成的数字列表被 Printer 通过 SimplePrint 输出。
整个过程是异步的,通过 asyncio 来运行和协调多个任务。
最终,Printer 角色根据任务要求生成并打印一个数字列表,例如斐波那契数列。
代码
python
import asyncio
import re
from metagpt.actions.action import Action, ActionNode
from metagpt.logs import logger
from metagpt.roles import Role
from metagpt.schema import Message
# 将思考斐波那契数列的10个数字作为prompt输入,在这里我们将"思考需要生成的数字列表"作为命令(instruction)写入
# 将期望返回格式(expected_type)设置为str,无需设置例子(example)
SIMPLE_THINK_NODE = ActionNode(
key="Simple Think Node",
expected_type=str,
instruction="""
Think about what list of numbers you need to generate
""",
example="",
)
# 在这里通过命令(instruction)来规定需要生成的数字列表格式,提供例子(example)来帮助LLM理解
SIMPLE_CHECK_NODE = ActionNode(
key="Simple CHECK Node",
expected_type=str,
instruction="""
Please provide the number list for me, strictly following the following requirements:
1. Answer strictly in the list format like [1,2,3,4]
2. Do not have extra spaces or line breaks.
Return the list here:
""",
example="[1,2,3,4]" "[4,5,6]",
)
class THINK_NODES(ActionNode):
def __init__(
self, name="Think Nodes", expected_type=str, instruction="", example=""
):
super().__init__(
key="",
expected_type=expected_type,
instruction=instruction,
example=example,
)
self.add_children(
[SIMPLE_THINK_NODE, SIMPLE_CHECK_NODE]
) # 初始化过程,将上面实现的两个子节点加入作为THINK_NODES类的子节点
async def simple_fill(
self,
schema,
mode,
exclude=None,
):
prompt = self.compile(
context=self.context, schema=schema, mode=mode, exclude=exclude
)
print(f"actionnode:{prompt}")
if schema != "raw":
mapping = self.get_mapping(mode, exclude=exclude)
class_name = f"{self.key}_AN"
content, scontent = await self._aask_v1(
prompt,
class_name,
mapping,
images=None,
schema=schema,
timeout=5,
)
self.content = content
self.instruct_content = scontent
else:
self.content = await self.llm.aask(prompt)
self.instruct_content = None
return self
async def fill(self, context, llm, schema="raw", mode="auto", strgy="complex"):
self.set_llm(llm)
self.set_context(context)
if self.schema:
schema = self.schema
if strgy == "simple":
return await self.simple_fill(schema=schema, mode=mode)
elif strgy == "complex":
# 这里隐式假设了拥有children
child_context = context # 输入context作为第一个子节点的context
for _, i in self.children.items():
i.set_context(child_context) # 为子节点设置context
child = await i.simple_fill(schema=schema, mode=mode)
child_context = (
child.content
) # 将返回内容(child.content)作为下一个子节点的context
self.content = child_context # 最后一个子节点返回的内容设置为父节点返回内容(self.content)
return self
class SimplePrint(Action):
"""
Action that print the num inputted
"""
def __init__(self, name="SimplePrint", input_num: int = 0):
super().__init__()
self.input_num = input_num
async def run(self, **kwargs):
print(str(self.input_num) + "\n")
return "0"
class ThinkAction(Action):
"""
Action that think
"""
def __init__(self, name="ThinkAction", context=None, llm=None):
super().__init__()
self.node = (
THINK_NODES()
) # 初始化Action时,初始化一个THINK_NODE实例并赋值给self.node
async def run(self, instruction) -> list:
PROMPT = """
You are now a number list generator, follow the instruction {instruction} and
generate a number list to be printed please.
"""
prompt = PROMPT.format(instruction=instruction)
print(f"thinkaction: {prompt}")
rsp_node = await self.node.fill(
context=prompt, llm=self.llm, schema="raw", strgy="complex"
) # 运行子节点,获取返回(返回格式为ActionNode)(注意设置 schema="raw" )
rsp = rsp_node.content # 获取返回的文本内容
rsp_match = self.find_in_brackets(
rsp
) # 按列表格式解析返回的文本内容,定位"["与"]"之间的内容
try:
rsp_list = list(
map(int, rsp_match[0].split(","))
) # 按列表格式解析返回的文本内容,按","对内容进行分割,并形成一个python语法中的列表
return rsp_list
except:
return []
@staticmethod
def find_in_brackets(s):
pattern = r"\[(.*?)\]"
match = re.findall(pattern, s)
return match
class Printer(Role):
def __init__(
self, name="Jerry", profile="Printer", goal="Print the number", constraints=""
):
super().__init__()
self.set_actions([ThinkAction])
# self.num_list = list()
async def _think(self) -> None:
"""Determine the action"""
# logger.info(self.rc.state)
if self.rc.todo is None:
self._set_state(0)
return
if self.rc.state + 1 < len(self.states):
self._set_state(self.rc.state + 1)
else:
self.rc.todo = None
async def _prepare_print(self, num_list: list) -> Message:
"""Add actions"""
actions = list()
for num in num_list:
actions.append(SimplePrint(input_num=num))
self.set_actions(actions)
self.rc.todo = None
return Message(content=str(num_list))
async def _act(self) -> Message:
"""Action"""
todo = self.rc.todo
if type(todo) is ThinkAction:
msg = self.rc.memory.get(k=1)[0]
self.goal = msg.content
resp = await todo.run(instruction=self.goal)
# logger.info(resp)
return await self._prepare_print(resp)
resp = await todo.run()
# logger.info(resp)
return Message(content=resp, role=self.profile)
async def _react(self) -> Message:
""""""
while True:
await self._think()
if self.rc.todo is None:
break
msg = await self._act()
return msg
async def main():
msg = "Provide the first 10 numbers of the Fibonacci series"
role = Printer()
logger.info(msg)
result = await role.run(msg)
logger.info(result)
asyncio.run(main())
输出:
(metagpt) D:\llm\MetaGPT> d: && cd d:\llm\MetaGPT && cmd /C "d:\soft\anaconda\envs\metagpt\python.exe c:\Users\32564\.vscode\extensions\ms-python.debugpy-2024.14.0-win32-x64\bundled\libs\debugpy\adapter/../..\debugpy\launcher 55967 -- D:\llm\MetaGPT\notebook\TEST12.PY "
2024-12-17 17:44:27.438 | INFO | metagpt.const:get_metagpt_package_root:21 - Package root set to d:\llm\metagpt
2024-12-17 17:44:33.566 | INFO | __main__:main:224 - Provide the first 10 numbers of the Fibonacci series
thinkaction:
You are now a number list generator, follow the instruction Provide the first 10 numbers of the Fibonacci series and
generate a number list to be printed please.
actionnode:
You are now a number list generator, follow the instruction Provide the first 10 numbers of the Fibonacci series and
generate a number list to be printed please.
## Actions
Language: Please use the same language as Human INPUT.
Think about what list of numbers you need to generate
The Fibonacci series is a sequence of numbers where each number is the sum of the two preceding ones, usually starting with 0 and 1. The first 10 numbers of the Fibonacci series are:
0, 1, 1, 2, 3, 5, 8, 13, 21, 34
Here is the number list generated:
[0, 1, 1, 2, 3, 5, 8, 13, 21, 34]
2024-12-17 17:44:38.394 | WARNING | metagpt.utils.cost_manager:update_cost:49 - Model GLM-4-flash not found in TOKEN_COSTS.
actionnode:The Fibonacci series is a sequence of numbers where each number is the sum of the two preceding ones, usually starting with 0 and 1. The first 10 numbers of the Fibonacci series are:
0, 1, 1, 2, 3, 5, 8, 13, 21, 34
Here is the number list generated:
[0, 1, 1, 2, 3, 5, 8, 13, 21, 34]
## Actions
Language: Please use the same language as Human INPUT.
Please provide the number list for me, strictly following the following requirements:
1. Answer strictly in the list format like [1,2,3,4]
2. Do not have extra spaces or line breaks.
Return the list here:
[0, 1, 1, 2, 3, 5, 8, 13, 21, 34]
2024-12-17 17:44:41.264 | WARNING | metagpt.utils.cost_manager:update_cost:49 - Model GLM-4-flash not found in TOKEN_COSTS.
0
1
1
2
3
5
8
13
21
34
2024-12-17 17:44:41.314 | INFO | __main__:main:226 - : 0
参考链接:
https://deepwisdom.feishu.cn/wiki/KhCcweQKmijXi6kDwnicM0qpnEf