add test case for action node

This commit is contained in:
geekan 2023-12-23 17:38:47 +08:00
parent 336350eba9
commit 6624819feb
5 changed files with 88 additions and 12 deletions

View file

@ -340,15 +340,15 @@ class ActionNode:
return self
def action_node_from_tuple_example():
# 示例:列表中包含元组
list_of_tuples = [("key1", str, "Instruction 1", "Example 1")]
def action_node_example():
node = ActionNode(key="key-0", expected_type=str, instruction="instruction-a", example="example-b")
# 从列表中创建 ActionNode 实例
nodes = [ActionNode(*data) for data in list_of_tuples]
for i in nodes:
logger.info(i)
logger.info(node.compile(context="123", schema="raw", mode="auto"))
logger.info(node.compile(context="123", schema="json", mode="auto"))
logger.info(node.compile(context="123", schema="markdown", mode="auto"))
logger.info(node.to_dict())
logger.info(node)
if __name__ == "__main__":
action_node_from_tuple_example()
action_node_example()

View file

@ -520,7 +520,7 @@ class Role(BaseModel):
return self._rc.memory.get(k=k)
@role_raise_decorator
async def run(self, with_message=None):
async def run(self, with_message=None) -> Message | None:
"""Observe, and think and act based on the results of the observation"""
if with_message:
msg = None

View file

@ -0,0 +1,76 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
@Time : 2023/12/23 15:49
@Author : alexanderwu
@File : test_action_node.py
"""
import pytest
from metagpt.actions import Action
from metagpt.actions.action_node import ActionNode
from metagpt.environment import Environment
from metagpt.roles import Role
from metagpt.schema import Message
from metagpt.team import Team
@pytest.mark.asyncio
async def test_debate_two_roles():
action1 = Action(name="BidenSay", instruction="Express opinions and argue vigorously, and strive to gain votes")
action2 = Action(name="TrumpSay", instruction="Express opinions and argue vigorously, and strive to gain votes")
biden = Role(
name="Biden", profile="Democratic candidate", goal="Win the election", actions=[action1], watch=[action2]
)
trump = Role(
name="Trump", profile="Republican candidate", goal="Win the election", actions=[action2], watch=[action1]
)
env = Environment(desc="US election live broadcast")
team = Team(investment=10.0, env=env, roles=[biden, trump])
history = await team.run(idea="Topic: climate change. Under 80 words per message.", send_to="Biden", n_round=3)
assert "BidenSay" in history
@pytest.mark.asyncio
async def test_debate_one_role_in_env():
action = Action(name="Debate", instruction="Express opinions and argue vigorously, and strive to gain votes")
biden = Role(name="Biden", profile="Democratic candidate", goal="Win the election", actions=[action])
env = Environment(desc="US election live broadcast")
team = Team(investment=10.0, env=env, roles=[biden])
history = await team.run(idea="Topic: climate change. Under 80 words per message.", send_to="Biden", n_round=3)
assert "Debate" in history
@pytest.mark.asyncio
async def test_debate_one_role():
action = Action(name="Debate", instruction="Express opinions and argue vigorously, and strive to gain votes")
biden = Role(name="Biden", profile="Democratic candidate", goal="Win the election", actions=[action])
msg: Message = await biden.run("Topic: climate change. Under 80 words per message.")
assert len(msg.content) > 10
assert msg.sent_from == "metagpt.roles.role.Role"
@pytest.mark.asyncio
async def test_action_node():
node = ActionNode(key="key-a", expected_type=str, instruction="instruction-b", example="example-c")
raw_template = node.compile(context="123", schema="raw", mode="auto")
json_template = node.compile(context="123", schema="json", mode="auto")
markdown_template = node.compile(context="123", schema="markdown", mode="auto")
node_dict = node.to_dict()
assert "123" in raw_template
assert "instruction" in raw_template
assert "123" in json_template
assert "format example" in json_template
assert "constraint" in json_template
assert "action" in json_template
assert "[/" in json_template
assert "123" in markdown_template
assert "key-a" in markdown_template
assert node_dict["key-a"] == "instruction-b"

View file

@ -21,8 +21,8 @@ context = """
@pytest.mark.asyncio
async def test_generate_questions():
detail_mining = GenerateQuestions()
rsp = await detail_mining.run(context)
action = GenerateQuestions()
rsp = await action.run(context)
logger.info(f"{rsp.content=}")
assert "Questions" in rsp.content

View file

@ -3,7 +3,7 @@
"""
@Time : 2023/9/13 00:26
@Author : fisherdeng
@File : test_detail_mining.py
@File : test_generate_questions.py
"""
import pytest