mirror of
https://github.com/FoundationAgents/MetaGPT.git
synced 2026-06-08 15:05:17 +02:00
feat: fix coding
This commit is contained in:
parent
946e6fa8b3
commit
71b4922f55
7 changed files with 23 additions and 33 deletions
|
|
@ -6,12 +6,11 @@
|
|||
@File : skill_loader.py
|
||||
@Desc : Skill YAML Configuration Loader.
|
||||
"""
|
||||
|
||||
from pathlib import Path
|
||||
from typing import List, Dict, Optional
|
||||
|
||||
import yaml
|
||||
from pydantic import BaseModel
|
||||
from pydantic import BaseModel, Field
|
||||
|
||||
|
||||
class Example(BaseModel):
|
||||
|
|
@ -24,11 +23,18 @@ class Returns(BaseModel):
|
|||
format: Optional[str] = None
|
||||
|
||||
|
||||
class Prerequisite(BaseModel):
|
||||
name: str
|
||||
type: Optional[str] = None
|
||||
description: Optional[str] = None
|
||||
default: Optional[str] = None
|
||||
|
||||
|
||||
class Skill(BaseModel):
|
||||
name: str
|
||||
description: str
|
||||
id: str
|
||||
requisite: List[str]
|
||||
x_prerequisite: Optional[List[Prerequisite]] = Field(default=None, alias="x-prerequisite")
|
||||
arguments: Dict
|
||||
examples: List[Example]
|
||||
returns: Returns
|
||||
|
|
|
|||
|
|
@ -20,7 +20,7 @@ from pathlib import Path
|
|||
from metagpt.actions import ActionOutput
|
||||
from metagpt.actions.skill_action import SkillAction, ArgumentsParingAction
|
||||
from metagpt.actions.talk_action import TalkAction
|
||||
from metagpt.config import Config
|
||||
from metagpt.config import Config, CONFIG
|
||||
from metagpt.const import BRAIN_MEMORY, SKILL_PATH
|
||||
from metagpt.learn.skill_loader import SkillLoader
|
||||
from metagpt.logs import logger
|
||||
|
|
@ -33,13 +33,13 @@ from metagpt.schema import Message
|
|||
class Assistant(Role):
|
||||
"""Assistant for solving common issues."""
|
||||
|
||||
def __init__(self, options, cost_manager, name="Lily", profile="An assistant", goal="Help to solve problem",
|
||||
def __init__(self, name="Lily", profile="An assistant", goal="Help to solve problem",
|
||||
constraints="Talk in {language}", desc="", *args, **kwargs):
|
||||
super(Assistant, self).__init__(options=options, cost_manager=cost_manager, name=name, profile=profile,
|
||||
super(Assistant, self).__init__(name=name, profile=profile,
|
||||
goal=goal, constraints=constraints, desc=desc, *args, **kwargs)
|
||||
brain_memory = options.get(BRAIN_MEMORY)
|
||||
brain_memory = CONFIG.BRAIN_MEMORY
|
||||
self.memory = BrainMemory(**brain_memory) if brain_memory else BrainMemory()
|
||||
skill_path = Path(options.get(SKILL_PATH)) if options.get(SKILL_PATH) else None
|
||||
skill_path = Path(CONFIG.SKILL_PATH) if CONFIG.SKILL_PATH else None
|
||||
self.skills = SkillLoader(skill_yaml_file_name=skill_path)
|
||||
|
||||
async def think(self) -> bool:
|
||||
|
|
@ -60,7 +60,7 @@ class Assistant(Role):
|
|||
return await self._plan(rsp, last_talk=last_talk)
|
||||
|
||||
async def act(self) -> ActionOutput:
|
||||
result = await self._rc.todo.run(**self._options)
|
||||
result = await self._rc.todo.run(**CONFIG.options)
|
||||
if not result:
|
||||
return None
|
||||
if isinstance(result, str):
|
||||
|
|
@ -87,7 +87,7 @@ class Assistant(Role):
|
|||
return await handler(text, **kwargs)
|
||||
|
||||
async def talk_handler(self, text, **kwargs) -> bool:
|
||||
action = TalkAction(options=self.options, talk=text, knowledge=self.memory.get_knowledge(), llm=self._llm,
|
||||
action = TalkAction(talk=text, knowledge=self.memory.get_knowledge(), llm=self._llm,
|
||||
**kwargs)
|
||||
self.add_to_do(action)
|
||||
return True
|
||||
|
|
@ -98,12 +98,11 @@ class Assistant(Role):
|
|||
if not skill:
|
||||
logger.info(f"skill not found: {text}")
|
||||
return await self.talk_handler(text=last_talk, **kwargs)
|
||||
action = ArgumentsParingAction(options=self.options, skill=skill, llm=self._llm, **kwargs)
|
||||
action = ArgumentsParingAction(skill=skill, llm=self._llm, **kwargs)
|
||||
await action.run(**kwargs)
|
||||
if action.args is None:
|
||||
return await self.talk_handler(text=last_talk, **kwargs)
|
||||
action = SkillAction(options=self.options, skill=skill, args=action.args, llm=self._llm, name=skill.name,
|
||||
desc=skill.description)
|
||||
action = SkillAction(skill=skill, args=action.args, llm=self._llm, name=skill.name, desc=skill.description)
|
||||
self.add_to_do(action)
|
||||
return True
|
||||
|
||||
|
|
@ -115,11 +114,11 @@ class Assistant(Role):
|
|||
if history_text == "":
|
||||
return last_talk
|
||||
history_summary = await self._llm.get_context_title(history_text, max_words=20)
|
||||
if last_talk and await self._llm.is_related(last_talk, history_summary): # 合并相关内容
|
||||
if last_talk and await self._llm.is_related(last_talk, history_summary): # Merge relevant content.
|
||||
last_talk = await self._llm.rewrite(sentence=last_talk, context=history_text)
|
||||
return last_talk
|
||||
|
||||
self.memory.move_to_solution() # 问题解决后及时清空内存
|
||||
self.memory.move_to_solution() # Promptly clear memory after the issue is resolved.
|
||||
return last_talk
|
||||
|
||||
@staticmethod
|
||||
|
|
@ -138,10 +137,9 @@ class Assistant(Role):
|
|||
|
||||
|
||||
async def main():
|
||||
options = Config().runtime_options
|
||||
cost_manager = CostManager(**options)
|
||||
cost_manager = CostManager()
|
||||
topic = "what's apple"
|
||||
role = Assistant(options=options, cost_manager=cost_manager, language="Chinese")
|
||||
role = Assistant(cost_manager=cost_manager, language="Chinese")
|
||||
await role.talk(topic)
|
||||
while True:
|
||||
has_action = await role.think()
|
||||
|
|
@ -156,4 +154,5 @@ async def main():
|
|||
|
||||
|
||||
if __name__ == '__main__':
|
||||
CONFIG.language = "Chinese"
|
||||
asyncio.run(main())
|
||||
|
|
|
|||
|
|
@ -13,7 +13,6 @@ import base64
|
|||
import sys
|
||||
|
||||
sys.path.append(str(Path(__file__).resolve().parent.parent.parent)) # fix-bug: No module named 'metagpt'
|
||||
from metagpt.utils.common import initialize_environment
|
||||
from metagpt.logs import logger
|
||||
from aiofile import async_open
|
||||
from azure.cognitiveservices.speech import AudioConfig, SpeechConfig, SpeechSynthesizer
|
||||
|
|
@ -109,8 +108,6 @@ async def oas3_azsure_tts(text, lang="", voice="", style="", role="", subscripti
|
|||
|
||||
|
||||
if __name__ == "__main__":
|
||||
initialize_environment()
|
||||
|
||||
loop = asyncio.new_event_loop()
|
||||
v = loop.create_task(oas3_azsure_tts("测试,test"))
|
||||
loop.run_until_complete(v)
|
||||
|
|
|
|||
|
|
@ -13,13 +13,10 @@ import sys
|
|||
import connexion
|
||||
|
||||
sys.path.append(str(Path(__file__).resolve().parent.parent.parent)) # fix-bug: No module named 'metagpt'
|
||||
from metagpt.utils.common import initialize_environment
|
||||
|
||||
|
||||
def oas_http_svc():
|
||||
"""Start the OAS 3.0 OpenAPI HTTP service"""
|
||||
initialize_environment()
|
||||
|
||||
app = connexion.AioHttpApp(__name__, specification_dir='../../.well-known/')
|
||||
app.add_api("metagpt_oas3_api.yaml")
|
||||
app.add_api("openapi.yaml")
|
||||
|
|
|
|||
|
|
@ -17,7 +17,6 @@ import requests
|
|||
from pydantic import BaseModel
|
||||
|
||||
sys.path.append(str(Path(__file__).resolve().parent.parent.parent)) # fix-bug: No module named 'metagpt'
|
||||
from metagpt.utils.common import initialize_environment
|
||||
from metagpt.logs import logger
|
||||
|
||||
|
||||
|
|
@ -104,8 +103,6 @@ async def oas3_metagpt_text_to_image(text, size_type: str = "512x512", model_url
|
|||
|
||||
|
||||
if __name__ == "__main__":
|
||||
initialize_environment()
|
||||
|
||||
v = oas3_metagpt_text_to_image("Panda emoji")
|
||||
data = base64.b64decode(v)
|
||||
with open("tmp.png", mode="wb") as writer:
|
||||
|
|
|
|||
|
|
@ -17,7 +17,6 @@ import requests
|
|||
from pydantic import BaseModel
|
||||
|
||||
sys.path.append(str(Path(__file__).resolve().parent.parent.parent)) # fix-bug: No module named 'metagpt'
|
||||
from metagpt.utils.common import initialize_environment
|
||||
from metagpt.logs import logger
|
||||
|
||||
|
||||
|
|
@ -96,7 +95,5 @@ async def oas3_openai_text_to_image(text, size_type: str = "1024x1024", openai_a
|
|||
|
||||
|
||||
if __name__ == "__main__":
|
||||
initialize_environment()
|
||||
|
||||
v = oas3_openai_text_to_image("Panda emoji")
|
||||
print(v)
|
||||
|
|
|
|||
|
|
@ -14,12 +14,9 @@ from pathlib import Path
|
|||
sys.path.append(str(Path(__file__).resolve().parent.parent.parent.parent)) # fix-bug: No module named 'metagpt'
|
||||
from metagpt.const import WORKSPACE_ROOT
|
||||
from metagpt.tools.azure_tts import AzureTTS
|
||||
from metagpt.utils.common import initialize_environment
|
||||
|
||||
|
||||
def test_azure_tts():
|
||||
initialize_environment()
|
||||
|
||||
azure_tts = AzureTTS(subscription_key="", region="")
|
||||
text = """
|
||||
女儿看见父亲走了进来,问道:
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue