Merge branch 'dev' into code_intepreter

This commit is contained in:
yzlin 2024-02-01 16:27:20 +08:00
commit afb702c3f3
38 changed files with 537 additions and 366 deletions

27
tests/config2.yaml Normal file
View file

@ -0,0 +1,27 @@
llm:
base_url: "https://api.openai.com/v1"
api_key: "sk-xxx"
model: "gpt-3.5-turbo-1106"
search:
api_type: "serpapi"
api_key: "xxx"
s3:
access_key: "MOCK_S3_ACCESS_KEY"
secret_key: "MOCK_S3_SECRET_KEY"
endpoint: "http://mock:9000"
secure: false
bucket: "mock"
azure_tts_subscription_key: "xxx"
azure_tts_region: "eastus"
iflytek_app_id: "xxx"
iflytek_api_key: "xxx"
iflytek_api_secret: "xxx"
metagpt_tti_url: "http://mock.com"
repair_llm_output: true

View file

@ -29,9 +29,9 @@ async def test_rebuild(context):
@pytest.mark.parametrize(
("path", "direction", "diff", "want"),
[
("metagpt/startup.py", "=", ".", "metagpt/startup.py"),
("metagpt/startup.py", "+", "MetaGPT", "MetaGPT/metagpt/startup.py"),
("metagpt/startup.py", "-", "metagpt", "startup.py"),
("metagpt/software_company.py", "=", ".", "metagpt/software_company.py"),
("metagpt/software_company.py", "+", "MetaGPT", "MetaGPT/metagpt/software_company.py"),
("metagpt/software_company.py", "-", "metagpt", "software_company.py"),
],
)
def test_align_path(path, direction, diff, want):

View file

@ -23,9 +23,9 @@ class TestSkillAction:
"type": "string",
"description": "OpenAI API key, For more details, checkout: `https://platform.openai.com/account/api-keys`",
},
"METAGPT_TEXT_TO_IMAGE_MODEL_URL": {"type": "string", "description": "Model url."},
"metagpt_tti_url": {"type": "string", "description": "Model url."},
},
"required": {"oneOf": ["OPENAI_API_KEY", "METAGPT_TEXT_TO_IMAGE_MODEL_URL"]},
"required": {"oneOf": ["OPENAI_API_KEY", "metagpt_tti_url"]},
},
parameters={
"text": Parameter(type="string", description="The text used for image conversion."),

View file

@ -27,7 +27,7 @@ async def test_text_to_image(mocker):
mocker.patch.object(S3, "cache", return_value="http://mock/s3")
config = Config.default()
assert config.METAGPT_TEXT_TO_IMAGE_MODEL_URL
assert config.metagpt_tti_url
data = await text_to_image("Panda emoji", size_type="512x512", config=config)
assert "base64" in data or "http" in data
@ -52,7 +52,7 @@ async def test_openai_text_to_image(mocker):
mocker.patch.object(S3, "cache", return_value="http://mock.s3.com/0.png")
config = Config.default()
config.METAGPT_TEXT_TO_IMAGE_MODEL_URL = None
config.metagpt_tti_url = None
assert config.get_openai_llm()
data = await text_to_image("Panda emoji", size_type="512x512", config=config)

View file

@ -20,9 +20,9 @@ from metagpt.utils.s3 import S3
async def test_azure_text_to_speech(mocker):
# mock
config = Config.default()
config.IFLYTEK_API_KEY = None
config.IFLYTEK_API_SECRET = None
config.IFLYTEK_APP_ID = None
config.iflytek_api_key = None
config.iflytek_api_secret = None
config.iflytek_app_id = None
mock_result = mocker.Mock()
mock_result.audio_data = b"mock audio data"
mock_result.reason = ResultReason.SynthesizingAudioCompleted
@ -32,11 +32,11 @@ async def test_azure_text_to_speech(mocker):
mocker.patch.object(S3, "cache", return_value="http://mock.s3.com/1.wav")
# Prerequisites
assert not config.IFLYTEK_APP_ID
assert not config.IFLYTEK_API_KEY
assert not config.IFLYTEK_API_SECRET
assert config.AZURE_TTS_SUBSCRIPTION_KEY and config.AZURE_TTS_SUBSCRIPTION_KEY != "YOUR_API_KEY"
assert config.AZURE_TTS_REGION
assert not config.iflytek_app_id
assert not config.iflytek_api_key
assert not config.iflytek_api_secret
assert config.azure_tts_subscription_key and config.azure_tts_subscription_key != "YOUR_API_KEY"
assert config.azure_tts_region
config.copy()
# test azure
@ -48,8 +48,8 @@ async def test_azure_text_to_speech(mocker):
async def test_iflytek_text_to_speech(mocker):
# mock
config = Config.default()
config.AZURE_TTS_SUBSCRIPTION_KEY = None
config.AZURE_TTS_REGION = None
config.azure_tts_subscription_key = None
config.azure_tts_region = None
mocker.patch.object(IFlyTekTTS, "synthesize_speech", return_value=None)
mock_data = mocker.AsyncMock()
mock_data.read.return_value = b"mock iflytek"
@ -58,11 +58,11 @@ async def test_iflytek_text_to_speech(mocker):
mocker.patch.object(S3, "cache", return_value="http://mock.s3.com/1.mp3")
# Prerequisites
assert config.IFLYTEK_APP_ID
assert config.IFLYTEK_API_KEY
assert config.IFLYTEK_API_SECRET
assert not config.AZURE_TTS_SUBSCRIPTION_KEY or config.AZURE_TTS_SUBSCRIPTION_KEY == "YOUR_API_KEY"
assert not config.AZURE_TTS_REGION
assert config.iflytek_app_id
assert config.iflytek_api_key
assert config.iflytek_api_secret
assert not config.azure_tts_subscription_key or config.azure_tts_subscription_key == "YOUR_API_KEY"
assert not config.azure_tts_region
# test azure
data = await text_to_speech("panda emoji", config=config)

View file

@ -14,7 +14,7 @@ from typer.testing import CliRunner
from metagpt.const import TEST_DATA_PATH
from metagpt.logs import logger
from metagpt.startup import app
from metagpt.software_company import app
runner = CliRunner()

View file

@ -3,13 +3,13 @@
"""
@Time : 2023/5/15 11:40
@Author : alexanderwu
@File : test_startup.py
@File : test_software_company.py
"""
import pytest
from typer.testing import CliRunner
from metagpt.logs import logger
from metagpt.startup import app
from metagpt.software_company import app
from metagpt.team import Team
runner = CliRunner()
@ -23,7 +23,7 @@ async def test_empty_team(new_filename):
logger.info(history)
def test_startup(new_filename):
def test_software_company(new_filename):
args = ["Make a cli snake game"]
result = runner.invoke(app, args)
logger.info(result)

View file

@ -28,10 +28,10 @@ async def test_azure_tts(mocker):
mocker.patch.object(Path, "exists", return_value=True)
# Prerequisites
assert config.AZURE_TTS_SUBSCRIPTION_KEY and config.AZURE_TTS_SUBSCRIPTION_KEY != "YOUR_API_KEY"
assert config.AZURE_TTS_REGION
assert config.azure_tts_subscription_key and config.azure_tts_subscription_key != "YOUR_API_KEY"
assert config.azure_tts_region
azure_tts = AzureTTS(subscription_key=config.AZURE_TTS_SUBSCRIPTION_KEY, region=config.AZURE_TTS_REGION)
azure_tts = AzureTTS(subscription_key=config.azure_tts_subscription_key, region=config.azure_tts_region)
text = """
女儿看见父亲走了进来问道
<mstts:express-as role="YoungAdultFemale" style="calm">

View file

@ -15,8 +15,8 @@ from metagpt.tools.iflytek_tts import IFlyTekTTS, oas3_iflytek_tts
async def test_iflytek_tts(mocker):
# mock
config = Config.default()
config.AZURE_TTS_SUBSCRIPTION_KEY = None
config.AZURE_TTS_REGION = None
config.azure_tts_subscription_key = None
config.azure_tts_region = None
mocker.patch.object(IFlyTekTTS, "synthesize_speech", return_value=None)
mock_data = mocker.AsyncMock()
mock_data.read.return_value = b"mock iflytek"
@ -24,15 +24,15 @@ async def test_iflytek_tts(mocker):
mock_reader.return_value.__aenter__.return_value = mock_data
# Prerequisites
assert config.IFLYTEK_APP_ID
assert config.IFLYTEK_API_KEY
assert config.IFLYTEK_API_SECRET
assert config.iflytek_app_id
assert config.iflytek_api_key
assert config.iflytek_api_secret
result = await oas3_iflytek_tts(
text="你好hello",
app_id=config.IFLYTEK_APP_ID,
api_key=config.IFLYTEK_API_KEY,
api_secret=config.IFLYTEK_API_SECRET,
app_id=config.iflytek_app_id,
api_key=config.iflytek_api_key,
api_secret=config.iflytek_api_secret,
)
assert result

View file

@ -24,7 +24,7 @@ async def test_draw(mocker):
mock_post.return_value.__aenter__.return_value = mock_response
# Prerequisites
assert config.METAGPT_TEXT_TO_IMAGE_MODEL_URL
assert config.metagpt_tti_url
binary_data = await oas3_metagpt_text_to_image("Panda emoji")
assert binary_data

View file

@ -8,6 +8,17 @@
from pathlib import Path
import pytest
from openai.resources.chat.completions import AsyncCompletions
from openai.types import CompletionUsage
from openai.types.chat.chat_completion import (
ChatCompletion,
ChatCompletionMessage,
Choice,
)
from openai.types.chat.chat_completion_message_tool_call import (
ChatCompletionMessageToolCall,
Function,
)
from metagpt.config2 import config
from metagpt.const import API_QUESTIONS_PATH, UT_PY_PATH
@ -16,7 +27,43 @@ from metagpt.tools.ut_writer import YFT_PROMPT_PREFIX, UTGenerator
class TestUTWriter:
@pytest.mark.asyncio
async def test_api_to_ut_sample(self):
async def test_api_to_ut_sample(self, mocker):
async def mock_create(*args, **kwargs):
return ChatCompletion(
id="chatcmpl-8n5fAd21w2J1IIFkI4qxWlNfM7QRC",
choices=[
Choice(
finish_reason="stop",
index=0,
logprobs=None,
message=ChatCompletionMessage(
content=None,
role="assistant",
function_call=None,
tool_calls=[
ChatCompletionMessageToolCall(
id="call_EjjmIY7GMspHu3r9mx8gPA2k",
function=Function(
arguments='{"code":"import string\\nimport random\\n\\ndef random_string'
"(length=10):\\n return ''.join(random.choice(string.ascii_"
'lowercase) for i in range(length))"}',
name="execute",
),
type="function",
)
],
),
)
],
created=1706710532,
model="gpt-3.5-turbo-1106",
object="chat.completion",
system_fingerprint="fp_04f9a1eebf",
usage=CompletionUsage(completion_tokens=35, prompt_tokens=1982, total_tokens=2017),
)
mocker.patch.object(AsyncCompletions, "create", mock_create)
# Prerequisites
swagger_file = Path(__file__).parent / "../../data/ut_writer/yft_swaggerApi.json"
assert swagger_file.exists()

View file

@ -141,6 +141,32 @@ def test_repair_json_format():
output = repair_llm_raw_output(output=raw_output, req_keys=[None], repair_type=RepairType.JSON)
assert output == target_output
raw_output = """
{
"Language": "en_us", // define language
"Programming Language": "Python" # define code language
}
"""
target_output = """{
"Language": "en_us",
"Programming Language": "Python"
}"""
output = repair_llm_raw_output(output=raw_output, req_keys=[None], repair_type=RepairType.JSON)
assert output == target_output
raw_output = """
{
"Language": "#en_us#", // define language
"Programming Language": "//Python # Code // Language//" # define code language
}
"""
target_output = """{
"Language": "#en_us#",
"Programming Language": "//Python # Code // Language//"
}"""
output = repair_llm_raw_output(output=raw_output, req_keys=[None], repair_type=RepairType.JSON)
assert output == target_output
def test_repair_invalid_json():
from metagpt.utils.repair_llm_raw_output import repair_invalid_json

7
tests/spark.yaml Normal file
View file

@ -0,0 +1,7 @@
llm:
api_type: "spark"
app_id: "xxx"
api_key: "xxx"
api_secret: "xxx"
domain: "generalv2"
base_url: "wss://spark-api.xf-yun.com/v3.1/chat"