mirror of
https://github.com/zhenxun-org/zhenxun_bot.git
synced 2025-12-15 06:12:53 +08:00
* ✨ feat(llm): 实现LLM服务模块,支持多提供商统一接口和高级功能 * 🎨 Ruff * ✨ Config配置类支持BaseModel存储 * 🎨 代码格式化 * 🎨 代码格式化 * 🎨 格式化代码 * ✨ feat(llm): 添加 AI 对话历史管理 * ✨ feat(llmConfig): 引入 LLM 配置模型及管理功能 * 🎨 Ruff --------- Co-authored-by: fccckaug <xxxmio123123@gmail.com> Co-authored-by: HibiKier <45528451+HibiKier@users.noreply.github.com> Co-authored-by: HibiKier <775757368@qq.com> Co-authored-by: fccckaug <xxxmcsmiomio3@gmail.com> Co-authored-by: webjoin111 <455457521@qq.com>
58 lines
1.5 KiB
Python
58 lines
1.5 KiB
Python
"""
|
|
智谱 AI API 适配器
|
|
|
|
支持智谱 AI 的 GLM 系列模型,使用 OpenAI 兼容的接口格式。
|
|
"""
|
|
|
|
from typing import TYPE_CHECKING
|
|
|
|
from .base import OpenAICompatAdapter, RequestData
|
|
|
|
if TYPE_CHECKING:
|
|
from ..service import LLMModel
|
|
|
|
|
|
class ZhipuAdapter(OpenAICompatAdapter):
|
|
"""智谱AI适配器 - 使用智谱AI专用的OpenAI兼容接口"""
|
|
|
|
@property
|
|
def api_type(self) -> str:
|
|
return "zhipu"
|
|
|
|
@property
|
|
def supported_api_types(self) -> list[str]:
|
|
return ["zhipu"]
|
|
|
|
def get_chat_endpoint(self) -> str:
|
|
"""返回智谱AI聊天完成端点"""
|
|
return "/api/paas/v4/chat/completions"
|
|
|
|
def get_embedding_endpoint(self) -> str:
|
|
"""返回智谱AI嵌入端点"""
|
|
return "/v4/embeddings"
|
|
|
|
def prepare_simple_request(
|
|
self,
|
|
model: "LLMModel",
|
|
api_key: str,
|
|
prompt: str,
|
|
history: list[dict[str, str]] | None = None,
|
|
) -> RequestData:
|
|
"""准备简单文本生成请求 - 智谱AI优化实现"""
|
|
url = self.get_api_url(model, self.get_chat_endpoint())
|
|
headers = self.get_base_headers(api_key)
|
|
|
|
messages = []
|
|
if history:
|
|
messages.extend(history)
|
|
messages.append({"role": "user", "content": prompt})
|
|
|
|
body = {
|
|
"model": model.model_name,
|
|
"messages": messages,
|
|
}
|
|
|
|
body = self.apply_config_override(model, body)
|
|
|
|
return RequestData(url=url, headers=headers, body=body)
|