mirror of
https://github.com/FoundationAgents/MetaGPT.git
synced 2026-06-02 14:45:17 +02:00
feat: 删掉meta role相关代码
This commit is contained in:
parent
cc89f3b726
commit
2574ecaecf
9 changed files with 0 additions and 623 deletions
|
|
@ -1,133 +0,0 @@
|
|||
#!/usr/bin/env python
|
||||
# -*- coding: utf-8 -*-
|
||||
"""
|
||||
@Time : 2023/8/7
|
||||
@Author : mashenquan
|
||||
@File : fork_meta_role.py
|
||||
@Desc : I am attempting to incorporate certain symbol concepts from UML into MetaGPT, enabling it to have the
|
||||
ability to freely construct flows through symbol concatenation. Simultaneously, I am also striving to
|
||||
make these symbols configurable and standardized, making the process of building flows more convenient.
|
||||
For more about `fork` node in activity diagrams, see: `https://www.uml-diagrams.org/activity-diagrams.html`
|
||||
This file defines a `fork` style meta role capable of generating arbitrary roles at runtime based on a
|
||||
configuration file.
|
||||
@Modified By: mashenquan, 2023/8/22. A definition has been provided for the return value of _think: returning false indicates that further reasoning cannot continue.
|
||||
|
||||
"""
|
||||
|
||||
import re
|
||||
|
||||
import aiofiles
|
||||
|
||||
from metagpt.actions.meta_action import MetaAction
|
||||
from metagpt.const import WORKSPACE_ROOT
|
||||
from metagpt.logs import logger
|
||||
from metagpt.roles import Role
|
||||
from metagpt.roles.uml_meta_role_options import MetaActionOptions, UMLMetaRoleOptions
|
||||
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, cost_manager, role_options, **kwargs):
|
||||
"""Initialize a `fork` style meta role
|
||||
|
||||
:param 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(**role_options)
|
||||
global_variables = {
|
||||
"name": Role.format_value(opts.name, kwargs),
|
||||
"profile": Role.format_value(opts.profile, kwargs),
|
||||
"goal": Role.format_value(opts.goal, kwargs),
|
||||
"constraints": Role.format_value(opts.constraints, kwargs),
|
||||
"desc": Role.format_value(opts.desc, kwargs),
|
||||
"role": Role.format_value(opts.role, kwargs)
|
||||
}
|
||||
for k, v in kwargs.items():
|
||||
if k not in global_variables:
|
||||
global_variables[k] = v
|
||||
|
||||
super(ForkMetaRole, self).__init__(
|
||||
options=options,
|
||||
cost_manager=cost_manager,
|
||||
name=global_variables["name"],
|
||||
profile=global_variables["profile"],
|
||||
goal=global_variables["goal"],
|
||||
constraints=global_variables["constraints"],
|
||||
desc=global_variables["desc"],
|
||||
**kwargs
|
||||
)
|
||||
actions = []
|
||||
for m in opts.actions:
|
||||
for k, v in m.items():
|
||||
v = Role.format_value(v, kwargs)
|
||||
m[k] = v
|
||||
for k, v in global_variables.items():
|
||||
if k not in m:
|
||||
m[k] = v
|
||||
|
||||
o = MetaActionOptions(**m)
|
||||
o.set_default_template(opts.templates[o.template_ix])
|
||||
|
||||
act = MetaAction(options=options, action_options=o, llm=self._llm, **m)
|
||||
actions.append(act)
|
||||
self._init_actions(actions)
|
||||
requirement_types = set()
|
||||
for v in opts.requirement:
|
||||
requirement_types.add(MetaAction.get_action_type(v))
|
||||
self._watch(requirement_types)
|
||||
|
||||
async def _think(self) -> None:
|
||||
"""Everything will be done part by part."""
|
||||
if self._rc.todo is None:
|
||||
self._set_state(0)
|
||||
return True
|
||||
|
||||
if self._rc.state + 1 < len(self._states):
|
||||
self._set_state(self._rc.state + 1)
|
||||
else:
|
||||
self._rc.todo = None
|
||||
return False
|
||||
|
||||
async def _react(self) -> Message:
|
||||
ret = Message(content="")
|
||||
while True:
|
||||
await self._think()
|
||||
if self._rc.todo is None:
|
||||
break
|
||||
logger.debug(f"{self._setting}: {self._rc.state=}, will do {self._rc.todo}")
|
||||
msg = await self._act()
|
||||
if ret.content != '':
|
||||
ret.content += "\n\n\n"
|
||||
ret.content += msg.content
|
||||
logger.info(ret.content)
|
||||
await self.save(ret.content)
|
||||
return ret
|
||||
|
||||
async def save(self, content):
|
||||
"""Save teaching plan"""
|
||||
output_filename = self.options.get("output_filename")
|
||||
if not output_filename:
|
||||
return
|
||||
filename = ForkMetaRole.new_file_name(output_filename)
|
||||
pathname = WORKSPACE_ROOT / "teaching_plan"
|
||||
pathname.mkdir(exist_ok=True)
|
||||
pathname = pathname / filename
|
||||
try:
|
||||
async with aiofiles.open(str(pathname), mode='w', encoding='utf-8') as writer:
|
||||
await writer.write(content)
|
||||
except Exception as e:
|
||||
logger.error(f'Save failed:{e}')
|
||||
logger.info(f"Save to:{pathname}")
|
||||
|
||||
@staticmethod
|
||||
def new_file_name(lesson_title, ext=".md"):
|
||||
"""Create a related file name based on `lesson_title` and `ext`."""
|
||||
# Define the special characters that need to be replaced.
|
||||
illegal_chars = r'[#@$%!*&\\/:*?"<>|\n\t \']'
|
||||
# Replace the special characters with underscores.
|
||||
filename = re.sub(illegal_chars, '_', lesson_title) + ext
|
||||
return re.sub(r'_+', '_', filename)
|
||||
|
|
@ -1,43 +0,0 @@
|
|||
#!/usr/bin/env python
|
||||
# -*- coding: utf-8 -*-
|
||||
"""
|
||||
@Time : 2023/8/7
|
||||
@Author : mashenquan
|
||||
@File : uml_meta_role_factory.py
|
||||
@Desc : I am attempting to incorporate certain symbol concepts from UML into MetaGPT, enabling it to have the
|
||||
ability to freely construct flows through symbol concatenation. Simultaneously, I am also striving to
|
||||
make these symbols configurable and standardized, making the process of building flows more convenient.
|
||||
For more about `fork` node in activity diagrams, see: `https://www.uml-diagrams.org/activity-diagrams.html`
|
||||
"""
|
||||
|
||||
from metagpt.roles.fork_meta_role import ForkMetaRole
|
||||
from metagpt.roles.uml_meta_role_options import UMLMetaRoleOptions
|
||||
|
||||
|
||||
class UMLMetaRoleFactory:
|
||||
"""Factory of UML activity role classes"""
|
||||
|
||||
@classmethod
|
||||
def create_roles(cls, role_configs, **kwargs):
|
||||
"""Generate the flow of the project based on the configuration in the format of config/pattern/template.yaml.
|
||||
|
||||
:param role_configs: `roles` field of template.yaml
|
||||
:param kwargs: Parameters passed in format: `python your_script.py --param1=value1 --param2=value2`
|
||||
|
||||
"""
|
||||
roles = []
|
||||
for m in role_configs:
|
||||
opt = UMLMetaRoleOptions(**m)
|
||||
constructor = cls.CONSTRUCTORS.get(opt.role_type)
|
||||
if constructor is None:
|
||||
raise NotImplementedError(
|
||||
f"{opt.role_type} is not implemented"
|
||||
)
|
||||
r = constructor(role_options=m, **kwargs)
|
||||
roles.append(r)
|
||||
return roles
|
||||
|
||||
CONSTRUCTORS = {
|
||||
"fork": ForkMetaRole,
|
||||
# TODO: add more activity node constructor here..
|
||||
}
|
||||
|
|
@ -1,69 +0,0 @@
|
|||
#!/usr/bin/env python
|
||||
# -*- coding: utf-8 -*-
|
||||
"""
|
||||
@Time : 2023/8/7
|
||||
@Author : mashenquan
|
||||
@File : uml_meta_role_options.py
|
||||
@Desc : I am attempting to incorporate certain symbol concepts from UML into MetaGPT, enabling it to have the
|
||||
ability to freely construct flows through symbol concatenation. Simultaneously, I am also striving to
|
||||
make these symbols configurable and standardized, making the process of building flows more convenient.
|
||||
For more about `fork` node in activity diagrams, see: `https://www.uml-diagrams.org/activity-diagrams.html`
|
||||
"""
|
||||
|
||||
from typing import List, Dict
|
||||
|
||||
from pydantic import BaseModel
|
||||
|
||||
|
||||
# `startup` field of config/pattern/template.yaml
|
||||
class StartupConfig(BaseModel):
|
||||
requirement: str
|
||||
role: str
|
||||
investment: float = 3.0
|
||||
n_round: int = 3
|
||||
|
||||
|
||||
# config/pattern/template.yaml
|
||||
class ProjectConfig(BaseModel):
|
||||
startup: StartupConfig
|
||||
roles: List[Dict]
|
||||
|
||||
|
||||
# element of `actions` field of config/pattern/template.yaml
|
||||
class MetaActionOptions(BaseModel):
|
||||
topic: str
|
||||
name: str = ""
|
||||
language: str = "Chinese"
|
||||
template_ix: int = 0
|
||||
statements: List[str] = []
|
||||
template: str = ""
|
||||
rsp_begin_tag: str = ""
|
||||
rsp_end_tag: str = ""
|
||||
|
||||
def set_default_template(self, v):
|
||||
if not self.template:
|
||||
self.template = v
|
||||
|
||||
def format_prompt(self, **kwargs):
|
||||
statements = "\n".join(self.statements)
|
||||
opts = kwargs.copy()
|
||||
opts["statements"] = statements
|
||||
|
||||
from metagpt.roles import Role
|
||||
prompt = Role.format_value(self.template, opts)
|
||||
return prompt
|
||||
|
||||
|
||||
# element of `roles` field of config/pattern/template.yaml
|
||||
class UMLMetaRoleOptions(BaseModel):
|
||||
role_type: str
|
||||
name: str = ""
|
||||
profile: str = ""
|
||||
goal: str = ""
|
||||
role: str = ""
|
||||
constraints: str = ""
|
||||
desc: str = ""
|
||||
templates: List[str] = []
|
||||
output_filename: str = ""
|
||||
actions: List
|
||||
requirement: List
|
||||
Loading…
Add table
Add a link
Reference in a new issue