diff --git a/.agent-store-config.yaml.example b/.agent-store-config.yaml.example new file mode 100644 index 000000000..037a44ed4 --- /dev/null +++ b/.agent-store-config.yaml.example @@ -0,0 +1,9 @@ +role: + name: Teacher # Referenced the `Teacher` in `metagpt/roles/teacher.py`. + module: metagpt.roles.teacher # Referenced `metagpt/roles/teacher.py`. + skills: # Refer to the skill `name` of the published skill in `.well-known/skills.yaml`. + - name: text_to_speech + description: Text-to-speech + - name: text_to_image + description: Create a drawing based on the text. + diff --git a/examples/write_teaching_plan.py b/examples/write_teaching_plan.py index 191547193..c3a647b94 100644 --- a/examples/write_teaching_plan.py +++ b/examples/write_teaching_plan.py @@ -5,15 +5,18 @@ @Author : mashenquan @File : write_teaching_plan.py @Desc: Write teaching plan demo + ``` + export PYTHONPATH=$PYTHONPATH:$PWD + python examples/write_teaching_plan.py --language=Chinese --teaching_language=English + + ``` """ import asyncio from pathlib import Path -import sys from metagpt.config import CONFIG -sys.path.append(str(Path(__file__).resolve().parent.parent)) import aiofiles import fire from metagpt.logs import logger diff --git a/metagpt/actions/skill_action.py b/metagpt/actions/skill_action.py index fb801b454..3ef0087fc 100644 --- a/metagpt/actions/skill_action.py +++ b/metagpt/actions/skill_action.py @@ -6,7 +6,7 @@ @File : skill_action.py @Desc : Call learned skill """ - +from __future__ import annotations import ast import importlib import traceback diff --git a/metagpt/tools/azure_tts.py b/metagpt/tools/azure_tts.py index e9bb55bed..3100e2a3a 100644 --- a/metagpt/tools/azure_tts.py +++ b/metagpt/tools/azure_tts.py @@ -12,11 +12,12 @@ from uuid import uuid4 import base64 import sys +from metagpt.config import CONFIG, Config + sys.path.append(str(Path(__file__).resolve().parent.parent.parent)) # fix-bug: No module named 'metagpt' from metagpt.logs import logger from aiofile import async_open from azure.cognitiveservices.speech import AudioConfig, SpeechConfig, SpeechSynthesizer -import os class AzureTTS: @@ -27,8 +28,8 @@ class AzureTTS: :param subscription_key: key is used to access your Azure AI service API, see: `https://portal.azure.com/` > `Resource Management` > `Keys and Endpoint` :param region: This is the location (or region) of your resource. You may need to use this field when making calls to this API. """ - self.subscription_key = subscription_key if subscription_key else os.environ.get('AZURE_TTS_SUBSCRIPTION_KEY') - self.region = region if region else os.environ.get('AZURE_TTS_REGION') + self.subscription_key = subscription_key if subscription_key else CONFIG.AZURE_TTS_SUBSCRIPTION_KEY + self.region = region if region else CONFIG.AZURE_TTS_REGION # 参数参考:https://learn.microsoft.com/zh-cn/azure/cognitive-services/speech-service/language-support?tabs=tts#voice-styles-and-roles async def synthesize_speech(self, lang, voice, text, output_file): @@ -87,9 +88,9 @@ async def oas3_azsure_tts(text, lang="", voice="", style="", role="", subscripti if not style: style = "affectionate" if not subscription_key: - subscription_key = os.environ.get("AZURE_TTS_SUBSCRIPTION_KEY") + subscription_key = CONFIG.AZURE_TTS_SUBSCRIPTION_KEY if not region: - region = os.environ.get("AZURE_TTS_REGION") + region = CONFIG.AZURE_TTS_REGION xml_value = AzureTTS.role_style_text(role=role, style=style, text=text) tts = AzureTTS(subscription_key=subscription_key, region=region) @@ -108,6 +109,7 @@ async def oas3_azsure_tts(text, lang="", voice="", style="", role="", subscripti if __name__ == "__main__": + Config() loop = asyncio.new_event_loop() v = loop.create_task(oas3_azsure_tts("测试,test")) loop.run_until_complete(v) diff --git a/metagpt/tools/metagpt_text_to_image.py b/metagpt/tools/metagpt_text_to_image.py index 43d22961b..c5a0b872f 100644 --- a/metagpt/tools/metagpt_text_to_image.py +++ b/metagpt/tools/metagpt_text_to_image.py @@ -6,6 +6,7 @@ @File : metagpt_text_to_image.py @Desc : MetaGPT Text-to-Image OAS3 api, which provides text-to-image functionality. """ +import asyncio import base64 import os import sys @@ -16,6 +17,8 @@ import aiohttp import requests from pydantic import BaseModel +from metagpt.config import CONFIG, Config + sys.path.append(str(Path(__file__).resolve().parent.parent.parent)) # fix-bug: No module named 'metagpt' from metagpt.logs import logger @@ -25,7 +28,7 @@ class MetaGPTText2Image: """ :param model_url: Model reset api url """ - self.model_url = model_url if model_url else os.environ.get('METAGPT_TEXT_TO_IMAGE_MODEL') + self.model_url = model_url if model_url else CONFIG.METAGPT_TEXT_TO_IMAGE_MODEL async def text_2_image(self, text, size_type="512x512"): """Text to image @@ -98,12 +101,16 @@ async def oas3_metagpt_text_to_image(text, size_type: str = "512x512", model_url if not text: return "" if not model_url: - model_url = os.environ.get('METAGPT_TEXT_TO_IMAGE_MODEL_URL') + model_url = CONFIG.METAGPT_TEXT_TO_IMAGE_MODEL_URL return await MetaGPTText2Image(model_url).text_2_image(text, size_type=size_type) if __name__ == "__main__": - v = oas3_metagpt_text_to_image("Panda emoji") + Config() + loop = asyncio.new_event_loop() + task = loop.create_task(oas3_metagpt_text_to_image("Panda emoji")) + v = loop.run_until_complete(task) + print(v) data = base64.b64decode(v) with open("tmp.png", mode="wb") as writer: writer.write(data) diff --git a/metagpt/tools/openai_text_to_embedding.py b/metagpt/tools/openai_text_to_embedding.py index 73984aff6..86b58d71f 100644 --- a/metagpt/tools/openai_text_to_embedding.py +++ b/metagpt/tools/openai_text_to_embedding.py @@ -17,7 +17,7 @@ import requests from pydantic import BaseModel import sys -from metagpt.config import CONFIG +from metagpt.config import CONFIG, Config sys.path.append(str(Path(__file__).resolve().parent.parent.parent)) # fix-bug: No module named 'metagpt' from metagpt.logs import logger @@ -48,7 +48,7 @@ class OpenAIText2Embedding: """ :param openai_api_key: OpenAI API key, For more details, checkout: `https://platform.openai.com/account/api-keys` """ - self.openai_api_key = openai_api_key if openai_api_key else os.environ.get('OPENAI_API_KEY') + self.openai_api_key = openai_api_key if openai_api_key else CONFIG.OPENAI_API_KEY async def text_2_embedding(self, text, model="text-embedding-ada-002"): """Text to embedding @@ -89,7 +89,8 @@ async def oas3_openai_text_to_embedding(text, model="text-embedding-ada-002", op if __name__ == "__main__": + Config() loop = asyncio.new_event_loop() - v = loop.create_task(oas3_openai_text_to_embedding("Panda emoji")) - loop.run_until_complete(v) + task = loop.create_task(oas3_openai_text_to_embedding("Panda emoji")) + v = loop.run_until_complete(task) print(v) diff --git a/metagpt/tools/openai_text_to_image.py b/metagpt/tools/openai_text_to_image.py index 052a429ae..395fa8133 100644 --- a/metagpt/tools/openai_text_to_image.py +++ b/metagpt/tools/openai_text_to_image.py @@ -6,6 +6,7 @@ @File : openai_text_to_image.py @Desc : OpenAI Text-to-Image OAS3 api, which provides text-to-image functionality. """ +import asyncio import base64 import os import sys @@ -16,6 +17,8 @@ import aiohttp import requests from pydantic import BaseModel +from metagpt.config import CONFIG, Config + sys.path.append(str(Path(__file__).resolve().parent.parent.parent)) # fix-bug: No module named 'metagpt' from metagpt.logs import logger @@ -25,7 +28,7 @@ class OpenAIText2Image: """ :param openai_api_key: OpenAI API key, For more details, checkout: `https://platform.openai.com/account/api-keys` """ - self.openai_api_key = openai_api_key if openai_api_key else os.environ.get('OPENAI_API_KEY') + self.openai_api_key = openai_api_key if openai_api_key else CONFIG.OPENAI_API_KEY async def text_2_image(self, text, size_type="1024x1024"): """Text to image @@ -90,10 +93,13 @@ async def oas3_openai_text_to_image(text, size_type: str = "1024x1024", openai_a if not text: return "" if not openai_api_key: - openai_api_key = os.environ.get("OPENAI_API_KEY") + openai_api_key = CONFIG.OPENAI_API_KEY return await OpenAIText2Image(openai_api_key).text_2_image(text, size_type=size_type) if __name__ == "__main__": - v = oas3_openai_text_to_image("Panda emoji") + Config() + loop = asyncio.new_event_loop() + task = loop.create_task(oas3_openai_text_to_image("Panda emoji")) + v = loop.run_until_complete(task) print(v) diff --git a/requirements.txt b/requirements.txt index ed3f755c9..25a480a68 100644 --- a/requirements.txt +++ b/requirements.txt @@ -40,5 +40,5 @@ libcst==1.0.1 qdrant-client==1.4.0 connexion[swagger-ui] aiohttp_jinja2 -azure-cognitiveservices-speech==1.30.0 +azure-cognitiveservices-speech==1.31.0 aiofile \ No newline at end of file diff --git a/tests/conftest.py b/tests/conftest.py index feecc7715..8f5069bbe 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -9,7 +9,9 @@ from unittest.mock import Mock import pytest +import pytest_asyncio +from metagpt.config import Config from metagpt.logs import logger from metagpt.provider.openai_api import OpenAIGPTAPI as GPTAPI import asyncio @@ -68,3 +70,7 @@ def proxy(): server = asyncio.get_event_loop().run_until_complete(asyncio.start_server(handle_client, "127.0.0.1", 0)) return "http://{}:{}".format(*server.sockets[0].getsockname()) + +@pytest.fixture(scope="session", autouse=True) +def init_config(): + Config()