feat: merge fixbug/rfc135_merge_geekan_cli_etc_1445

This commit is contained in:
莘权 马 2023-12-15 20:32:16 +08:00
parent 8636026c55
commit a9479843f6
8 changed files with 33 additions and 16 deletions

View file

@ -11,7 +11,7 @@ OPENAI_API_BASE: "https://api.openai.com/v1"
OPENAI_API_MODEL: "gpt-4-1106-preview"
MAX_TOKENS: 4096
RPM: 10
#LLM_TYPE: OpenAI # Except for these three major models OpenAI, MetaGPT LLM, and Azure other large models can be distinguished based on the validity of the key.
LLM_TYPE: OpenAI # Except for these three major models OpenAI, MetaGPT LLM, and Azure other large models can be distinguished based on the validity of the key.
#### if Spark
#SPARK_APPID : "YOUR_APPID"

View file

@ -6,7 +6,6 @@
@File : text_to_image.py
@Desc : Text-to-Image skill, which provides text-to-image functionality.
"""
import openai.error
from metagpt.config import CONFIG
from metagpt.const import BASE64_FORMAT
@ -30,10 +29,10 @@ async def text_to_image(text, size_type: str = "512x512", openai_api_key="", mod
elif CONFIG.OPENAI_API_KEY or openai_api_key:
base64_data = await oas3_openai_text_to_image(text, size_type, openai_api_key)
else:
raise openai.error.InvalidRequestError("缺少必要的参数")
raise ValueError("Missing necessary parameters.")
s3 = S3()
url = await s3.cache(data=base64_data, file_ext=".png", format=BASE64_FORMAT)
url = await s3.cache(data=base64_data, file_ext=".png", format=BASE64_FORMAT) if s3.is_valid else ""
if url:
return f"![{text}]({url})"
return image_declaration + base64_data if base64_data else ""

View file

@ -49,7 +49,7 @@ async def text_to_speech(
audio_declaration = "data:audio/wav;base64,"
base64_data = await oas3_azsure_tts(text, lang, voice, style, role, subscription_key, region)
s3 = S3()
url = await s3.cache(data=base64_data, file_ext=".wav", format=BASE64_FORMAT)
url = await s3.cache(data=base64_data, file_ext=".wav", format=BASE64_FORMAT) if s3.is_valid else ""
if url:
return f"[{text}]({url})"
return audio_declaration + base64_data if base64_data else base64_data
@ -61,7 +61,7 @@ async def text_to_speech(
text=text, app_id=iflytek_app_id, api_key=iflytek_api_key, api_secret=iflytek_api_secret
)
s3 = S3()
url = await s3.cache(data=base64_data, file_ext=".mp3", format=BASE64_FORMAT)
url = await s3.cache(data=base64_data, file_ext=".mp3", format=BASE64_FORMAT) if s3.is_valid else ""
if url:
return f"[{text}]({url})"
return audio_declaration + base64_data if base64_data else base64_data

View file

@ -152,3 +152,19 @@ class S3:
logger.exception(f"{e}, stack:{traceback.format_exc()}")
pathname.unlink(missing_ok=True)
return None
@property
def is_valid(self):
is_invalid = (
not CONFIG.S3_ACCESS_KEY
or CONFIG.S3_ACCESS_KEY == "YOUR_S3_ACCESS_KEY"
or not CONFIG.S3_SECRET_KEY
or CONFIG.S3_SECRET_KEY == "YOUR_S3_SECRET_KEY"
or not CONFIG.S3_ENDPOINT_URL
or CONFIG.S3_ENDPOINT_URL == "YOUR_S3_ENDPOINT_URL"
or not CONFIG.S3_BUCKET
or CONFIG.S3_BUCKET == "YOUR_S3_BUCKET"
)
if is_invalid:
logger.info("S3 is invalid")
return not is_invalid

View file

@ -56,3 +56,4 @@ zhipuai==1.0.7
socksio~=1.0.0
gitignore-parser==0.1.9
connexion[swagger-ui]
websockets~=12.0

View file

@ -13,7 +13,7 @@ from unittest.mock import Mock
import pytest
from metagpt.config import CONFIG
from metagpt.config import CONFIG, Config
from metagpt.const import DEFAULT_WORKSPACE_ROOT
from metagpt.logs import logger
from metagpt.provider.openai_api import OpenAIGPTAPI as GPTAPI

View file

@ -6,15 +6,17 @@
@File : test_text_to_image.py
@Desc : Unit tests.
"""
import asyncio
import base64
import pytest
from pydantic import BaseModel
from metagpt.learn.text_to_image import text_to_image
async def mock_text_to_image():
@pytest.mark.asyncio
async def test():
class Input(BaseModel):
input: str
size_type: str
@ -36,11 +38,5 @@ async def mock_text_to_image():
assert base64.b64decode(data, validate=True)
def test_suite():
loop = asyncio.get_event_loop()
task = loop.create_task(mock_text_to_image())
loop.run_until_complete(task)
if __name__ == "__main__":
test_suite()
pytest.main([__file__, "-s"])

View file

@ -11,6 +11,7 @@
import pytest
from metagpt.actions import UserRequirement
from metagpt.config import CONFIG
from metagpt.environment import Environment
from metagpt.logs import logger
from metagpt.roles import Architect, ProductManager, Role
@ -41,6 +42,10 @@ 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
product_manager = ProductManager(name="Alice", profile="Product Manager", goal="做AI Native产品", constraints="资源有限")
architect = Architect(
name="Bob", profile="Architect", goal="设计一个可用、高效、较低成本的系统,包括数据结构与接口", constraints="资源有限,需要节省成本"