Merge pull request #734 from RockChinQ/feat/moonshot

Feat: 添加对 moonshot 模型的支持
This commit is contained in:
Junyan Qin
2024-03-19 22:41:40 +08:00
committed by GitHub
10 changed files with 84 additions and 8 deletions

View File

@@ -19,7 +19,7 @@ class AnthropicRequesterConfigCompletionMigration(migration.Migration):
""" """
if 'anthropic-messages' not in self.ap.provider_cfg.data['requester']: if 'anthropic-messages' not in self.ap.provider_cfg.data['requester']:
self.ap.provider_cfg.data['requester']['anthropic-messages'] = { self.ap.provider_cfg.data['requester']['anthropic-messages'] = {
'base-url': 'https://api.anthropic.com/v1', 'base-url': 'https://api.anthropic.com',
'args': { 'args': {
'max_tokens': 1024 'max_tokens': 1024
}, },

View File

@@ -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()

View File

@@ -4,7 +4,7 @@ import importlib
from .. import stage, app from .. import stage, app
from ...config import migration 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") @stage.stage_class("MigrationStage")

View File

@@ -10,7 +10,7 @@ import openai.types.chat.chat_completion as chat_completion
import httpx import httpx
from .. import api, entities, errors 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 ... import entities as llm_entities
from ...tools import entities as tools_entities from ...tools import entities as tools_entities
@@ -21,11 +21,19 @@ class OpenAIChatCompletions(api.LLMAPIRequester):
client: openai.AsyncClient 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): async def initialize(self):
self.client = openai.AsyncClient( self.client = openai.AsyncClient(
api_key="", api_key="",
base_url=self.ap.provider_cfg.data['requester']['openai-chat-completions']['base-url'], base_url=self.requester_cfg['base-url'],
timeout=self.ap.provider_cfg.data['requester']['openai-chat-completions']['timeout'], timeout=self.requester_cfg['timeout'],
http_client=httpx.AsyncClient( http_client=httpx.AsyncClient(
proxies=self.ap.proxy_mgr.get_forward_proxies() proxies=self.ap.proxy_mgr.get_forward_proxies()
) )
@@ -56,7 +64,7 @@ class OpenAIChatCompletions(api.LLMAPIRequester):
) -> llm_entities.Message: ) -> llm_entities.Message:
self.client.api_key = use_model.token_mgr.get_token() 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 args["model"] = use_model.name if use_model.model_name is None else use_model.model_name
if use_model.tool_call_supported: if use_model.tool_call_supported:

View File

@@ -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

View File

@@ -6,7 +6,7 @@ from . import entities
from ...core import app from ...core import app
from . import token, api 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" FETCH_MODEL_LIST_URL = "https://api.qchatgpt.rockchin.top/api/v2/fetch/model_list"

View File

@@ -44,6 +44,21 @@
"name": "claude-3-haiku-20240307", "name": "claude-3-haiku-20240307",
"requester": "anthropic-messages", "requester": "anthropic-messages",
"token_mgr": "anthropic" "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"
} }
] ]
} }

View File

@@ -6,6 +6,9 @@
], ],
"anthropic": [ "anthropic": [
"sk-1234567890" "sk-1234567890"
],
"moonshot": [
"sk-1234567890"
] ]
}, },
"requester": { "requester": {
@@ -15,11 +18,16 @@
"timeout": 120 "timeout": 120
}, },
"anthropic-messages": { "anthropic-messages": {
"base-url": "https://api.anthropic.com/v1", "base-url": "https://api.anthropic.com",
"args": { "args": {
"max_tokens": 1024 "max_tokens": 1024
}, },
"timeout": 120 "timeout": 120
},
"moonshot-chat-completions": {
"base-url": "https://api.moonshot.cn/v1",
"args": {},
"timeout": 120
} }
}, },
"model": "gpt-3.5-turbo", "model": "gpt-3.5-turbo",