-
Notifications
You must be signed in to change notification settings - Fork 341
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #929 from RockChinQ/feat/gitee-ai
feat: 添加对 Gitee AI 的支持
- Loading branch information
Showing
13 changed files
with
112 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
from __future__ import annotations | ||
|
||
from .. import migration | ||
|
||
|
||
@migration.migration_class("gitee-ai-config", 15) | ||
class GiteeAIConfigMigration(migration.Migration): | ||
"""迁移""" | ||
|
||
async def need_migrate(self) -> bool: | ||
"""判断当前环境是否需要运行此迁移""" | ||
return 'gitee-ai-chat-completions' not in self.ap.provider_cfg.data['requester'] or 'gitee-ai' not in self.ap.provider_cfg.data['keys'] | ||
|
||
async def run(self): | ||
"""执行迁移""" | ||
self.ap.provider_cfg.data['requester']['gitee-ai-chat-completions'] = { | ||
"base-url": "https://ai.gitee.com/v1", | ||
"args": {}, | ||
"timeout": 120 | ||
} | ||
|
||
self.ap.provider_cfg.data['keys']['gitee-ai'] = [ | ||
"XXXXX" | ||
] | ||
|
||
await self.ap.provider_cfg.dump_config() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
from __future__ import annotations | ||
|
||
import json | ||
|
||
import asyncio | ||
import aiohttp | ||
import typing | ||
|
||
from . import chatcmpl | ||
from .. import entities, errors, requester | ||
from ....core import app | ||
from ... import entities as llm_entities | ||
from ...tools import entities as tools_entities | ||
from .. import entities as modelmgr_entities | ||
|
||
|
||
@requester.requester_class("gitee-ai-chat-completions") | ||
class GiteeAIChatCompletions(chatcmpl.OpenAIChatCompletions): | ||
"""Gitee AI ChatCompletions API 请求器""" | ||
|
||
def __init__(self, ap: app.Application): | ||
self.ap = ap | ||
self.requester_cfg = ap.provider_cfg.data['requester']['gitee-ai-chat-completions'].copy() | ||
|
||
async def _closure( | ||
self, | ||
req_messages: list[dict], | ||
use_model: entities.LLMModelInfo, | ||
use_funcs: list[tools_entities.LLMFunction] = None, | ||
) -> llm_entities.Message: | ||
self.client.api_key = use_model.token_mgr.get_token() | ||
|
||
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_funcs: | ||
tools = await self.ap.tool_mgr.generate_tools_for_openai(use_funcs) | ||
|
||
if tools: | ||
args["tools"] = tools | ||
|
||
# gitee 不支持多模态,把content都转换成纯文字 | ||
for m in req_messages: | ||
if 'content' in m and isinstance(m["content"], list): | ||
m["content"] = " ".join([c["text"] for c in m["content"]]) | ||
|
||
args["messages"] = req_messages | ||
|
||
resp = await self._req(args) | ||
|
||
message = await self._make_msg(resp) | ||
|
||
return message |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters