fixbug: 修改Teacher role相关结构

This commit is contained in:
莘权 马 2023-12-25 16:14:50 +08:00
parent 29bbe5752d
commit e162fd36fc
4 changed files with 112 additions and 85 deletions

View file

@ -5,15 +5,19 @@
@Author : mashenquan
@File : test_teacher.py
"""
import os
from typing import Dict, Optional
import pytest
from pydantic import BaseModel
from metagpt.config import CONFIG, Config
from metagpt.roles.teacher import Teacher
from metagpt.schema import Message
def test_init():
@pytest.mark.asyncio
async def test_init():
class Inputs(BaseModel):
name: str
profile: str
@ -28,19 +32,6 @@ def test_init():
expect_desc: str
inputs = [
{
"name": "Lily{language}",
"expect_name": "LilyCN",
"profile": "X {teaching_language}",
"expect_profile": "X EN",
"goal": "Do {something_big}, {language}",
"expect_goal": "Do sleep, CN",
"constraints": "Do in {key1}, {language}",
"expect_constraints": "Do in HaHa, CN",
"kwargs": {"language": "CN", "key1": "HaHa", "something_big": "sleep", "teaching_language": "EN"},
"desc": "aaa{language}",
"expect_desc": "aaaCN",
},
{
"name": "Lily{language}",
"expect_name": "Lily{language}",
@ -54,17 +45,37 @@ def test_init():
"desc": "aaa{language}",
"expect_desc": "aaa{language}",
},
{
"name": "Lily{language}",
"expect_name": "LilyCN",
"profile": "X {teaching_language}",
"expect_profile": "X EN",
"goal": "Do {something_big}, {language}",
"expect_goal": "Do sleep, CN",
"constraints": "Do in {key1}, {language}",
"expect_constraints": "Do in HaHa, CN",
"kwargs": {"language": "CN", "key1": "HaHa", "something_big": "sleep", "teaching_language": "EN"},
"desc": "aaa{language}",
"expect_desc": "aaaCN",
},
]
env = os.environ.copy()
for i in inputs:
seed = Inputs(**i)
os.environ.clear()
os.environ.update(env)
CONFIG = Config()
CONFIG.set_context(seed.kwargs)
print(CONFIG.options)
assert bool("language" in seed.kwargs) == bool("language" in CONFIG.options)
teacher = Teacher(
name=seed.name,
profile=seed.profile,
goal=seed.goal,
constraints=seed.constraints,
desc=seed.desc,
**seed.kwargs
)
assert teacher.name == seed.expect_name
assert teacher.desc == seed.expect_desc
@ -74,7 +85,8 @@ def test_init():
assert teacher.course_title == "teaching_plan"
def test_new_file_name():
@pytest.mark.asyncio
async def test_new_file_name():
class Inputs(BaseModel):
lesson_title: str
ext: str
@ -90,6 +102,13 @@ def test_new_file_name():
assert result == seed.expect
@pytest.mark.asyncio
async def test_run():
CONFIG.set_context({"language": "Chinese", "teaching_language": "English"})
lesson = "Lesson 1: How to draw a tree. First step, buy a book."
teacher = Teacher()
await teacher.run(Message(content=lesson))
if __name__ == "__main__":
test_init()
test_new_file_name()
pytest.main([__file__, "-s"])

View file

@ -5,20 +5,30 @@
@Author : Stitch-z
@File : test_tutorial_assistant.py
"""
import aiofiles
import shutil
import pytest
from metagpt.const import TUTORIAL_PATH
from metagpt.roles.tutorial_assistant import TutorialAssistant
@pytest.mark.asyncio
@pytest.mark.parametrize(("language", "topic"), [("Chinese", "Write a tutorial about Python")])
async def test_tutorial_assistant(language: str, topic: str):
shutil.rmtree(path=TUTORIAL_PATH, ignore_errors=True)
topic = "Write a tutorial about MySQL"
role = TutorialAssistant(language=language)
msg = await role.run(topic)
filename = msg.content
title = filename.split("/")[-1].split(".")[0]
async with aiofiles.open(filename, mode="r") as reader:
content = await reader.read()
assert content.startswith(f"# {title}")
assert "MySQL" in msg.content
assert TUTORIAL_PATH.exists()
# filename = msg.content
# title = filename.split("/")[-1].split(".")[0]
# async with aiofiles.open(filename, mode="r") as reader:
# content = await reader.read()
# assert content.startswith(f"# {title}")
if __name__ == "__main__":
pytest.main([__file__, "-s"])