diff --git a/metagpt/actions/action_node.py b/metagpt/actions/action_node.py index 7445e5000..3529942c3 100644 --- a/metagpt/actions/action_node.py +++ b/metagpt/actions/action_node.py @@ -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() diff --git a/metagpt/roles/role.py b/metagpt/roles/role.py index 4becef625..8d229beec 100644 --- a/metagpt/roles/role.py +++ b/metagpt/roles/role.py @@ -521,7 +521,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 diff --git a/tests/metagpt/actions/test_action_node.py b/tests/metagpt/actions/test_action_node.py new file mode 100644 index 000000000..24b48f2f6 --- /dev/null +++ b/tests/metagpt/actions/test_action_node.py @@ -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" diff --git a/tests/metagpt/actions/test_detail_mining.py b/tests/metagpt/actions/test_generate_questions.py similarity index 86% rename from tests/metagpt/actions/test_detail_mining.py rename to tests/metagpt/actions/test_generate_questions.py index a178ec840..b7c9d3984 100644 --- a/tests/metagpt/actions/test_detail_mining.py +++ b/tests/metagpt/actions/test_generate_questions.py @@ -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 diff --git a/tests/metagpt/actions/test_prepare_interview.py b/tests/metagpt/actions/test_prepare_interview.py index 7c32882e0..cd0c850ed 100644 --- a/tests/metagpt/actions/test_prepare_interview.py +++ b/tests/metagpt/actions/test_prepare_interview.py @@ -3,7 +3,7 @@ """ @Time : 2023/9/13 00:26 @Author : fisherdeng -@File : test_detail_mining.py +@File : test_generate_questions.py """ import pytest