From 69ef295b26f185f12c9e8bb05d79695425d01df2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8E=98=E6=9D=83=20=E9=A9=AC?= Date: Sun, 3 Sep 2023 12:11:37 +0800 Subject: [PATCH 1/5] fixbug: skill name --- metagpt/roles/assistant.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/metagpt/roles/assistant.py b/metagpt/roles/assistant.py index c707cb6f1..0bce4a3f9 100644 --- a/metagpt/roles/assistant.py +++ b/metagpt/roles/assistant.py @@ -57,7 +57,9 @@ class Assistant(Role): prompt = f"Refer to this sentence:\n {last_talk}\n" skills = self.skills.get_skill_list() for desc, name in skills.items(): - prompt += f"If want you to do {desc}, return `[SKILL]: {name}` brief and clear. For instance: [SKILL]: text_to_image\n" + prompt += ( + f"If want you to do {desc}, return `[SKILL]: {name}` brief and clear. For instance: [SKILL]: {name}\n" + ) prompt += "If the preceding text presents a complete question and solution, rewrite and return `[SOLUTION]: {problem}` brief and clear. For instance: [SOLUTION]: Solution for distributing watermelon\n" prompt += "If the preceding text presents an unresolved issue and its corresponding discussion, rewrite and return `[PROBLEM]: {problem}` brief and clear. For instance: [PROBLEM]: How to distribute watermelon?\n" prompt += "Otherwise, rewrite and return `[TALK]: {talk}` brief and clear. For instance: [TALK]: distribute watermelon" From 5079add5f829b05f193f91bb9dce121cf29e6517 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8E=98=E6=9D=83=20=E9=A9=AC?= Date: Sun, 3 Sep 2023 12:55:25 +0800 Subject: [PATCH 2/5] debug: +code --- metagpt/actions/skill_action.py | 28 ++++++++++++++++------------ 1 file changed, 16 insertions(+), 12 deletions(-) diff --git a/metagpt/actions/skill_action.py b/metagpt/actions/skill_action.py index 3ef0087fc..6bce2a634 100644 --- a/metagpt/actions/skill_action.py +++ b/metagpt/actions/skill_action.py @@ -7,8 +7,8 @@ @Desc : Call learned skill """ from __future__ import annotations + import ast -import importlib import traceback from metagpt.actions import Action, ActionOutput @@ -18,7 +18,7 @@ from metagpt.logs import logger class ArgumentsParingAction(Action): def __init__(self, last_talk: str, skill: Skill, context=None, llm=None, **kwargs): - super(ArgumentsParingAction, self).__init__(name='', context=context, llm=llm) + super(ArgumentsParingAction, self).__init__(name="", context=context, llm=llm) self.skill = skill self.ask = last_talk self.rsp = None @@ -56,10 +56,10 @@ class ArgumentsParingAction(Action): return None begin_ix = txt.find(prefix) end_ix = txt.rfind(")") - args_txt = txt[begin_ix + len(prefix): end_ix] + args_txt = txt[begin_ix + len(prefix) : end_ix] logger.info(args_txt) fake_expression = f"dict({args_txt})" - parsed_expression = ast.parse(fake_expression, mode='eval') + parsed_expression = ast.parse(fake_expression, mode="eval") args = {} for keyword in parsed_expression.body.keywords: key = keyword.arg @@ -70,7 +70,7 @@ class ArgumentsParingAction(Action): class SkillAction(Action): def __init__(self, skill: Skill, args: dict, context=None, llm=None, **kwargs): - super(SkillAction, self).__init__(name='', context=context, llm=llm) + super(SkillAction, self).__init__(name="", context=context, llm=llm) self._skill = skill self._args = args self.rsp = None @@ -86,17 +86,21 @@ class SkillAction(Action): @staticmethod async def find_and_call_function(function_name, args, **kwargs): + from metagpt.learn import text_to_speech + try: - module = importlib.import_module("metagpt.learn") - function = getattr(module, function_name) - # 调用函数并返回结果 - result = await function(**args, **kwargs) + result = await text_to_speech(**args, **kwargs) + # module = importlib.import_module("metagpt.learn") + # function = getattr(module, function_name) + # # 调用函数并返回结果 + # result = await function(**args, **kwargs) return result except (ModuleNotFoundError, AttributeError): logger.error(f"{function_name} not found") return None -if __name__ == '__main__': - ArgumentsParingAction.parse_arguments(skill_name="text_to_image", - txt='`text_to_image(text="Draw an apple", size_type="512x512")`') +if __name__ == "__main__": + ArgumentsParingAction.parse_arguments( + skill_name="text_to_image", txt='`text_to_image(text="Draw an apple", size_type="512x512")`' + ) From 04b348e92967d6a99ca0425c6aad1f3b34485e30 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8E=98=E6=9D=83=20=E9=A9=AC?= Date: Sun, 3 Sep 2023 13:31:52 +0800 Subject: [PATCH 3/5] feat: archive --- metagpt/actions/skill_action.py | 36 ++++++++++++++++++++++++--------- metagpt/learn/text_to_speech.py | 10 ++++----- 2 files changed, 31 insertions(+), 15 deletions(-) diff --git a/metagpt/actions/skill_action.py b/metagpt/actions/skill_action.py index 6bce2a634..660d785ff 100644 --- a/metagpt/actions/skill_action.py +++ b/metagpt/actions/skill_action.py @@ -9,10 +9,14 @@ from __future__ import annotations import ast +import asyncio +import importlib import traceback +from copy import deepcopy from metagpt.actions import Action, ActionOutput -from metagpt.learn.skill_loader import Skill +from metagpt.config import CONFIG +from metagpt.learn.skill_loader import Returns, Skill from metagpt.logs import logger @@ -77,8 +81,13 @@ class SkillAction(Action): async def run(self, *args, **kwargs) -> str | ActionOutput | None: """Run action""" + options = deepcopy(kwargs) + if self._args: + for k in self._args.keys(): + if k in options: + options.pop(k) try: - self.rsp = await self.find_and_call_function(self._skill.name, args=self._args, **kwargs) + self.rsp = await self.find_and_call_function(self._skill.name, args=self._args, **options) except Exception as e: logger.exception(f"{e}, traceback:{traceback.format_exc()}") self.rsp = f"Error: {e}" @@ -86,14 +95,11 @@ class SkillAction(Action): @staticmethod async def find_and_call_function(function_name, args, **kwargs): - from metagpt.learn import text_to_speech - try: - result = await text_to_speech(**args, **kwargs) - # module = importlib.import_module("metagpt.learn") - # function = getattr(module, function_name) - # # 调用函数并返回结果 - # result = await function(**args, **kwargs) + module = importlib.import_module("metagpt.learn") + function = getattr(module, function_name) + # 调用函数并返回结果 + result = await function(**args, **kwargs) return result except (ModuleNotFoundError, AttributeError): logger.error(f"{function_name} not found") @@ -104,3 +110,15 @@ if __name__ == "__main__": ArgumentsParingAction.parse_arguments( skill_name="text_to_image", txt='`text_to_image(text="Draw an apple", size_type="512x512")`' ) + CONFIG.set_context({}) + args = {"text": "hello world", "role": "Girl"} + action = SkillAction( + skill=Skill( + name="text_to_speech", description="", id="", arguments={}, examples=[], returns=Returns(type="string") + ), + args=args, + ) + loop = asyncio.new_event_loop() + t = loop.create_task(action.run()) + r = loop.run_until_complete(t) + print(r) diff --git a/metagpt/learn/text_to_speech.py b/metagpt/learn/text_to_speech.py index 819da2364..eaceb3313 100644 --- a/metagpt/learn/text_to_speech.py +++ b/metagpt/learn/text_to_speech.py @@ -9,9 +9,7 @@ import openai from metagpt.config import CONFIG -from metagpt.const import BASE64_FORMAT from metagpt.tools.azure_tts import oas3_azsure_tts -from metagpt.utils.s3 import S3 async def text_to_speech( @@ -40,10 +38,10 @@ async def text_to_speech( audio_declaration = "data:audio/wav;base64," if (CONFIG.AZURE_TTS_SUBSCRIPTION_KEY and CONFIG.AZURE_TTS_REGION) or (subscription_key and region): base64_data = await oas3_azsure_tts(text, lang, voice, style, role, subscription_key, region) - s3 = S3() - url = await s3.cache(data=base64_data, file_ext=".wav", format=BASE64_FORMAT) - if url: - return f"[{text}]({url})" + # s3 = S3() + # url = await s3.cache(data=base64_data, file_ext=".wav", format=BASE64_FORMAT) + # if url: + # return f"[{text}]({url})" return audio_declaration + base64_data if base64_data else base64_data raise openai.error.InvalidRequestError("缺少必要的参数") From 0dddab18b44a053ef2d2206bfbf669750de0df3a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8E=98=E6=9D=83=20=E9=A9=AC?= Date: Sun, 3 Sep 2023 13:40:05 +0800 Subject: [PATCH 4/5] fixbug: no param --- metagpt/learn/text_to_speech.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/metagpt/learn/text_to_speech.py b/metagpt/learn/text_to_speech.py index eaceb3313..691aa7f6a 100644 --- a/metagpt/learn/text_to_speech.py +++ b/metagpt/learn/text_to_speech.py @@ -44,4 +44,4 @@ async def text_to_speech( # return f"[{text}]({url})" return audio_declaration + base64_data if base64_data else base64_data - raise openai.error.InvalidRequestError("缺少必要的参数") + raise openai.error.InvalidRequestError(message="AZURE_TTS_SUBSCRIPTION_KEY and AZURE_TTS_REGION error", param={}) From ef98ad4043b377037dd38d2aec1354bb7ea7be03 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8E=98=E6=9D=83=20=E9=A9=AC?= Date: Sun, 3 Sep 2023 13:46:23 +0800 Subject: [PATCH 5/5] fixbug: no param --- metagpt/actions/skill_action.py | 16 +--------------- metagpt/learn/text_to_speech.py | 10 ++++++---- 2 files changed, 7 insertions(+), 19 deletions(-) diff --git a/metagpt/actions/skill_action.py b/metagpt/actions/skill_action.py index 660d785ff..758591fdd 100644 --- a/metagpt/actions/skill_action.py +++ b/metagpt/actions/skill_action.py @@ -9,14 +9,12 @@ from __future__ import annotations import ast -import asyncio import importlib import traceback from copy import deepcopy from metagpt.actions import Action, ActionOutput -from metagpt.config import CONFIG -from metagpt.learn.skill_loader import Returns, Skill +from metagpt.learn.skill_loader import Skill from metagpt.logs import logger @@ -110,15 +108,3 @@ if __name__ == "__main__": ArgumentsParingAction.parse_arguments( skill_name="text_to_image", txt='`text_to_image(text="Draw an apple", size_type="512x512")`' ) - CONFIG.set_context({}) - args = {"text": "hello world", "role": "Girl"} - action = SkillAction( - skill=Skill( - name="text_to_speech", description="", id="", arguments={}, examples=[], returns=Returns(type="string") - ), - args=args, - ) - loop = asyncio.new_event_loop() - t = loop.create_task(action.run()) - r = loop.run_until_complete(t) - print(r) diff --git a/metagpt/learn/text_to_speech.py b/metagpt/learn/text_to_speech.py index 691aa7f6a..81bc8512b 100644 --- a/metagpt/learn/text_to_speech.py +++ b/metagpt/learn/text_to_speech.py @@ -9,7 +9,9 @@ import openai from metagpt.config import CONFIG +from metagpt.const import BASE64_FORMAT from metagpt.tools.azure_tts import oas3_azsure_tts +from metagpt.utils.s3 import S3 async def text_to_speech( @@ -38,10 +40,10 @@ async def text_to_speech( audio_declaration = "data:audio/wav;base64," if (CONFIG.AZURE_TTS_SUBSCRIPTION_KEY and CONFIG.AZURE_TTS_REGION) or (subscription_key and region): base64_data = await oas3_azsure_tts(text, lang, voice, style, role, subscription_key, region) - # s3 = S3() - # url = await s3.cache(data=base64_data, file_ext=".wav", format=BASE64_FORMAT) - # if url: - # return f"[{text}]({url})" + s3 = S3() + url = await s3.cache(data=base64_data, file_ext=".wav", format=BASE64_FORMAT) + if url: + return f"[{text}]({url})" return audio_declaration + base64_data if base64_data else base64_data raise openai.error.InvalidRequestError(message="AZURE_TTS_SUBSCRIPTION_KEY and AZURE_TTS_REGION error", param={})