feat: merge role_option

This commit is contained in:
莘权 马 2023-08-20 20:22:59 +08:00
parent ec20629561
commit ae94b6dff8
7 changed files with 34 additions and 19 deletions

View file

@ -20,7 +20,7 @@ class TeachingPlanRequirement(Action):
class WriteTeachingPlanPart(Action):
"""Write Teaching Plan Part"""
def __init__(self, name: str = "", context=None, llm=None, topic: str = "", language: str = "Chinese"):
def __init__(self, options, name: str = "", context=None, llm=None, topic: str = "", language: str = "Chinese"):
"""
:param name: action name
@ -29,7 +29,7 @@ class WriteTeachingPlanPart(Action):
:param topic: topic part of teaching plan
:param language: A human language, such as Chinese, English, French, etc.
"""
super().__init__(name, context, llm)
super().__init__(options, name, context, llm)
self.topic = topic
self.language = language
self.rsp = None

View file

@ -26,14 +26,16 @@ from metagpt.schema import Message
class ForkMetaRole(Role):
"""A `fork` style meta role capable of generating arbitrary roles at runtime based on a configuration file"""
def __init__(self, options, **kwargs):
def __init__(self, runtime_options, cost_manager, role_options, **kwargs):
"""Initialize a `fork` style meta role
:param options: pattern yaml file data
:param runtime_options: System configuration
:param cost_manager: Cost manager
:param role_options: pattern yaml file data
:param args: Parameters passed in format: `python your_script.py arg1 arg2 arg3`
:param kwargs: Parameters passed in format: `python your_script.py --param1=value1 --param2=value2`
"""
opts = UMLMetaRoleOptions(**options)
opts = UMLMetaRoleOptions(**role_options)
global_variables = {
"name": Role.format_value(opts.name, kwargs),
"profile": Role.format_value(opts.profile, kwargs),
@ -47,6 +49,8 @@ class ForkMetaRole(Role):
global_variables[k] = v
super(ForkMetaRole, self).__init__(
options=runtime_options,
cost_manager=cost_manager,
name=global_variables["name"],
profile=global_variables["profile"],
goal=global_variables["goal"],
@ -54,7 +58,6 @@ class ForkMetaRole(Role):
desc=global_variables["desc"],
**kwargs
)
self.options = options
actions = []
for m in opts.actions:
for k, v in m.items():

View file

@ -173,6 +173,7 @@ class Role:
"""Return number of action"""
return len(self._actions)
@property
def options(self):
return self._options

View file

@ -20,13 +20,13 @@ import re
class Teacher(Role):
"""Support configurable teacher roles,
with native and teaching languages being replaceable through configurations."""
def __init__(self, name='Lily', profile='{teaching_language} Teacher',
def __init__(self, options, name='Lily', profile='{teaching_language} Teacher',
goal='writing a {language} teaching plan part by part',
constraints='writing in {language}', desc="", *args, **kwargs):
super().__init__(name=name, profile=profile, goal=goal, constraints=constraints, desc=desc, *args, **kwargs)
super().__init__(options=options, name=name, profile=profile, goal=goal, constraints=constraints, desc=desc, *args, **kwargs)
actions = []
for topic in WriteTeachingPlanPart.TOPICS:
act = WriteTeachingPlanPart(topic=topic, llm=self._llm)
act = WriteTeachingPlanPart(options=options, topic=topic, llm=self._llm)
actions.append(act)
self._init_actions(actions)
self._watch({TeachingPlanRequirement})

View file

@ -12,12 +12,13 @@ from pydantic import BaseModel
from langchain.llms.base import LLM
from metagpt.actions.write_teaching_plan import WriteTeachingPlanPart
from metagpt.config import Config
from metagpt.schema import Message
class MockWriteTeachingPlanPart(WriteTeachingPlanPart):
def __init__(self, name: str = '', context=None, llm: LLM = None, topic="", language="Chinese"):
super().__init__(name, context, llm, topic, language)
def __init__(self, options, name: str = '', context=None, llm: LLM = None, topic="", language="Chinese"):
super().__init__(options, name, context, llm, topic, language)
async def _aask(self, prompt: str, system_msgs: Optional[list[str]] = None) -> str:
return f"{WriteTeachingPlanPart.DATA_BEGIN_TAG}\nprompt\n{WriteTeachingPlanPart.DATA_END_TAG}"
@ -47,7 +48,8 @@ async def mock_write_teaching_plan_part():
for i in inputs:
seed = Inputs(**i)
act = MockWriteTeachingPlanPart(name=seed.name, topic=seed.topic, language=seed.language)
options = Config().runtime_options
act = MockWriteTeachingPlanPart(options=options, name=seed.name, topic=seed.topic, language=seed.language)
await act.run([Message(content="")])
assert act.topic == seed.topic
assert str(act) == seed.topic

View file

@ -9,6 +9,8 @@ from typing import Dict
from pydantic import BaseModel
from metagpt.config import Config
from metagpt.provider.openai_api import CostManager
from metagpt.roles.fork_meta_role import ForkMetaRole
@ -79,7 +81,9 @@ def test_creat_role():
"teaching_language": "AA",
"language": "BB"
}
role = ForkMetaRole(seed.role, **kwargs)
runtime_options = Config().runtime_options
cost_manager = CostManager(options=runtime_options)
role = ForkMetaRole(runtime_options=runtime_options, cost_manager=cost_manager, role_options=seed.role, **kwargs)
assert role.action_count == 2
assert "{" not in role.profile
assert "{" not in role.goal

View file

@ -9,6 +9,8 @@
from typing import Dict, Optional
from pydantic import BaseModel
from metagpt.config import Config
from metagpt.provider.openai_api import CostManager
from metagpt.roles.teacher import Teacher
@ -42,22 +44,25 @@ def test_init():
},
{
"name": "Lily{language}",
"expect_name": "LilyChinese",
"expect_name": "Lily{language}",
"profile": "X {teaching_language}",
"expect_profile": "X English",
"expect_profile": "X {teaching_language}",
"goal": "Do {something_big}, {language}",
"expect_goal": "Do {something_big}, Chinese",
"expect_goal": "Do {something_big}, {language}",
"constraints": "Do in {key1}, {language}",
"expect_constraints": "Do in {key1}, Chinese",
"expect_constraints": "Do in {key1}, {language}",
"kwargs": {},
"desc": "aaa{language}",
"expect_desc": "aaaChinese"
"expect_desc": "aaa{language}"
},
]
for i in inputs:
seed = Inputs(**i)
teacher = Teacher(name=seed.name, profile=seed.profile, goal=seed.goal, constraints=seed.constraints,
options = Config().runtime_options
cost_manager = CostManager(options=options)
teacher = Teacher(options=options, cost_manager=cost_manager, name=seed.name, profile=seed.profile,
goal=seed.goal, constraints=seed.constraints,
desc=seed.desc, **seed.kwargs)
assert teacher.name == seed.expect_name
assert teacher.desc == seed.expect_desc