refine code

This commit is contained in:
geekan 2024-01-02 20:49:20 +08:00
parent f5ed1349ba
commit c50ae4d8d7
2 changed files with 26 additions and 23 deletions

View file

@ -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_validator
from metagpt.actions.action_node import ActionNode
from metagpt.llm import LLM
@ -34,16 +34,19 @@ class Action(SerializationMixin, is_polymorphic_base=True):
desc: str = "" # for skill manager
node: 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
@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
def __init__(self, **data: Any):
super().__init__(**data)
if "instruction" in data:
self.__init_with_instruction(data["instruction"])
@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 values
def set_prefix(self, prefix):
"""Set prefix for later usage"""

View file

@ -21,35 +21,35 @@ 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")
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.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])
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.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])
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