Merge pull request #12 from iorisa/feature/metagpt

feat: +metagpt llm
This commit is contained in:
Guess 2023-09-01 22:50:33 +08:00 committed by GitHub
commit d8a9d40e0b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 74 additions and 13 deletions

View file

@ -14,9 +14,11 @@ def get_project_root():
"""逐级向上寻找项目根目录"""
current_path = Path.cwd()
while True:
if (current_path / '.git').exists() or \
(current_path / '.project_root').exists() or \
(current_path / '.gitignore').exists():
if (
(current_path / ".git").exists()
or (current_path / ".project_root").exists()
or (current_path / ".gitignore").exists()
):
return current_path
parent_path = current_path.parent
if parent_path == current_path:
@ -25,15 +27,15 @@ def get_project_root():
PROJECT_ROOT = get_project_root()
DATA_PATH = PROJECT_ROOT / 'data'
WORKSPACE_ROOT = PROJECT_ROOT / 'workspace'
PROMPT_PATH = PROJECT_ROOT / 'metagpt/prompts'
UT_PATH = PROJECT_ROOT / 'data/ut'
DATA_PATH = PROJECT_ROOT / "data"
WORKSPACE_ROOT = PROJECT_ROOT / "workspace"
PROMPT_PATH = PROJECT_ROOT / "metagpt/prompts"
UT_PATH = PROJECT_ROOT / "data/ut"
SWAGGER_PATH = UT_PATH / "files/api/"
UT_PY_PATH = UT_PATH / "files/ut/"
API_QUESTIONS_PATH = UT_PATH / "files/question/"
YAPI_URL = "http://yapi.deepwisdomai.com/"
TMP = PROJECT_ROOT / 'tmp'
TMP = PROJECT_ROOT / "tmp"
RESEARCH_PATH = DATA_PATH / "research"
MEM_TTL = 24 * 30 * 3600
@ -43,4 +45,12 @@ DEFAULT_LANGUAGE = "English"
DEFAULT_MAX_TOKENS = 1500
COMMAND_TOKENS = 500
BRAIN_MEMORY = "BRAIN_MEMORY"
SKILL_PATH = "SKILL_PATH"
SKILL_PATH = "SKILL_PATH"
SERPER_API_KEY = "SERPER_API_KEY"
# Key Definitions for MetaGPT LLM
METAGPT_API_MODEL = "METAGPT_API_MODEL"
METAGPT_API_KEY = "METAGPT_API_KEY"
METAGPT_API_BASE = "METAGPT_API_BASE"
METAGPT_API_TYPE = "METAGPT_API_TYPE"
METAGPT_API_VERSION = "METAGPT_API_VERSION"

View file

@ -15,7 +15,8 @@ from metagpt.provider.base_chatbot import BaseChatbot
class BaseGPTAPI(BaseChatbot):
"""GPT API abstract class, requiring all inheritors to provide a series of standard capabilities"""
system_prompt = 'You are a helpful assistant.'
system_prompt = "You are a helpful assistant."
def _user_msg(self, msg: str) -> dict[str, str]:
return {"role": "user", "content": msg}
@ -46,9 +47,9 @@ class BaseGPTAPI(BaseChatbot):
rsp = await self.acompletion_text(message, stream=True)
except Exception as e:
logger.exception(f"{e}")
logger.info(f"ask:{msg}, error:{e}")
raise e
logger.debug(message)
# logger.debug(rsp)
logger.info(f"ask:{msg}, anwser:{rsp}")
return rsp
def _extract_assistant_rsp(self, context):
@ -115,7 +116,7 @@ class BaseGPTAPI(BaseChatbot):
def messages_to_prompt(self, messages: list[dict]):
"""[{"role": "user", "content": msg}] to user: <msg> etc."""
return '\n'.join([f"{i['role']}: {i['content']}" for i in messages])
return "\n".join([f"{i['role']}: {i['content']}" for i in messages])
def messages_to_dict(self, messages):
"""objects to [{"role": "user", "content": msg}] etc."""

View file

@ -0,0 +1,33 @@
# -*- coding: utf-8 -*-
"""
@Time : 2023/8/30
@Author : mashenquan
@File : metagpt_llm_api.py
@Desc : MetaGPT LLM related APIs
"""
import openai
from metagpt.config import CONFIG
from metagpt.provider import OpenAIGPTAPI
from metagpt.provider.openai_api import RateLimiter
class MetaGPTLLMAPI(OpenAIGPTAPI):
"""MetaGPT LLM api"""
def __init__(self):
self.__init_openai()
self.llm = openai
self.model = CONFIG.METAGPT_API_MODEL
self.auto_max_tokens = False
RateLimiter.__init__(self, rpm=self.rpm)
def __init_openai(self, *args, **kwargs):
openai.api_key = CONFIG.METAGPT_API_KEY
if CONFIG.METAGPT_API_BASE:
openai.api_base = CONFIG.METAGPT_API_BASE
if CONFIG.METAGPT_API_TYPE:
openai.api_type = CONFIG.METAGPT_API_TYPE
openai.api_version = CONFIG.METAGPT_API_VERSION
self.rpm = int(CONFIG.RPM) if CONFIG.RPM else 10

View file

@ -0,0 +1,17 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
@Time : 2023/8/30
@Author : mashenquan
@File : test_metagpt_llm_api.py
"""
from metagpt.provider.metagpt_llm_api import MetaGPTLLMAPI
def test_metagpt():
llm = MetaGPTLLMAPI()
assert llm
if __name__ == "__main__":
test_metagpt()