2025-06-21 16:33:21 +08:00
|
|
|
"""
|
|
|
|
|
OpenAI API 适配器
|
|
|
|
|
|
2025-07-08 11:15:15 +08:00
|
|
|
支持 OpenAI、DeepSeek、智谱AI 和其他 OpenAI 兼容的 API 服务。
|
2025-06-21 16:33:21 +08:00
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
from typing import TYPE_CHECKING
|
|
|
|
|
|
2025-07-08 11:15:15 +08:00
|
|
|
from .base import OpenAICompatAdapter
|
2025-06-21 16:33:21 +08:00
|
|
|
|
|
|
|
|
if TYPE_CHECKING:
|
|
|
|
|
from ..service import LLMModel
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class OpenAIAdapter(OpenAICompatAdapter):
|
|
|
|
|
"""OpenAI兼容API适配器"""
|
|
|
|
|
|
|
|
|
|
@property
|
|
|
|
|
def api_type(self) -> str:
|
|
|
|
|
return "openai"
|
|
|
|
|
|
|
|
|
|
@property
|
|
|
|
|
def supported_api_types(self) -> list[str]:
|
2025-10-01 18:41:46 +08:00
|
|
|
return [
|
|
|
|
|
"openai",
|
|
|
|
|
"deepseek",
|
|
|
|
|
"zhipu",
|
|
|
|
|
"general_openai_compat",
|
|
|
|
|
"ark",
|
|
|
|
|
"openrouter",
|
|
|
|
|
]
|
2025-06-21 16:33:21 +08:00
|
|
|
|
2025-07-08 11:15:15 +08:00
|
|
|
def get_chat_endpoint(self, model: "LLMModel") -> str:
|
2025-06-21 16:33:21 +08:00
|
|
|
"""返回聊天完成端点"""
|
2025-07-08 11:15:15 +08:00
|
|
|
if model.api_type == "ark":
|
|
|
|
|
return "/api/v3/chat/completions"
|
|
|
|
|
if model.api_type == "zhipu":
|
|
|
|
|
return "/api/paas/v4/chat/completions"
|
2025-06-21 16:33:21 +08:00
|
|
|
return "/v1/chat/completions"
|
|
|
|
|
|
2025-07-08 11:15:15 +08:00
|
|
|
def get_embedding_endpoint(self, model: "LLMModel") -> str:
|
|
|
|
|
"""根据API类型返回嵌入端点"""
|
|
|
|
|
if model.api_type == "zhipu":
|
|
|
|
|
return "/v4/embeddings"
|
2025-06-21 16:33:21 +08:00
|
|
|
return "/v1/embeddings"
|