mirror of
https://github.com/FoundationAgents/MetaGPT.git
synced 2026-05-08 15:22:38 +02:00
feat(core): Add stream data return and reception
1. add file: utils/steam_pipe.py 2. add demo: samples/flask_web_api.py 3. Other core code modifications, Add and use the StreamPipe class at night 4. Add flask library to requirements
This commit is contained in:
parent
643450388a
commit
7706b88f03
14 changed files with 213 additions and 4 deletions
|
|
@ -39,6 +39,8 @@ from metagpt.strategy.planner import Planner
|
|||
from metagpt.utils.common import any_to_name, any_to_str, role_raise_decorator
|
||||
from metagpt.utils.project_repo import ProjectRepo
|
||||
from metagpt.utils.repair_llm_raw_output import extract_state_value_from_output
|
||||
from metagpt.utils.stream_pipe import StreamPipe
|
||||
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from metagpt.environment import Environment # noqa: F401
|
||||
|
|
@ -139,6 +141,8 @@ class Role(SerializationMixin, ContextMixin, BaseModel):
|
|||
role_id: str = ""
|
||||
states: list[str] = []
|
||||
|
||||
stream_pipe: Optional[StreamPipe] = None
|
||||
|
||||
# scenarios to set action system_prompt:
|
||||
# 1. `__init__` while using Role(actions=[...])
|
||||
# 2. add action to role while using `role.set_action(action)`
|
||||
|
|
|
|||
|
|
@ -10,7 +10,7 @@ from datetime import datetime
|
|||
from typing import Dict
|
||||
|
||||
from metagpt.actions.write_tutorial import WriteContent, WriteDirectory
|
||||
from metagpt.const import TUTORIAL_PATH
|
||||
from metagpt.const import TUTORIAL_PATH, METAGPT_ROOT
|
||||
from metagpt.logs import logger
|
||||
from metagpt.roles.role import Role, RoleReactMode
|
||||
from metagpt.schema import Message
|
||||
|
|
@ -40,7 +40,7 @@ class TutorialAssistant(Role):
|
|||
|
||||
def __init__(self, **kwargs):
|
||||
super().__init__(**kwargs)
|
||||
self.set_actions([WriteDirectory(language=self.language)])
|
||||
self.set_actions([WriteDirectory(language=self.language, stream_pipe=self.stream_pipe)])
|
||||
self._set_react_mode(react_mode=RoleReactMode.BY_ORDER.value)
|
||||
|
||||
async def _handle_directory(self, titles: Dict) -> Message:
|
||||
|
|
@ -58,7 +58,7 @@ class TutorialAssistant(Role):
|
|||
self.total_content += f"# {self.main_title}"
|
||||
actions = list()
|
||||
for first_dir in titles.get("directory"):
|
||||
actions.append(WriteContent(language=self.language, directory=first_dir))
|
||||
actions.append(WriteContent(language=self.language, directory=first_dir, stream_pipe=self.stream_pipe))
|
||||
key = list(first_dir.keys())[0]
|
||||
directory += f"- {key}\n"
|
||||
for second_dir in first_dir[key]:
|
||||
|
|
@ -91,4 +91,8 @@ class TutorialAssistant(Role):
|
|||
root_path = TUTORIAL_PATH / datetime.now().strftime("%Y-%m-%d_%H-%M-%S")
|
||||
await File.write(root_path, f"{self.main_title}.md", self.total_content.encode("utf-8"))
|
||||
msg.content = str(root_path / f"{self.main_title}.md")
|
||||
|
||||
if self.stream_pipe:
|
||||
self.stream_pipe.set_k_message("file_name", msg.content.replace(str(METAGPT_ROOT), ""))
|
||||
self.stream_pipe.with_finish()
|
||||
return msg
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue