debug: +code

This commit is contained in:
莘权 马 2023-09-03 12:55:25 +08:00
parent 69ef295b26
commit 5079add5f8

View file

@ -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")`'
)