fix code parser etc.

This commit is contained in:
geekan 2023-12-29 00:45:17 +08:00
parent 4abc37ba3a
commit 933cd1f049
4 changed files with 19 additions and 21 deletions

View file

@ -95,4 +95,4 @@ class SearchEngine:
Returns:
The search results as a string or a list of dictionaries.
"""
return await self.run_func(query, max_results=max_results, as_string=as_string)
return await self.run_func(query, max_results, as_string)

View file

@ -16,6 +16,7 @@ from tests.metagpt.roles.mock import MockMessages
@pytest.mark.asyncio
async def test_architect():
# FIXME: make git as env? Or should we support
role = Architect()
role.put_message(MockMessages.req)
rsp = await role.run(MockMessages.prd)

View file

@ -7,6 +7,8 @@
"""
from __future__ import annotations
from typing import Callable
import pytest
from metagpt.config import CONFIG
@ -25,7 +27,7 @@ class MockSearchEnine:
@pytest.mark.asyncio
@pytest.mark.parametrize(
("search_engine_typpe", "run_func", "max_results", "as_string"),
("search_engine_type", "run_func", "max_results", "as_string"),
[
(SearchEngineType.SERPAPI_GOOGLE, None, 8, True),
(SearchEngineType.SERPAPI_GOOGLE, None, 4, False),
@ -39,23 +41,18 @@ class MockSearchEnine:
(SearchEngineType.CUSTOM_ENGINE, MockSearchEnine().run, 6, False),
],
)
async def test_search_engine(
search_engine_typpe,
run_func,
max_results,
as_string,
):
async def test_search_engine(search_engine_type, run_func: Callable, max_results: int, as_string: bool):
# Prerequisites
if search_engine_typpe is SearchEngineType.SERPAPI_GOOGLE:
if search_engine_type is SearchEngineType.SERPAPI_GOOGLE:
assert CONFIG.SERPAPI_API_KEY and CONFIG.SERPAPI_API_KEY != "YOUR_API_KEY"
elif search_engine_typpe is SearchEngineType.DIRECT_GOOGLE:
elif search_engine_type is SearchEngineType.DIRECT_GOOGLE:
assert CONFIG.GOOGLE_API_KEY and CONFIG.GOOGLE_API_KEY != "YOUR_API_KEY"
assert CONFIG.GOOGLE_CSE_ID and CONFIG.GOOGLE_CSE_ID != "YOUR_CSE_ID"
elif search_engine_typpe is SearchEngineType.SERPER_GOOGLE:
elif search_engine_type is SearchEngineType.SERPER_GOOGLE:
assert CONFIG.SERPER_API_KEY and CONFIG.SERPER_API_KEY != "YOUR_API_KEY"
search_engine = SearchEngine(search_engine_typpe, run_func)
rsp = await search_engine.run("metagpt", max_results=max_results, as_string=as_string)
search_engine = SearchEngine(search_engine_type, run_func)
rsp = await search_engine.run("metagpt", max_results, as_string)
logger.info(rsp)
if as_string:
assert isinstance(rsp, str)

View file

@ -111,27 +111,27 @@ class TestCodeParser:
def test_parse_blocks(self, parser, text):
result = parser.parse_blocks(text)
print(result)
assert result == {"title": "content", "title2": "content2"}
assert "game.py" in result["Task list"]
def test_parse_block(self, parser, text):
result = parser.parse_block("title", text)
result = parser.parse_block("Task list", text)
print(result)
assert result == "content"
assert "game.py" in result
def test_parse_code(self, parser, text):
result = parser.parse_code("title", text, "python")
result = parser.parse_code("Task list", text, "python")
print(result)
assert result == "print('hello world')"
assert "game.py" in result
def test_parse_str(self, parser, text):
result = parser.parse_str("title", text, "python")
result = parser.parse_str("Anything UNCLEAR", text, "python")
print(result)
assert result == "hello world"
assert "We need clarification on how the high score " in result
def test_parse_file_list(self, parser, text):
result = parser.parse_file_list("Task list", text)
print(result)
assert result == ["task1", "task2"]
assert "game.py" in result
if __name__ == "__main__":