mirror of
https://github.com/FoundationAgents/MetaGPT.git
synced 2026-04-26 09:16:21 +02:00
feat: +unit test
This commit is contained in:
parent
8844fa74f7
commit
c61a3d2a99
10 changed files with 177 additions and 86 deletions
|
|
@ -58,7 +58,29 @@ class TestSkillAction:
|
|||
action = SkillAction(skill=self.skill, args=parser_action.args)
|
||||
rsp = await action.run()
|
||||
assert rsp
|
||||
assert "image/png;base64," in rsp.content
|
||||
assert "image/png;base64," in rsp.content or "http" in rsp.content
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("skill_name", "txt", "want"),
|
||||
[
|
||||
("skill1", 'skill1(a="1", b="2")', {"a": "1", "b": "2"}),
|
||||
("skill1", '(a="1", b="2")', None),
|
||||
("skill1", 'skill1(a="1", b="2"', None),
|
||||
],
|
||||
)
|
||||
def test_parse_arguments(self, skill_name, txt, want):
|
||||
args = ArgumentsParingAction.parse_arguments(skill_name, txt)
|
||||
assert args == want
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_find_and_call_function_error(self):
|
||||
with pytest.raises(ValueError):
|
||||
await SkillAction.find_and_call_function("dummy_call", {"a": 1})
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_skill_action_error(self):
|
||||
action = SkillAction(skill=self.skill, args={})
|
||||
await action.run()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
|
|
|||
|
|
@ -6,12 +6,24 @@
|
|||
@File : test_write_code.py
|
||||
@Modifiled By: mashenquan, 2023-12-6. According to RFC 135
|
||||
"""
|
||||
|
||||
from pathlib import Path
|
||||
|
||||
import pytest
|
||||
|
||||
from metagpt.actions.write_code import WriteCode
|
||||
from metagpt.config import CONFIG
|
||||
from metagpt.const import (
|
||||
CODE_SUMMARIES_FILE_REPO,
|
||||
SYSTEM_DESIGN_FILE_REPO,
|
||||
TASK_FILE_REPO,
|
||||
TEST_OUTPUTS_FILE_REPO,
|
||||
)
|
||||
from metagpt.logs import logger
|
||||
from metagpt.provider.openai_api import OpenAILLM as LLM
|
||||
from metagpt.schema import CodingContext, Document
|
||||
from metagpt.utils.common import aread
|
||||
from metagpt.utils.file_repository import FileRepository
|
||||
from tests.metagpt.actions.mock_markdown import TASKS_2, WRITE_CODE_PROMPT_SAMPLE
|
||||
|
||||
|
||||
|
|
@ -37,3 +49,47 @@ async def test_write_code_directly():
|
|||
llm = LLM()
|
||||
rsp = await llm.aask(prompt)
|
||||
logger.info(rsp)
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_write_code_deps():
|
||||
# Prerequisites
|
||||
CONFIG.src_workspace = CONFIG.git_repo.workdir / "snake1/snake1"
|
||||
demo_path = Path(__file__).parent / "../../data/demo_project"
|
||||
await FileRepository.save_file(
|
||||
filename="test_game.py.json",
|
||||
content=await aread(str(demo_path / "test_game.py.json")),
|
||||
relative_path=TEST_OUTPUTS_FILE_REPO,
|
||||
)
|
||||
await FileRepository.save_file(
|
||||
filename="20231221155954.json",
|
||||
content=await aread(str(demo_path / "code_summaries.json")),
|
||||
relative_path=CODE_SUMMARIES_FILE_REPO,
|
||||
)
|
||||
await FileRepository.save_file(
|
||||
filename="20231221155954.json",
|
||||
content=await aread(str(demo_path / "system_design.json")),
|
||||
relative_path=SYSTEM_DESIGN_FILE_REPO,
|
||||
)
|
||||
await FileRepository.save_file(
|
||||
filename="20231221155954.json", content=await aread(str(demo_path / "tasks.json")), relative_path=TASK_FILE_REPO
|
||||
)
|
||||
await FileRepository.save_file(
|
||||
filename="main.py", content='if __name__ == "__main__":\nmain()', relative_path=CONFIG.src_workspace
|
||||
)
|
||||
context = CodingContext(
|
||||
filename="game.py",
|
||||
design_doc=await FileRepository.get_file(filename="20231221155954.json", relative_path=SYSTEM_DESIGN_FILE_REPO),
|
||||
task_doc=await FileRepository.get_file(filename="20231221155954.json", relative_path=TASK_FILE_REPO),
|
||||
code_doc=Document(filename="game.py", content="", root_path="snake1"),
|
||||
)
|
||||
coding_doc = Document(root_path="snake1", filename="game.py", content=context.json())
|
||||
|
||||
action = WriteCode(context=coding_doc)
|
||||
rsp = await action.run()
|
||||
assert rsp
|
||||
assert rsp.code_doc.content
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
pytest.main([__file__, "-s"])
|
||||
|
|
|
|||
|
|
@ -6,40 +6,33 @@
|
|||
@File : test_text_to_speech.py
|
||||
@Desc : Unit tests.
|
||||
"""
|
||||
import asyncio
|
||||
import base64
|
||||
|
||||
from pydantic import BaseModel
|
||||
import pytest
|
||||
|
||||
from metagpt.config import CONFIG
|
||||
from metagpt.learn.text_to_speech import text_to_speech
|
||||
|
||||
|
||||
async def mock_text_to_speech():
|
||||
class Input(BaseModel):
|
||||
input: str
|
||||
@pytest.mark.asyncio
|
||||
async def test_text_to_speech():
|
||||
# Prerequisites
|
||||
assert CONFIG.IFLYTEK_APP_ID
|
||||
assert CONFIG.IFLYTEK_API_KEY
|
||||
assert CONFIG.IFLYTEK_API_SECRET
|
||||
assert CONFIG.AZURE_TTS_SUBSCRIPTION_KEY and CONFIG.AZURE_TTS_SUBSCRIPTION_KEY != "YOUR_API_KEY"
|
||||
assert CONFIG.AZURE_TTS_REGION
|
||||
|
||||
inputs = [{"input": "Panda emoji"}]
|
||||
# test azure
|
||||
data = await text_to_speech("panda emoji")
|
||||
assert "base64" in data or "http" in data
|
||||
|
||||
for i in inputs:
|
||||
seed = Input(**i)
|
||||
base64_data = await text_to_speech(seed.input)
|
||||
assert base64_data != ""
|
||||
print(f"{seed.input} -> {base64_data}")
|
||||
flags = ";base64,"
|
||||
assert flags in base64_data
|
||||
ix = base64_data.find(flags) + len(flags)
|
||||
declaration = base64_data[0:ix]
|
||||
assert declaration
|
||||
data = base64_data[ix:]
|
||||
assert data
|
||||
assert base64.b64decode(data, validate=True)
|
||||
|
||||
|
||||
def test_suite():
|
||||
loop = asyncio.get_event_loop()
|
||||
task = loop.create_task(mock_text_to_speech())
|
||||
loop.run_until_complete(task)
|
||||
# test iflytek
|
||||
key = CONFIG.AZURE_TTS_SUBSCRIPTION_KEY
|
||||
CONFIG.AZURE_TTS_SUBSCRIPTION_KEY = ""
|
||||
data = await text_to_speech("panda emoji")
|
||||
assert "base64" in data or "http" in data
|
||||
CONFIG.AZURE_TTS_SUBSCRIPTION_KEY = key
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
test_suite()
|
||||
pytest.main([__file__, "-s"])
|
||||
|
|
|
|||
|
|
@ -5,47 +5,63 @@
|
|||
@Author : mashenquan
|
||||
@File : test_brain_memory.py
|
||||
"""
|
||||
# import json
|
||||
# from typing import List
|
||||
#
|
||||
# import pydantic
|
||||
#
|
||||
# from metagpt.memory.brain_memory import BrainMemory
|
||||
# from metagpt.schema import Message
|
||||
#
|
||||
#
|
||||
# def test_json():
|
||||
# class Input(pydantic.BaseModel):
|
||||
# history: List[str]
|
||||
# solution: List[str]
|
||||
# knowledge: List[str]
|
||||
# stack: List[str]
|
||||
#
|
||||
# inputs = [{"history": ["a", "b"], "solution": ["c"], "knowledge": ["d", "e"], "stack": ["f"]}]
|
||||
#
|
||||
# for i in inputs:
|
||||
# v = Input(**i)
|
||||
# bm = BrainMemory()
|
||||
# for h in v.history:
|
||||
# msg = Message(content=h)
|
||||
# bm.history.append(msg.dict())
|
||||
# for h in v.solution:
|
||||
# msg = Message(content=h)
|
||||
# bm.solution.append(msg.dict())
|
||||
# for h in v.knowledge:
|
||||
# msg = Message(content=h)
|
||||
# bm.knowledge.append(msg.dict())
|
||||
# for h in v.stack:
|
||||
# msg = Message(content=h)
|
||||
# bm.stack.append(msg.dict())
|
||||
# s = bm.json()
|
||||
# m = json.loads(s)
|
||||
# bm = BrainMemory(**m)
|
||||
# assert bm
|
||||
# for v in bm.history:
|
||||
# msg = Message(**v)
|
||||
# assert msg
|
||||
#
|
||||
#
|
||||
# if __name__ == "__main__":
|
||||
# test_json()
|
||||
import pytest
|
||||
|
||||
from metagpt.config import LLMProviderEnum
|
||||
from metagpt.llm import LLM
|
||||
from metagpt.memory.brain_memory import BrainMemory
|
||||
from metagpt.schema import Message
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_memory():
|
||||
memory = BrainMemory()
|
||||
memory.add_talk(Message(content="talk"))
|
||||
assert memory.history[0].role == "user"
|
||||
memory.add_answer(Message(content="answer"))
|
||||
assert memory.history[1].role == "assistant"
|
||||
redis_key = BrainMemory.to_redis_key("none", "user_id", "chat_id")
|
||||
await memory.dumps(redis_key=redis_key)
|
||||
assert memory.exists("talk")
|
||||
assert 1 == memory.to_int("1", 0)
|
||||
memory.last_talk = "AAA"
|
||||
assert memory.pop_last_talk() == "AAA"
|
||||
assert memory.last_talk is None
|
||||
assert memory.is_history_available
|
||||
assert memory.history_text
|
||||
|
||||
memory = await BrainMemory.loads(redis_key=redis_key)
|
||||
assert memory
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("input", "tag", "val"),
|
||||
[("[TALK]:Hello", "TALK", "Hello"), ("Hello", None, "Hello"), ("[TALK]Hello", None, "[TALK]Hello")],
|
||||
)
|
||||
def test_extract_info(input, tag, val):
|
||||
t, v = BrainMemory.extract_info(input)
|
||||
assert tag == t
|
||||
assert val == v
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
@pytest.mark.parametrize("llm", [LLM(provider=LLMProviderEnum.OPENAI), LLM(provider=LLMProviderEnum.METAGPT)])
|
||||
async def test_memory_llm(llm):
|
||||
memory = BrainMemory()
|
||||
for i in range(500):
|
||||
memory.add_talk(Message(content="Lily is a girl.\n"))
|
||||
|
||||
res = await memory.is_related("apple", "moon", llm)
|
||||
assert not res
|
||||
|
||||
res = await memory.rewrite(sentence="apple Lily eating", context="", llm=llm)
|
||||
assert "Lily" in res
|
||||
|
||||
res = await memory.get_title(llm=llm)
|
||||
assert res
|
||||
assert "Lily" in res
|
||||
assert memory.history or memory.historical_summary
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
pytest.main([__file__, "-s"])
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue