diff --git a/pkg/config/migrations/m2_openai_config_migration.py b/pkg/config/migrations/m2_openai_config_migration.py new file mode 100644 index 0000000..2f2553e --- /dev/null +++ b/pkg/config/migrations/m2_openai_config_migration.py @@ -0,0 +1,47 @@ +from __future__ import annotations + +from .. import migration + + +@migration.migration_class("openai-config-migration", 2) +class OpenAIConfigMigration(migration.Migration): + """OpenAI配置迁移 + """ + + async def need_migrate(self) -> bool: + """判断当前环境是否需要运行此迁移 + """ + return 'openai-config' in self.ap.provider_cfg.data + + async def run(self): + """执行迁移 + """ + old_openai_config = self.ap.provider_cfg.data['openai-config'].copy() + + if 'keys' not in self.ap.provider_cfg.data: + self.ap.provider_cfg.data['keys'] = {} + + if 'openai' not in self.ap.provider_cfg.data['keys']: + self.ap.provider_cfg.data['keys']['openai'] = [] + + self.ap.provider_cfg.data['keys']['openai'] = old_openai_config['api-keys'] + + self.ap.provider_cfg.data['model'] = old_openai_config['chat-completions-params']['model'] + + del old_openai_config['chat-completions-params']['model'] + + if 'requester' not in self.ap.provider_cfg.data: + self.ap.provider_cfg.data['requester'] = {} + + if 'openai-chat-completions' not in self.ap.provider_cfg.data['requester']: + self.ap.provider_cfg.data['requester']['openai-chat-completions'] = {} + + self.ap.provider_cfg.data['requester']['openai-chat-completions'] = { + 'base-url': old_openai_config['base_url'], + 'args': old_openai_config['chat-completions-params'], + 'timeout': old_openai_config['request-timeout'], + } + + del self.ap.provider_cfg.data['openai-config'] + + await self.ap.provider_cfg.dump_config() \ No newline at end of file diff --git a/pkg/core/stages/migrate.py b/pkg/core/stages/migrate.py index 5e84b18..edf3cba 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 +from ...config.migrations import m1_sensitive_word_migration, m2_openai_config_migration @stage.stage_class("MigrationStage") diff --git a/pkg/provider/modelmgr/apis/chatcmpl.py b/pkg/provider/modelmgr/apis/chatcmpl.py index 4965acf..c8d1a81 100644 --- a/pkg/provider/modelmgr/apis/chatcmpl.py +++ b/pkg/provider/modelmgr/apis/chatcmpl.py @@ -17,8 +17,8 @@ from ... import entities as llm_entities from ...tools import entities as tools_entities -@api.requester_class("openai-chat-completion") -class OpenAIChatCompletion(api.LLMAPIRequester): +@api.requester_class("openai-chat-completions") +class OpenAIChatCompletions(api.LLMAPIRequester): """OpenAI ChatCompletion API 请求器""" client: openai.AsyncClient @@ -26,8 +26,8 @@ class OpenAIChatCompletion(api.LLMAPIRequester): async def initialize(self): self.client = openai.AsyncClient( api_key="", - base_url=self.ap.provider_cfg.data['openai-config']['base_url'], - timeout=self.ap.provider_cfg.data['openai-config']['request-timeout'], + base_url=self.ap.provider_cfg.data['requester']['openai-chat-completions']['base-url'], + timeout=self.ap.provider_cfg.data['requester']['openai-chat-completions']['timeout'], http_client=httpx.AsyncClient( proxies=self.ap.proxy_mgr.get_forward_proxies() ) @@ -58,7 +58,7 @@ class OpenAIChatCompletion(api.LLMAPIRequester): ) -> llm_entities.Message: self.client.api_key = use_model.token_mgr.get_token() - args = self.ap.provider_cfg.data['openai-config']['chat-completions-params'].copy() + args = self.ap.provider_cfg.data['requester']['openai-chat-completions']['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/modelmgr.py b/pkg/provider/modelmgr/modelmgr.py index 3ad968d..f5b130c 100644 --- a/pkg/provider/modelmgr/modelmgr.py +++ b/pkg/provider/modelmgr/modelmgr.py @@ -39,9 +39,8 @@ class ModelManager: async def initialize(self): # 初始化token_mgr, requester - self.token_mgrs = { - "openai": token.TokenManager("openai", list(self.ap.provider_cfg.data['openai-config']['api-keys'])) - } + for k, v in self.ap.provider_cfg.data['keys'].items(): + self.token_mgrs[k] = token.TokenManager(k, v) for api_cls in api.preregistered_requesters: api_inst = api_cls(self.ap) diff --git a/pkg/provider/session/sessionmgr.py b/pkg/provider/session/sessionmgr.py index 4ffec02..9130cbe 100644 --- a/pkg/provider/session/sessionmgr.py +++ b/pkg/provider/session/sessionmgr.py @@ -50,7 +50,7 @@ class SessionManager: conversation = core_entities.Conversation( prompt=await self.ap.prompt_mgr.get_prompt(session.use_prompt_name), messages=[], - use_model=await self.ap.model_mgr.get_model_by_name(self.ap.provider_cfg.data['openai-config']['chat-completions-params']['model']), + use_model=await self.ap.model_mgr.get_model_by_name(self.ap.provider_cfg.data['model']), use_funcs=await self.ap.tool_mgr.get_all_functions(), ) session.conversations.append(conversation) diff --git a/templates/metadata/llm-models.json b/templates/metadata/llm-models.json index bfeaf62..8775e66 100644 --- a/templates/metadata/llm-models.json +++ b/templates/metadata/llm-models.json @@ -2,7 +2,7 @@ "list": [ { "name": "default", - "requester": "openai-chat-completion", + "requester": "openai-chat-completions", "token_mgr": "openai", "tool_call_supported": false }, diff --git a/templates/provider.json b/templates/provider.json index bbaf5f4..d1a68d7 100644 --- a/templates/provider.json +++ b/templates/provider.json @@ -1,15 +1,18 @@ { "enable-chat": true, - "openai-config": { - "api-keys": [ + "keys": { + "openai": [ "sk-1234567890" - ], - "base_url": "https://api.openai.com/v1", - "chat-completions-params": { - "model": "gpt-3.5-turbo" - }, - "request-timeout": 120 + ] }, + "requester": { + "openai-chat-completions": { + "base-url": "https://api.openai.com/v1", + "args": {}, + "timeout": 120 + } + }, + "model": "gpt-3.5-turbo", "prompt-mode": "normal", "prompt": { "default": ""