From dead8fa168b5434bac920ad4f752c8387cb0c00f Mon Sep 17 00:00:00 2001 From: RockChinQ <1010553892@qq.com> Date: Tue, 19 Mar 2024 22:39:45 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=B7=BB=E5=8A=A0=E5=AF=B9=20moonshot?= =?UTF-8?q?=20=E6=A8=A1=E5=9E=8B=E7=9A=84=E6=94=AF=E6=8C=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...on.py => m001_sensitive_word_migration.py} | 0 ...ion.py => m002_openai_config_migration.py} | 0 ...003_anthropic_requester_cfg_completion.py} | 2 +- .../m004_moonshot_cfg_completion.py | 30 +++++++++++++++++++ pkg/core/stages/migrate.py | 2 +- pkg/provider/modelmgr/apis/chatcmpl.py | 16 +++++++--- .../modelmgr/apis/moonshotchatcmpl.py | 15 ++++++++++ pkg/provider/modelmgr/modelmgr.py | 2 +- templates/metadata/llm-models.json | 15 ++++++++++ templates/provider.json | 10 ++++++- 10 files changed, 84 insertions(+), 8 deletions(-) rename pkg/config/migrations/{m1_sensitive_word_migration.py => m001_sensitive_word_migration.py} (100%) rename pkg/config/migrations/{m2_openai_config_migration.py => m002_openai_config_migration.py} (100%) rename pkg/config/migrations/{m3_anthropic_requester_cfg_completion.py => m003_anthropic_requester_cfg_completion.py} (94%) create mode 100644 pkg/config/migrations/m004_moonshot_cfg_completion.py create mode 100644 pkg/provider/modelmgr/apis/moonshotchatcmpl.py diff --git a/pkg/config/migrations/m1_sensitive_word_migration.py b/pkg/config/migrations/m001_sensitive_word_migration.py similarity index 100% rename from pkg/config/migrations/m1_sensitive_word_migration.py rename to pkg/config/migrations/m001_sensitive_word_migration.py diff --git a/pkg/config/migrations/m2_openai_config_migration.py b/pkg/config/migrations/m002_openai_config_migration.py similarity index 100% rename from pkg/config/migrations/m2_openai_config_migration.py rename to pkg/config/migrations/m002_openai_config_migration.py diff --git a/pkg/config/migrations/m3_anthropic_requester_cfg_completion.py b/pkg/config/migrations/m003_anthropic_requester_cfg_completion.py similarity index 94% rename from pkg/config/migrations/m3_anthropic_requester_cfg_completion.py rename to pkg/config/migrations/m003_anthropic_requester_cfg_completion.py index 82ad2a7..101b03d 100644 --- a/pkg/config/migrations/m3_anthropic_requester_cfg_completion.py +++ b/pkg/config/migrations/m003_anthropic_requester_cfg_completion.py @@ -19,7 +19,7 @@ class AnthropicRequesterConfigCompletionMigration(migration.Migration): """ if 'anthropic-messages' not in self.ap.provider_cfg.data['requester']: self.ap.provider_cfg.data['requester']['anthropic-messages'] = { - 'base-url': 'https://api.anthropic.com/v1', + 'base-url': 'https://api.anthropic.com', 'args': { 'max_tokens': 1024 }, diff --git a/pkg/config/migrations/m004_moonshot_cfg_completion.py b/pkg/config/migrations/m004_moonshot_cfg_completion.py new file mode 100644 index 0000000..b1f7e9e --- /dev/null +++ b/pkg/config/migrations/m004_moonshot_cfg_completion.py @@ -0,0 +1,30 @@ +from __future__ import annotations + +from .. import migration + + +@migration.migration_class("moonshot-config-completion", 4) +class MoonshotConfigCompletionMigration(migration.Migration): + """OpenAI配置迁移 + """ + + async def need_migrate(self) -> bool: + """判断当前环境是否需要运行此迁移 + """ + return 'moonshot-chat-completions' not in self.ap.provider_cfg.data['requester'] \ + or 'moonshot' not in self.ap.provider_cfg.data['keys'] + + async def run(self): + """执行迁移 + """ + if 'moonshot-chat-completions' not in self.ap.provider_cfg.data['requester']: + self.ap.provider_cfg.data['requester']['moonshot-chat-completions'] = { + 'base-url': 'https://api.moonshot.cn/v1', + 'args': {}, + 'timeout': 120, + } + + if 'moonshot' not in self.ap.provider_cfg.data['keys']: + self.ap.provider_cfg.data['keys']['moonshot'] = [] + + await self.ap.provider_cfg.dump_config() diff --git a/pkg/core/stages/migrate.py b/pkg/core/stages/migrate.py index b7685c3..1146ab6 100644 --- a/pkg/core/stages/migrate.py +++ b/pkg/core/stages/migrate.py @@ -4,7 +4,7 @@ import importlib from .. import stage, app from ...config import migration -from ...config.migrations import m1_sensitive_word_migration, m2_openai_config_migration, m3_anthropic_requester_cfg_completion +from ...config.migrations import m001_sensitive_word_migration, m002_openai_config_migration, m003_anthropic_requester_cfg_completion, m004_moonshot_cfg_completion @stage.stage_class("MigrationStage") diff --git a/pkg/provider/modelmgr/apis/chatcmpl.py b/pkg/provider/modelmgr/apis/chatcmpl.py index 0fe2788..f637f16 100644 --- a/pkg/provider/modelmgr/apis/chatcmpl.py +++ b/pkg/provider/modelmgr/apis/chatcmpl.py @@ -10,7 +10,7 @@ import openai.types.chat.chat_completion as chat_completion import httpx from .. import api, entities, errors -from ....core import entities as core_entities +from ....core import entities as core_entities, app from ... import entities as llm_entities from ...tools import entities as tools_entities @@ -21,11 +21,19 @@ class OpenAIChatCompletions(api.LLMAPIRequester): client: openai.AsyncClient + requester_cfg: dict + + def __init__(self, ap: app.Application): + self.ap = ap + + self.requester_cfg = self.ap.provider_cfg.data['requester']['openai-chat-completions'] + async def initialize(self): + self.client = openai.AsyncClient( api_key="", - base_url=self.ap.provider_cfg.data['requester']['openai-chat-completions']['base-url'], - timeout=self.ap.provider_cfg.data['requester']['openai-chat-completions']['timeout'], + base_url=self.requester_cfg['base-url'], + timeout=self.requester_cfg['timeout'], http_client=httpx.AsyncClient( proxies=self.ap.proxy_mgr.get_forward_proxies() ) @@ -56,7 +64,7 @@ class OpenAIChatCompletions(api.LLMAPIRequester): ) -> llm_entities.Message: self.client.api_key = use_model.token_mgr.get_token() - args = self.ap.provider_cfg.data['requester']['openai-chat-completions']['args'].copy() + args = self.requester_cfg['args'].copy() args["model"] = use_model.name if use_model.model_name is None else use_model.model_name if use_model.tool_call_supported: diff --git a/pkg/provider/modelmgr/apis/moonshotchatcmpl.py b/pkg/provider/modelmgr/apis/moonshotchatcmpl.py new file mode 100644 index 0000000..cb9fd93 --- /dev/null +++ b/pkg/provider/modelmgr/apis/moonshotchatcmpl.py @@ -0,0 +1,15 @@ +from __future__ import annotations + +from ....core import app + +from . import chatcmpl +from .. import api + + +@api.requester_class("moonshot-chat-completions") +class MoonshotChatCompletions(chatcmpl.OpenAIChatCompletions): + """Moonshot ChatCompletion API 请求器""" + + def __init__(self, ap: app.Application): + self.requester_cfg = ap.provider_cfg.data['requester']['moonshot-chat-completions'] + self.ap = ap diff --git a/pkg/provider/modelmgr/modelmgr.py b/pkg/provider/modelmgr/modelmgr.py index 21a1c75..3ec432f 100644 --- a/pkg/provider/modelmgr/modelmgr.py +++ b/pkg/provider/modelmgr/modelmgr.py @@ -6,7 +6,7 @@ from . import entities from ...core import app from . import token, api -from .apis import chatcmpl, anthropicmsgs +from .apis import chatcmpl, anthropicmsgs, moonshotchatcmpl FETCH_MODEL_LIST_URL = "https://api.qchatgpt.rockchin.top/api/v2/fetch/model_list" diff --git a/templates/metadata/llm-models.json b/templates/metadata/llm-models.json index 061a7cc..db10313 100644 --- a/templates/metadata/llm-models.json +++ b/templates/metadata/llm-models.json @@ -44,6 +44,21 @@ "name": "claude-3-haiku-20240307", "requester": "anthropic-messages", "token_mgr": "anthropic" + }, + { + "name": "moonshot-v1-8k", + "requester": "moonshot-chat-completions", + "token_mgr": "moonshot" + }, + { + "name": "moonshot-v1-32k", + "requester": "moonshot-chat-completions", + "token_mgr": "moonshot" + }, + { + "name": "moonshot-v1-128k", + "requester": "moonshot-chat-completions", + "token_mgr": "moonshot" } ] } \ No newline at end of file diff --git a/templates/provider.json b/templates/provider.json index 1c26bd4..ffe1e70 100644 --- a/templates/provider.json +++ b/templates/provider.json @@ -6,6 +6,9 @@ ], "anthropic": [ "sk-1234567890" + ], + "moonshot": [ + "sk-1234567890" ] }, "requester": { @@ -15,11 +18,16 @@ "timeout": 120 }, "anthropic-messages": { - "base-url": "https://api.anthropic.com/v1", + "base-url": "https://api.anthropic.com", "args": { "max_tokens": 1024 }, "timeout": 120 + }, + "moonshot-chat-completions": { + "base-url": "https://api.moonshot.cn/v1", + "args": {}, + "timeout": 120 } }, "model": "gpt-3.5-turbo",