From 664710e6e81b41302793566dd0be37428f78f59d Mon Sep 17 00:00:00 2001 From: femto Date: Sat, 16 Sep 2023 20:23:51 +0800 Subject: [PATCH] add test --- metagpt/planner/__init__.py | 7 +++++ metagpt/planner/test_action_planner.py | 38 +++++++++++++++++++++++ metagpt/planner/test_basic_planner.py | 42 ++++++++++++++++++++++++++ 3 files changed, 87 insertions(+) create mode 100644 metagpt/planner/__init__.py create mode 100644 metagpt/planner/test_action_planner.py create mode 100644 metagpt/planner/test_basic_planner.py diff --git a/metagpt/planner/__init__.py b/metagpt/planner/__init__.py new file mode 100644 index 000000000..85e01b36b --- /dev/null +++ b/metagpt/planner/__init__.py @@ -0,0 +1,7 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +""" +@Time : 2023/9/16 20:03 +@Author : femto Zheng +@File : __init__.py +""" diff --git a/metagpt/planner/test_action_planner.py b/metagpt/planner/test_action_planner.py new file mode 100644 index 000000000..e5176d4d3 --- /dev/null +++ b/metagpt/planner/test_action_planner.py @@ -0,0 +1,38 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +""" +@Time : 2023/9/16 20:03 +@Author : femto Zheng +@File : test_basic_planner.py +""" +import os + +import pytest +from semantic_kernel.core_skills import FileIOSkill, MathSkill, TextSkill, TimeSkill +from semantic_kernel.planning.action_planner.action_planner import ActionPlanner + +from metagpt.actions import BossRequirement +from metagpt.roles.sk_agent import SkAgent +from metagpt.schema import Message + +# Get the directory of the current file +current_file_directory = os.path.dirname(os.path.abspath(__file__)) +# Construct the skills_directory by joining the parent directory and "skillss" +skills_directory = os.path.join(current_file_directory, "..", "skills") +# Normalize the path to ensure it's in the correct format +skills_directory = os.path.normpath(skills_directory) + + +@pytest.mark.asyncio +async def test_action_planner(): + role = SkAgent(planner_cls=ActionPlanner) + # let's give the agent 4 skills + role.import_skill(MathSkill(), "math") + role.import_skill(FileIOSkill(), "fileIO") + role.import_skill(TimeSkill(), "time") + role.import_skill(TextSkill(), "text") + task = "What is the sum of 110 and 990?" + role.recv(Message(content=task, cause_by=BossRequirement)) + + await role._think() # it will choose mathskill.Add + assert "1100" == (await role._act()).content.result diff --git a/metagpt/planner/test_basic_planner.py b/metagpt/planner/test_basic_planner.py new file mode 100644 index 000000000..b5cb62b6d --- /dev/null +++ b/metagpt/planner/test_basic_planner.py @@ -0,0 +1,42 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +""" +@Time : 2023/9/16 20:03 +@Author : femto Zheng +@File : test_basic_planner.py +""" +import os + +import pytest +from semantic_kernel.core_skills import TextSkill + +from metagpt.actions import BossRequirement +from metagpt.roles.sk_agent import SkAgent +from metagpt.schema import Message + +# Get the directory of the current file +current_file_directory = os.path.dirname(os.path.abspath(__file__)) +# Construct the skills_directory by joining the parent directory and "skillss" +skills_directory = os.path.join(current_file_directory, "..", "skills") +# Normalize the path to ensure it's in the correct format +skills_directory = os.path.normpath(skills_directory) + + +@pytest.mark.asyncio +async def test_basic_planner(): + task = """ + Tomorrow is Valentine's day. I need to come up with a few date ideas. She speaks French so write it in French. + Convert the text to uppercase""" + role = SkAgent() + + # let's give the agent some skills + role.import_semantic_skill_from_directory(skills_directory, "SummarizeSkill") + role.import_semantic_skill_from_directory(skills_directory, "WriterSkill") + role.import_skill(TextSkill(), "TextSkill") + # using BasicPlanner + role.recv(Message(content=task, cause_by=BossRequirement)) + await role._think() + # assuming sk_agent will think he needs WriterSkill.Brainstorm and WriterSkill.Translate + assert "WriterSkill.Brainstorm" in role.plan.generated_plan.result + assert "WriterSkill.Translate" in role.plan.generated_plan.result + await role._act()