add context and config2

This commit is contained in:
geekan 2024-01-04 22:02:47 +08:00
parent e5d11a046c
commit 10436172ca
25 changed files with 72 additions and 113 deletions

View file

@ -104,9 +104,9 @@ class Context:
@pytest.fixture(scope="package")
def llm_api():
logger.info("Setting up the test")
_context = Context()
g_context = Context()
yield _context.llm_api
yield g_context.llm_api
logger.info("Tearing down the test")

View file

@ -9,8 +9,8 @@
import pytest
from metagpt.actions.prepare_documents import PrepareDocuments
from metagpt.config import CONFIG
from metagpt.const import DOCS_FILE_REPO, REQUIREMENT_FILENAME
from metagpt.context import context
from metagpt.schema import Message
from metagpt.utils.file_repository import FileRepository
@ -19,12 +19,12 @@ from metagpt.utils.file_repository import FileRepository
async def test_prepare_documents():
msg = Message(content="New user requirements balabala...")
if CONFIG.git_repo:
CONFIG.git_repo.delete_repository()
CONFIG.git_repo = None
if context.git_repo:
context.git_repo.delete_repository()
context.git_repo = None
await PrepareDocuments().run(with_messages=[msg])
assert CONFIG.git_repo
await PrepareDocuments(g_context=context).run(with_messages=[msg])
assert context.git_repo
doc = await FileRepository.get_file(filename=REQUIREMENT_FILENAME, relative_path=DOCS_FILE_REPO)
assert doc
assert doc.content == msg.content

View file

@ -1,7 +0,0 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
@Time : 2023/5/11 14:44
@Author : alexanderwu
@File : test_action.py
"""

View file

@ -5,7 +5,7 @@
@Author : alexanderwu
@File : test_document.py
"""
from metagpt.config import CONFIG
from metagpt.config2 import config
from metagpt.document import Repo
from metagpt.logs import logger
@ -28,6 +28,6 @@ def load_existing_repo(path):
def test_repo_set_load():
repo_path = CONFIG.path / "test_repo"
repo_path = config.workspace.path / "test_repo"
set_existing_repo(repo_path)
load_existing_repo(repo_path)

View file

@ -13,7 +13,7 @@ from pathlib import Path
import pytest
from metagpt.actions import UserRequirement
from metagpt.config import CONFIG
from metagpt.context import context
from metagpt.environment import Environment
from metagpt.logs import logger
from metagpt.roles import Architect, ProductManager, Role
@ -46,9 +46,9 @@ def test_get_roles(env: Environment):
@pytest.mark.asyncio
async def test_publish_and_process_message(env: Environment):
if CONFIG.git_repo:
CONFIG.git_repo.delete_repository()
CONFIG.git_repo = None
if context.git_repo:
context.git_repo.delete_repository()
context.git_repo = None
product_manager = ProductManager(name="Alice", profile="Product Manager", goal="做AI Native产品", constraints="资源有限")
architect = Architect(

View file

@ -1,45 +0,0 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
@Time : 2023/4/29 19:47
@Author : alexanderwu
@File : test_gpt.py
"""
import openai
import pytest
from metagpt.config import CONFIG
from metagpt.logs import logger
@pytest.mark.usefixtures("llm_api")
class TestGPT:
@pytest.mark.asyncio
async def test_llm_api_aask(self, llm_api):
answer = await llm_api.aask("hello chatgpt", stream=False)
logger.info(answer)
assert len(answer) > 0
answer = await llm_api.aask("hello chatgpt", stream=True)
logger.info(answer)
assert len(answer) > 0
@pytest.mark.asyncio
async def test_llm_api_aask_code(self, llm_api):
try:
answer = await llm_api.aask_code(["请扮演一个Google Python专家工程师如果理解回复明白", "写一个hello world"], timeout=60)
logger.info(answer)
assert len(answer) > 0
except openai.BadRequestError:
assert CONFIG.OPENAI_API_TYPE == "azure"
@pytest.mark.asyncio
async def test_llm_api_costs(self, llm_api):
await llm_api.aask("hello chatgpt", stream=False)
costs = llm_api.get_costs()
logger.info(costs)
assert costs.total_cost > 0
if __name__ == "__main__":
pytest.main([__file__, "-s"])

View file

@ -9,7 +9,7 @@
import pytest
from metagpt.provider.openai_api import OpenAILLM as LLM
from metagpt.llm import LLM
@pytest.fixture()
@ -23,6 +23,12 @@ async def test_llm_aask(llm):
assert len(rsp) > 0
@pytest.mark.asyncio
async def test_llm_aask_stream(llm):
rsp = await llm.aask("hello world", stream=True)
assert len(rsp) > 0
@pytest.mark.asyncio
async def test_llm_acompletion(llm):
hello_msg = [{"role": "user", "content": "hello"}]

View file

@ -1,7 +0,0 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
@Time : 2023/5/11 14:45
@Author : alexanderwu
@File : test_manager.py
"""