feat: 新增对多家平台的模型适配
This commit is contained in:
@@ -21,9 +21,11 @@
|
|||||||
- 模型名称(name)
|
- 模型名称(name)
|
||||||
- 模型类型(type):`KnowledgeQA`、`Embedding`、`Rerank` 或 `VLLM`
|
- 模型类型(type):`KnowledgeQA`、`Embedding`、`Rerank` 或 `VLLM`
|
||||||
- 模型来源(source):`local` 或 `remote`
|
- 模型来源(source):`local` 或 `remote`
|
||||||
- 模型参数(parameters):包括 base_url、api_key 等
|
- 模型参数(parameters):包括 base_url、api_key、provider 等
|
||||||
- 租户ID(tenant_id):建议使用小于10000的租户ID,避免冲突
|
- 租户ID(tenant_id):建议使用小于10000的租户ID,避免冲突
|
||||||
|
|
||||||
|
**支持的服务商(provider)**:`generic`(自定义)、`openai`、`aliyun`、`zhipu`、`volcengine`、`hunyuan`、`deepseek`、`minimax`、`mimo`、`siliconflow`、`jina`、`openrouter`、`gemini`
|
||||||
|
|
||||||
### 2. 执行 SQL 插入语句
|
### 2. 执行 SQL 插入语句
|
||||||
|
|
||||||
使用以下 SQL 语句插入内置模型:
|
使用以下 SQL 语句插入内置模型:
|
||||||
@@ -48,7 +50,7 @@ INSERT INTO models (
|
|||||||
'KnowledgeQA', -- 模型类型
|
'KnowledgeQA', -- 模型类型
|
||||||
'remote', -- 模型来源
|
'remote', -- 模型来源
|
||||||
'内置 LLM 模型', -- 描述
|
'内置 LLM 模型', -- 描述
|
||||||
'{"base_url": "https://api.openai.com/v1", "api_key": "sk-xxx"}'::jsonb, -- 参数(JSON格式)
|
'{"base_url": "https://api.openai.com/v1", "api_key": "sk-xxx", "provider": "openai"}'::jsonb, -- 参数(JSON格式)
|
||||||
false, -- 是否默认
|
false, -- 是否默认
|
||||||
'active', -- 状态
|
'active', -- 状态
|
||||||
true -- 标记为内置模型
|
true -- 标记为内置模型
|
||||||
@@ -73,7 +75,7 @@ INSERT INTO models (
|
|||||||
'Embedding',
|
'Embedding',
|
||||||
'remote',
|
'remote',
|
||||||
'内置 Embedding 模型',
|
'内置 Embedding 模型',
|
||||||
'{"base_url": "https://api.openai.com/v1", "api_key": "sk-xxx", "embedding_parameters": {"dimension": 1536, "truncate_prompt_tokens": 0}}'::jsonb,
|
'{"base_url": "https://api.openai.com/v1", "api_key": "sk-xxx", "provider": "openai", "embedding_parameters": {"dimension": 1536, "truncate_prompt_tokens": 0}}'::jsonb,
|
||||||
false,
|
false,
|
||||||
'active',
|
'active',
|
||||||
true
|
true
|
||||||
@@ -98,7 +100,7 @@ INSERT INTO models (
|
|||||||
'Rerank',
|
'Rerank',
|
||||||
'remote',
|
'remote',
|
||||||
'内置 ReRank 模型',
|
'内置 ReRank 模型',
|
||||||
'{"base_url": "https://api.example.com/v1", "api_key": "xxx"}'::jsonb,
|
'{"base_url": "https://api.jina.ai/v1", "api_key": "jina-xxx", "provider": "jina"}'::jsonb,
|
||||||
false,
|
false,
|
||||||
'active',
|
'active',
|
||||||
true
|
true
|
||||||
@@ -123,7 +125,7 @@ INSERT INTO models (
|
|||||||
'VLLM',
|
'VLLM',
|
||||||
'remote',
|
'remote',
|
||||||
'内置 VLLM 模型',
|
'内置 VLLM 模型',
|
||||||
'{"base_url": "https://api.openai.com/v1", "api_key": "sk-xxx"}'::jsonb,
|
'{"base_url": "https://dashscope.aliyuncs.com/compatible-mode/v1", "api_key": "sk-xxx", "provider": "aliyun"}'::jsonb,
|
||||||
false,
|
false,
|
||||||
'active',
|
'active',
|
||||||
true
|
true
|
||||||
|
|||||||
+293
-70
@@ -2,22 +2,100 @@
|
|||||||
|
|
||||||
[返回目录](./README.md)
|
[返回目录](./README.md)
|
||||||
|
|
||||||
| 方法 | 路径 | 描述 |
|
| 方法 | 路径 | 描述 |
|
||||||
| ------ | --------------------- | --------------------- |
|
| ------ | ----------------------- | --------------------- |
|
||||||
| POST | `/models` | 创建模型 |
|
| POST | `/models` | 创建模型 |
|
||||||
| GET | `/models` | 获取模型列表 |
|
| GET | `/models` | 获取模型列表 |
|
||||||
| GET | `/models/:id` | 获取模型详情 |
|
| GET | `/models/:id` | 获取模型详情 |
|
||||||
| PUT | `/models/:id` | 更新模型 |
|
| PUT | `/models/:id` | 更新模型 |
|
||||||
| DELETE | `/models/:id` | 删除模型 |
|
| DELETE | `/models/:id` | 删除模型 |
|
||||||
|
| GET | `/models/providers` | 获取模型服务商列表 |
|
||||||
|
|
||||||
|
## 服务商支持 (Provider Support)
|
||||||
|
|
||||||
|
WeKnora 支持多种主流 AI 模型服务商,在创建模型时可通过 `provider` 字段指定服务商类型以获得更好的兼容性。
|
||||||
|
|
||||||
|
### 支持的服务商列表
|
||||||
|
|
||||||
|
| 服务商标识 | 名称 | 支持的模型类型 |
|
||||||
|
| -------------- | ------------------ | ------------------------------- |
|
||||||
|
| `generic` | 自定义API OpenAI 兼容 | Chat, Embedding, Rerank, VLLM |
|
||||||
|
| `aliyun` | 阿里云 DashScope | Chat, Embedding, Rerank, VLLM |
|
||||||
|
| `zhipu` | 智谱 AI | Chat, Embedding, Rerank, VLLM |
|
||||||
|
| `volcengine` | 火山引擎 | Chat, Embedding, VLLM |
|
||||||
|
| `hunyuan` | 腾讯混元 | Chat, Embedding, VLLM |
|
||||||
|
| `deepseek` | DeepSeek | Chat |
|
||||||
|
| `minimax` | MiniMax | Chat, Embedding, VLLM |
|
||||||
|
| `mimo` | 小米 Mimo | Chat |
|
||||||
|
| `siliconflow` | 硅基流动 | Chat, Embedding, Rerank |
|
||||||
|
| `jina` | Jina AI | Embedding, Rerank |
|
||||||
|
| `openrouter` | OpenRouter | Chat |
|
||||||
|
| `openai` | OpenAI | Chat, Embedding, VLLM |
|
||||||
|
| `gemini` | Google Gemini | Chat, Embedding, VLLM |
|
||||||
|
|
||||||
|
## GET `/models/providers` - 获取模型服务商列表
|
||||||
|
|
||||||
|
根据模型类型获取支持的服务商列表及配置信息。
|
||||||
|
|
||||||
|
**请求参数**:
|
||||||
|
|
||||||
|
| 参数 | 类型 | 必填 | 描述 |
|
||||||
|
| ---------- | ------ | ---- | ---------------------------------------------- |
|
||||||
|
| model_type | string | 否 | 模型类型:`chat`, `embedding`, `rerank`, `vllm` |
|
||||||
|
|
||||||
|
**请求**:
|
||||||
|
|
||||||
|
```curl
|
||||||
|
# 获取所有服务商
|
||||||
|
curl --location 'http://localhost:8080/api/v1/models/providers' \
|
||||||
|
--header 'X-API-Key: your_api_key'
|
||||||
|
|
||||||
|
# 获取支持 Embedding 类型的服务商
|
||||||
|
curl --location 'http://localhost:8080/api/v1/models/providers?model_type=embedding' \
|
||||||
|
--header 'X-API-Key: your_api_key'
|
||||||
|
```
|
||||||
|
|
||||||
|
**响应**:
|
||||||
|
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
"success": true,
|
||||||
|
"data": [
|
||||||
|
{
|
||||||
|
"value": "aliyun",
|
||||||
|
"label": "阿里云 DashScope",
|
||||||
|
"description": "阿里云百炼大模型服务",
|
||||||
|
"defaultUrls": {
|
||||||
|
"chat": "https://dashscope.aliyuncs.com/compatible-mode/v1",
|
||||||
|
"embedding": "https://dashscope.aliyuncs.com/compatible-mode/v1",
|
||||||
|
"rerank": "https://dashscope.aliyuncs.com/api/v1/services/rerank/text-rerank/text-rerank"
|
||||||
|
},
|
||||||
|
"modelTypes": ["chat", "embedding", "rerank", "vllm"]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"value": "zhipu",
|
||||||
|
"label": "智谱 AI",
|
||||||
|
"description": "智谱清言大模型",
|
||||||
|
"defaultUrls": {
|
||||||
|
"chat": "https://open.bigmodel.cn/api/paas/v4",
|
||||||
|
"embedding": "https://open.bigmodel.cn/api/paas/v4/embeddings"
|
||||||
|
},
|
||||||
|
"modelTypes": ["chat", "embedding", "vllm"]
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
## POST `/models` - 创建模型
|
## POST `/models` - 创建模型
|
||||||
|
|
||||||
### 创建对话模型(KnowledgeQA)
|
### 创建对话模型(KnowledgeQA)
|
||||||
|
|
||||||
|
**本地 Ollama 模型**:
|
||||||
|
|
||||||
```curl
|
```curl
|
||||||
curl --location 'http://localhost:8080/api/v1/models' \
|
curl --location 'http://localhost:8080/api/v1/models' \
|
||||||
--header 'Content-Type: application/json' \
|
--header 'Content-Type: application/json' \
|
||||||
--header 'X-API-Key: sk-vQHV2NZI_LK5W7wHQvH3yGYExX8YnhaHwZipUYbiZKCYJbBQ' \
|
--header 'X-API-Key: your_api_key' \
|
||||||
--data '{
|
--data '{
|
||||||
"name": "qwen3:8b",
|
"name": "qwen3:8b",
|
||||||
"type": "KnowledgeQA",
|
"type": "KnowledgeQA",
|
||||||
@@ -26,17 +104,37 @@ curl --location 'http://localhost:8080/api/v1/models' \
|
|||||||
"parameters": {
|
"parameters": {
|
||||||
"base_url": "",
|
"base_url": "",
|
||||||
"api_key": ""
|
"api_key": ""
|
||||||
},
|
}
|
||||||
"is_default": false
|
}'
|
||||||
|
```
|
||||||
|
|
||||||
|
**远程 API 模型(指定服务商)**:
|
||||||
|
|
||||||
|
```curl
|
||||||
|
curl --location 'http://localhost:8080/api/v1/models' \
|
||||||
|
--header 'Content-Type: application/json' \
|
||||||
|
--header 'X-API-Key: your_api_key' \
|
||||||
|
--data '{
|
||||||
|
"name": "qwen-plus",
|
||||||
|
"type": "KnowledgeQA",
|
||||||
|
"source": "remote",
|
||||||
|
"description": "阿里云 Qwen 大模型",
|
||||||
|
"parameters": {
|
||||||
|
"base_url": "https://dashscope.aliyuncs.com/compatible-mode/v1",
|
||||||
|
"api_key": "sk-your-dashscope-api-key",
|
||||||
|
"provider": "aliyun"
|
||||||
|
}
|
||||||
}'
|
}'
|
||||||
```
|
```
|
||||||
|
|
||||||
### 创建嵌入模型(Embedding)
|
### 创建嵌入模型(Embedding)
|
||||||
|
|
||||||
|
**本地 Ollama 模型**:
|
||||||
|
|
||||||
```curl
|
```curl
|
||||||
curl --location 'http://localhost:8080/api/v1/models' \
|
curl --location 'http://localhost:8080/api/v1/models' \
|
||||||
--header 'Content-Type: application/json' \
|
--header 'Content-Type: application/json' \
|
||||||
--header 'X-API-Key: sk-vQHV2NZI_LK5W7wHQvH3yGYExX8YnhaHwZipUYbiZKCYJbBQ' \
|
--header 'X-API-Key: your_api_key' \
|
||||||
--data '{
|
--data '{
|
||||||
"name": "nomic-embed-text:latest",
|
"name": "nomic-embed-text:latest",
|
||||||
"type": "Embedding",
|
"type": "Embedding",
|
||||||
@@ -49,27 +147,112 @@ curl --location 'http://localhost:8080/api/v1/models' \
|
|||||||
"dimension": 768,
|
"dimension": 768,
|
||||||
"truncate_prompt_tokens": 0
|
"truncate_prompt_tokens": 0
|
||||||
}
|
}
|
||||||
},
|
}
|
||||||
"is_default": false
|
}'
|
||||||
|
```
|
||||||
|
|
||||||
|
**远程 API 模型(阿里云 DashScope)**:
|
||||||
|
|
||||||
|
```curl
|
||||||
|
curl --location 'http://localhost:8080/api/v1/models' \
|
||||||
|
--header 'Content-Type: application/json' \
|
||||||
|
--header 'X-API-Key: your_api_key' \
|
||||||
|
--data '{
|
||||||
|
"name": "text-embedding-v3",
|
||||||
|
"type": "Embedding",
|
||||||
|
"source": "remote",
|
||||||
|
"description": "阿里云通义千问 Embedding 模型",
|
||||||
|
"parameters": {
|
||||||
|
"base_url": "https://dashscope.aliyuncs.com/compatible-mode/v1",
|
||||||
|
"api_key": "sk-your-dashscope-api-key",
|
||||||
|
"provider": "aliyun",
|
||||||
|
"embedding_parameters": {
|
||||||
|
"dimension": 1024,
|
||||||
|
"truncate_prompt_tokens": 0
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}'
|
||||||
|
```
|
||||||
|
|
||||||
|
**远程 API 模型(Jina AI)**:
|
||||||
|
|
||||||
|
```curl
|
||||||
|
curl --location 'http://localhost:8080/api/v1/models' \
|
||||||
|
--header 'Content-Type: application/json' \
|
||||||
|
--header 'X-API-Key: your_api_key' \
|
||||||
|
--data '{
|
||||||
|
"name": "jina-embeddings-v3",
|
||||||
|
"type": "Embedding",
|
||||||
|
"source": "remote",
|
||||||
|
"description": "Jina AI Embedding 模型",
|
||||||
|
"parameters": {
|
||||||
|
"base_url": "https://api.jina.ai/v1",
|
||||||
|
"api_key": "jina_your_api_key",
|
||||||
|
"provider": "jina",
|
||||||
|
"embedding_parameters": {
|
||||||
|
"dimension": 1024,
|
||||||
|
"truncate_prompt_tokens": 0
|
||||||
|
}
|
||||||
|
}
|
||||||
}'
|
}'
|
||||||
```
|
```
|
||||||
|
|
||||||
### 创建排序模型(Rerank)
|
### 创建排序模型(Rerank)
|
||||||
|
|
||||||
|
**远程 API 模型(阿里云 DashScope)**:
|
||||||
|
|
||||||
```curl
|
```curl
|
||||||
curl --location 'http://localhost:8080/api/v1/models' \
|
curl --location 'http://localhost:8080/api/v1/models' \
|
||||||
--header 'Content-Type: application/json' \
|
--header 'Content-Type: application/json' \
|
||||||
--header 'X-API-Key: sk-vQHV2NZI_LK5W7wHQvH3yGYExX8YnhaHwZipUYbiZKCYJbBQ' \
|
--header 'X-API-Key: your_api_key' \
|
||||||
--data '{
|
--data '{
|
||||||
"name": "linux6200/bge-reranker-v2-m3:latest",
|
"name": "gte-rerank",
|
||||||
"type": "Rerank",
|
"type": "Rerank",
|
||||||
"source": "local",
|
"source": "remote",
|
||||||
"description": "Rerank Model for Knowledge QA",
|
"description": "阿里云 GTE Rerank 模型",
|
||||||
"parameters": {
|
"parameters": {
|
||||||
"base_url": "",
|
"base_url": "https://dashscope.aliyuncs.com/api/v1/services/rerank/text-rerank/text-rerank",
|
||||||
"api_key": ""
|
"api_key": "sk-your-dashscope-api-key",
|
||||||
},
|
"provider": "aliyun"
|
||||||
"is_default": false
|
}
|
||||||
|
}'
|
||||||
|
```
|
||||||
|
|
||||||
|
**远程 API 模型(Jina AI)**:
|
||||||
|
|
||||||
|
```curl
|
||||||
|
curl --location 'http://localhost:8080/api/v1/models' \
|
||||||
|
--header 'Content-Type: application/json' \
|
||||||
|
--header 'X-API-Key: your_api_key' \
|
||||||
|
--data '{
|
||||||
|
"name": "jina-reranker-v2-base-multilingual",
|
||||||
|
"type": "Rerank",
|
||||||
|
"source": "remote",
|
||||||
|
"description": "Jina AI Rerank 模型",
|
||||||
|
"parameters": {
|
||||||
|
"base_url": "https://api.jina.ai/v1",
|
||||||
|
"api_key": "jina_your_api_key",
|
||||||
|
"provider": "jina"
|
||||||
|
}
|
||||||
|
}'
|
||||||
|
```
|
||||||
|
|
||||||
|
### 创建视觉模型(VLLM)
|
||||||
|
|
||||||
|
```curl
|
||||||
|
curl --location 'http://localhost:8080/api/v1/models' \
|
||||||
|
--header 'Content-Type: application/json' \
|
||||||
|
--header 'X-API-Key: your_api_key' \
|
||||||
|
--data '{
|
||||||
|
"name": "qwen-vl-plus",
|
||||||
|
"type": "VLLM",
|
||||||
|
"source": "remote",
|
||||||
|
"description": "阿里云通义千问视觉模型",
|
||||||
|
"parameters": {
|
||||||
|
"base_url": "https://dashscope.aliyuncs.com/compatible-mode/v1",
|
||||||
|
"api_key": "sk-your-dashscope-api-key",
|
||||||
|
"provider": "aliyun"
|
||||||
|
}
|
||||||
}'
|
}'
|
||||||
```
|
```
|
||||||
|
|
||||||
@@ -77,28 +260,29 @@ curl --location 'http://localhost:8080/api/v1/models' \
|
|||||||
|
|
||||||
```json
|
```json
|
||||||
{
|
{
|
||||||
|
"success": true,
|
||||||
"data": {
|
"data": {
|
||||||
"id": "09c5a1d6-ee8b-4657-9a17-d3dcbd5c70cb",
|
"id": "09c5a1d6-ee8b-4657-9a17-d3dcbd5c70cb",
|
||||||
"tenant_id": 1,
|
"tenant_id": 1,
|
||||||
"name": "nomic-embed-text:latest3",
|
"name": "text-embedding-v3",
|
||||||
"type": "Embedding",
|
"type": "Embedding",
|
||||||
"source": "local",
|
"source": "remote",
|
||||||
"description": "Embedding Model",
|
"description": "阿里云通义千问 Embedding 模型",
|
||||||
"parameters": {
|
"parameters": {
|
||||||
"base_url": "",
|
"base_url": "https://dashscope.aliyuncs.com/compatible-mode/v1",
|
||||||
"api_key": "",
|
"api_key": "sk-***",
|
||||||
|
"provider": "aliyun",
|
||||||
"embedding_parameters": {
|
"embedding_parameters": {
|
||||||
"dimension": 768,
|
"dimension": 1024,
|
||||||
"truncate_prompt_tokens": 0
|
"truncate_prompt_tokens": 0
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"is_default": false,
|
"is_default": false,
|
||||||
"status": "downloading",
|
"status": "active",
|
||||||
"created_at": "2025-08-12T10:39:01.454591766+08:00",
|
"created_at": "2025-08-12T10:39:01.454591766+08:00",
|
||||||
"updated_at": "2025-08-12T10:39:01.454591766+08:00",
|
"updated_at": "2025-08-12T10:39:01.454591766+08:00",
|
||||||
"deleted_at": null
|
"deleted_at": null
|
||||||
},
|
}
|
||||||
"success": true
|
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
@@ -109,26 +293,28 @@ curl --location 'http://localhost:8080/api/v1/models' \
|
|||||||
```curl
|
```curl
|
||||||
curl --location 'http://localhost:8080/api/v1/models' \
|
curl --location 'http://localhost:8080/api/v1/models' \
|
||||||
--header 'Content-Type: application/json' \
|
--header 'Content-Type: application/json' \
|
||||||
--header 'X-API-Key: sk-vQHV2NZI_LK5W7wHQvH3yGYExX8YnhaHwZipUYbiZKCYJbBQ'
|
--header 'X-API-Key: your_api_key'
|
||||||
```
|
```
|
||||||
|
|
||||||
**响应**:
|
**响应**:
|
||||||
|
|
||||||
```json
|
```json
|
||||||
{
|
{
|
||||||
|
"success": true,
|
||||||
"data": [
|
"data": [
|
||||||
{
|
{
|
||||||
"id": "dff7bc94-7885-4dd1-bfd5-bd96e4df2fc3",
|
"id": "dff7bc94-7885-4dd1-bfd5-bd96e4df2fc3",
|
||||||
"tenant_id": 1,
|
"tenant_id": 1,
|
||||||
"name": "nomic-embed-text:latest",
|
"name": "text-embedding-v3",
|
||||||
"type": "Embedding",
|
"type": "Embedding",
|
||||||
"source": "local",
|
"source": "remote",
|
||||||
"description": "Embedding Model",
|
"description": "阿里云通义千问 Embedding 模型",
|
||||||
"parameters": {
|
"parameters": {
|
||||||
"base_url": "",
|
"base_url": "https://dashscope.aliyuncs.com/compatible-mode/v1",
|
||||||
"api_key": "",
|
"api_key": "sk-***",
|
||||||
|
"provider": "aliyun",
|
||||||
"embedding_parameters": {
|
"embedding_parameters": {
|
||||||
"dimension": 768,
|
"dimension": 1024,
|
||||||
"truncate_prompt_tokens": 0
|
"truncate_prompt_tokens": 0
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
@@ -141,13 +327,14 @@ curl --location 'http://localhost:8080/api/v1/models' \
|
|||||||
{
|
{
|
||||||
"id": "8aea788c-bb30-4898-809e-e40c14ffb48c",
|
"id": "8aea788c-bb30-4898-809e-e40c14ffb48c",
|
||||||
"tenant_id": 1,
|
"tenant_id": 1,
|
||||||
"name": "qwen3:8b",
|
"name": "qwen-plus",
|
||||||
"type": "KnowledgeQA",
|
"type": "KnowledgeQA",
|
||||||
"source": "local",
|
"source": "remote",
|
||||||
"description": "LLM Model for Knowledge QA",
|
"description": "阿里云 Qwen 大模型",
|
||||||
"parameters": {
|
"parameters": {
|
||||||
"base_url": "",
|
"base_url": "https://dashscope.aliyuncs.com/compatible-mode/v1",
|
||||||
"api_key": "",
|
"api_key": "sk-***",
|
||||||
|
"provider": "aliyun",
|
||||||
"embedding_parameters": {
|
"embedding_parameters": {
|
||||||
"dimension": 0,
|
"dimension": 0,
|
||||||
"truncate_prompt_tokens": 0
|
"truncate_prompt_tokens": 0
|
||||||
@@ -159,8 +346,7 @@ curl --location 'http://localhost:8080/api/v1/models' \
|
|||||||
"updated_at": "2025-08-11T20:10:41.825381+08:00",
|
"updated_at": "2025-08-11T20:10:41.825381+08:00",
|
||||||
"deleted_at": null
|
"deleted_at": null
|
||||||
}
|
}
|
||||||
],
|
]
|
||||||
"success": true
|
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
@@ -171,25 +357,27 @@ curl --location 'http://localhost:8080/api/v1/models' \
|
|||||||
```curl
|
```curl
|
||||||
curl --location 'http://localhost:8080/api/v1/models/dff7bc94-7885-4dd1-bfd5-bd96e4df2fc3' \
|
curl --location 'http://localhost:8080/api/v1/models/dff7bc94-7885-4dd1-bfd5-bd96e4df2fc3' \
|
||||||
--header 'Content-Type: application/json' \
|
--header 'Content-Type: application/json' \
|
||||||
--header 'X-API-Key: sk-vQHV2NZI_LK5W7wHQvH3yGYExX8YnhaHwZipUYbiZKCYJbBQ'
|
--header 'X-API-Key: your_api_key'
|
||||||
```
|
```
|
||||||
|
|
||||||
**响应**:
|
**响应**:
|
||||||
|
|
||||||
```json
|
```json
|
||||||
{
|
{
|
||||||
|
"success": true,
|
||||||
"data": {
|
"data": {
|
||||||
"id": "dff7bc94-7885-4dd1-bfd5-bd96e4df2fc3",
|
"id": "dff7bc94-7885-4dd1-bfd5-bd96e4df2fc3",
|
||||||
"tenant_id": 1,
|
"tenant_id": 1,
|
||||||
"name": "nomic-embed-text:latest",
|
"name": "text-embedding-v3",
|
||||||
"type": "Embedding",
|
"type": "Embedding",
|
||||||
"source": "local",
|
"source": "remote",
|
||||||
"description": "Embedding Model",
|
"description": "阿里云通义千问 Embedding 模型",
|
||||||
"parameters": {
|
"parameters": {
|
||||||
"base_url": "",
|
"base_url": "https://dashscope.aliyuncs.com/compatible-mode/v1",
|
||||||
"api_key": "",
|
"api_key": "sk-***",
|
||||||
|
"provider": "aliyun",
|
||||||
"embedding_parameters": {
|
"embedding_parameters": {
|
||||||
"dimension": 768,
|
"dimension": 1024,
|
||||||
"truncate_prompt_tokens": 0
|
"truncate_prompt_tokens": 0
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
@@ -198,8 +386,7 @@ curl --location 'http://localhost:8080/api/v1/models/dff7bc94-7885-4dd1-bfd5-bd9
|
|||||||
"created_at": "2025-08-11T20:10:41.813832+08:00",
|
"created_at": "2025-08-11T20:10:41.813832+08:00",
|
||||||
"updated_at": "2025-08-11T20:10:41.822354+08:00",
|
"updated_at": "2025-08-11T20:10:41.822354+08:00",
|
||||||
"deleted_at": null
|
"deleted_at": null
|
||||||
},
|
}
|
||||||
"success": true
|
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
@@ -210,15 +397,15 @@ curl --location 'http://localhost:8080/api/v1/models/dff7bc94-7885-4dd1-bfd5-bd9
|
|||||||
```curl
|
```curl
|
||||||
curl --location --request PUT 'http://localhost:8080/api/v1/models/8fdc464d-8eaa-44d4-a85b-094b28af5330' \
|
curl --location --request PUT 'http://localhost:8080/api/v1/models/8fdc464d-8eaa-44d4-a85b-094b28af5330' \
|
||||||
--header 'Content-Type: application/json' \
|
--header 'Content-Type: application/json' \
|
||||||
--header 'X-API-Key: sk-vQHV2NZI_LK5W7wHQvH3yGYExX8YnhaHwZipUYbiZKCYJbBQ' \
|
--header 'X-API-Key: your_api_key' \
|
||||||
--data '{
|
--data '{
|
||||||
"name": "linux6200/bge-reranker-v2-m3:latest",
|
"name": "gte-rerank-v2",
|
||||||
"description": "Rerank Model for Knowledge QA new",
|
"description": "阿里云 GTE Rerank 模型 V2",
|
||||||
"parameters": {
|
"parameters": {
|
||||||
"base_url": "",
|
"base_url": "https://dashscope.aliyuncs.com/api/v1/services/rerank/text-rerank/text-rerank",
|
||||||
"api_key": ""
|
"api_key": "sk-your-new-api-key",
|
||||||
},
|
"provider": "aliyun"
|
||||||
"is_default": false
|
}
|
||||||
}'
|
}'
|
||||||
```
|
```
|
||||||
|
|
||||||
@@ -226,16 +413,18 @@ curl --location --request PUT 'http://localhost:8080/api/v1/models/8fdc464d-8eaa
|
|||||||
|
|
||||||
```json
|
```json
|
||||||
{
|
{
|
||||||
|
"success": true,
|
||||||
"data": {
|
"data": {
|
||||||
"id": "8fdc464d-8eaa-44d4-a85b-094b28af5330",
|
"id": "8fdc464d-8eaa-44d4-a85b-094b28af5330",
|
||||||
"tenant_id": 1,
|
"tenant_id": 1,
|
||||||
"name": "linux6200/bge-reranker-v2-m3:latest",
|
"name": "gte-rerank-v2",
|
||||||
"type": "Rerank",
|
"type": "Rerank",
|
||||||
"source": "local",
|
"source": "remote",
|
||||||
"description": "Rerank Model for Knowledge QA new",
|
"description": "阿里云 GTE Rerank 模型 V2",
|
||||||
"parameters": {
|
"parameters": {
|
||||||
"base_url": "",
|
"base_url": "https://dashscope.aliyuncs.com/api/v1/services/rerank/text-rerank/text-rerank",
|
||||||
"api_key": "",
|
"api_key": "sk-***",
|
||||||
|
"provider": "aliyun",
|
||||||
"embedding_parameters": {
|
"embedding_parameters": {
|
||||||
"dimension": 0,
|
"dimension": 0,
|
||||||
"truncate_prompt_tokens": 0
|
"truncate_prompt_tokens": 0
|
||||||
@@ -246,8 +435,7 @@ curl --location --request PUT 'http://localhost:8080/api/v1/models/8fdc464d-8eaa
|
|||||||
"created_at": "2025-08-12T10:57:39.512681+08:00",
|
"created_at": "2025-08-12T10:57:39.512681+08:00",
|
||||||
"updated_at": "2025-08-12T11:00:27.271678+08:00",
|
"updated_at": "2025-08-12T11:00:27.271678+08:00",
|
||||||
"deleted_at": null
|
"deleted_at": null
|
||||||
},
|
}
|
||||||
"success": true
|
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
@@ -258,14 +446,49 @@ curl --location --request PUT 'http://localhost:8080/api/v1/models/8fdc464d-8eaa
|
|||||||
```curl
|
```curl
|
||||||
curl --location --request DELETE 'http://localhost:8080/api/v1/models/8fdc464d-8eaa-44d4-a85b-094b28af5330' \
|
curl --location --request DELETE 'http://localhost:8080/api/v1/models/8fdc464d-8eaa-44d4-a85b-094b28af5330' \
|
||||||
--header 'Content-Type: application/json' \
|
--header 'Content-Type: application/json' \
|
||||||
--header 'X-API-Key: sk-vQHV2NZI_LK5W7wHQvH3yGYExX8YnhaHwZipUYbiZKCYJbBQ'
|
--header 'X-API-Key: your_api_key'
|
||||||
```
|
```
|
||||||
|
|
||||||
**响应**:
|
**响应**:
|
||||||
|
|
||||||
```json
|
```json
|
||||||
{
|
{
|
||||||
"message": "Model deleted",
|
"success": true,
|
||||||
"success": true
|
"message": "Model deleted"
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
## 参数说明
|
||||||
|
|
||||||
|
### ModelType (模型类型)
|
||||||
|
|
||||||
|
| 值 | 说明 | 用途 |
|
||||||
|
| ------------ | ------------ | ------------------------------ |
|
||||||
|
| KnowledgeQA | 对话模型 | 知识库问答、对话生成 |
|
||||||
|
| Embedding | 嵌入模型 | 文本向量化、知识库检索 |
|
||||||
|
| Rerank | 排序模型 | 检索结果重排序、相关性优化 |
|
||||||
|
| VLLM | 视觉语言模型 | 多模态分析、图文理解 |
|
||||||
|
|
||||||
|
### ModelSource (模型来源)
|
||||||
|
|
||||||
|
| 值 | 说明 | 配置要求 |
|
||||||
|
| -------- | ---------- | ------------------------------ |
|
||||||
|
| local | 本地模型 | 需要已安装 Ollama 并拉取模型 |
|
||||||
|
| remote | 远程 API | 需要提供 `base_url` 和 `api_key` |
|
||||||
|
|
||||||
|
### Parameters (模型参数)
|
||||||
|
|
||||||
|
| 字段 | 类型 | 说明 |
|
||||||
|
| -------------------- | ------ | -------------------------------------------- |
|
||||||
|
| base_url | string | API 服务地址(远程模型必填) |
|
||||||
|
| api_key | string | API 密钥(远程模型必填) |
|
||||||
|
| provider | string | 服务商标识(可选,用于选择特定的 API 适配器)|
|
||||||
|
| embedding_parameters | object | Embedding 模型专用参数 |
|
||||||
|
| extra_config | object | 服务商特定的额外配置 |
|
||||||
|
|
||||||
|
### EmbeddingParameters (嵌入参数)
|
||||||
|
|
||||||
|
| 字段 | 类型 | 说明 |
|
||||||
|
| ---------------------- | ---- | -------------------------- |
|
||||||
|
| dimension | int | 向量维度(如:768, 1024) |
|
||||||
|
| truncate_prompt_tokens | int | 截断 Token 数(0 表示不截断)|
|
||||||
|
|||||||
@@ -278,6 +278,7 @@ export function testEmbeddingModel(modelConfig: {
|
|||||||
baseUrl?: string;
|
baseUrl?: string;
|
||||||
apiKey?: string;
|
apiKey?: string;
|
||||||
dimension?: number;
|
dimension?: number;
|
||||||
|
provider?: string;
|
||||||
}): Promise<{ available: boolean; message?: string; dimension?: number }> {
|
}): Promise<{ available: boolean; message?: string; dimension?: number }> {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
post('/api/v1/initialization/embedding/test', modelConfig)
|
post('/api/v1/initialization/embedding/test', modelConfig)
|
||||||
@@ -318,7 +319,7 @@ export function testMultimodalFunction(testData: {
|
|||||||
vlm_base_url: string;
|
vlm_base_url: string;
|
||||||
vlm_api_key?: string;
|
vlm_api_key?: string;
|
||||||
vlm_interface_type?: string;
|
vlm_interface_type?: string;
|
||||||
storage_type?: 'cos'|'minio';
|
storage_type?: 'cos' | 'minio';
|
||||||
// COS optional fields (required only when storage_type === 'cos')
|
// COS optional fields (required only when storage_type === 'cos')
|
||||||
cos_secret_id?: string;
|
cos_secret_id?: string;
|
||||||
cos_secret_key?: string;
|
cos_secret_key?: string;
|
||||||
@@ -397,18 +398,18 @@ export function testMultimodalFunction(testData: {
|
|||||||
headers,
|
headers,
|
||||||
body: formData
|
body: formData
|
||||||
})
|
})
|
||||||
.then(response => response.json())
|
.then(response => response.json())
|
||||||
.then((data: any) => {
|
.then((data: any) => {
|
||||||
if (data.success) {
|
if (data.success) {
|
||||||
resolve(data.data || {});
|
resolve(data.data || {});
|
||||||
} else {
|
} else {
|
||||||
resolve({ success: false, message: data.message || '测试失败' });
|
resolve({ success: false, message: data.message || '测试失败' });
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.catch((error: any) => {
|
.catch((error: any) => {
|
||||||
console.error('多模态测试失败:', error);
|
console.error('多模态测试失败:', error);
|
||||||
reject(error);
|
reject(error);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -487,7 +488,7 @@ export interface FabriTagResponse {
|
|||||||
tags: string[];
|
tags: string[];
|
||||||
}
|
}
|
||||||
|
|
||||||
// 文本内容生成
|
// 标签生成
|
||||||
export function fabriTag(request: FabriTagRequest): Promise<FabriTagResponse> {
|
export function fabriTag(request: FabriTagRequest): Promise<FabriTagResponse> {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
post('/api/v1/initialization/extract/fabri-tag', request)
|
post('/api/v1/initialization/extract/fabri-tag', request)
|
||||||
@@ -500,3 +501,29 @@ export function fabriTag(request: FabriTagRequest): Promise<FabriTagResponse> {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 模型厂商信息类型
|
||||||
|
export interface ModelProviderOption {
|
||||||
|
value: string; // provider 标识符
|
||||||
|
label: string; // 显示名称
|
||||||
|
description: string; // 描述
|
||||||
|
defaultUrls: Record<string, string>; // 按模型类型区分的默认 URL
|
||||||
|
modelTypes: string[]; // 支持的模型类型
|
||||||
|
}
|
||||||
|
|
||||||
|
// 获取模型厂商列表
|
||||||
|
export function listModelProviders(modelType?: string): Promise<ModelProviderOption[]> {
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
const url = modelType
|
||||||
|
? `/api/v1/models/providers?model_type=${encodeURIComponent(modelType)}`
|
||||||
|
: '/api/v1/models/providers';
|
||||||
|
get(url)
|
||||||
|
.then((response: any) => {
|
||||||
|
resolve(response.data || []);
|
||||||
|
})
|
||||||
|
.catch((error: any) => {
|
||||||
|
console.error('获取模型厂商列表失败:', error);
|
||||||
|
resolve([]); // 失败时返回空数组,前端可以回退到默认值
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
@@ -11,12 +11,14 @@ export interface ModelConfig {
|
|||||||
parameters: {
|
parameters: {
|
||||||
base_url?: string;
|
base_url?: string;
|
||||||
api_key?: string;
|
api_key?: string;
|
||||||
|
provider?: string; // Provider identifier: openai, aliyun, zhipu, generic
|
||||||
embedding_parameters?: {
|
embedding_parameters?: {
|
||||||
dimension?: number;
|
dimension?: number;
|
||||||
truncate_prompt_tokens?: number;
|
truncate_prompt_tokens?: number;
|
||||||
};
|
};
|
||||||
interface_type?: 'ollama' | 'openai'; // VLLM专用
|
interface_type?: 'ollama' | 'openai'; // VLLM专用
|
||||||
parameter_size?: string; // Ollama模型参数大小 (e.g., "7B", "13B", "70B")
|
parameter_size?: string; // Ollama模型参数大小 (e.g., "7B", "13B", "70B")
|
||||||
|
extra_config?: Record<string, string>; // Provider-specific configuration
|
||||||
};
|
};
|
||||||
is_default?: boolean;
|
is_default?: boolean;
|
||||||
is_builtin?: boolean;
|
is_builtin?: boolean;
|
||||||
|
|||||||
@@ -119,17 +119,39 @@
|
|||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<!-- Remote API 和 VLLM 保持原有的 input -->
|
|
||||||
<div v-else class="form-item">
|
|
||||||
<label class="form-label required">{{ $t('model.modelName') }}</label>
|
|
||||||
<t-input
|
|
||||||
v-model="formData.modelName"
|
|
||||||
:placeholder="getModelNamePlaceholder()"
|
|
||||||
/>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<!-- Remote API 配置 -->
|
<!-- Remote API 配置 -->
|
||||||
<template v-if="formData.source === 'remote'">
|
<template v-if="formData.source === 'remote'">
|
||||||
|
<!-- 厂商选择器 -->
|
||||||
|
<div class="form-item">
|
||||||
|
<label class="form-label">{{ $t('model.editor.providerLabel') }}</label>
|
||||||
|
<t-select
|
||||||
|
v-model="formData.provider"
|
||||||
|
:placeholder="$t('model.editor.providerPlaceholder')"
|
||||||
|
@change="handleProviderChange"
|
||||||
|
>
|
||||||
|
<t-option
|
||||||
|
v-for="opt in providerOptions"
|
||||||
|
:key="opt.value"
|
||||||
|
:value="opt.value"
|
||||||
|
:label="opt.label"
|
||||||
|
>
|
||||||
|
<div class="provider-option">
|
||||||
|
<span class="provider-name">{{ opt.label }}</span>
|
||||||
|
<span class="provider-desc">{{ opt.description }}</span>
|
||||||
|
</div>
|
||||||
|
</t-option>
|
||||||
|
</t-select>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<!-- 模型名称 -->
|
||||||
|
<div class="form-item">
|
||||||
|
<label class="form-label required">{{ $t('model.modelName') }}</label>
|
||||||
|
<t-input
|
||||||
|
v-model="formData.modelName"
|
||||||
|
:placeholder="getModelNamePlaceholder()"
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
|
||||||
<div class="form-item">
|
<div class="form-item">
|
||||||
<label class="form-label required">{{ $t('model.editor.baseUrlLabel') }}</label>
|
<label class="form-label required">{{ $t('model.editor.baseUrlLabel') }}</label>
|
||||||
<t-input
|
<t-input
|
||||||
@@ -229,7 +251,7 @@
|
|||||||
<script setup lang="ts">
|
<script setup lang="ts">
|
||||||
import { ref, watch, computed, onUnmounted, nextTick } from 'vue'
|
import { ref, watch, computed, onUnmounted, nextTick } from 'vue'
|
||||||
import { MessagePlugin } from 'tdesign-vue-next'
|
import { MessagePlugin } from 'tdesign-vue-next'
|
||||||
import { checkOllamaModels, checkRemoteModel, testEmbeddingModel, checkRerankModel, listOllamaModels, downloadOllamaModel, getDownloadProgress, checkOllamaStatus, type OllamaModelInfo } from '@/api/initialization'
|
import { checkOllamaModels, checkRemoteModel, testEmbeddingModel, checkRerankModel, listOllamaModels, downloadOllamaModel, getDownloadProgress, checkOllamaStatus, listModelProviders, type OllamaModelInfo, type ModelProviderOption } from '@/api/initialization'
|
||||||
import { useI18n } from 'vue-i18n'
|
import { useI18n } from 'vue-i18n'
|
||||||
import { useUIStore } from '@/stores/ui'
|
import { useUIStore } from '@/stores/ui'
|
||||||
|
|
||||||
@@ -237,6 +259,7 @@ interface ModelFormData {
|
|||||||
id: string
|
id: string
|
||||||
name: string
|
name: string
|
||||||
source: 'local' | 'remote'
|
source: 'local' | 'remote'
|
||||||
|
provider?: string // Provider identifier: openai, aliyun, zhipu, generic, etc.
|
||||||
modelName: string
|
modelName: string
|
||||||
baseUrl?: string
|
baseUrl?: string
|
||||||
apiKey?: string
|
apiKey?: string
|
||||||
@@ -264,6 +287,111 @@ const emit = defineEmits<{
|
|||||||
'confirm': [data: ModelFormData]
|
'confirm': [data: ModelFormData]
|
||||||
}>()
|
}>()
|
||||||
|
|
||||||
|
// API 返回的 Provider 列表
|
||||||
|
const apiProviderOptions = ref<ModelProviderOption[]>([])
|
||||||
|
const loadingProviders = ref(false)
|
||||||
|
|
||||||
|
// 硬编码的后备 Provider 配置 (当 API 不可用时使用)
|
||||||
|
const fallbackProviderOptions = computed(() => [
|
||||||
|
{
|
||||||
|
value: 'openai',
|
||||||
|
label: t('model.editor.providers.openai.label'),
|
||||||
|
defaultUrls: {
|
||||||
|
chat: 'https://api.openai.com/v1',
|
||||||
|
embedding: 'https://api.openai.com/v1',
|
||||||
|
rerank: 'https://api.openai.com/v1',
|
||||||
|
vllm: 'https://api.openai.com/v1'
|
||||||
|
},
|
||||||
|
description: t('model.editor.providers.openai.description'),
|
||||||
|
modelTypes: ['chat', 'embedding', 'vllm']
|
||||||
|
},
|
||||||
|
{
|
||||||
|
value: 'aliyun',
|
||||||
|
label: t('model.editor.providers.aliyun.label'),
|
||||||
|
defaultUrls: {
|
||||||
|
chat: 'https://dashscope.aliyuncs.com/compatible-mode/v1',
|
||||||
|
embedding: 'https://dashscope.aliyuncs.com/compatible-mode/v1',
|
||||||
|
rerank: 'https://dashscope.aliyuncs.com/api/v1/services/rerank/text-rerank/text-rerank',
|
||||||
|
vllm: 'https://dashscope.aliyuncs.com/compatible-mode/v1'
|
||||||
|
},
|
||||||
|
description: t('model.editor.providers.aliyun.description'),
|
||||||
|
modelTypes: ['chat', 'embedding', 'rerank', 'vllm']
|
||||||
|
},
|
||||||
|
{
|
||||||
|
value: 'zhipu',
|
||||||
|
label: t('model.editor.providers.zhipu.label'),
|
||||||
|
defaultUrls: {
|
||||||
|
chat: 'https://open.bigmodel.cn/api/paas/v4',
|
||||||
|
embedding: 'https://open.bigmodel.cn/api/paas/v4/embeddings',
|
||||||
|
vllm: 'https://open.bigmodel.cn/api/paas/v4'
|
||||||
|
},
|
||||||
|
description: t('model.editor.providers.zhipu.description'),
|
||||||
|
modelTypes: ['chat', 'embedding', 'vllm']
|
||||||
|
},
|
||||||
|
{
|
||||||
|
value: 'openrouter',
|
||||||
|
label: t('model.editor.providers.openrouter.label'),
|
||||||
|
defaultUrls: { chat: 'https://openrouter.ai/api/v1' },
|
||||||
|
description: t('model.editor.providers.openrouter.description'),
|
||||||
|
modelTypes: ['chat']
|
||||||
|
},
|
||||||
|
{
|
||||||
|
value: 'siliconflow',
|
||||||
|
label: t('model.editor.providers.siliconflow.label'),
|
||||||
|
defaultUrls: {
|
||||||
|
chat: 'https://api.siliconflow.cn/v1',
|
||||||
|
embedding: 'https://api.siliconflow.cn/v1',
|
||||||
|
rerank: 'https://api.siliconflow.cn/v1'
|
||||||
|
},
|
||||||
|
description: t('model.editor.providers.siliconflow.description'),
|
||||||
|
modelTypes: ['chat', 'embedding', 'rerank']
|
||||||
|
},
|
||||||
|
{
|
||||||
|
value: 'jina',
|
||||||
|
label: t('model.editor.providers.jina.label'),
|
||||||
|
defaultUrls: {
|
||||||
|
embedding: 'https://api.jina.ai/v1',
|
||||||
|
rerank: 'https://api.jina.ai/v1'
|
||||||
|
},
|
||||||
|
description: t('model.editor.providers.jina.description'),
|
||||||
|
modelTypes: ['embedding', 'rerank']
|
||||||
|
},
|
||||||
|
{
|
||||||
|
value: 'generic',
|
||||||
|
label: t('model.editor.providers.generic.label'),
|
||||||
|
defaultUrls: {},
|
||||||
|
description: t('model.editor.providers.generic.description'),
|
||||||
|
modelTypes: ['chat', 'embedding', 'rerank', 'vllm']
|
||||||
|
},
|
||||||
|
])
|
||||||
|
|
||||||
|
// 从 API 获取 Provider 列表
|
||||||
|
const loadProviders = async () => {
|
||||||
|
loadingProviders.value = true
|
||||||
|
try {
|
||||||
|
const providers = await listModelProviders(props.modelType)
|
||||||
|
if (providers.length > 0) {
|
||||||
|
apiProviderOptions.value = providers
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
console.error('Failed to load providers from API, using fallback', error)
|
||||||
|
} finally {
|
||||||
|
loadingProviders.value = false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 根据当前模型类型过滤的 Provider 列表
|
||||||
|
const providerOptions = computed(() => {
|
||||||
|
// 优先使用 API 返回的数据
|
||||||
|
if (apiProviderOptions.value.length > 0) {
|
||||||
|
return apiProviderOptions.value
|
||||||
|
}
|
||||||
|
// 回退到硬编码值,按 modelTypes 过滤
|
||||||
|
return fallbackProviderOptions.value.filter(p =>
|
||||||
|
p.modelTypes.includes(props.modelType)
|
||||||
|
)
|
||||||
|
})
|
||||||
|
|
||||||
const dialogVisible = computed({
|
const dialogVisible = computed({
|
||||||
get: () => props.visible,
|
get: () => props.visible,
|
||||||
set: (val) => emit('update:visible', val)
|
set: (val) => emit('update:visible', val)
|
||||||
@@ -300,6 +428,7 @@ const formData = ref<ModelFormData>({
|
|||||||
id: '',
|
id: '',
|
||||||
name: '',
|
name: '',
|
||||||
source: 'local',
|
source: 'local',
|
||||||
|
provider: 'openai',
|
||||||
modelName: '',
|
modelName: '',
|
||||||
baseUrl: '',
|
baseUrl: '',
|
||||||
apiKey: '',
|
apiKey: '',
|
||||||
@@ -416,6 +545,9 @@ watch(() => props.visible, (val) => {
|
|||||||
// 检查Ollama服务状态
|
// 检查Ollama服务状态
|
||||||
checkOllamaServiceStatus()
|
checkOllamaServiceStatus()
|
||||||
|
|
||||||
|
// 从 API 加载 Model Provider 列表
|
||||||
|
loadProviders()
|
||||||
|
|
||||||
if (props.modelData) {
|
if (props.modelData) {
|
||||||
formData.value = { ...props.modelData }
|
formData.value = { ...props.modelData }
|
||||||
} else {
|
} else {
|
||||||
@@ -438,6 +570,7 @@ const resetForm = () => {
|
|||||||
id: generateId(),
|
id: generateId(),
|
||||||
name: '', // 保留字段但不使用,保存时用 modelName
|
name: '', // 保留字段但不使用,保存时用 modelName
|
||||||
source: 'local',
|
source: 'local',
|
||||||
|
provider: 'generic',
|
||||||
modelName: '',
|
modelName: '',
|
||||||
baseUrl: '',
|
baseUrl: '',
|
||||||
apiKey: '',
|
apiKey: '',
|
||||||
@@ -455,6 +588,22 @@ const resetForm = () => {
|
|||||||
dimensionMessage.value = ''
|
dimensionMessage.value = ''
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 处理厂商选择变化 (自动填充默认 URL)
|
||||||
|
const handleProviderChange = (value: string) => {
|
||||||
|
const provider = providerOptions.value.find(opt => opt.value === value)
|
||||||
|
if (provider && provider.defaultUrls) {
|
||||||
|
// 根据当前模型类型获取对应的默认 URL
|
||||||
|
const defaultUrl = provider.defaultUrls[props.modelType]
|
||||||
|
if (defaultUrl) {
|
||||||
|
formData.value.baseUrl = defaultUrl
|
||||||
|
}
|
||||||
|
// 重置校验状态
|
||||||
|
remoteChecked.value = false
|
||||||
|
remoteAvailable.value = false
|
||||||
|
remoteMessage.value = ''
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// 监听来源变化,重置校验状态(已合并到下面的 watch)
|
// 监听来源变化,重置校验状态(已合并到下面的 watch)
|
||||||
|
|
||||||
// 生成唯一ID
|
// 生成唯一ID
|
||||||
@@ -613,7 +762,8 @@ const checkRemoteAPI = async () => {
|
|||||||
modelName: formData.value.modelName,
|
modelName: formData.value.modelName,
|
||||||
baseUrl: formData.value.baseUrl,
|
baseUrl: formData.value.baseUrl,
|
||||||
apiKey: formData.value.apiKey || '',
|
apiKey: formData.value.apiKey || '',
|
||||||
dimension: formData.value.dimension
|
dimension: formData.value.dimension,
|
||||||
|
provider: formData.value.provider
|
||||||
})
|
})
|
||||||
// 如果测试成功且返回了维度,自动填充
|
// 如果测试成功且返回了维度,自动填充
|
||||||
if (result.available && result.dimension) {
|
if (result.available && result.dimension) {
|
||||||
@@ -1013,6 +1163,25 @@ const handleCancel = () => {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 厂商选择器样式
|
||||||
|
.provider-option {
|
||||||
|
display: flex;
|
||||||
|
flex-direction: column;
|
||||||
|
gap: 2px;
|
||||||
|
padding: 4px 0;
|
||||||
|
|
||||||
|
.provider-name {
|
||||||
|
font-size: 14px;
|
||||||
|
font-weight: 500;
|
||||||
|
color: #333333;
|
||||||
|
}
|
||||||
|
|
||||||
|
.provider-desc {
|
||||||
|
font-size: 12px;
|
||||||
|
color: #999999;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// 单选按钮组
|
// 单选按钮组
|
||||||
:deep(.t-radio-group) {
|
:deep(.t-radio-group) {
|
||||||
display: flex;
|
display: flex;
|
||||||
|
|||||||
@@ -1422,7 +1422,64 @@ export default {
|
|||||||
baseUrlRequired: 'Please enter the Base URL',
|
baseUrlRequired: 'Please enter the Base URL',
|
||||||
baseUrlEmpty: 'Base URL cannot be empty',
|
baseUrlEmpty: 'Base URL cannot be empty',
|
||||||
baseUrlInvalid: 'Invalid Base URL, please enter a valid URL'
|
baseUrlInvalid: 'Invalid Base URL, please enter a valid URL'
|
||||||
}
|
},
|
||||||
|
// Provider related translations
|
||||||
|
providerLabel: 'Provider',
|
||||||
|
providerPlaceholder: 'Select model provider',
|
||||||
|
providers: {
|
||||||
|
openai: {
|
||||||
|
label: 'OpenAI',
|
||||||
|
description: 'gpt-5.2, gpt-5-mini, etc.',
|
||||||
|
},
|
||||||
|
aliyun: {
|
||||||
|
label: 'Aliyun DashScope',
|
||||||
|
description: 'qwen-plus, tongyi-embedding-vision-plus, qwen3-rerank, etc.',
|
||||||
|
},
|
||||||
|
zhipu: {
|
||||||
|
label: 'Zhipu AI',
|
||||||
|
description: 'glm-4.7, embedding-3, rerank, etc.',
|
||||||
|
},
|
||||||
|
openrouter: {
|
||||||
|
label: 'OpenRouter',
|
||||||
|
description: 'openai/gpt-5.2-chat, google/gemini-3-flash-preview, etc.',
|
||||||
|
},
|
||||||
|
generic: {
|
||||||
|
label: 'Custom API',
|
||||||
|
description: 'Generic API endpoint',
|
||||||
|
},
|
||||||
|
siliconflow: {
|
||||||
|
label: 'SiliconFlow',
|
||||||
|
description: 'deepseek-ai/DeepSeek-V3.1, etc.',
|
||||||
|
},
|
||||||
|
jina: {
|
||||||
|
label: 'Jina AI',
|
||||||
|
description: 'jina-clip-v1, jina-embeddings-v2-base-zh, etc.',
|
||||||
|
},
|
||||||
|
volcengine: {
|
||||||
|
label: 'Volcengine',
|
||||||
|
description: 'doubao-1-5-pro-32k-250115, doubao-embedding-vision-250615, etc.',
|
||||||
|
},
|
||||||
|
deepseek: {
|
||||||
|
label: 'DeepSeek',
|
||||||
|
description: 'deepseek-chat, deepseek-reasoner, etc.',
|
||||||
|
},
|
||||||
|
hunyuan: {
|
||||||
|
label: 'Hunyuan',
|
||||||
|
description: 'hunyuan-pro, hunyuan-standard, hunyuan-embedding, etc.',
|
||||||
|
},
|
||||||
|
minimax: {
|
||||||
|
label: 'MiniMax',
|
||||||
|
description: 'MiniMax-M2.1, MiniMax-M2.1-lightning, etc.',
|
||||||
|
},
|
||||||
|
mimo: {
|
||||||
|
label: 'MiMo',
|
||||||
|
description: 'mimo-v2-flash',
|
||||||
|
},
|
||||||
|
gemini: {
|
||||||
|
label: 'Google Gemini',
|
||||||
|
description: 'gemini-3-flash-preview, gemini-2.5-pro, etc.',
|
||||||
|
},
|
||||||
|
},
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
language: {
|
language: {
|
||||||
|
|||||||
@@ -889,7 +889,64 @@ export default {
|
|||||||
baseUrlRequired: 'Введите Base URL',
|
baseUrlRequired: 'Введите Base URL',
|
||||||
baseUrlEmpty: 'Base URL не может быть пустым',
|
baseUrlEmpty: 'Base URL не может быть пустым',
|
||||||
baseUrlInvalid: 'Недопустимый Base URL, введите корректный адрес'
|
baseUrlInvalid: 'Недопустимый Base URL, введите корректный адрес'
|
||||||
}
|
},
|
||||||
|
// Переводы для провайдеров
|
||||||
|
providerLabel: 'Провайдер',
|
||||||
|
providerPlaceholder: 'Выберите провайдера модели',
|
||||||
|
providers: {
|
||||||
|
openai: {
|
||||||
|
label: 'OpenAI',
|
||||||
|
description: 'gpt-5.2, gpt-5-mini, etc.',
|
||||||
|
},
|
||||||
|
aliyun: {
|
||||||
|
label: 'Aliyun DashScope',
|
||||||
|
description: 'qwen-plus, tongyi-embedding-vision-plus, qwen3-rerank, etc.',
|
||||||
|
},
|
||||||
|
zhipu: {
|
||||||
|
label: 'Zhipu AI',
|
||||||
|
description: 'glm-4.7, embedding-3, rerank, etc.',
|
||||||
|
},
|
||||||
|
openrouter: {
|
||||||
|
label: 'OpenRouter',
|
||||||
|
description: 'openai/gpt-5.2-chat, google/gemini-3-flash-preview, etc.',
|
||||||
|
},
|
||||||
|
generic: {
|
||||||
|
label: 'Пользовательский API',
|
||||||
|
description: 'Generic API endpoint',
|
||||||
|
},
|
||||||
|
siliconflow: {
|
||||||
|
label: 'SiliconFlow',
|
||||||
|
description: 'deepseek-ai/DeepSeek-V3.1, etc.',
|
||||||
|
},
|
||||||
|
jina: {
|
||||||
|
label: 'Jina AI',
|
||||||
|
description: 'jina-clip-v1, jina-embeddings-v2-base-zh, etc.',
|
||||||
|
},
|
||||||
|
volcengine: {
|
||||||
|
label: 'Volcengine',
|
||||||
|
description: 'doubao-1-5-pro-32k-250115, doubao-embedding-vision-250615, etc.',
|
||||||
|
},
|
||||||
|
deepseek: {
|
||||||
|
label: 'DeepSeek',
|
||||||
|
description: 'deepseek-chat, deepseek-reasoner, etc.',
|
||||||
|
},
|
||||||
|
hunyuan: {
|
||||||
|
label: 'Hunyuan',
|
||||||
|
description: 'hunyuan-pro, hunyuan-standard, hunyuan-embedding, etc.',
|
||||||
|
},
|
||||||
|
minimax: {
|
||||||
|
label: 'MiniMax',
|
||||||
|
description: 'MiniMax-M2.1, MiniMax-M2.1-lightning, etc.',
|
||||||
|
},
|
||||||
|
mimo: {
|
||||||
|
label: 'MiMo',
|
||||||
|
description: 'mimo-v2-flash',
|
||||||
|
},
|
||||||
|
gemini: {
|
||||||
|
label: 'Google Gemini',
|
||||||
|
description: 'gemini-3-flash-preview, gemini-2.5-pro, etc.',
|
||||||
|
},
|
||||||
|
},
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
createChat: {
|
createChat: {
|
||||||
|
|||||||
@@ -982,6 +982,39 @@ export default {
|
|||||||
baseUrlEmpty: "Base URL 不能为空",
|
baseUrlEmpty: "Base URL 不能为空",
|
||||||
baseUrlInvalid: "Base URL 格式不正确,请输入有效的 URL",
|
baseUrlInvalid: "Base URL 格式不正确,请输入有效的 URL",
|
||||||
},
|
},
|
||||||
|
// Provider (厂商) 相关翻译
|
||||||
|
providerLabel: "服务商",
|
||||||
|
providerPlaceholder: "选择模型服务商",
|
||||||
|
providers: {
|
||||||
|
openai: {
|
||||||
|
label: "OpenAI",
|
||||||
|
description: "OpenAI 官方 API",
|
||||||
|
},
|
||||||
|
aliyun: {
|
||||||
|
label: "阿里云 DashScope",
|
||||||
|
description: "支持 Qwen、DeepSeek 等模型",
|
||||||
|
},
|
||||||
|
zhipu: {
|
||||||
|
label: "智谱 AI",
|
||||||
|
description: "支持 GLM-4 系列模型",
|
||||||
|
},
|
||||||
|
openrouter: {
|
||||||
|
label: "OpenRouter",
|
||||||
|
description: "多模型聚合平台",
|
||||||
|
},
|
||||||
|
generic: {
|
||||||
|
label: "自定义 API",
|
||||||
|
description: "OpenAI 兼容的自定义端点",
|
||||||
|
},
|
||||||
|
siliconflow: {
|
||||||
|
label: "SiliconFlow",
|
||||||
|
description: "硅基流动,支持多种模型",
|
||||||
|
},
|
||||||
|
jina: {
|
||||||
|
label: "Jina AI",
|
||||||
|
description: "专注于 Embedding 和 Rerank",
|
||||||
|
},
|
||||||
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
language: {
|
language: {
|
||||||
@@ -1154,9 +1187,9 @@ export default {
|
|||||||
messages: {
|
messages: {
|
||||||
deleted: "已删除",
|
deleted: "已删除",
|
||||||
deleteFailed: "删除失败",
|
deleteFailed: "删除失败",
|
||||||
file: "文件",
|
file: "文件",
|
||||||
knowledgeBase: "知识库",
|
knowledgeBase: "知识库",
|
||||||
noResult: "无结果",
|
noResult: "无结果",
|
||||||
},
|
},
|
||||||
features: {
|
features: {
|
||||||
knowledgeGraph: "已启用知识图谱",
|
knowledgeGraph: "已启用知识图谱",
|
||||||
|
|||||||
@@ -290,6 +290,7 @@ function convertToLegacyFormat(model: ModelConfig) {
|
|||||||
modelName: model.name, // 显示名称作为模型名
|
modelName: model.name, // 显示名称作为模型名
|
||||||
baseUrl: model.parameters.base_url || '',
|
baseUrl: model.parameters.base_url || '',
|
||||||
apiKey: model.parameters.api_key || '',
|
apiKey: model.parameters.api_key || '',
|
||||||
|
provider: model.parameters.provider || '', // 添加 provider 字段
|
||||||
dimension: model.parameters.embedding_parameters?.dimension,
|
dimension: model.parameters.embedding_parameters?.dimension,
|
||||||
isBuiltin: model.is_builtin || false
|
isBuiltin: model.is_builtin || false
|
||||||
}
|
}
|
||||||
@@ -400,6 +401,7 @@ const handleModelSave = async (modelData: any) => {
|
|||||||
parameters: {
|
parameters: {
|
||||||
base_url: modelData.baseUrl?.trim() || '',
|
base_url: modelData.baseUrl?.trim() || '',
|
||||||
api_key: modelData.apiKey?.trim() || '',
|
api_key: modelData.apiKey?.trim() || '',
|
||||||
|
provider: modelData.provider || '', // 添加 provider 字段
|
||||||
...(currentModelType.value === 'embedding' && modelData.dimension ? {
|
...(currentModelType.value === 'embedding' && modelData.dimension ? {
|
||||||
embedding_parameters: {
|
embedding_parameters: {
|
||||||
dimension: modelData.dimension,
|
dimension: modelData.dimension,
|
||||||
|
|||||||
@@ -252,6 +252,7 @@ func (s *modelService) GetEmbeddingModel(ctx context.Context, modelId string) (e
|
|||||||
ModelName: model.Name,
|
ModelName: model.Name,
|
||||||
Dimensions: model.Parameters.EmbeddingParameters.Dimension,
|
Dimensions: model.Parameters.EmbeddingParameters.Dimension,
|
||||||
TruncatePromptTokens: model.Parameters.EmbeddingParameters.TruncatePromptTokens,
|
TruncatePromptTokens: model.Parameters.EmbeddingParameters.TruncatePromptTokens,
|
||||||
|
Provider: model.Parameters.Provider,
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logger.ErrorWithFields(ctx, err, map[string]interface{}{
|
logger.ErrorWithFields(ctx, err, map[string]interface{}{
|
||||||
|
|||||||
@@ -1506,6 +1506,7 @@ func (h *InitializationHandler) TestEmbeddingModel(c *gin.Context) {
|
|||||||
BaseURL string `json:"baseUrl"`
|
BaseURL string `json:"baseUrl"`
|
||||||
APIKey string `json:"apiKey"`
|
APIKey string `json:"apiKey"`
|
||||||
Dimension int `json:"dimension"`
|
Dimension int `json:"dimension"`
|
||||||
|
Provider string `json:"provider"`
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := c.ShouldBindJSON(&req); err != nil {
|
if err := c.ShouldBindJSON(&req); err != nil {
|
||||||
@@ -1514,6 +1515,23 @@ func (h *InitializationHandler) TestEmbeddingModel(c *gin.Context) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 检查是否是阿里云多模态 embedding 模型(暂不支持)
|
||||||
|
if strings.ToLower(req.Provider) == "aliyun" {
|
||||||
|
modelNameLower := strings.ToLower(req.ModelName)
|
||||||
|
if strings.Contains(modelNameLower, "vision") || strings.Contains(modelNameLower, "multimodal") {
|
||||||
|
logger.Infof(ctx, "Aliyun multimodal embedding model not supported: %s", req.ModelName)
|
||||||
|
c.JSON(http.StatusOK, gin.H{
|
||||||
|
"success": true,
|
||||||
|
"data": gin.H{
|
||||||
|
"available": false,
|
||||||
|
"message": "阿里云多模态 Embedding 模型暂不支持,请使用纯文本 Embedding 模型(如 text-embedding-v4)",
|
||||||
|
"dimension": 0,
|
||||||
|
},
|
||||||
|
})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// 构造 embedder 配置
|
// 构造 embedder 配置
|
||||||
cfg := embedding.Config{
|
cfg := embedding.Config{
|
||||||
Source: types.ModelSource(strings.ToLower(req.Source)),
|
Source: types.ModelSource(strings.ToLower(req.Source)),
|
||||||
@@ -1523,6 +1541,7 @@ func (h *InitializationHandler) TestEmbeddingModel(c *gin.Context) {
|
|||||||
TruncatePromptTokens: 256,
|
TruncatePromptTokens: 256,
|
||||||
Dimensions: req.Dimension,
|
Dimensions: req.Dimension,
|
||||||
ModelID: "",
|
ModelID: "",
|
||||||
|
Provider: req.Provider,
|
||||||
}
|
}
|
||||||
|
|
||||||
emb, err := embedding.NewEmbedder(cfg)
|
emb, err := embedding.NewEmbedder(cfg)
|
||||||
|
|||||||
+105
-1
@@ -6,6 +6,7 @@ import (
|
|||||||
"github.com/Tencent/WeKnora/internal/application/service"
|
"github.com/Tencent/WeKnora/internal/application/service"
|
||||||
"github.com/Tencent/WeKnora/internal/errors"
|
"github.com/Tencent/WeKnora/internal/errors"
|
||||||
"github.com/Tencent/WeKnora/internal/logger"
|
"github.com/Tencent/WeKnora/internal/logger"
|
||||||
|
"github.com/Tencent/WeKnora/internal/models/provider"
|
||||||
"github.com/Tencent/WeKnora/internal/types"
|
"github.com/Tencent/WeKnora/internal/types"
|
||||||
"github.com/Tencent/WeKnora/internal/types/interfaces"
|
"github.com/Tencent/WeKnora/internal/types/interfaces"
|
||||||
secutils "github.com/Tencent/WeKnora/internal/utils"
|
secutils "github.com/Tencent/WeKnora/internal/utils"
|
||||||
@@ -290,7 +291,8 @@ func (h *ModelHandler) UpdateModel(c *gin.Context) {
|
|||||||
model.Name = req.Name
|
model.Name = req.Name
|
||||||
}
|
}
|
||||||
model.Description = req.Description
|
model.Description = req.Description
|
||||||
if req.Parameters != (types.ModelParameters{}) {
|
// Check if any Parameters field is set (can't use struct comparison due to map field)
|
||||||
|
if req.Parameters.BaseURL != "" || req.Parameters.APIKey != "" || req.Parameters.Provider != "" {
|
||||||
model.Parameters = req.Parameters
|
model.Parameters = req.Parameters
|
||||||
}
|
}
|
||||||
model.Source = req.Source
|
model.Source = req.Source
|
||||||
@@ -356,3 +358,105 @@ func (h *ModelHandler) DeleteModel(c *gin.Context) {
|
|||||||
"message": "Model deleted",
|
"message": "Model deleted",
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ModelProviderDTO 模型厂商信息 DTO
|
||||||
|
type ModelProviderDTO struct {
|
||||||
|
Value string `json:"value"` // provider 标识符
|
||||||
|
Label string `json:"label"` // 显示名称
|
||||||
|
Description string `json:"description"` // 描述
|
||||||
|
DefaultURLs map[string]string `json:"defaultUrls"` // 按模型类型区分的默认 URL
|
||||||
|
ModelTypes []string `json:"modelTypes"` // 支持的模型类型
|
||||||
|
}
|
||||||
|
|
||||||
|
// modelTypeToFrontend 将后端 ModelType 转换为前端兼容的字符串
|
||||||
|
// KnowledgeQA -> chat, Embedding -> embedding, Rerank -> rerank, VLLM -> vllm
|
||||||
|
func modelTypeToFrontend(mt types.ModelType) string {
|
||||||
|
switch mt {
|
||||||
|
case types.ModelTypeKnowledgeQA:
|
||||||
|
return "chat"
|
||||||
|
case types.ModelTypeEmbedding:
|
||||||
|
return "embedding"
|
||||||
|
case types.ModelTypeRerank:
|
||||||
|
return "rerank"
|
||||||
|
case types.ModelTypeVLLM:
|
||||||
|
return "vllm"
|
||||||
|
default:
|
||||||
|
return string(mt)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// ListModelProviders godoc
|
||||||
|
// @Summary 获取模型厂商列表
|
||||||
|
// @Description 根据模型类型获取支持的厂商列表及配置信息
|
||||||
|
// @Tags 模型管理
|
||||||
|
// @Accept json
|
||||||
|
// @Produce json
|
||||||
|
// @Param model_type query string false "模型类型 (chat, embedding, rerank, vllm)"
|
||||||
|
// @Success 200 {object} map[string]interface{} "厂商列表"
|
||||||
|
// @Security Bearer
|
||||||
|
// @Security ApiKeyAuth
|
||||||
|
// @Router /models/providers [get]
|
||||||
|
func (h *ModelHandler) ListModelProviders(c *gin.Context) {
|
||||||
|
ctx := c.Request.Context()
|
||||||
|
|
||||||
|
modelType := c.Query("model_type")
|
||||||
|
logger.Infof(ctx, "Listing model providers for type: %s", secutils.SanitizeForLog(modelType))
|
||||||
|
|
||||||
|
// 将前端类型映射到后端类型
|
||||||
|
// 前端: chat, embedding, rerank, vllm
|
||||||
|
// 后端: KnowledgeQA, Embedding, Rerank, VLLM
|
||||||
|
var backendModelType types.ModelType
|
||||||
|
switch modelType {
|
||||||
|
case "chat":
|
||||||
|
backendModelType = types.ModelTypeKnowledgeQA
|
||||||
|
case "embedding":
|
||||||
|
backendModelType = types.ModelTypeEmbedding
|
||||||
|
case "rerank":
|
||||||
|
backendModelType = types.ModelTypeRerank
|
||||||
|
case "vllm":
|
||||||
|
backendModelType = types.ModelTypeVLLM
|
||||||
|
default:
|
||||||
|
backendModelType = types.ModelType(modelType)
|
||||||
|
}
|
||||||
|
|
||||||
|
var providers []provider.ProviderInfo
|
||||||
|
if modelType != "" {
|
||||||
|
// 按模型类型过滤
|
||||||
|
providers = provider.ListByModelType(backendModelType)
|
||||||
|
} else {
|
||||||
|
// 返回所有 provider
|
||||||
|
providers = provider.List()
|
||||||
|
}
|
||||||
|
|
||||||
|
// 转换为 DTO
|
||||||
|
result := make([]ModelProviderDTO, 0, len(providers))
|
||||||
|
for _, p := range providers {
|
||||||
|
// 转换 DefaultURLs map[types.ModelType]string -> map[string]string
|
||||||
|
// 使用前端兼容的 key (chat 而不是 KnowledgeQA)
|
||||||
|
defaultURLs := make(map[string]string)
|
||||||
|
for mt, url := range p.DefaultURLs {
|
||||||
|
frontendType := modelTypeToFrontend(mt)
|
||||||
|
defaultURLs[frontendType] = url
|
||||||
|
}
|
||||||
|
|
||||||
|
// 转换 ModelTypes 为前端兼容格式
|
||||||
|
modelTypes := make([]string, 0, len(p.ModelTypes))
|
||||||
|
for _, mt := range p.ModelTypes {
|
||||||
|
modelTypes = append(modelTypes, modelTypeToFrontend(mt))
|
||||||
|
}
|
||||||
|
|
||||||
|
result = append(result, ModelProviderDTO{
|
||||||
|
Value: string(p.Name),
|
||||||
|
Label: p.DisplayName,
|
||||||
|
Description: p.Description,
|
||||||
|
DefaultURLs: defaultURLs,
|
||||||
|
ModelTypes: modelTypes,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
logger.Infof(ctx, "Retrieved %d providers", len(result))
|
||||||
|
c.JSON(http.StatusOK, gin.H{
|
||||||
|
"success": true,
|
||||||
|
"data": result,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|||||||
@@ -80,6 +80,8 @@ type ChatConfig struct {
|
|||||||
ModelName string
|
ModelName string
|
||||||
APIKey string
|
APIKey string
|
||||||
ModelID string
|
ModelID string
|
||||||
|
Provider string
|
||||||
|
Extra map[string]any
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewChat 创建聊天实例
|
// NewChat 创建聊天实例
|
||||||
|
|||||||
@@ -6,9 +6,9 @@ import (
|
|||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
"strings"
|
|
||||||
|
|
||||||
"github.com/Tencent/WeKnora/internal/logger"
|
"github.com/Tencent/WeKnora/internal/logger"
|
||||||
|
"github.com/Tencent/WeKnora/internal/models/provider"
|
||||||
"github.com/Tencent/WeKnora/internal/types"
|
"github.com/Tencent/WeKnora/internal/types"
|
||||||
"github.com/sashabaranov/go-openai"
|
"github.com/sashabaranov/go-openai"
|
||||||
)
|
)
|
||||||
@@ -20,6 +20,7 @@ type RemoteAPIChat struct {
|
|||||||
modelID string
|
modelID string
|
||||||
baseURL string
|
baseURL string
|
||||||
apiKey string
|
apiKey string
|
||||||
|
provider provider.ProviderName // Provider identifier for routing
|
||||||
}
|
}
|
||||||
|
|
||||||
// QwenChatCompletionRequest 用于 qwen 模型的自定义请求结构体
|
// QwenChatCompletionRequest 用于 qwen 模型的自定义请求结构体
|
||||||
@@ -35,12 +36,20 @@ func NewRemoteAPIChat(chatConfig *ChatConfig) (*RemoteAPIChat, error) {
|
|||||||
if baseURL := chatConfig.BaseURL; baseURL != "" {
|
if baseURL := chatConfig.BaseURL; baseURL != "" {
|
||||||
config.BaseURL = baseURL
|
config.BaseURL = baseURL
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Detect or use configured provider
|
||||||
|
providerName := provider.ProviderName(chatConfig.Provider)
|
||||||
|
if providerName == "" {
|
||||||
|
providerName = provider.DetectProvider(chatConfig.BaseURL)
|
||||||
|
}
|
||||||
|
|
||||||
return &RemoteAPIChat{
|
return &RemoteAPIChat{
|
||||||
modelName: chatConfig.ModelName,
|
modelName: chatConfig.ModelName,
|
||||||
client: openai.NewClientWithConfig(config),
|
client: openai.NewClientWithConfig(config),
|
||||||
modelID: chatConfig.ModelID,
|
modelID: chatConfig.ModelID,
|
||||||
baseURL: chatConfig.BaseURL,
|
baseURL: chatConfig.BaseURL,
|
||||||
apiKey: apiKey,
|
apiKey: apiKey,
|
||||||
|
provider: providerName,
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -86,12 +95,12 @@ func (c *RemoteAPIChat) convertMessages(messages []Message) []openai.ChatComplet
|
|||||||
|
|
||||||
// isQwenModel 检查是否为 qwen 模型
|
// isQwenModel 检查是否为 qwen 模型
|
||||||
func (c *RemoteAPIChat) isAliyunQwen3Model() bool {
|
func (c *RemoteAPIChat) isAliyunQwen3Model() bool {
|
||||||
return strings.HasPrefix(c.modelName, "qwen3-") && c.baseURL == "https://dashscope.aliyuncs.com/compatible-mode/v1"
|
return c.provider == provider.ProviderAliyun && provider.IsQwen3Model(c.modelName)
|
||||||
}
|
}
|
||||||
|
|
||||||
// isDeepSeekModel 检查是否为 DeepSeek 模型
|
// isDeepSeekModel 检查是否为 DeepSeek 模型
|
||||||
func (c *RemoteAPIChat) isDeepSeekModel() bool {
|
func (c *RemoteAPIChat) isDeepSeekModel() bool {
|
||||||
return strings.Contains(strings.ToLower(c.modelName), "deepseek")
|
return provider.IsDeepSeekModel(c.modelName)
|
||||||
}
|
}
|
||||||
|
|
||||||
// buildQwenChatCompletionRequest 构建 qwen 模型的聊天请求参数
|
// buildQwenChatCompletionRequest 构建 qwen 模型的聊天请求参数
|
||||||
|
|||||||
@@ -0,0 +1,247 @@
|
|||||||
|
package embedding
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"context"
|
||||||
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
|
"net/http"
|
||||||
|
"strings"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/Tencent/WeKnora/internal/logger"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
// AliyunMultimodalEmbeddingEndpoint 阿里云 DashScope 多模态 Embedding API 端点
|
||||||
|
AliyunMultimodalEmbeddingEndpoint = "/api/v1/services/embeddings/multimodal-embedding/multimodal-embedding"
|
||||||
|
)
|
||||||
|
|
||||||
|
// AliyunEmbedder implements text vectorization using Aliyun DashScope multimodal embedding API
|
||||||
|
type AliyunEmbedder struct {
|
||||||
|
apiKey string
|
||||||
|
baseURL string
|
||||||
|
modelName string
|
||||||
|
truncatePromptTokens int
|
||||||
|
dimensions int
|
||||||
|
modelID string
|
||||||
|
httpClient *http.Client
|
||||||
|
timeout time.Duration
|
||||||
|
maxRetries int
|
||||||
|
EmbedderPooler
|
||||||
|
}
|
||||||
|
|
||||||
|
// AliyunEmbedRequest represents an Aliyun DashScope multimodal embedding request
|
||||||
|
type AliyunEmbedRequest struct {
|
||||||
|
Model string `json:"model"`
|
||||||
|
Input AliyunEmbedInput `json:"input"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// AliyunEmbedInput represents the input structure for Aliyun embedding
|
||||||
|
type AliyunEmbedInput struct {
|
||||||
|
Contents []AliyunContent `json:"contents"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// AliyunContent represents a single content item in the input
|
||||||
|
type AliyunContent struct {
|
||||||
|
Text string `json:"text,omitempty"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// AliyunEmbedResponse represents an Aliyun DashScope embedding response
|
||||||
|
type AliyunEmbedResponse struct {
|
||||||
|
Output struct {
|
||||||
|
Embeddings []struct {
|
||||||
|
Embedding []float32 `json:"embedding"`
|
||||||
|
TextIndex int `json:"text_index"`
|
||||||
|
} `json:"embeddings"`
|
||||||
|
} `json:"output"`
|
||||||
|
Usage struct {
|
||||||
|
TotalTokens int `json:"total_tokens"`
|
||||||
|
} `json:"usage"`
|
||||||
|
RequestID string `json:"request_id"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// AliyunErrorResponse represents an error response from Aliyun DashScope
|
||||||
|
type AliyunErrorResponse struct {
|
||||||
|
Code string `json:"code"`
|
||||||
|
Message string `json:"message"`
|
||||||
|
RequestID string `json:"request_id"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewAliyunEmbedder creates a new Aliyun DashScope embedder
|
||||||
|
func NewAliyunEmbedder(apiKey, baseURL, modelName string,
|
||||||
|
truncatePromptTokens int, dimensions int, modelID string, pooler EmbedderPooler,
|
||||||
|
) (*AliyunEmbedder, error) {
|
||||||
|
if baseURL == "" {
|
||||||
|
baseURL = "https://dashscope.aliyuncs.com"
|
||||||
|
}
|
||||||
|
|
||||||
|
// Remove trailing slash and any existing path suffix
|
||||||
|
baseURL = strings.TrimRight(baseURL, "/")
|
||||||
|
// If baseURL contains /compatible-mode/v1, strip it for multimodal API
|
||||||
|
if strings.Contains(baseURL, "/compatible-mode/v1") {
|
||||||
|
baseURL = strings.Replace(baseURL, "/compatible-mode/v1", "", 1)
|
||||||
|
}
|
||||||
|
|
||||||
|
if modelName == "" {
|
||||||
|
return nil, fmt.Errorf("model name is required")
|
||||||
|
}
|
||||||
|
|
||||||
|
if truncatePromptTokens == 0 {
|
||||||
|
truncatePromptTokens = 511
|
||||||
|
}
|
||||||
|
|
||||||
|
timeout := 60 * time.Second
|
||||||
|
|
||||||
|
client := &http.Client{
|
||||||
|
Timeout: timeout,
|
||||||
|
}
|
||||||
|
|
||||||
|
return &AliyunEmbedder{
|
||||||
|
apiKey: apiKey,
|
||||||
|
baseURL: baseURL,
|
||||||
|
modelName: modelName,
|
||||||
|
httpClient: client,
|
||||||
|
truncatePromptTokens: truncatePromptTokens,
|
||||||
|
EmbedderPooler: pooler,
|
||||||
|
dimensions: dimensions,
|
||||||
|
modelID: modelID,
|
||||||
|
timeout: timeout,
|
||||||
|
maxRetries: 3,
|
||||||
|
}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Embed converts text to vector
|
||||||
|
func (e *AliyunEmbedder) Embed(ctx context.Context, text string) ([]float32, error) {
|
||||||
|
for range 3 {
|
||||||
|
embeddings, err := e.BatchEmbed(ctx, []string{text})
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if len(embeddings) > 0 {
|
||||||
|
return embeddings[0], nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil, fmt.Errorf("no embedding returned")
|
||||||
|
}
|
||||||
|
|
||||||
|
func (e *AliyunEmbedder) doRequestWithRetry(ctx context.Context, jsonData []byte) (*http.Response, error) {
|
||||||
|
var resp *http.Response
|
||||||
|
var err error
|
||||||
|
url := e.baseURL + AliyunMultimodalEmbeddingEndpoint
|
||||||
|
|
||||||
|
for i := 0; i <= e.maxRetries; i++ {
|
||||||
|
if i > 0 {
|
||||||
|
backoffTime := time.Duration(1<<uint(i-1)) * time.Second
|
||||||
|
if backoffTime > 10*time.Second {
|
||||||
|
backoffTime = 10 * time.Second
|
||||||
|
}
|
||||||
|
logger.GetLogger(ctx).
|
||||||
|
Infof("AliyunEmbedder retrying request (%d/%d), waiting %v", i, e.maxRetries, backoffTime)
|
||||||
|
|
||||||
|
select {
|
||||||
|
case <-time.After(backoffTime):
|
||||||
|
case <-ctx.Done():
|
||||||
|
return nil, ctx.Err()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
req, err := http.NewRequestWithContext(ctx, "POST", url, bytes.NewReader(jsonData))
|
||||||
|
if err != nil {
|
||||||
|
logger.GetLogger(ctx).Errorf("AliyunEmbedder failed to create request: %v", err)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
req.Header.Set("Content-Type", "application/json")
|
||||||
|
req.Header.Set("Authorization", "Bearer "+e.apiKey)
|
||||||
|
|
||||||
|
resp, err = e.httpClient.Do(req)
|
||||||
|
if err == nil {
|
||||||
|
return resp, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
logger.GetLogger(ctx).Errorf("AliyunEmbedder request failed (attempt %d/%d): %v", i+1, e.maxRetries+1, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
func (e *AliyunEmbedder) BatchEmbed(ctx context.Context, texts []string) ([][]float32, error) {
|
||||||
|
// Build contents array from texts
|
||||||
|
contents := make([]AliyunContent, 0, len(texts))
|
||||||
|
for _, text := range texts {
|
||||||
|
contents = append(contents, AliyunContent{Text: text})
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create request body
|
||||||
|
reqBody := AliyunEmbedRequest{
|
||||||
|
Model: e.modelName,
|
||||||
|
Input: AliyunEmbedInput{
|
||||||
|
Contents: contents,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
jsonData, err := json.Marshal(reqBody)
|
||||||
|
if err != nil {
|
||||||
|
logger.GetLogger(ctx).Errorf("AliyunEmbedder BatchEmbed marshal request error: %v", err)
|
||||||
|
return nil, fmt.Errorf("marshal request: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
resp, err := e.doRequestWithRetry(ctx, jsonData)
|
||||||
|
if err != nil {
|
||||||
|
logger.GetLogger(ctx).Errorf("AliyunEmbedder BatchEmbed send request error: %v", err)
|
||||||
|
return nil, fmt.Errorf("send request: %w", err)
|
||||||
|
}
|
||||||
|
if resp.Body != nil {
|
||||||
|
defer resp.Body.Close()
|
||||||
|
}
|
||||||
|
|
||||||
|
body, err := io.ReadAll(resp.Body)
|
||||||
|
if err != nil {
|
||||||
|
logger.GetLogger(ctx).Errorf("AliyunEmbedder BatchEmbed read response error: %v", err)
|
||||||
|
return nil, fmt.Errorf("read response: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if resp.StatusCode != http.StatusOK {
|
||||||
|
// Try to parse error response
|
||||||
|
var errResp AliyunErrorResponse
|
||||||
|
if json.Unmarshal(body, &errResp) == nil && errResp.Message != "" {
|
||||||
|
logger.GetLogger(ctx).Errorf("AliyunEmbedder BatchEmbed API error: %s - %s", errResp.Code, errResp.Message)
|
||||||
|
return nil, fmt.Errorf("API error: %s - %s", errResp.Code, errResp.Message)
|
||||||
|
}
|
||||||
|
logger.GetLogger(ctx).Errorf("AliyunEmbedder BatchEmbed API error: Http Status %s", resp.Status)
|
||||||
|
return nil, fmt.Errorf("BatchEmbed API error: Http Status %s", resp.Status)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Parse response
|
||||||
|
var response AliyunEmbedResponse
|
||||||
|
if err := json.Unmarshal(body, &response); err != nil {
|
||||||
|
logger.GetLogger(ctx).Errorf("AliyunEmbedder BatchEmbed unmarshal response error: %v", err)
|
||||||
|
return nil, fmt.Errorf("unmarshal response: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Extract embedding vectors, preserving order by text_index
|
||||||
|
embeddings := make([][]float32, len(texts))
|
||||||
|
for _, emb := range response.Output.Embeddings {
|
||||||
|
if emb.TextIndex >= 0 && emb.TextIndex < len(embeddings) {
|
||||||
|
embeddings[emb.TextIndex] = emb.Embedding
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return embeddings, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetModelName returns the model name
|
||||||
|
func (e *AliyunEmbedder) GetModelName() string {
|
||||||
|
return e.modelName
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetDimensions returns the vector dimensions
|
||||||
|
func (e *AliyunEmbedder) GetDimensions() int {
|
||||||
|
return e.dimensions
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetModelID returns the model ID
|
||||||
|
func (e *AliyunEmbedder) GetModelID() string {
|
||||||
|
return e.modelID
|
||||||
|
}
|
||||||
@@ -5,6 +5,7 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
"github.com/Tencent/WeKnora/internal/models/provider"
|
||||||
"github.com/Tencent/WeKnora/internal/models/utils/ollama"
|
"github.com/Tencent/WeKnora/internal/models/utils/ollama"
|
||||||
"github.com/Tencent/WeKnora/internal/runtime"
|
"github.com/Tencent/WeKnora/internal/runtime"
|
||||||
"github.com/Tencent/WeKnora/internal/types"
|
"github.com/Tencent/WeKnora/internal/types"
|
||||||
@@ -46,6 +47,7 @@ type Config struct {
|
|||||||
TruncatePromptTokens int `json:"truncate_prompt_tokens"`
|
TruncatePromptTokens int `json:"truncate_prompt_tokens"`
|
||||||
Dimensions int `json:"dimensions"`
|
Dimensions int `json:"dimensions"`
|
||||||
ModelID string `json:"model_id"`
|
ModelID string `json:"model_id"`
|
||||||
|
Provider string `json:"provider"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewEmbedder creates an embedder based on the configuration
|
// NewEmbedder creates an embedder based on the configuration
|
||||||
@@ -60,16 +62,94 @@ func NewEmbedder(config Config) (Embedder, error) {
|
|||||||
})
|
})
|
||||||
return embedder, err
|
return embedder, err
|
||||||
case string(types.ModelSourceRemote):
|
case string(types.ModelSourceRemote):
|
||||||
runtime.GetContainer().Invoke(func(pooler EmbedderPooler) {
|
// Detect or use configured provider for routing
|
||||||
embedder, err = NewOpenAIEmbedder(config.APIKey,
|
providerName := provider.ProviderName(config.Provider)
|
||||||
config.BaseURL,
|
if providerName == "" {
|
||||||
config.ModelName,
|
providerName = provider.DetectProvider(config.BaseURL)
|
||||||
config.TruncatePromptTokens,
|
}
|
||||||
config.Dimensions,
|
|
||||||
config.ModelID,
|
// Route to provider-specific embedders
|
||||||
pooler)
|
switch providerName {
|
||||||
})
|
case provider.ProviderAliyun:
|
||||||
return embedder, err
|
// 检查是否是多模态嵌入模型
|
||||||
|
// 多模态模型: tongyi-embedding-vision-*, multimodal-embedding-*
|
||||||
|
// tex-only模型: text-embedding-v1/v2/v3/v4 应该使用 OpenAI 兼容接口,否则响应格式不匹配、embedding 返回空数组
|
||||||
|
isMultimodalModel := strings.Contains(strings.ToLower(config.ModelName), "vision") ||
|
||||||
|
strings.Contains(strings.ToLower(config.ModelName), "multimodal")
|
||||||
|
|
||||||
|
if isMultimodalModel {
|
||||||
|
// 多模态模型需要使用DashScope专用 API 端点
|
||||||
|
// 如果用户填写了 OpenAI 兼容模式的 URL,自动修正为多模态 API 的baseURL
|
||||||
|
baseURL := config.BaseURL
|
||||||
|
if baseURL == "" {
|
||||||
|
baseURL = "https://dashscope.aliyuncs.com"
|
||||||
|
} else if strings.Contains(baseURL, "/compatible-mode/") {
|
||||||
|
// 移除 compatible-mode 路径,AliyunEmbedder 会自动添加多模态端点
|
||||||
|
baseURL = strings.Replace(baseURL, "/compatible-mode/v1", "", 1)
|
||||||
|
baseURL = strings.Replace(baseURL, "/compatible-mode", "", 1)
|
||||||
|
}
|
||||||
|
runtime.GetContainer().Invoke(func(pooler EmbedderPooler) {
|
||||||
|
embedder, err = NewAliyunEmbedder(config.APIKey,
|
||||||
|
baseURL,
|
||||||
|
config.ModelName,
|
||||||
|
config.TruncatePromptTokens,
|
||||||
|
config.Dimensions,
|
||||||
|
config.ModelID,
|
||||||
|
pooler)
|
||||||
|
})
|
||||||
|
} else {
|
||||||
|
baseURL := config.BaseURL
|
||||||
|
if baseURL == "" || !strings.Contains(baseURL, "/compatible-mode/") {
|
||||||
|
baseURL = "https://dashscope.aliyuncs.com/compatible-mode/v1"
|
||||||
|
}
|
||||||
|
runtime.GetContainer().Invoke(func(pooler EmbedderPooler) {
|
||||||
|
embedder, err = NewOpenAIEmbedder(config.APIKey,
|
||||||
|
baseURL,
|
||||||
|
config.ModelName,
|
||||||
|
config.TruncatePromptTokens,
|
||||||
|
config.Dimensions,
|
||||||
|
config.ModelID,
|
||||||
|
pooler)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
return embedder, err
|
||||||
|
case provider.ProviderVolcengine:
|
||||||
|
// Volcengine Ark uses multimodal embedding API
|
||||||
|
runtime.GetContainer().Invoke(func(pooler EmbedderPooler) {
|
||||||
|
embedder, err = NewVolcengineEmbedder(config.APIKey,
|
||||||
|
config.BaseURL,
|
||||||
|
config.ModelName,
|
||||||
|
config.TruncatePromptTokens,
|
||||||
|
config.Dimensions,
|
||||||
|
config.ModelID,
|
||||||
|
pooler)
|
||||||
|
})
|
||||||
|
return embedder, err
|
||||||
|
case provider.ProviderJina:
|
||||||
|
// Jina AI uses different API format (truncate instead of truncate_prompt_tokens)
|
||||||
|
runtime.GetContainer().Invoke(func(pooler EmbedderPooler) {
|
||||||
|
embedder, err = NewJinaEmbedder(config.APIKey,
|
||||||
|
config.BaseURL,
|
||||||
|
config.ModelName,
|
||||||
|
config.TruncatePromptTokens,
|
||||||
|
config.Dimensions,
|
||||||
|
config.ModelID,
|
||||||
|
pooler)
|
||||||
|
})
|
||||||
|
return embedder, err
|
||||||
|
default:
|
||||||
|
// Use OpenAI-compatible embedder for other providers
|
||||||
|
runtime.GetContainer().Invoke(func(pooler EmbedderPooler) {
|
||||||
|
embedder, err = NewOpenAIEmbedder(config.APIKey,
|
||||||
|
config.BaseURL,
|
||||||
|
config.ModelName,
|
||||||
|
config.TruncatePromptTokens,
|
||||||
|
config.Dimensions,
|
||||||
|
config.ModelID,
|
||||||
|
pooler)
|
||||||
|
})
|
||||||
|
return embedder, err
|
||||||
|
}
|
||||||
default:
|
default:
|
||||||
return nil, fmt.Errorf("unsupported embedder source: %s", config.Source)
|
return nil, fmt.Errorf("unsupported embedder source: %s", config.Source)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,203 @@
|
|||||||
|
package embedding
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"context"
|
||||||
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
|
"net/http"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/Tencent/WeKnora/internal/logger"
|
||||||
|
)
|
||||||
|
|
||||||
|
// JinaEmbedder implements text vectorization functionality using Jina AI API
|
||||||
|
// Jina API is mostly OpenAI-compatible but does NOT support truncate_prompt_tokens
|
||||||
|
type JinaEmbedder struct {
|
||||||
|
apiKey string
|
||||||
|
baseURL string
|
||||||
|
modelName string
|
||||||
|
dimensions int
|
||||||
|
modelID string
|
||||||
|
httpClient *http.Client
|
||||||
|
timeout time.Duration
|
||||||
|
maxRetries int
|
||||||
|
EmbedderPooler
|
||||||
|
}
|
||||||
|
|
||||||
|
// JinaEmbedRequest represents a Jina embedding request
|
||||||
|
// Note: Jina uses 'truncate' (boolean) instead of 'truncate_prompt_tokens' (integer)
|
||||||
|
type JinaEmbedRequest struct {
|
||||||
|
Model string `json:"model"`
|
||||||
|
Input []string `json:"input"`
|
||||||
|
Truncate bool `json:"truncate,omitempty"` // Whether to truncate text exceeding max token length
|
||||||
|
Dimensions int `json:"dimensions,omitempty"` // Output embedding dimensions (for models that support it)
|
||||||
|
}
|
||||||
|
|
||||||
|
// JinaEmbedResponse represents a Jina embedding response
|
||||||
|
type JinaEmbedResponse struct {
|
||||||
|
Data []struct {
|
||||||
|
Embedding []float32 `json:"embedding"`
|
||||||
|
Index int `json:"index"`
|
||||||
|
} `json:"data"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewJinaEmbedder creates a new Jina embedder
|
||||||
|
func NewJinaEmbedder(apiKey, baseURL, modelName string,
|
||||||
|
truncatePromptTokens int, dimensions int, modelID string, pooler EmbedderPooler,
|
||||||
|
) (*JinaEmbedder, error) {
|
||||||
|
if baseURL == "" {
|
||||||
|
baseURL = "https://api.jina.ai/v1"
|
||||||
|
}
|
||||||
|
|
||||||
|
if modelName == "" {
|
||||||
|
return nil, fmt.Errorf("model name is required")
|
||||||
|
}
|
||||||
|
|
||||||
|
timeout := 60 * time.Second
|
||||||
|
|
||||||
|
// Create HTTP client
|
||||||
|
client := &http.Client{
|
||||||
|
Timeout: timeout,
|
||||||
|
}
|
||||||
|
|
||||||
|
return &JinaEmbedder{
|
||||||
|
apiKey: apiKey,
|
||||||
|
baseURL: baseURL,
|
||||||
|
modelName: modelName,
|
||||||
|
httpClient: client,
|
||||||
|
EmbedderPooler: pooler,
|
||||||
|
dimensions: dimensions,
|
||||||
|
modelID: modelID,
|
||||||
|
timeout: timeout,
|
||||||
|
maxRetries: 3,
|
||||||
|
}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Embed converts text to vector
|
||||||
|
func (e *JinaEmbedder) Embed(ctx context.Context, text string) ([]float32, error) {
|
||||||
|
for range 3 {
|
||||||
|
embeddings, err := e.BatchEmbed(ctx, []string{text})
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if len(embeddings) > 0 {
|
||||||
|
return embeddings[0], nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil, fmt.Errorf("no embedding returned")
|
||||||
|
}
|
||||||
|
|
||||||
|
func (e *JinaEmbedder) doRequestWithRetry(ctx context.Context, jsonData []byte) (*http.Response, error) {
|
||||||
|
var resp *http.Response
|
||||||
|
var err error
|
||||||
|
url := e.baseURL + "/embeddings"
|
||||||
|
|
||||||
|
for i := 0; i <= e.maxRetries; i++ {
|
||||||
|
if i > 0 {
|
||||||
|
backoffTime := time.Duration(1<<uint(i-1)) * time.Second
|
||||||
|
if backoffTime > 10*time.Second {
|
||||||
|
backoffTime = 10 * time.Second
|
||||||
|
}
|
||||||
|
logger.GetLogger(ctx).
|
||||||
|
Infof("JinaEmbedder retrying request (%d/%d), waiting %v", i, e.maxRetries, backoffTime)
|
||||||
|
|
||||||
|
select {
|
||||||
|
case <-time.After(backoffTime):
|
||||||
|
case <-ctx.Done():
|
||||||
|
return nil, ctx.Err()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Rebuild request each time to ensure Body is valid
|
||||||
|
req, err := http.NewRequestWithContext(ctx, "POST", url, bytes.NewReader(jsonData))
|
||||||
|
if err != nil {
|
||||||
|
logger.GetLogger(ctx).Errorf("JinaEmbedder failed to create request: %v", err)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
req.Header.Set("Content-Type", "application/json")
|
||||||
|
req.Header.Set("Authorization", "Bearer "+e.apiKey)
|
||||||
|
|
||||||
|
resp, err = e.httpClient.Do(req)
|
||||||
|
if err == nil {
|
||||||
|
return resp, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
logger.GetLogger(ctx).Errorf("JinaEmbedder request failed (attempt %d/%d): %v", i+1, e.maxRetries+1, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
func (e *JinaEmbedder) BatchEmbed(ctx context.Context, texts []string) ([][]float32, error) {
|
||||||
|
// Create request body - Jina uses 'truncate' boolean instead of 'truncate_prompt_tokens'
|
||||||
|
reqBody := JinaEmbedRequest{
|
||||||
|
Model: e.modelName,
|
||||||
|
Input: texts,
|
||||||
|
Truncate: true, // Enable truncation for long texts
|
||||||
|
}
|
||||||
|
|
||||||
|
// Only include dimensions if specified and greater than 0
|
||||||
|
if e.dimensions > 0 {
|
||||||
|
reqBody.Dimensions = e.dimensions
|
||||||
|
}
|
||||||
|
|
||||||
|
jsonData, err := json.Marshal(reqBody)
|
||||||
|
if err != nil {
|
||||||
|
logger.GetLogger(ctx).Errorf("JinaEmbedder EmbedBatch marshal request error: %v", err)
|
||||||
|
return nil, fmt.Errorf("marshal request: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Send request
|
||||||
|
resp, err := e.doRequestWithRetry(ctx, jsonData)
|
||||||
|
if err != nil {
|
||||||
|
logger.GetLogger(ctx).Errorf("JinaEmbedder EmbedBatch send request error: %v", err)
|
||||||
|
return nil, fmt.Errorf("send request: %w", err)
|
||||||
|
}
|
||||||
|
if resp.Body != nil {
|
||||||
|
defer resp.Body.Close()
|
||||||
|
}
|
||||||
|
|
||||||
|
// Read response
|
||||||
|
body, err := io.ReadAll(resp.Body)
|
||||||
|
if err != nil {
|
||||||
|
logger.GetLogger(ctx).Errorf("JinaEmbedder EmbedBatch read response error: %v", err)
|
||||||
|
return nil, fmt.Errorf("read response: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if resp.StatusCode != http.StatusOK {
|
||||||
|
logger.GetLogger(ctx).Errorf("JinaEmbedder EmbedBatch API error: Http Status %s, Body: %s", resp.Status, string(body))
|
||||||
|
return nil, fmt.Errorf("EmbedBatch API error: Http Status %s", resp.Status)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Parse response
|
||||||
|
var response JinaEmbedResponse
|
||||||
|
if err := json.Unmarshal(body, &response); err != nil {
|
||||||
|
logger.GetLogger(ctx).Errorf("JinaEmbedder EmbedBatch unmarshal response error: %v", err)
|
||||||
|
return nil, fmt.Errorf("unmarshal response: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Extract embedding vectors
|
||||||
|
embeddings := make([][]float32, 0, len(response.Data))
|
||||||
|
for _, data := range response.Data {
|
||||||
|
embeddings = append(embeddings, data.Embedding)
|
||||||
|
}
|
||||||
|
|
||||||
|
return embeddings, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetModelName returns the model name
|
||||||
|
func (e *JinaEmbedder) GetModelName() string {
|
||||||
|
return e.modelName
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetDimensions returns the vector dimensions
|
||||||
|
func (e *JinaEmbedder) GetDimensions() int {
|
||||||
|
return e.dimensions
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetModelID returns the model ID
|
||||||
|
func (e *JinaEmbedder) GetModelID() string {
|
||||||
|
return e.modelID
|
||||||
|
}
|
||||||
@@ -0,0 +1,253 @@
|
|||||||
|
package embedding
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"context"
|
||||||
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
|
"net/http"
|
||||||
|
"strings"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/Tencent/WeKnora/internal/logger"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
// VolcengineMultimodalEmbeddingPath 火山引擎 Ark 多模态 Embedding API 路径
|
||||||
|
VolcengineMultimodalEmbeddingPath = "/api/v3/embeddings/multimodal"
|
||||||
|
)
|
||||||
|
|
||||||
|
// VolcengineEmbedder implements text vectorization using Volcengine Ark multimodal embedding API
|
||||||
|
type VolcengineEmbedder struct {
|
||||||
|
apiKey string
|
||||||
|
baseURL string
|
||||||
|
modelName string
|
||||||
|
truncatePromptTokens int
|
||||||
|
dimensions int
|
||||||
|
modelID string
|
||||||
|
httpClient *http.Client
|
||||||
|
timeout time.Duration
|
||||||
|
maxRetries int
|
||||||
|
EmbedderPooler
|
||||||
|
}
|
||||||
|
|
||||||
|
// VolcengineEmbedRequest represents a Volcengine Ark multimodal embedding request
|
||||||
|
type VolcengineEmbedRequest struct {
|
||||||
|
Model string `json:"model"`
|
||||||
|
Input []VolcengineInputContent `json:"input"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// VolcengineInputContent represents a single input item for Volcengine
|
||||||
|
type VolcengineInputContent struct {
|
||||||
|
Type string `json:"type"`
|
||||||
|
Text string `json:"text,omitempty"`
|
||||||
|
ImageURL *VolcengineImageURL `json:"image_url,omitempty"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// VolcengineImageURL represents the image URL structure for Volcengine
|
||||||
|
type VolcengineImageURL struct {
|
||||||
|
URL string `json:"url"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// VolcengineEmbedResponse represents a Volcengine Ark multimodal embedding response
|
||||||
|
// Multimodal API returns data as an object with embedding array directly
|
||||||
|
type VolcengineEmbedResponse struct {
|
||||||
|
Object string `json:"object"`
|
||||||
|
Data struct {
|
||||||
|
Embedding []float32 `json:"embedding"`
|
||||||
|
} `json:"data"`
|
||||||
|
Model string `json:"model"`
|
||||||
|
Usage struct {
|
||||||
|
PromptTokens int `json:"prompt_tokens"`
|
||||||
|
TotalTokens int `json:"total_tokens"`
|
||||||
|
} `json:"usage"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// VolcengineErrorResponse represents an error response from Volcengine
|
||||||
|
type VolcengineErrorResponse struct {
|
||||||
|
Error struct {
|
||||||
|
Code string `json:"code"`
|
||||||
|
Message string `json:"message"`
|
||||||
|
Type string `json:"type"`
|
||||||
|
} `json:"error"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewVolcengineEmbedder creates a new Volcengine Ark embedder
|
||||||
|
func NewVolcengineEmbedder(apiKey, baseURL, modelName string,
|
||||||
|
truncatePromptTokens int, dimensions int, modelID string, pooler EmbedderPooler,
|
||||||
|
) (*VolcengineEmbedder, error) {
|
||||||
|
if baseURL == "" {
|
||||||
|
baseURL = "https://ark.cn-beijing.volces.com"
|
||||||
|
}
|
||||||
|
|
||||||
|
// Remove trailing slash
|
||||||
|
baseURL = strings.TrimRight(baseURL, "/")
|
||||||
|
|
||||||
|
// Extract base host if URL contains the full multimodal path
|
||||||
|
if strings.Contains(baseURL, "/embeddings/multimodal") {
|
||||||
|
// Strip the path to get base URL
|
||||||
|
if idx := strings.Index(baseURL, "/api/"); idx != -1 {
|
||||||
|
baseURL = baseURL[:idx]
|
||||||
|
}
|
||||||
|
} else if strings.HasSuffix(baseURL, "/api/v3") {
|
||||||
|
// If it ends with /api/v3, keep just the host
|
||||||
|
baseURL = strings.TrimSuffix(baseURL, "/api/v3")
|
||||||
|
}
|
||||||
|
|
||||||
|
if modelName == "" {
|
||||||
|
return nil, fmt.Errorf("model name is required")
|
||||||
|
}
|
||||||
|
|
||||||
|
if truncatePromptTokens == 0 {
|
||||||
|
truncatePromptTokens = 511
|
||||||
|
}
|
||||||
|
|
||||||
|
timeout := 60 * time.Second
|
||||||
|
|
||||||
|
client := &http.Client{
|
||||||
|
Timeout: timeout,
|
||||||
|
}
|
||||||
|
|
||||||
|
return &VolcengineEmbedder{
|
||||||
|
apiKey: apiKey,
|
||||||
|
baseURL: baseURL,
|
||||||
|
modelName: modelName,
|
||||||
|
httpClient: client,
|
||||||
|
truncatePromptTokens: truncatePromptTokens,
|
||||||
|
EmbedderPooler: pooler,
|
||||||
|
dimensions: dimensions,
|
||||||
|
modelID: modelID,
|
||||||
|
timeout: timeout,
|
||||||
|
maxRetries: 3,
|
||||||
|
}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Embed converts text to vector
|
||||||
|
func (e *VolcengineEmbedder) Embed(ctx context.Context, text string) ([]float32, error) {
|
||||||
|
for range 3 {
|
||||||
|
embeddings, err := e.BatchEmbed(ctx, []string{text})
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if len(embeddings) > 0 {
|
||||||
|
return embeddings[0], nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil, fmt.Errorf("no embedding returned")
|
||||||
|
}
|
||||||
|
|
||||||
|
func (e *VolcengineEmbedder) doRequestWithRetry(ctx context.Context, jsonData []byte) (*http.Response, error) {
|
||||||
|
var resp *http.Response
|
||||||
|
var err error
|
||||||
|
url := e.baseURL + VolcengineMultimodalEmbeddingPath
|
||||||
|
|
||||||
|
for i := 0; i <= e.maxRetries; i++ {
|
||||||
|
if i > 0 {
|
||||||
|
backoffTime := time.Duration(1<<uint(i-1)) * time.Second
|
||||||
|
if backoffTime > 10*time.Second {
|
||||||
|
backoffTime = 10 * time.Second
|
||||||
|
}
|
||||||
|
logger.GetLogger(ctx).
|
||||||
|
Infof("VolcengineEmbedder retrying request (%d/%d), waiting %v", i, e.maxRetries, backoffTime)
|
||||||
|
|
||||||
|
select {
|
||||||
|
case <-time.After(backoffTime):
|
||||||
|
case <-ctx.Done():
|
||||||
|
return nil, ctx.Err()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
req, err := http.NewRequestWithContext(ctx, "POST", url, bytes.NewReader(jsonData))
|
||||||
|
if err != nil {
|
||||||
|
logger.GetLogger(ctx).Errorf("VolcengineEmbedder failed to create request: %v", err)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
req.Header.Set("Content-Type", "application/json")
|
||||||
|
req.Header.Set("Authorization", "Bearer "+e.apiKey)
|
||||||
|
|
||||||
|
resp, err = e.httpClient.Do(req)
|
||||||
|
if err == nil {
|
||||||
|
return resp, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
logger.GetLogger(ctx).Errorf("VolcengineEmbedder request failed (attempt %d/%d): %v", i+1, e.maxRetries+1, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
func (e *VolcengineEmbedder) BatchEmbed(ctx context.Context, texts []string) ([][]float32, error) {
|
||||||
|
embeddings := make([][]float32, len(texts))
|
||||||
|
|
||||||
|
// Volcengine multimodal API returns a single combined embedding for all inputs,
|
||||||
|
// so we need to call the API once per text for proper batch embedding
|
||||||
|
for i, text := range texts {
|
||||||
|
input := []VolcengineInputContent{
|
||||||
|
{
|
||||||
|
Type: "text",
|
||||||
|
Text: text,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
reqBody := VolcengineEmbedRequest{
|
||||||
|
Model: e.modelName,
|
||||||
|
Input: input,
|
||||||
|
}
|
||||||
|
|
||||||
|
jsonData, err := json.Marshal(reqBody)
|
||||||
|
if err != nil {
|
||||||
|
logger.GetLogger(ctx).Errorf("VolcengineEmbedder BatchEmbed marshal request error: %v", err)
|
||||||
|
return nil, fmt.Errorf("marshal request: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
resp, err := e.doRequestWithRetry(ctx, jsonData)
|
||||||
|
if err != nil {
|
||||||
|
logger.GetLogger(ctx).Errorf("VolcengineEmbedder BatchEmbed send request error: %v", err)
|
||||||
|
return nil, fmt.Errorf("send request: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
body, err := io.ReadAll(resp.Body)
|
||||||
|
resp.Body.Close()
|
||||||
|
if err != nil {
|
||||||
|
logger.GetLogger(ctx).Errorf("VolcengineEmbedder BatchEmbed read response error: %v", err)
|
||||||
|
return nil, fmt.Errorf("read response: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if resp.StatusCode != http.StatusOK {
|
||||||
|
var errResp VolcengineErrorResponse
|
||||||
|
if json.Unmarshal(body, &errResp) == nil && errResp.Error.Message != "" {
|
||||||
|
logger.GetLogger(ctx).Errorf("VolcengineEmbedder BatchEmbed API error: %s - %s", errResp.Error.Code, errResp.Error.Message)
|
||||||
|
return nil, fmt.Errorf("API error: %s - %s", errResp.Error.Code, errResp.Error.Message)
|
||||||
|
}
|
||||||
|
logger.GetLogger(ctx).Errorf("VolcengineEmbedder BatchEmbed API error: Http Status %s", resp.Status)
|
||||||
|
return nil, fmt.Errorf("BatchEmbed API error: Http Status %s", resp.Status)
|
||||||
|
}
|
||||||
|
|
||||||
|
var response VolcengineEmbedResponse
|
||||||
|
if err := json.Unmarshal(body, &response); err != nil {
|
||||||
|
logger.GetLogger(ctx).Errorf("VolcengineEmbedder BatchEmbed unmarshal response error: %v", err)
|
||||||
|
return nil, fmt.Errorf("unmarshal response: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
embeddings[i] = response.Data.Embedding
|
||||||
|
}
|
||||||
|
|
||||||
|
return embeddings, nil
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetModelName returns the model name
|
||||||
|
func (e *VolcengineEmbedder) GetModelName() string {
|
||||||
|
return e.modelName
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetDimensions returns the vector dimensions
|
||||||
|
func (e *VolcengineEmbedder) GetDimensions() int {
|
||||||
|
return e.dimensions
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetModelID returns the model ID
|
||||||
|
func (e *VolcengineEmbedder) GetModelID() string {
|
||||||
|
return e.modelID
|
||||||
|
}
|
||||||
@@ -0,0 +1,67 @@
|
|||||||
|
package provider
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"github.com/Tencent/WeKnora/internal/types"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
// AliyunChatBaseURL 阿里云 DashScope Chat/Embedding 的默认 BaseURL
|
||||||
|
AliyunChatBaseURL = "https://dashscope.aliyuncs.com/compatible-mode/v1"
|
||||||
|
// AliyunRerankBaseURL 阿里云 DashScope Rerank 的默认 BaseURL
|
||||||
|
AliyunRerankBaseURL = "https://dashscope.aliyuncs.com/api/v1/services/rerank/text-rerank/text-rerank"
|
||||||
|
)
|
||||||
|
|
||||||
|
// AliyunProvider 实现阿里云 DashScope 的 Provider 接口
|
||||||
|
type AliyunProvider struct{}
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
Register(&AliyunProvider{})
|
||||||
|
}
|
||||||
|
|
||||||
|
// Info 返回阿里云 provider 的元数据
|
||||||
|
func (p *AliyunProvider) Info() ProviderInfo {
|
||||||
|
return ProviderInfo{
|
||||||
|
Name: ProviderAliyun,
|
||||||
|
DisplayName: "阿里云 DashScope",
|
||||||
|
Description: "qwen-plus, tongyi-embedding-vision-plus, qwen3-rerank, etc.",
|
||||||
|
DefaultURLs: map[types.ModelType]string{
|
||||||
|
types.ModelTypeKnowledgeQA: AliyunChatBaseURL,
|
||||||
|
types.ModelTypeEmbedding: AliyunChatBaseURL,
|
||||||
|
types.ModelTypeRerank: AliyunRerankBaseURL,
|
||||||
|
types.ModelTypeVLLM: AliyunChatBaseURL,
|
||||||
|
},
|
||||||
|
ModelTypes: []types.ModelType{
|
||||||
|
types.ModelTypeKnowledgeQA,
|
||||||
|
types.ModelTypeEmbedding,
|
||||||
|
types.ModelTypeRerank,
|
||||||
|
types.ModelTypeVLLM,
|
||||||
|
},
|
||||||
|
RequiresAuth: true,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// ValidateConfig 验证阿里云 provider 配置
|
||||||
|
func (p *AliyunProvider) ValidateConfig(config *Config) error {
|
||||||
|
if config.APIKey == "" {
|
||||||
|
return fmt.Errorf("API key is required for Aliyun DashScope")
|
||||||
|
}
|
||||||
|
if config.ModelName == "" {
|
||||||
|
return fmt.Errorf("model name is required")
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// IsQwen3Model 检查模型名是否为 Qwen3 模型
|
||||||
|
// Qwen3 模型需要特殊处理 enable_thinking 参数
|
||||||
|
func IsQwen3Model(modelName string) bool {
|
||||||
|
return strings.HasPrefix(modelName, "qwen3-")
|
||||||
|
}
|
||||||
|
|
||||||
|
// IsDeepSeekModel 检查模型名是否为 DeepSeek 模型
|
||||||
|
// DeepSeek 模型不支持 tool_choice 参数
|
||||||
|
func IsDeepSeekModel(modelName string) bool {
|
||||||
|
return strings.Contains(strings.ToLower(modelName), "deepseek")
|
||||||
|
}
|
||||||
@@ -0,0 +1,46 @@
|
|||||||
|
package provider
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
"github.com/Tencent/WeKnora/internal/types"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
// DeepSeekBaseURL DeepSeek 官方 API BaseURL
|
||||||
|
DeepSeekBaseURL = "https://api.deepseek.com/v1"
|
||||||
|
)
|
||||||
|
|
||||||
|
// DeepSeekProvider 实现 DeepSeek 的 Provider 接口
|
||||||
|
type DeepSeekProvider struct{}
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
Register(&DeepSeekProvider{})
|
||||||
|
}
|
||||||
|
|
||||||
|
// Info 返回 DeepSeek provider 的元数据
|
||||||
|
func (p *DeepSeekProvider) Info() ProviderInfo {
|
||||||
|
return ProviderInfo{
|
||||||
|
Name: ProviderDeepSeek,
|
||||||
|
DisplayName: "DeepSeek",
|
||||||
|
Description: "deepseek-chat, deepseek-reasoner, etc.",
|
||||||
|
DefaultURLs: map[types.ModelType]string{
|
||||||
|
types.ModelTypeKnowledgeQA: DeepSeekBaseURL,
|
||||||
|
},
|
||||||
|
ModelTypes: []types.ModelType{
|
||||||
|
types.ModelTypeKnowledgeQA,
|
||||||
|
},
|
||||||
|
RequiresAuth: true,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// ValidateConfig 验证 DeepSeek provider 配置
|
||||||
|
func (p *DeepSeekProvider) ValidateConfig(config *Config) error {
|
||||||
|
if config.APIKey == "" {
|
||||||
|
return fmt.Errorf("API key is required for DeepSeek provider")
|
||||||
|
}
|
||||||
|
if config.ModelName == "" {
|
||||||
|
return fmt.Errorf("model name is required")
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
@@ -0,0 +1,48 @@
|
|||||||
|
package provider
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
"github.com/Tencent/WeKnora/internal/types"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
// GeminiBaseURL Google Gemini API BaseURL
|
||||||
|
GeminiBaseURL = "https://generativelanguage.googleapis.com/v1beta"
|
||||||
|
// GeminiOpenAICompatBaseURL Gemini OpenAI 兼容模式 BaseURL
|
||||||
|
GeminiOpenAICompatBaseURL = "https://generativelanguage.googleapis.com/v1beta/openai"
|
||||||
|
)
|
||||||
|
|
||||||
|
// GeminiProvider 实现 Google Gemini 的 Provider 接口
|
||||||
|
type GeminiProvider struct{}
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
Register(&GeminiProvider{})
|
||||||
|
}
|
||||||
|
|
||||||
|
// Info 返回 Gemini provider 的元数据
|
||||||
|
func (p *GeminiProvider) Info() ProviderInfo {
|
||||||
|
return ProviderInfo{
|
||||||
|
Name: ProviderGemini,
|
||||||
|
DisplayName: "Google Gemini",
|
||||||
|
Description: "gemini-3-flash-preview, gemini-2.5-pro, etc.",
|
||||||
|
DefaultURLs: map[types.ModelType]string{
|
||||||
|
types.ModelTypeKnowledgeQA: GeminiOpenAICompatBaseURL,
|
||||||
|
},
|
||||||
|
ModelTypes: []types.ModelType{
|
||||||
|
types.ModelTypeKnowledgeQA,
|
||||||
|
},
|
||||||
|
RequiresAuth: true,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// ValidateConfig 验证 Gemini provider 配置
|
||||||
|
func (p *GeminiProvider) ValidateConfig(config *Config) error {
|
||||||
|
if config.APIKey == "" {
|
||||||
|
return fmt.Errorf("API key is required for Google Gemini provider")
|
||||||
|
}
|
||||||
|
if config.ModelName == "" {
|
||||||
|
return fmt.Errorf("model name is required")
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
@@ -0,0 +1,42 @@
|
|||||||
|
package provider
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
"github.com/Tencent/WeKnora/internal/types"
|
||||||
|
)
|
||||||
|
|
||||||
|
// GenericProvider 实现通用 OpenAI 兼容的 Provider 接口
|
||||||
|
type GenericProvider struct{}
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
Register(&GenericProvider{})
|
||||||
|
}
|
||||||
|
|
||||||
|
// Info 返回通用 provider 的元数据
|
||||||
|
func (p *GenericProvider) Info() ProviderInfo {
|
||||||
|
return ProviderInfo{
|
||||||
|
Name: ProviderGeneric,
|
||||||
|
DisplayName: "自定义 (OpenAI格式兼容接口)",
|
||||||
|
Description: "Generic API endpoint",
|
||||||
|
DefaultURLs: map[types.ModelType]string{}, // 需要用户自行配置填写
|
||||||
|
ModelTypes: []types.ModelType{
|
||||||
|
types.ModelTypeKnowledgeQA,
|
||||||
|
types.ModelTypeEmbedding,
|
||||||
|
types.ModelTypeRerank,
|
||||||
|
types.ModelTypeVLLM,
|
||||||
|
},
|
||||||
|
RequiresAuth: false, // 可能需要也可能不需要
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// ValidateConfig 验证通用 provider 配置
|
||||||
|
func (p *GenericProvider) ValidateConfig(config *Config) error {
|
||||||
|
if config.BaseURL == "" {
|
||||||
|
return fmt.Errorf("base URL is required for generic provider")
|
||||||
|
}
|
||||||
|
if config.ModelName == "" {
|
||||||
|
return fmt.Errorf("model name is required")
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
@@ -0,0 +1,48 @@
|
|||||||
|
package provider
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
"github.com/Tencent/WeKnora/internal/types"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
// HunyuanBaseURL 腾讯混元 API BaseURL (OpenAI 兼容模式)
|
||||||
|
HunyuanBaseURL = "https://api.hunyuan.cloud.tencent.com/v1"
|
||||||
|
)
|
||||||
|
|
||||||
|
// HunyuanProvider 实现腾讯混元的 Provider 接口
|
||||||
|
type HunyuanProvider struct{}
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
Register(&HunyuanProvider{})
|
||||||
|
}
|
||||||
|
|
||||||
|
// Info 返回腾讯混元 provider 的元数据
|
||||||
|
func (p *HunyuanProvider) Info() ProviderInfo {
|
||||||
|
return ProviderInfo{
|
||||||
|
Name: ProviderHunyuan,
|
||||||
|
DisplayName: "腾讯混元 Hunyuan",
|
||||||
|
Description: "hunyuan-pro, hunyuan-standard, hunyuan-embedding, etc.",
|
||||||
|
DefaultURLs: map[types.ModelType]string{
|
||||||
|
types.ModelTypeKnowledgeQA: HunyuanBaseURL,
|
||||||
|
types.ModelTypeEmbedding: HunyuanBaseURL,
|
||||||
|
},
|
||||||
|
ModelTypes: []types.ModelType{
|
||||||
|
types.ModelTypeKnowledgeQA,
|
||||||
|
types.ModelTypeEmbedding,
|
||||||
|
},
|
||||||
|
RequiresAuth: true,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// ValidateConfig 验证腾讯混元 provider 配置
|
||||||
|
func (p *HunyuanProvider) ValidateConfig(config *Config) error {
|
||||||
|
if config.APIKey == "" {
|
||||||
|
return fmt.Errorf("API key is required for Hunyuan provider")
|
||||||
|
}
|
||||||
|
if config.ModelName == "" {
|
||||||
|
return fmt.Errorf("model name is required")
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
@@ -0,0 +1,44 @@
|
|||||||
|
package provider
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
"github.com/Tencent/WeKnora/internal/types"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
JinaBaseURL = "https://api.jina.ai/v1"
|
||||||
|
)
|
||||||
|
|
||||||
|
// JinaProvider 实现 Jina AI 的 Provider 接口
|
||||||
|
type JinaProvider struct{}
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
Register(&JinaProvider{})
|
||||||
|
}
|
||||||
|
|
||||||
|
// Info 返回 Jina AI provider 的元数据
|
||||||
|
func (p *JinaProvider) Info() ProviderInfo {
|
||||||
|
return ProviderInfo{
|
||||||
|
Name: ProviderJina,
|
||||||
|
DisplayName: "Jina",
|
||||||
|
Description: "jina-clip-v1, jina-embeddings-v2-base-zh, etc.",
|
||||||
|
DefaultURLs: map[types.ModelType]string{
|
||||||
|
types.ModelTypeEmbedding: JinaBaseURL,
|
||||||
|
types.ModelTypeRerank: JinaBaseURL,
|
||||||
|
},
|
||||||
|
ModelTypes: []types.ModelType{
|
||||||
|
types.ModelTypeEmbedding,
|
||||||
|
types.ModelTypeRerank,
|
||||||
|
},
|
||||||
|
RequiresAuth: true,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// ValidateConfig 验证 Jina AI provider 配置
|
||||||
|
func (p *JinaProvider) ValidateConfig(config *Config) error {
|
||||||
|
if config.APIKey == "" {
|
||||||
|
return fmt.Errorf("API key is required for Jina AI provider")
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
@@ -0,0 +1,46 @@
|
|||||||
|
package provider
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
"github.com/Tencent/WeKnora/internal/types"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
// MimoBaseURL 小米 Mimo API BaseURL
|
||||||
|
MimoBaseURL = "https://api.mimo.xiaomi.com/v1"
|
||||||
|
)
|
||||||
|
|
||||||
|
// MimoProvider 实现小米 Mimo 的 Provider 接口
|
||||||
|
type MimoProvider struct{}
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
Register(&MimoProvider{})
|
||||||
|
}
|
||||||
|
|
||||||
|
// Info 返回小米 Mimo provider 的元数据
|
||||||
|
func (p *MimoProvider) Info() ProviderInfo {
|
||||||
|
return ProviderInfo{
|
||||||
|
Name: ProviderMimo,
|
||||||
|
DisplayName: "小米 MiMo",
|
||||||
|
Description: "mimo-v2-flash",
|
||||||
|
DefaultURLs: map[types.ModelType]string{
|
||||||
|
types.ModelTypeKnowledgeQA: MimoBaseURL,
|
||||||
|
},
|
||||||
|
ModelTypes: []types.ModelType{
|
||||||
|
types.ModelTypeKnowledgeQA,
|
||||||
|
},
|
||||||
|
RequiresAuth: true,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// ValidateConfig 验证小米 Mimo provider 配置
|
||||||
|
func (p *MimoProvider) ValidateConfig(config *Config) error {
|
||||||
|
if config.APIKey == "" {
|
||||||
|
return fmt.Errorf("API key is required for Mimo provider")
|
||||||
|
}
|
||||||
|
if config.ModelName == "" {
|
||||||
|
return fmt.Errorf("model name is required")
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
@@ -0,0 +1,48 @@
|
|||||||
|
package provider
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
"github.com/Tencent/WeKnora/internal/types"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
// MiniMaxBaseURL MiniMax 国际版 API BaseURL
|
||||||
|
MiniMaxBaseURL = "https://api.minimax.io/v1"
|
||||||
|
// MiniMaxCNBaseURL MiniMax 国内版 API BaseURL
|
||||||
|
MiniMaxCNBaseURL = "https://api.minimaxi.com/v1"
|
||||||
|
)
|
||||||
|
|
||||||
|
// MiniMaxProvider 实现 MiniMax 的 Provider 接口
|
||||||
|
type MiniMaxProvider struct{}
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
Register(&MiniMaxProvider{})
|
||||||
|
}
|
||||||
|
|
||||||
|
// Info 返回 MiniMax provider 的元数据
|
||||||
|
func (p *MiniMaxProvider) Info() ProviderInfo {
|
||||||
|
return ProviderInfo{
|
||||||
|
Name: ProviderMiniMax,
|
||||||
|
DisplayName: "MiniMax",
|
||||||
|
Description: "MiniMax-M2.1, MiniMax-M2.1-lightning, etc.",
|
||||||
|
DefaultURLs: map[types.ModelType]string{
|
||||||
|
types.ModelTypeKnowledgeQA: MiniMaxCNBaseURL,
|
||||||
|
},
|
||||||
|
ModelTypes: []types.ModelType{
|
||||||
|
types.ModelTypeKnowledgeQA,
|
||||||
|
},
|
||||||
|
RequiresAuth: true,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// ValidateConfig 验证 MiniMax provider 配置
|
||||||
|
func (p *MiniMaxProvider) ValidateConfig(config *Config) error {
|
||||||
|
if config.APIKey == "" {
|
||||||
|
return fmt.Errorf("API key is required for MiniMax provider")
|
||||||
|
}
|
||||||
|
if config.ModelName == "" {
|
||||||
|
return fmt.Errorf("model name is required")
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
@@ -0,0 +1,51 @@
|
|||||||
|
package provider
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
"github.com/Tencent/WeKnora/internal/types"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
OpenAIBaseURL = "https://api.openai.com/v1"
|
||||||
|
)
|
||||||
|
|
||||||
|
// OpenAIProvider 实现 OpenAI 的 Provider 接口
|
||||||
|
type OpenAIProvider struct{}
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
Register(&OpenAIProvider{})
|
||||||
|
}
|
||||||
|
|
||||||
|
// Info 返回 OpenAI provider 的元数据
|
||||||
|
func (p *OpenAIProvider) Info() ProviderInfo {
|
||||||
|
return ProviderInfo{
|
||||||
|
Name: ProviderOpenAI,
|
||||||
|
DisplayName: "OpenAI",
|
||||||
|
Description: "gpt-5.2, gpt-5-mini, etc.",
|
||||||
|
DefaultURLs: map[types.ModelType]string{
|
||||||
|
types.ModelTypeKnowledgeQA: OpenAIBaseURL,
|
||||||
|
types.ModelTypeEmbedding: OpenAIBaseURL,
|
||||||
|
types.ModelTypeRerank: OpenAIBaseURL,
|
||||||
|
types.ModelTypeVLLM: OpenAIBaseURL,
|
||||||
|
},
|
||||||
|
ModelTypes: []types.ModelType{
|
||||||
|
types.ModelTypeKnowledgeQA,
|
||||||
|
types.ModelTypeEmbedding,
|
||||||
|
types.ModelTypeRerank,
|
||||||
|
types.ModelTypeVLLM,
|
||||||
|
},
|
||||||
|
RequiresAuth: true,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// ValidateConfig 验证 OpenAI provider 配置
|
||||||
|
func (p *OpenAIProvider) ValidateConfig(config *Config) error {
|
||||||
|
if config.APIKey == "" {
|
||||||
|
return fmt.Errorf("API key is required for OpenAI provider")
|
||||||
|
}
|
||||||
|
if config.ModelName == "" {
|
||||||
|
return fmt.Errorf("model name is required")
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
@@ -0,0 +1,44 @@
|
|||||||
|
package provider
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
"github.com/Tencent/WeKnora/internal/types"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
OpenRouterBaseURL = "https://openrouter.ai/api/v1"
|
||||||
|
)
|
||||||
|
|
||||||
|
// OpenRouterProvider 实现 OpenRouter 的 Provider 接口
|
||||||
|
type OpenRouterProvider struct{}
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
Register(&OpenRouterProvider{})
|
||||||
|
}
|
||||||
|
|
||||||
|
// Info 返回 OpenRouter provider 的元数据
|
||||||
|
func (p *OpenRouterProvider) Info() ProviderInfo {
|
||||||
|
return ProviderInfo{
|
||||||
|
Name: ProviderOpenRouter,
|
||||||
|
DisplayName: "OpenRouter",
|
||||||
|
Description: "openai/gpt-5.2-chat, google/gemini-3-flash-preview, etc.",
|
||||||
|
DefaultURLs: map[types.ModelType]string{
|
||||||
|
types.ModelTypeKnowledgeQA: OpenRouterBaseURL,
|
||||||
|
types.ModelTypeVLLM: OpenRouterBaseURL,
|
||||||
|
},
|
||||||
|
ModelTypes: []types.ModelType{
|
||||||
|
types.ModelTypeKnowledgeQA,
|
||||||
|
types.ModelTypeVLLM,
|
||||||
|
},
|
||||||
|
RequiresAuth: true,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// ValidateConfig 验证 OpenRouter provider 配置
|
||||||
|
func (p *OpenRouterProvider) ValidateConfig(config *Config) error {
|
||||||
|
if config.APIKey == "" {
|
||||||
|
return fmt.Errorf("API key is required for OpenRouter provider")
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
@@ -0,0 +1,242 @@
|
|||||||
|
// Package provider defines the unified interface and registry for multi-vendor model API adapters.
|
||||||
|
package provider
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"strings"
|
||||||
|
"sync"
|
||||||
|
|
||||||
|
"github.com/Tencent/WeKnora/internal/types"
|
||||||
|
)
|
||||||
|
|
||||||
|
// ProviderName 模型服务商名称
|
||||||
|
type ProviderName string
|
||||||
|
|
||||||
|
const (
|
||||||
|
// OpenAI
|
||||||
|
ProviderOpenAI ProviderName = "openai"
|
||||||
|
// 阿里云 DashScope
|
||||||
|
ProviderAliyun ProviderName = "aliyun"
|
||||||
|
// 智谱AI (GLM 系列)
|
||||||
|
ProviderZhipu ProviderName = "zhipu"
|
||||||
|
// OpenRouter
|
||||||
|
ProviderOpenRouter ProviderName = "openrouter"
|
||||||
|
// 硅基流动
|
||||||
|
ProviderSiliconFlow ProviderName = "siliconflow"
|
||||||
|
// Jina AI (Embedding and Rerank)
|
||||||
|
ProviderJina ProviderName = "jina"
|
||||||
|
// Generic 兼容OpenAI (自定义部署)
|
||||||
|
ProviderGeneric ProviderName = "generic"
|
||||||
|
// DeepSeek
|
||||||
|
ProviderDeepSeek ProviderName = "deepseek"
|
||||||
|
// Google Gemini
|
||||||
|
ProviderGemini ProviderName = "gemini"
|
||||||
|
// 火山引擎 Ark
|
||||||
|
ProviderVolcengine ProviderName = "volcengine"
|
||||||
|
// 腾讯混元
|
||||||
|
ProviderHunyuan ProviderName = "hunyuan"
|
||||||
|
// MiniMax
|
||||||
|
ProviderMiniMax ProviderName = "minimax"
|
||||||
|
// 小米 Mimo
|
||||||
|
ProviderMimo ProviderName = "mimo"
|
||||||
|
)
|
||||||
|
|
||||||
|
// AllProviders 返回所有注册的提供者名称
|
||||||
|
func AllProviders() []ProviderName {
|
||||||
|
return []ProviderName{
|
||||||
|
ProviderGeneric,
|
||||||
|
ProviderAliyun,
|
||||||
|
ProviderZhipu,
|
||||||
|
ProviderVolcengine,
|
||||||
|
ProviderHunyuan,
|
||||||
|
ProviderSiliconFlow,
|
||||||
|
ProviderDeepSeek,
|
||||||
|
ProviderMiniMax,
|
||||||
|
ProviderOpenAI,
|
||||||
|
ProviderGemini,
|
||||||
|
ProviderOpenRouter,
|
||||||
|
ProviderJina,
|
||||||
|
ProviderMimo,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// ProviderInfo 包含提供者的元数据
|
||||||
|
type ProviderInfo struct {
|
||||||
|
Name ProviderName // 提供者标识
|
||||||
|
DisplayName string // 可读名称
|
||||||
|
Description string // 提供者描述
|
||||||
|
DefaultURLs map[types.ModelType]string // 按模型类型区分的默认 BaseURL
|
||||||
|
ModelTypes []types.ModelType // 支持的模型类型
|
||||||
|
RequiresAuth bool // 是否需要 API key
|
||||||
|
ExtraFields []ExtraFieldConfig // 额外配置字段
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetDefaultURL 获取指定模型类型的默认 URL
|
||||||
|
func (p ProviderInfo) GetDefaultURL(modelType types.ModelType) string {
|
||||||
|
if url, ok := p.DefaultURLs[modelType]; ok {
|
||||||
|
return url
|
||||||
|
}
|
||||||
|
// 回退到 Chat URL
|
||||||
|
if url, ok := p.DefaultURLs[types.ModelTypeKnowledgeQA]; ok {
|
||||||
|
return url
|
||||||
|
}
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
||||||
|
// ExtraFieldConfig 定义提供者的额外配置字段
|
||||||
|
type ExtraFieldConfig struct {
|
||||||
|
Key string `json:"key"`
|
||||||
|
Label string `json:"label"`
|
||||||
|
Type string `json:"type"` // "string", "number", "boolean", "select"
|
||||||
|
Required bool `json:"required"`
|
||||||
|
Default string `json:"default"`
|
||||||
|
Placeholder string `json:"placeholder"`
|
||||||
|
Options []struct {
|
||||||
|
Label string `json:"label"`
|
||||||
|
Value string `json:"value"`
|
||||||
|
} `json:"options,omitempty"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// Config 表示模型提供者的配置
|
||||||
|
type Config struct {
|
||||||
|
Provider ProviderName `json:"provider"`
|
||||||
|
BaseURL string `json:"base_url"`
|
||||||
|
APIKey string `json:"api_key"`
|
||||||
|
ModelName string `json:"model_name"`
|
||||||
|
ModelID string `json:"model_id"`
|
||||||
|
Extra map[string]any `json:"extra,omitempty"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type Provider interface {
|
||||||
|
// Info 返回服务商的元数据
|
||||||
|
Info() ProviderInfo
|
||||||
|
|
||||||
|
// ValidateConfig 验证服务商的配置
|
||||||
|
ValidateConfig(config *Config) error
|
||||||
|
}
|
||||||
|
|
||||||
|
// registry 存储所有注册的提供者
|
||||||
|
var (
|
||||||
|
registryMu sync.RWMutex
|
||||||
|
registry = make(map[ProviderName]Provider)
|
||||||
|
)
|
||||||
|
|
||||||
|
// Register 添加一个提供者到全局注册表
|
||||||
|
func Register(p Provider) {
|
||||||
|
registryMu.Lock()
|
||||||
|
defer registryMu.Unlock()
|
||||||
|
registry[p.Info().Name] = p
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get 通过名称从注册表中获取提供者
|
||||||
|
func Get(name ProviderName) (Provider, bool) {
|
||||||
|
registryMu.RLock()
|
||||||
|
defer registryMu.RUnlock()
|
||||||
|
p, ok := registry[name]
|
||||||
|
return p, ok
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetOrDefault 通过名称从注册表中获取提供者,如果未找到则返回默认提供者
|
||||||
|
func GetOrDefault(name ProviderName) Provider {
|
||||||
|
p, ok := Get(name)
|
||||||
|
if ok {
|
||||||
|
return p
|
||||||
|
}
|
||||||
|
// 如果未找到则返回默认提供者
|
||||||
|
p, _ = Get(ProviderGeneric)
|
||||||
|
return p
|
||||||
|
}
|
||||||
|
|
||||||
|
// List 返回所有注册的提供者(按 AllProviders 定义的顺序)
|
||||||
|
func List() []ProviderInfo {
|
||||||
|
registryMu.RLock()
|
||||||
|
defer registryMu.RUnlock()
|
||||||
|
|
||||||
|
result := make([]ProviderInfo, 0, len(registry))
|
||||||
|
for _, name := range AllProviders() {
|
||||||
|
if p, ok := registry[name]; ok {
|
||||||
|
result = append(result, p.Info())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
|
// ListByModelType 返回所有支持指定模型类型的提供者(按 AllProviders 定义的顺序)
|
||||||
|
func ListByModelType(modelType types.ModelType) []ProviderInfo {
|
||||||
|
registryMu.RLock()
|
||||||
|
defer registryMu.RUnlock()
|
||||||
|
|
||||||
|
result := make([]ProviderInfo, 0)
|
||||||
|
for _, name := range AllProviders() {
|
||||||
|
if p, ok := registry[name]; ok {
|
||||||
|
info := p.Info()
|
||||||
|
for _, t := range info.ModelTypes {
|
||||||
|
if t == modelType {
|
||||||
|
result = append(result, info)
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
|
// DetectProvider 通过 BaseURL 检测服务商
|
||||||
|
func DetectProvider(baseURL string) ProviderName {
|
||||||
|
switch {
|
||||||
|
case containsAny(baseURL, "dashscope.aliyuncs.com"):
|
||||||
|
return ProviderAliyun
|
||||||
|
case containsAny(baseURL, "open.bigmodel.cn", "zhipu"):
|
||||||
|
return ProviderZhipu
|
||||||
|
case containsAny(baseURL, "openrouter.ai"):
|
||||||
|
return ProviderOpenRouter
|
||||||
|
case containsAny(baseURL, "siliconflow.cn"):
|
||||||
|
return ProviderSiliconFlow
|
||||||
|
case containsAny(baseURL, "api.jina.ai"):
|
||||||
|
return ProviderJina
|
||||||
|
case containsAny(baseURL, "api.openai.com"):
|
||||||
|
return ProviderOpenAI
|
||||||
|
case containsAny(baseURL, "api.deepseek.com"):
|
||||||
|
return ProviderDeepSeek
|
||||||
|
case containsAny(baseURL, "generativelanguage.googleapis.com"):
|
||||||
|
return ProviderGemini
|
||||||
|
case containsAny(baseURL, "volces.com", "volcengine"):
|
||||||
|
return ProviderVolcengine
|
||||||
|
case containsAny(baseURL, "hunyuan.cloud.tencent.com"):
|
||||||
|
return ProviderHunyuan
|
||||||
|
case containsAny(baseURL, "minimax.io", "minimaxi.com"):
|
||||||
|
return ProviderMiniMax
|
||||||
|
case containsAny(baseURL, "mimo.xiaomi.com"):
|
||||||
|
return ProviderMimo
|
||||||
|
default:
|
||||||
|
return ProviderGeneric
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func containsAny(s string, substrs ...string) bool {
|
||||||
|
for _, sub := range substrs {
|
||||||
|
if strings.Contains(s, sub) {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewConfigFromModel(model *types.Model) (*Config, error) {
|
||||||
|
if model == nil {
|
||||||
|
return nil, fmt.Errorf("model is nil")
|
||||||
|
}
|
||||||
|
|
||||||
|
providerName := ProviderName(model.Parameters.Provider)
|
||||||
|
if providerName == "" {
|
||||||
|
providerName = DetectProvider(model.Parameters.BaseURL)
|
||||||
|
}
|
||||||
|
|
||||||
|
return &Config{
|
||||||
|
Provider: providerName,
|
||||||
|
BaseURL: model.Parameters.BaseURL,
|
||||||
|
APIKey: model.Parameters.APIKey,
|
||||||
|
ModelName: model.Name,
|
||||||
|
ModelID: model.ID,
|
||||||
|
}, nil
|
||||||
|
}
|
||||||
@@ -0,0 +1,170 @@
|
|||||||
|
package provider
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/Tencent/WeKnora/internal/types"
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
"github.com/stretchr/testify/require"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestProviderRegistry(t *testing.T) {
|
||||||
|
// Test that all default providers are registered
|
||||||
|
t.Run("default providers registered", func(t *testing.T) {
|
||||||
|
providers := List()
|
||||||
|
assert.NotEmpty(t, providers, "should have registered providers")
|
||||||
|
|
||||||
|
// Check specific providers exist
|
||||||
|
for _, name := range []ProviderName{ProviderOpenAI, ProviderAliyun, ProviderZhipu, ProviderGeneric} {
|
||||||
|
p, ok := Get(name)
|
||||||
|
assert.True(t, ok, "provider %s should be registered", name)
|
||||||
|
assert.NotNil(t, p, "provider %s should not be nil", name)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("GetOrDefault fallback", func(t *testing.T) {
|
||||||
|
// Non-existent provider should fall back to generic
|
||||||
|
p := GetOrDefault("nonexistent")
|
||||||
|
require.NotNil(t, p)
|
||||||
|
assert.Equal(t, ProviderGeneric, p.Info().Name)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestDetectProvider(t *testing.T) {
|
||||||
|
tests := []struct {
|
||||||
|
url string
|
||||||
|
expected ProviderName
|
||||||
|
}{
|
||||||
|
{"https://api.openai.com/v1", ProviderOpenAI},
|
||||||
|
{"https://openrouter.ai/api/v1", ProviderOpenRouter},
|
||||||
|
{"https://dashscope.aliyuncs.com/compatible-mode/v1", ProviderAliyun},
|
||||||
|
{"https://open.bigmodel.cn/api/paas/v4", ProviderZhipu},
|
||||||
|
{"https://api.deepseek.com/v1", ProviderDeepSeek},
|
||||||
|
{"https://generativelanguage.googleapis.com/v1beta/openai", ProviderGemini},
|
||||||
|
{"https://ark.cn-beijing.volces.com/api/v3", ProviderVolcengine},
|
||||||
|
{"https://api.hunyuan.cloud.tencent.com/v1", ProviderHunyuan},
|
||||||
|
{"https://api.minimaxi.com/v1", ProviderMiniMax},
|
||||||
|
{"https://api.minimax.io/v1", ProviderMiniMax},
|
||||||
|
{"https://api.mimo.xiaomi.com/v1", ProviderMimo},
|
||||||
|
{"https://custom-endpoint.example.com/v1", ProviderGeneric},
|
||||||
|
{"http://localhost:11434/v1", ProviderGeneric},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tt := range tests {
|
||||||
|
t.Run(tt.url, func(t *testing.T) {
|
||||||
|
result := DetectProvider(tt.url)
|
||||||
|
assert.Equal(t, tt.expected, result)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestOpenAIProviderValidation(t *testing.T) {
|
||||||
|
p := &OpenAIProvider{}
|
||||||
|
|
||||||
|
t.Run("valid config", func(t *testing.T) {
|
||||||
|
config := &Config{
|
||||||
|
APIKey: "sk-test",
|
||||||
|
ModelName: "gpt-4",
|
||||||
|
}
|
||||||
|
err := p.ValidateConfig(config)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("missing API key", func(t *testing.T) {
|
||||||
|
config := &Config{
|
||||||
|
ModelName: "gpt-4",
|
||||||
|
}
|
||||||
|
err := p.ValidateConfig(config)
|
||||||
|
assert.Error(t, err)
|
||||||
|
assert.Contains(t, err.Error(), "API key")
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("missing model name", func(t *testing.T) {
|
||||||
|
config := &Config{
|
||||||
|
APIKey: "sk-test",
|
||||||
|
}
|
||||||
|
err := p.ValidateConfig(config)
|
||||||
|
assert.Error(t, err)
|
||||||
|
assert.Contains(t, err.Error(), "model name")
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestAliyunProviderValidation(t *testing.T) {
|
||||||
|
p := &AliyunProvider{}
|
||||||
|
|
||||||
|
t.Run("valid config", func(t *testing.T) {
|
||||||
|
config := &Config{
|
||||||
|
APIKey: "sk-test",
|
||||||
|
ModelName: "qwen-max",
|
||||||
|
}
|
||||||
|
err := p.ValidateConfig(config)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("info", func(t *testing.T) {
|
||||||
|
info := p.Info()
|
||||||
|
assert.Equal(t, ProviderAliyun, info.Name)
|
||||||
|
assert.Contains(t, info.ModelTypes, types.ModelTypeKnowledgeQA)
|
||||||
|
assert.Contains(t, info.ModelTypes, types.ModelTypeEmbedding)
|
||||||
|
assert.Contains(t, info.ModelTypes, types.ModelTypeRerank)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestAliyunModelDetection(t *testing.T) {
|
||||||
|
t.Run("Qwen3 model detection", func(t *testing.T) {
|
||||||
|
assert.True(t, IsQwen3Model("qwen3-32b"))
|
||||||
|
assert.True(t, IsQwen3Model("qwen3-72b"))
|
||||||
|
assert.False(t, IsQwen3Model("qwen-max"))
|
||||||
|
assert.False(t, IsQwen3Model("qwen2.5-72b"))
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("DeepSeek model detection", func(t *testing.T) {
|
||||||
|
assert.True(t, IsDeepSeekModel("deepseek-chat"))
|
||||||
|
assert.True(t, IsDeepSeekModel("deepseek-v3.1"))
|
||||||
|
assert.True(t, IsDeepSeekModel("DeepSeek-Chat"))
|
||||||
|
assert.False(t, IsDeepSeekModel("qwen-max"))
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestZhipuProviderValidation(t *testing.T) {
|
||||||
|
p := &ZhipuProvider{}
|
||||||
|
|
||||||
|
t.Run("valid config", func(t *testing.T) {
|
||||||
|
config := &Config{
|
||||||
|
APIKey: "test-key",
|
||||||
|
ModelName: "glm-4",
|
||||||
|
}
|
||||||
|
err := p.ValidateConfig(config)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("info", func(t *testing.T) {
|
||||||
|
info := p.Info()
|
||||||
|
assert.Equal(t, ProviderZhipu, info.Name)
|
||||||
|
assert.Equal(t, ZhipuChatBaseURL, info.GetDefaultURL(types.ModelTypeKnowledgeQA))
|
||||||
|
assert.Equal(t, ZhipuEmbeddingBaseURL, info.GetDefaultURL(types.ModelTypeEmbedding))
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestListByModelType(t *testing.T) {
|
||||||
|
t.Run("chat models", func(t *testing.T) {
|
||||||
|
providers := ListByModelType(types.ModelTypeKnowledgeQA)
|
||||||
|
assert.NotEmpty(t, providers)
|
||||||
|
// Multiple providers support chat
|
||||||
|
assert.GreaterOrEqual(t, len(providers), 9)
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("rerank models", func(t *testing.T) {
|
||||||
|
providers := ListByModelType(types.ModelTypeRerank)
|
||||||
|
assert.NotEmpty(t, providers)
|
||||||
|
// Check that Aliyun supports rerank
|
||||||
|
found := false
|
||||||
|
for _, p := range providers {
|
||||||
|
if p.Name == ProviderAliyun {
|
||||||
|
found = true
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
assert.True(t, found, "Aliyun should support rerank")
|
||||||
|
})
|
||||||
|
}
|
||||||
@@ -0,0 +1,48 @@
|
|||||||
|
package provider
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
"github.com/Tencent/WeKnora/internal/types"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
SiliconFlowBaseURL = "https://api.siliconflow.cn/v1"
|
||||||
|
)
|
||||||
|
|
||||||
|
// SiliconFlowProvider 实现硅基流动的 Provider 接口
|
||||||
|
type SiliconFlowProvider struct{}
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
Register(&SiliconFlowProvider{})
|
||||||
|
}
|
||||||
|
|
||||||
|
// Info 返回硅基流动 provider 的元数据
|
||||||
|
func (p *SiliconFlowProvider) Info() ProviderInfo {
|
||||||
|
return ProviderInfo{
|
||||||
|
Name: ProviderSiliconFlow,
|
||||||
|
DisplayName: "硅基流动 SiliconFlow",
|
||||||
|
Description: "deepseek-ai/DeepSeek-V3.1, etc.",
|
||||||
|
DefaultURLs: map[types.ModelType]string{
|
||||||
|
types.ModelTypeKnowledgeQA: SiliconFlowBaseURL,
|
||||||
|
types.ModelTypeEmbedding: SiliconFlowBaseURL,
|
||||||
|
types.ModelTypeRerank: SiliconFlowBaseURL,
|
||||||
|
types.ModelTypeVLLM: SiliconFlowBaseURL,
|
||||||
|
},
|
||||||
|
ModelTypes: []types.ModelType{
|
||||||
|
types.ModelTypeKnowledgeQA,
|
||||||
|
types.ModelTypeEmbedding,
|
||||||
|
types.ModelTypeRerank,
|
||||||
|
types.ModelTypeVLLM,
|
||||||
|
},
|
||||||
|
RequiresAuth: true,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// ValidateConfig 验证硅基流动 provider 配置
|
||||||
|
func (p *SiliconFlowProvider) ValidateConfig(config *Config) error {
|
||||||
|
if config.APIKey == "" {
|
||||||
|
return fmt.Errorf("API key is required for SiliconFlow provider")
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
@@ -0,0 +1,52 @@
|
|||||||
|
package provider
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
"github.com/Tencent/WeKnora/internal/types"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
// VolcengineChatBaseURL 火山引擎 Ark Chat API BaseURL (OpenAI 兼容模式)
|
||||||
|
VolcengineChatBaseURL = "https://ark.cn-beijing.volces.com/api/v3"
|
||||||
|
// VolcengineEmbeddingBaseURL 火山引擎 Ark Multimodal Embedding API BaseURL
|
||||||
|
VolcengineEmbeddingBaseURL = "https://ark.cn-beijing.volces.com/api/v3/embeddings/multimodal"
|
||||||
|
)
|
||||||
|
|
||||||
|
// VolcengineProvider 实现火山引擎 Ark 的 Provider 接口
|
||||||
|
type VolcengineProvider struct{}
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
Register(&VolcengineProvider{})
|
||||||
|
}
|
||||||
|
|
||||||
|
// Info 返回火山引擎 provider 的元数据
|
||||||
|
func (p *VolcengineProvider) Info() ProviderInfo {
|
||||||
|
return ProviderInfo{
|
||||||
|
Name: ProviderVolcengine,
|
||||||
|
DisplayName: "火山引擎 Volcengine",
|
||||||
|
Description: "doubao-1-5-pro-32k-250115, doubao-embedding-vision-250615, etc.",
|
||||||
|
DefaultURLs: map[types.ModelType]string{
|
||||||
|
types.ModelTypeKnowledgeQA: VolcengineChatBaseURL,
|
||||||
|
types.ModelTypeEmbedding: VolcengineEmbeddingBaseURL,
|
||||||
|
types.ModelTypeVLLM: VolcengineChatBaseURL,
|
||||||
|
},
|
||||||
|
ModelTypes: []types.ModelType{
|
||||||
|
types.ModelTypeKnowledgeQA,
|
||||||
|
types.ModelTypeEmbedding,
|
||||||
|
types.ModelTypeVLLM,
|
||||||
|
},
|
||||||
|
RequiresAuth: true,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// ValidateConfig 验证火山引擎 provider 配置
|
||||||
|
func (p *VolcengineProvider) ValidateConfig(config *Config) error {
|
||||||
|
if config.APIKey == "" {
|
||||||
|
return fmt.Errorf("API key is required for Volcengine Ark provider")
|
||||||
|
}
|
||||||
|
if config.ModelName == "" {
|
||||||
|
return fmt.Errorf("model name is required")
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
@@ -0,0 +1,56 @@
|
|||||||
|
package provider
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
"github.com/Tencent/WeKnora/internal/types"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
// ZhipuChatBaseURL 智谱 AI Chat 的默认 BaseURL
|
||||||
|
ZhipuChatBaseURL = "https://open.bigmodel.cn/api/paas/v4"
|
||||||
|
// ZhipuEmbeddingBaseURL 智谱 AI Embedding 的默认 BaseURL
|
||||||
|
ZhipuEmbeddingBaseURL = "https://open.bigmodel.cn/api/paas/v4/embeddings"
|
||||||
|
// ZhipuRerankBaseURL 智谱 AI Rerank 的默认 BaseURL
|
||||||
|
ZhipuRerankBaseURL = "https://open.bigmodel.cn/api/paas/v4/rerank"
|
||||||
|
)
|
||||||
|
|
||||||
|
// ZhipuProvider 实现智谱 AI 的 Provider 接口
|
||||||
|
type ZhipuProvider struct{}
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
Register(&ZhipuProvider{})
|
||||||
|
}
|
||||||
|
|
||||||
|
// Info 返回智谱 AI provider 的元数据
|
||||||
|
func (p *ZhipuProvider) Info() ProviderInfo {
|
||||||
|
return ProviderInfo{
|
||||||
|
Name: ProviderZhipu,
|
||||||
|
DisplayName: "智谱 BigModel",
|
||||||
|
Description: "glm-4.7, embedding-3, rerank, etc.",
|
||||||
|
DefaultURLs: map[types.ModelType]string{
|
||||||
|
types.ModelTypeKnowledgeQA: ZhipuChatBaseURL,
|
||||||
|
types.ModelTypeEmbedding: ZhipuEmbeddingBaseURL,
|
||||||
|
types.ModelTypeRerank: ZhipuRerankBaseURL,
|
||||||
|
types.ModelTypeVLLM: ZhipuChatBaseURL,
|
||||||
|
},
|
||||||
|
ModelTypes: []types.ModelType{
|
||||||
|
types.ModelTypeKnowledgeQA,
|
||||||
|
types.ModelTypeEmbedding,
|
||||||
|
types.ModelTypeRerank,
|
||||||
|
types.ModelTypeVLLM,
|
||||||
|
},
|
||||||
|
RequiresAuth: true,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// ValidateConfig 验证智谱 AI provider 配置
|
||||||
|
func (p *ZhipuProvider) ValidateConfig(config *Config) error {
|
||||||
|
if config.APIKey == "" {
|
||||||
|
return fmt.Errorf("API key is required for Zhipu AI")
|
||||||
|
}
|
||||||
|
if config.ModelName == "" {
|
||||||
|
return fmt.Errorf("model name is required")
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
@@ -0,0 +1,121 @@
|
|||||||
|
package rerank
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"context"
|
||||||
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
|
"net/http"
|
||||||
|
|
||||||
|
"github.com/Tencent/WeKnora/internal/logger"
|
||||||
|
)
|
||||||
|
|
||||||
|
// JinaReranker implements a reranking system using Jina AI API
|
||||||
|
// Jina API uses different parameters than standard OpenAI-compatible APIs
|
||||||
|
type JinaReranker struct {
|
||||||
|
modelName string // Name of the model used for reranking
|
||||||
|
modelID string // Unique identifier of the model
|
||||||
|
apiKey string // API key for authentication
|
||||||
|
baseURL string // Base URL for API requests
|
||||||
|
client *http.Client // HTTP client for making API requests
|
||||||
|
}
|
||||||
|
|
||||||
|
// JinaRerankRequest represents a Jina rerank request
|
||||||
|
// Note: Jina does NOT support truncate_prompt_tokens parameter
|
||||||
|
type JinaRerankRequest struct {
|
||||||
|
Model string `json:"model"` // Model to use for reranking
|
||||||
|
Query string `json:"query"` // Query text to compare documents against
|
||||||
|
Documents []string `json:"documents"` // List of document texts to rerank
|
||||||
|
TopN int `json:"top_n,omitempty"` // Number of top results to return
|
||||||
|
ReturnDocuments bool `json:"return_documents,omitempty"` // Whether to return document text in response
|
||||||
|
}
|
||||||
|
|
||||||
|
// JinaRerankResponse represents the response from a Jina reranking request
|
||||||
|
type JinaRerankResponse struct {
|
||||||
|
Model string `json:"model"` // Model used for reranking
|
||||||
|
Results []RankResult `json:"results"` // Ranked results with relevance scores
|
||||||
|
Usage struct {
|
||||||
|
TotalTokens int `json:"total_tokens"` // Total tokens consumed
|
||||||
|
} `json:"usage"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewJinaReranker creates a new instance of Jina reranker with the provided configuration
|
||||||
|
func NewJinaReranker(config *RerankerConfig) (*JinaReranker, error) {
|
||||||
|
apiKey := config.APIKey
|
||||||
|
baseURL := "https://api.jina.ai/v1"
|
||||||
|
if url := config.BaseURL; url != "" {
|
||||||
|
baseURL = url
|
||||||
|
}
|
||||||
|
|
||||||
|
return &JinaReranker{
|
||||||
|
modelName: config.ModelName,
|
||||||
|
modelID: config.ModelID,
|
||||||
|
apiKey: apiKey,
|
||||||
|
baseURL: baseURL,
|
||||||
|
client: &http.Client{},
|
||||||
|
}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Rerank performs document reranking based on relevance to the query
|
||||||
|
func (r *JinaReranker) Rerank(ctx context.Context, query string, documents []string) ([]RankResult, error) {
|
||||||
|
// Build the request body - Jina does NOT use truncate_prompt_tokens
|
||||||
|
requestBody := &JinaRerankRequest{
|
||||||
|
Model: r.modelName,
|
||||||
|
Query: query,
|
||||||
|
Documents: documents,
|
||||||
|
ReturnDocuments: true, // Return document text in response
|
||||||
|
}
|
||||||
|
|
||||||
|
jsonData, err := json.Marshal(requestBody)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("marshal request body: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Send the request
|
||||||
|
req, err := http.NewRequestWithContext(ctx, "POST", fmt.Sprintf("%s/rerank", r.baseURL), bytes.NewBuffer(jsonData))
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("create request: %w", err)
|
||||||
|
}
|
||||||
|
req.Header.Set("Content-Type", "application/json")
|
||||||
|
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", r.apiKey))
|
||||||
|
|
||||||
|
// Log the curl equivalent for debugging (API key masked for security)
|
||||||
|
logger.GetLogger(ctx).Infof(
|
||||||
|
"curl -X POST %s/rerank -H \"Content-Type: application/json\" -H \"Authorization: Bearer ***\" -d '%s'",
|
||||||
|
r.baseURL, string(jsonData),
|
||||||
|
)
|
||||||
|
|
||||||
|
resp, err := r.client.Do(req)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("do request: %w", err)
|
||||||
|
}
|
||||||
|
defer resp.Body.Close()
|
||||||
|
|
||||||
|
// Read the response
|
||||||
|
body, err := io.ReadAll(resp.Body)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("read response body: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if resp.StatusCode != http.StatusOK {
|
||||||
|
logger.GetLogger(ctx).Errorf("JinaReranker API error: Http Status: %s, Body: %s", resp.Status, string(body))
|
||||||
|
return nil, fmt.Errorf("Rerank API error: Http Status: %s", resp.Status)
|
||||||
|
}
|
||||||
|
|
||||||
|
var response JinaRerankResponse
|
||||||
|
if err := json.Unmarshal(body, &response); err != nil {
|
||||||
|
return nil, fmt.Errorf("unmarshal response: %w", err)
|
||||||
|
}
|
||||||
|
return response.Results, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetModelName returns the name of the reranking model
|
||||||
|
func (r *JinaReranker) GetModelName() string {
|
||||||
|
return r.modelName
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetModelID returns the unique identifier of the reranking model
|
||||||
|
func (r *JinaReranker) GetModelID() string {
|
||||||
|
return r.modelID
|
||||||
|
}
|
||||||
@@ -4,8 +4,8 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"strings"
|
|
||||||
|
|
||||||
|
"github.com/Tencent/WeKnora/internal/models/provider"
|
||||||
"github.com/Tencent/WeKnora/internal/types"
|
"github.com/Tencent/WeKnora/internal/types"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -83,17 +83,25 @@ type RerankerConfig struct {
|
|||||||
ModelName string
|
ModelName string
|
||||||
Source types.ModelSource
|
Source types.ModelSource
|
||||||
ModelID string
|
ModelID string
|
||||||
|
Provider string // Provider identifier: openai, aliyun, zhipu, siliconflow, jina, generic
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewReranker creates a reranker
|
// NewReranker creates a reranker based on the configuration
|
||||||
func NewReranker(config *RerankerConfig) (Reranker, error) {
|
func NewReranker(config *RerankerConfig) (Reranker, error) {
|
||||||
// 根据URL判断模型来源,而不是依赖Source字段
|
// Use provider field if set, otherwise detect from URL using provider registry
|
||||||
if strings.Contains(
|
providerName := provider.ProviderName(config.Provider)
|
||||||
config.BaseURL,
|
if providerName == "" {
|
||||||
"https://dashscope.aliyuncs.com/api/v1/services/rerank/text-rerank/text-rerank",
|
providerName = provider.DetectProvider(config.BaseURL)
|
||||||
) {
|
}
|
||||||
|
|
||||||
|
switch providerName {
|
||||||
|
case provider.ProviderAliyun:
|
||||||
return NewAliyunReranker(config)
|
return NewAliyunReranker(config)
|
||||||
} else {
|
case provider.ProviderZhipu:
|
||||||
|
return NewZhipuReranker(config)
|
||||||
|
case provider.ProviderJina:
|
||||||
|
return NewJinaReranker(config)
|
||||||
|
default:
|
||||||
return NewOpenAIReranker(config)
|
return NewOpenAIReranker(config)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,145 @@
|
|||||||
|
package rerank
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"context"
|
||||||
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
|
"net/http"
|
||||||
|
|
||||||
|
"github.com/Tencent/WeKnora/internal/logger"
|
||||||
|
)
|
||||||
|
|
||||||
|
// ZhipuReranker implements a reranking system based on Zhipu AI models
|
||||||
|
type ZhipuReranker struct {
|
||||||
|
modelName string // Name of the model used for reranking
|
||||||
|
modelID string // Unique identifier of the model
|
||||||
|
apiKey string // API key for authentication
|
||||||
|
baseURL string // Base URL for API requests
|
||||||
|
client *http.Client // HTTP client for making API requests
|
||||||
|
}
|
||||||
|
|
||||||
|
// ZhipuRerankRequest represents a request to rerank documents using Zhipu AI API
|
||||||
|
type ZhipuRerankRequest struct {
|
||||||
|
Model string `json:"model"` // Model to use for reranking
|
||||||
|
Query string `json:"query"` // Query text to compare documents against
|
||||||
|
Documents []string `json:"documents"` // List of document texts to rerank
|
||||||
|
TopN int `json:"top_n,omitempty"` // Number of top results to return (0 = all)
|
||||||
|
ReturnDocuments bool `json:"return_documents,omitempty"` // Whether to return documents in response
|
||||||
|
ReturnRawScores bool `json:"return_raw_scores,omitempty"` // Whether to return raw scores
|
||||||
|
}
|
||||||
|
|
||||||
|
// ZhipuRerankResponse represents the response from Zhipu AI reranking request
|
||||||
|
type ZhipuRerankResponse struct {
|
||||||
|
RequestID string `json:"request_id"` // Request ID from client or platform
|
||||||
|
ID string `json:"id"` // Task order ID from Zhipu platform
|
||||||
|
Results []ZhipuRankResult `json:"results"` // Ranked results with relevance scores
|
||||||
|
Usage ZhipuUsage `json:"usage"` // Token usage information
|
||||||
|
}
|
||||||
|
|
||||||
|
// ZhipuRankResult represents a single reranking result from Zhipu AI
|
||||||
|
type ZhipuRankResult struct {
|
||||||
|
Index int `json:"index"` // Original index of the document
|
||||||
|
RelevanceScore float64 `json:"relevance_score"` // Relevance score
|
||||||
|
Document string `json:"document,omitempty"` // Document text (optional)
|
||||||
|
}
|
||||||
|
|
||||||
|
// ZhipuUsage contains information about token usage in the Zhipu API request
|
||||||
|
type ZhipuUsage struct {
|
||||||
|
TotalTokens int `json:"total_tokens"` // Total tokens consumed
|
||||||
|
PromptTokens int `json:"prompt_tokens"` // Prompt tokens
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewZhipuReranker creates a new instance of Zhipu reranker with the provided configuration
|
||||||
|
func NewZhipuReranker(config *RerankerConfig) (*ZhipuReranker, error) {
|
||||||
|
apiKey := config.APIKey
|
||||||
|
baseURL := "https://open.bigmodel.cn/api/paas/v4/rerank"
|
||||||
|
if url := config.BaseURL; url != "" {
|
||||||
|
baseURL = url
|
||||||
|
}
|
||||||
|
|
||||||
|
return &ZhipuReranker{
|
||||||
|
modelName: config.ModelName,
|
||||||
|
modelID: config.ModelID,
|
||||||
|
apiKey: apiKey,
|
||||||
|
baseURL: baseURL,
|
||||||
|
client: &http.Client{},
|
||||||
|
}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Rerank performs document reranking based on relevance to the query using Zhipu AI API
|
||||||
|
func (r *ZhipuReranker) Rerank(ctx context.Context, query string, documents []string) ([]RankResult, error) {
|
||||||
|
// Build the request body
|
||||||
|
requestBody := &ZhipuRerankRequest{
|
||||||
|
Model: r.modelName,
|
||||||
|
Query: query,
|
||||||
|
Documents: documents,
|
||||||
|
TopN: 0, // Return all documents
|
||||||
|
ReturnDocuments: true,
|
||||||
|
ReturnRawScores: false,
|
||||||
|
}
|
||||||
|
|
||||||
|
jsonData, err := json.Marshal(requestBody)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("marshal request body: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Send the request
|
||||||
|
req, err := http.NewRequestWithContext(ctx, "POST", r.baseURL, bytes.NewBuffer(jsonData))
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("create request: %w", err)
|
||||||
|
}
|
||||||
|
req.Header.Set("Content-Type", "application/json")
|
||||||
|
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", r.apiKey))
|
||||||
|
|
||||||
|
// Log the curl equivalent for debugging
|
||||||
|
logger.Debugf(ctx, "curl -X POST %s -H \"Content-Type: application/json\" -H \"Authorization: Bearer %s\" -d '%s'",
|
||||||
|
r.baseURL, r.apiKey, string(jsonData),
|
||||||
|
)
|
||||||
|
|
||||||
|
resp, err := r.client.Do(req)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("do request: %w", err)
|
||||||
|
}
|
||||||
|
defer resp.Body.Close()
|
||||||
|
|
||||||
|
// Read the response
|
||||||
|
body, err := io.ReadAll(resp.Body)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("read response body: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if resp.StatusCode != http.StatusOK {
|
||||||
|
return nil, fmt.Errorf("zhipu rerank API error: Http Status: %s, Body: %s", resp.Status, string(body))
|
||||||
|
}
|
||||||
|
|
||||||
|
var response ZhipuRerankResponse
|
||||||
|
if err := json.Unmarshal(body, &response); err != nil {
|
||||||
|
return nil, fmt.Errorf("unmarshal response: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Convert Zhipu results to standard RankResult format
|
||||||
|
results := make([]RankResult, len(response.Results))
|
||||||
|
for i, zhipuResult := range response.Results {
|
||||||
|
results[i] = RankResult{
|
||||||
|
Index: zhipuResult.Index,
|
||||||
|
Document: DocumentInfo{
|
||||||
|
Text: zhipuResult.Document,
|
||||||
|
},
|
||||||
|
RelevanceScore: zhipuResult.RelevanceScore,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return results, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetModelName returns the name of the reranking model
|
||||||
|
func (r *ZhipuReranker) GetModelName() string {
|
||||||
|
return r.modelName
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetModelID returns the unique identifier of the reranking model
|
||||||
|
func (r *ZhipuReranker) GetModelID() string {
|
||||||
|
return r.modelID
|
||||||
|
}
|
||||||
@@ -313,6 +313,8 @@ func RegisterModelRoutes(r *gin.RouterGroup, handler *handler.ModelHandler) {
|
|||||||
// 模型路由组
|
// 模型路由组
|
||||||
models := r.Group("/models")
|
models := r.Group("/models")
|
||||||
{
|
{
|
||||||
|
// 获取模型厂商列表
|
||||||
|
models.GET("/providers", handler.ListModelProviders)
|
||||||
// 创建模型
|
// 创建模型
|
||||||
models.POST("", handler.CreateModel)
|
models.POST("", handler.CreateModel)
|
||||||
// 获取模型列表
|
// 获取模型列表
|
||||||
|
|||||||
+16
-3
@@ -32,9 +32,20 @@ const (
|
|||||||
type ModelSource string
|
type ModelSource string
|
||||||
|
|
||||||
const (
|
const (
|
||||||
ModelSourceLocal ModelSource = "local" // Local model
|
ModelSourceLocal ModelSource = "local" // Local model
|
||||||
ModelSourceRemote ModelSource = "remote" // Remote model
|
ModelSourceRemote ModelSource = "remote" // Remote model
|
||||||
ModelSourceAliyun ModelSource = "aliyun" // Aliyun DashScope model
|
ModelSourceAliyun ModelSource = "aliyun" // Aliyun DashScope model
|
||||||
|
ModelSourceZhipu ModelSource = "zhipu" // Zhipu model
|
||||||
|
ModelSourceVolcengine ModelSource = "volcengine" // Volcengine model
|
||||||
|
ModelSourceDeepseek ModelSource = "deepseek" // Deepseek model
|
||||||
|
ModelSourceHunyuan ModelSource = "hunyuan" // Hunyuan model
|
||||||
|
ModelSourceMinimax ModelSource = "minimax" // Minimax mode
|
||||||
|
ModelSourceOpenAI ModelSource = "openai" // OpenAI model
|
||||||
|
ModelSourceGemini ModelSource = "gemini" // Gemini model
|
||||||
|
ModelSourceMimo ModelSource = "mimo" // Mimo model
|
||||||
|
ModelSourceSiliconFlow ModelSource = "siliconflow" // SiliconFlow model
|
||||||
|
ModelSourceJina ModelSource = "jina" // Jina AI model
|
||||||
|
ModelSourceOpenRouter ModelSource = "openrouter" // OpenRouter model
|
||||||
)
|
)
|
||||||
|
|
||||||
// EmbeddingParameters represents the embedding parameters for a model
|
// EmbeddingParameters represents the embedding parameters for a model
|
||||||
@@ -49,6 +60,8 @@ type ModelParameters struct {
|
|||||||
InterfaceType string `yaml:"interface_type" json:"interface_type"`
|
InterfaceType string `yaml:"interface_type" json:"interface_type"`
|
||||||
EmbeddingParameters EmbeddingParameters `yaml:"embedding_parameters" json:"embedding_parameters"`
|
EmbeddingParameters EmbeddingParameters `yaml:"embedding_parameters" json:"embedding_parameters"`
|
||||||
ParameterSize string `yaml:"parameter_size" json:"parameter_size"` // Ollama model parameter size (e.g., "7B", "13B", "70B")
|
ParameterSize string `yaml:"parameter_size" json:"parameter_size"` // Ollama model parameter size (e.g., "7B", "13B", "70B")
|
||||||
|
Provider string `yaml:"provider" json:"provider"` // Provider identifier: openai, aliyun, zhipu, generic
|
||||||
|
ExtraConfig map[string]string `yaml:"extra_config" json:"extra_config"` // Provider-specific configuration
|
||||||
}
|
}
|
||||||
|
|
||||||
// Model represents the AI model
|
// Model represents the AI model
|
||||||
|
|||||||
Reference in New Issue
Block a user