Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create a simple webui with gradio #316

Open
wants to merge 16 commits into
base: main
Choose a base branch
from
113 changes: 49 additions & 64 deletions metagpt/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,85 +6,70 @@
@File : app.py
"""
import gradio as gr
from metagpt.software_company import SoftwareCompany, SoftwareCompanyWithHuman
from metagpt.roles import ProjectManager,\
ProductManager, Architect,\
Engineer, QaEngineer
from metagpt.software_company import SoftwareCompany
from metagpt.roles import ProjectManager,ProductManager, Architect,Engineer, QaEngineer
from metagpt.roles.researcher import RESEARCH_PATH, Researcher
from metagpt.const import PROJECT_ROOT
import sys

def clear_logs():
with open(PROJECT_ROOT / 'logs/log.txt', 'w') as f:
f.write("")

class Report:
def __init__(self, role, action, sent_from, send_to, content):
self.role = role
self.action = action
self.sent_from = sent_from
self.send_to = send_to
self.content = content

async def startup(company_name : str,
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PEP8. 可以尝试使用PyCharm自带的format或者black / flake8

idea : str,
staffs : list,
investment : float = 6.0,
n_round : int = 5,
code_review : bool = True,
staffs : list = ["ProjectManager",
"ProductManager",
"Architect"]
)->SoftwareCompany:

if company_name == "SoftwareCompany":
company = SoftwareCompany()
elif company_name == "SoftwareCompany_With_Human":
company = SoftwareCompanyWithHuman()
else:
return "Company type not supported"
if idea == "":
return "Please input your idea"
code_review : bool = False,
auto_mode : bool = False,
):
global SoftwareCompany_Company

if company_name == "SoftwareCompany": company = SoftwareCompany()
else: return "Company type not supported"
if idea == "": return "Please input your idea"
staff_list = []
for staff in staffs:
if staff == "ProjectManager":
staff_list.append(ProjectManager())
elif staff == "ProductManager":
staff_list.append(ProductManager())
elif staff == "Architect":
staff_list.append(Architect())
elif staff == "Engineer":
staff_list.append(Engineer(n_borg=5,use_code_review=code_review))
elif staff == "QaEngineer":
staff_list.append(QaEngineer())
if staff == "ProjectManager": staff_list.append(ProjectManager())
elif staff == "ProductManager": staff_list.append(ProductManager())
elif staff == "Architect": staff_list.append(Architect())
elif staff == "Engineer": staff_list.append(Engineer(n_borg=5,use_code_review=code_review))
elif staff == "QaEngineer": staff_list.append(QaEngineer())
company.hire(staff_list)
company.invest(investment)
company.start_project(idea)
# report all output to webui
global SoftwareCompany_Company
SoftwareCompany_Company = company
if company_name == "SoftwareCompany":
if auto_mode:
company.environment.human_interaction = False
await company.run(n_round)
elif company_name == "SoftwareCompany_With_Human":
await company.continue_run()
return "Role: "+company.environment.short_term_history.role,\
"Action: "+str(company.environment.short_term_history.cause_by),\
company.environment.short_term_history.sent_from,\
company.environment.short_term_history.send_to,\
company.environment.short_term_history.content,\
company.environment.short_term_history.content
else :
company.environment.human_interaction = True
report = Report(company.environment.short_term_history.role,
str(company.environment.short_term_history.cause_by),
company.environment.short_term_history.sent_from,
company.environment.short_term_history.send_to,
company.environment.short_term_history.content)
return report.role, report.action, report.sent_from, report.send_to, report.content, report.content

async def __continue(message_content : str):
company = SoftwareCompany_Company
company.environment.short_term_history.content = message_content
company.environment.memory.add(company.environment.short_term_history)
company.environment.history += f"\n{company.environment.short_term_history}"
await company.continue_run()
return "Role: "+company.environment.short_term_history.role,\
"Action: "+str(company.environment.short_term_history.cause_by),\
company.environment.short_term_history.sent_from,company.environment.short_term_history.send_to,\
company.environment.short_term_history.content,\
company.environment.short_term_history.content
await company.run_one_round()
report = Report(company.environment.short_term_history.role,
str(company.environment.short_term_history.cause_by),
company.environment.short_term_history.sent_from,
company.environment.short_term_history.send_to,
company.environment.short_term_history.content)
return report.role, report.action, report.sent_from, report.send_to, report.content, report.content

async def research_startup(language : str,
topic : str):
if language == "en-us":
language = "en-us"
elif language == "zh-ch":
language = "zh-ch"
else:
raise Exception("Language not supported")
async def research_startup(language : str, topic : str):
role = Researcher(language="en-us")
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

上面的判断逻辑与这里的实现无关。一般而言,我建议提交代码只提交在当时的最优解,尽可能不提交最优解之外的逻辑

await role.run(topic)
return f"save report to {RESEARCH_PATH / f'{topic}.md'}."
Expand All @@ -97,17 +82,17 @@ async def research_startup(language : str,
gr.Markdown("""
# MetaGPT
""")

with gr.Tabs():
with gr.TabItem("MetaGPT") as generate_tab:
company_choise = gr.Dropdown(label = "Choose the company type",\
choices = ["SoftwareCompany", "SoftwareCompany_With_Human"],\
value = "SoftwareCompany_With_Human")
company_choise = gr.Dropdown(label = "Choose the company type",choices = ["SoftwareCompany"],value = "SoftwareCompany")
with gr.Row():
investment = gr.Slider(minimum=0.0, maximum=20.0, step=0.1, label="Investment",value = 6.0,\
info="The maxmium money you want to spend on the GPT generation")
n_round = gr.Number( label="Round", value = 5, info="The maxmium round you want to run",visible = False)
n_round = gr.Number( label="Round", value = 5, info="The maxmium round you want to run",visible = True)
with gr.Row():
code_review = gr.Checkbox(label = "Whether to use code review", value = False)
auto_mode = gr.Checkbox(label = "Whether to use auto mode", info = "run without human interaction", value = False)
staffs = gr.CheckboxGroup(["ProjectManager", "ProductManager", "Architect", "Engineer", "QaEngineer"],\
label="Choose the staff you would like to hire",\
value = ["ProjectManager", "ProductManager", "Architect", "Engineer"])
Expand All @@ -129,9 +114,8 @@ async def research_startup(language : str,
output_content_metagpt = gr.Textbox(label="The phased output of MetaGPT, modify it as your will",\
max_lines=999,show_copy_button = True)
output_content_markdown_metagpt = gr.Markdown(label="The markdown output of MetaGPT", visible = False)


Start_MetaGPT.click(startup, [company_choise, idea, investment, n_round, code_review, staffs],\

Start_MetaGPT.click(startup, [company_choise, idea, staffs, investment, n_round, code_review, auto_mode],\
[output_role_metagpt,output_cause_metagpt,output_sent_from_metagpt,output_send_to_metagpt,\
output_content_metagpt,output_content_markdown_metagpt])
# clear_log.click(clear_logs, [],[])
Expand All @@ -144,6 +128,7 @@ async def research_startup(language : str,
if company_choise == "SoftwareCompany_With_Human" else True), [company_choise], [n_round])
show_markdown.change(lambda x: gr.update(visible = True if x == True else False),\
[show_markdown], [output_content_markdown_metagpt])

with gr.TabItem("Research") as research_tab:
language = gr.Dropdown(label = "Choose the language", choices = ["en-us","zh-ch"], value = "en-us")
topic = gr.Textbox(label="Your research topic, such as 'dataiku vs. datarobot'", value = "dataiku vs. datarobot")
Expand Down
168 changes: 0 additions & 168 deletions metagpt/app_chs.py
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

删除中文版

This file was deleted.

6 changes: 3 additions & 3 deletions metagpt/environment.py
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. 修改了short_term_memory的类型,成为了message类型
  2. 将环境类型改为human_interaction变量,如果为True,则不直接将message储存到记忆中与历史中,而是在用户修改后再储存。在app.py中有具体实现方式

Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ class Environment(BaseModel):
"""环境,承载一批角色,角色可以向环境发布消息,可以被其他角色观察到
Environment, hosting a batch of roles, roles can publish messages to the environment, and can be observed by other roles
"""
environment_type: str = Field(default='SoftwareCompany')
short_term_history: str = Field(default_factory=Memory)
roles: dict[str, Role] = Field(default_factory=dict)
memory: Memory = Field(default_factory=Memory)
history: str = Field(default='')
short_term_history : Message = Field(default=Message(content=""))
human_interaction : bool = Field(default=False)

class Config:
arbitrary_types_allowed = True
Expand All @@ -47,7 +47,7 @@ def publish_message(self, message: Message):
Post information to the current environment
"""
self.short_term_history = message
if self.environment_type == "SoftwareCompany":
if self.human_interaction == False:
self.memory.add(message)
self.history += f"\n{message}"

Expand Down