🌟 The Multi-Agent Framework: First AI Software Company, Towards Natural Language Programming
refine code
geekanc50ae4d8d789
diff --git a/metagpt/actions/action.py b/metagpt/actions/action.pyindex 9b94ce461..b586bcc22 100644--- a/metagpt/actions/action.py+++ b/metagpt/actions/action.py@@ -8,9 +8,9 @@from __future__ import annotations-from typing import Any, Optional, Union+from typing import Optional, Union-from pydantic import ConfigDict, Field+from pydantic import ConfigDict, Field, model_validatorfrom metagpt.actions.action_node import ActionNodefrom metagpt.llm import LLM@@ -34,16 +34,19 @@ class Action(SerializationMixin, is_polymorphic_base=True):desc: str = "" # for skill managernode: ActionNode = Field(default=None, exclude=True)- def __init_with_instruction(self, instruction: str):- """Initialize action with instruction"""- self.node = ActionNode(key=self.name, expected_type=str, instruction=instruction, example="", schema="raw")- return self-- def __init__(self, **data: Any):- super().__init__(**data)-- if "instruction" in data:- self.__init_with_instruction(data["instruction"])+ @model_validator(mode="before")+ def set_name_if_empty(cls, values):+ if "name" not in values or not values["name"]:+ values["name"] = cls.__name__+ return values++ @model_validator(mode="before")+ def _init_with_instruction(cls, values):+ if "instruction" in values:+ name = values["name"]+ i = values["instruction"]+ values["node"] = ActionNode(key=name, expected_type=str, instruction=i, example="", schema="raw")+ return valuesdef set_prefix(self, prefix):"""Set prefix for later usage"""diff --git a/tests/metagpt/actions/test_action_node.py b/tests/metagpt/actions/test_action_node.pyindex 25aceaa2e..384c4507b 100644--- a/tests/metagpt/actions/test_action_node.py+++ b/tests/metagpt/actions/test_action_node.py@@ -21,35 +21,35 @@ from metagpt.team import Team@pytest.mark.asyncioasync 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")+ action1 = Action(name="AlexSay", instruction="Express your opinion with emotion and don't repeat it")+ action2 = Action(name="BobSay", instruction="Express your opinion with emotion and don't repeat it")biden = Role(- name="Biden", profile="Democratic candidate", goal="Win the election", actions=[action1], watch=[action2]+ name="Alex", 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]+ name="Bob", 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 "Biden" in history+ history = await team.run(idea="Topic: climate change. Under 80 words per message.", send_to="Alex", n_round=3)+ assert "Alex" in history@pytest.mark.asyncioasync 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])+ action = Action(name="Debate", instruction="Express your opinion with emotion and don't repeat it")+ biden = Role(name="Alex", 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 "Biden" in history+ history = await team.run(idea="Topic: climate change. Under 80 words per message.", send_to="Alex", n_round=3)+ assert "Alex" in history@pytest.mark.asyncioasync 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])+ action = Action(name="Debate", instruction="Express your opinion with emotion and don't repeat it")+ biden = Role(name="Alex", 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