diff --git a/docs/BUILTIN_MODELS.md b/docs/BUILTIN_MODELS.md index 99e5b739..c1886a2b 100644 --- a/docs/BUILTIN_MODELS.md +++ b/docs/BUILTIN_MODELS.md @@ -21,9 +21,11 @@ - 模型名称(name) - 模型类型(type):`KnowledgeQA`、`Embedding`、`Rerank` 或 `VLLM` - 模型来源(source):`local` 或 `remote` -- 模型参数(parameters):包括 base_url、api_key 等 +- 模型参数(parameters):包括 base_url、api_key、provider 等 - 租户ID(tenant_id):建议使用小于10000的租户ID,避免冲突 +**支持的服务商(provider)**:`generic`(自定义)、`openai`、`aliyun`、`zhipu`、`volcengine`、`hunyuan`、`deepseek`、`minimax`、`mimo`、`siliconflow`、`jina`、`openrouter`、`gemini` + ### 2. 执行 SQL 插入语句 使用以下 SQL 语句插入内置模型: @@ -48,7 +50,7 @@ INSERT INTO models ( 'KnowledgeQA', -- 模型类型 'remote', -- 模型来源 '内置 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, -- 是否默认 'active', -- 状态 true -- 标记为内置模型 @@ -73,7 +75,7 @@ INSERT INTO models ( 'Embedding', 'remote', '内置 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, 'active', true @@ -98,7 +100,7 @@ INSERT INTO models ( 'Rerank', 'remote', '内置 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, 'active', true @@ -123,7 +125,7 @@ INSERT INTO models ( 'VLLM', 'remote', '内置 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, 'active', true diff --git a/docs/api/model.md b/docs/api/model.md index 9f9e5aa1..66dbb546 100644 --- a/docs/api/model.md +++ b/docs/api/model.md @@ -2,22 +2,100 @@ [返回目录](./README.md) -| 方法 | 路径 | 描述 | -| ------ | --------------------- | --------------------- | -| POST | `/models` | 创建模型 | -| GET | `/models` | 获取模型列表 | -| GET | `/models/:id` | 获取模型详情 | -| PUT | `/models/:id` | 更新模型 | -| DELETE | `/models/:id` | 删除模型 | +| 方法 | 路径 | 描述 | +| ------ | ----------------------- | --------------------- | +| POST | `/models` | 创建模型 | +| GET | `/models` | 获取模型列表 | +| GET | `/models/:id` | 获取模型详情 | +| PUT | `/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` - 创建模型 ### 创建对话模型(KnowledgeQA) +**本地 Ollama 模型**: + ```curl curl --location 'http://localhost:8080/api/v1/models' \ --header 'Content-Type: application/json' \ ---header 'X-API-Key: sk-vQHV2NZI_LK5W7wHQvH3yGYExX8YnhaHwZipUYbiZKCYJbBQ' \ +--header 'X-API-Key: your_api_key' \ --data '{ "name": "qwen3:8b", "type": "KnowledgeQA", @@ -26,17 +104,37 @@ curl --location 'http://localhost:8080/api/v1/models' \ "parameters": { "base_url": "", "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) +**本地 Ollama 模型**: + ```curl curl --location 'http://localhost:8080/api/v1/models' \ --header 'Content-Type: application/json' \ ---header 'X-API-Key: sk-vQHV2NZI_LK5W7wHQvH3yGYExX8YnhaHwZipUYbiZKCYJbBQ' \ +--header 'X-API-Key: your_api_key' \ --data '{ "name": "nomic-embed-text:latest", "type": "Embedding", @@ -49,27 +147,112 @@ curl --location 'http://localhost:8080/api/v1/models' \ "dimension": 768, "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) +**远程 API 模型(阿里云 DashScope)**: + ```curl curl --location 'http://localhost:8080/api/v1/models' \ --header 'Content-Type: application/json' \ ---header 'X-API-Key: sk-vQHV2NZI_LK5W7wHQvH3yGYExX8YnhaHwZipUYbiZKCYJbBQ' \ +--header 'X-API-Key: your_api_key' \ --data '{ - "name": "linux6200/bge-reranker-v2-m3:latest", + "name": "gte-rerank", "type": "Rerank", - "source": "local", - "description": "Rerank Model for Knowledge QA", + "source": "remote", + "description": "阿里云 GTE Rerank 模型", "parameters": { - "base_url": "", - "api_key": "" - }, - "is_default": false + "base_url": "https://dashscope.aliyuncs.com/api/v1/services/rerank/text-rerank/text-rerank", + "api_key": "sk-your-dashscope-api-key", + "provider": "aliyun" + } +}' +``` + +**远程 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 { + "success": true, "data": { "id": "09c5a1d6-ee8b-4657-9a17-d3dcbd5c70cb", "tenant_id": 1, - "name": "nomic-embed-text:latest3", + "name": "text-embedding-v3", "type": "Embedding", - "source": "local", - "description": "Embedding Model", + "source": "remote", + "description": "阿里云通义千问 Embedding 模型", "parameters": { - "base_url": "", - "api_key": "", + "base_url": "https://dashscope.aliyuncs.com/compatible-mode/v1", + "api_key": "sk-***", + "provider": "aliyun", "embedding_parameters": { - "dimension": 768, + "dimension": 1024, "truncate_prompt_tokens": 0 } }, "is_default": false, - "status": "downloading", + "status": "active", "created_at": "2025-08-12T10:39:01.454591766+08:00", "updated_at": "2025-08-12T10:39:01.454591766+08:00", "deleted_at": null - }, - "success": true + } } ``` @@ -109,26 +293,28 @@ curl --location 'http://localhost:8080/api/v1/models' \ ```curl curl --location 'http://localhost:8080/api/v1/models' \ --header 'Content-Type: application/json' \ ---header 'X-API-Key: sk-vQHV2NZI_LK5W7wHQvH3yGYExX8YnhaHwZipUYbiZKCYJbBQ' +--header 'X-API-Key: your_api_key' ``` **响应**: ```json { + "success": true, "data": [ { "id": "dff7bc94-7885-4dd1-bfd5-bd96e4df2fc3", "tenant_id": 1, - "name": "nomic-embed-text:latest", + "name": "text-embedding-v3", "type": "Embedding", - "source": "local", - "description": "Embedding Model", + "source": "remote", + "description": "阿里云通义千问 Embedding 模型", "parameters": { - "base_url": "", - "api_key": "", + "base_url": "https://dashscope.aliyuncs.com/compatible-mode/v1", + "api_key": "sk-***", + "provider": "aliyun", "embedding_parameters": { - "dimension": 768, + "dimension": 1024, "truncate_prompt_tokens": 0 } }, @@ -141,13 +327,14 @@ curl --location 'http://localhost:8080/api/v1/models' \ { "id": "8aea788c-bb30-4898-809e-e40c14ffb48c", "tenant_id": 1, - "name": "qwen3:8b", + "name": "qwen-plus", "type": "KnowledgeQA", - "source": "local", - "description": "LLM Model for Knowledge QA", + "source": "remote", + "description": "阿里云 Qwen 大模型", "parameters": { - "base_url": "", - "api_key": "", + "base_url": "https://dashscope.aliyuncs.com/compatible-mode/v1", + "api_key": "sk-***", + "provider": "aliyun", "embedding_parameters": { "dimension": 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", "deleted_at": null } - ], - "success": true + ] } ``` @@ -171,25 +357,27 @@ curl --location 'http://localhost:8080/api/v1/models' \ ```curl curl --location 'http://localhost:8080/api/v1/models/dff7bc94-7885-4dd1-bfd5-bd96e4df2fc3' \ --header 'Content-Type: application/json' \ ---header 'X-API-Key: sk-vQHV2NZI_LK5W7wHQvH3yGYExX8YnhaHwZipUYbiZKCYJbBQ' +--header 'X-API-Key: your_api_key' ``` **响应**: ```json { + "success": true, "data": { "id": "dff7bc94-7885-4dd1-bfd5-bd96e4df2fc3", "tenant_id": 1, - "name": "nomic-embed-text:latest", + "name": "text-embedding-v3", "type": "Embedding", - "source": "local", - "description": "Embedding Model", + "source": "remote", + "description": "阿里云通义千问 Embedding 模型", "parameters": { - "base_url": "", - "api_key": "", + "base_url": "https://dashscope.aliyuncs.com/compatible-mode/v1", + "api_key": "sk-***", + "provider": "aliyun", "embedding_parameters": { - "dimension": 768, + "dimension": 1024, "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", "updated_at": "2025-08-11T20:10:41.822354+08:00", "deleted_at": null - }, - "success": true + } } ``` @@ -210,15 +397,15 @@ curl --location 'http://localhost:8080/api/v1/models/dff7bc94-7885-4dd1-bfd5-bd9 ```curl curl --location --request PUT 'http://localhost:8080/api/v1/models/8fdc464d-8eaa-44d4-a85b-094b28af5330' \ --header 'Content-Type: application/json' \ ---header 'X-API-Key: sk-vQHV2NZI_LK5W7wHQvH3yGYExX8YnhaHwZipUYbiZKCYJbBQ' \ +--header 'X-API-Key: your_api_key' \ --data '{ - "name": "linux6200/bge-reranker-v2-m3:latest", - "description": "Rerank Model for Knowledge QA new", + "name": "gte-rerank-v2", + "description": "阿里云 GTE Rerank 模型 V2", "parameters": { - "base_url": "", - "api_key": "" - }, - "is_default": false + "base_url": "https://dashscope.aliyuncs.com/api/v1/services/rerank/text-rerank/text-rerank", + "api_key": "sk-your-new-api-key", + "provider": "aliyun" + } }' ``` @@ -226,16 +413,18 @@ curl --location --request PUT 'http://localhost:8080/api/v1/models/8fdc464d-8eaa ```json { + "success": true, "data": { "id": "8fdc464d-8eaa-44d4-a85b-094b28af5330", "tenant_id": 1, - "name": "linux6200/bge-reranker-v2-m3:latest", + "name": "gte-rerank-v2", "type": "Rerank", - "source": "local", - "description": "Rerank Model for Knowledge QA new", + "source": "remote", + "description": "阿里云 GTE Rerank 模型 V2", "parameters": { - "base_url": "", - "api_key": "", + "base_url": "https://dashscope.aliyuncs.com/api/v1/services/rerank/text-rerank/text-rerank", + "api_key": "sk-***", + "provider": "aliyun", "embedding_parameters": { "dimension": 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", "updated_at": "2025-08-12T11:00:27.271678+08:00", "deleted_at": null - }, - "success": true + } } ``` @@ -258,14 +446,49 @@ curl --location --request PUT 'http://localhost:8080/api/v1/models/8fdc464d-8eaa ```curl curl --location --request DELETE 'http://localhost:8080/api/v1/models/8fdc464d-8eaa-44d4-a85b-094b28af5330' \ --header 'Content-Type: application/json' \ ---header 'X-API-Key: sk-vQHV2NZI_LK5W7wHQvH3yGYExX8YnhaHwZipUYbiZKCYJbBQ' +--header 'X-API-Key: your_api_key' ``` **响应**: ```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 表示不截断)| diff --git a/frontend/src/api/initialization/index.ts b/frontend/src/api/initialization/index.ts index e031e52d..0ab23ced 100644 --- a/frontend/src/api/initialization/index.ts +++ b/frontend/src/api/initialization/index.ts @@ -278,6 +278,7 @@ export function testEmbeddingModel(modelConfig: { baseUrl?: string; apiKey?: string; dimension?: number; + provider?: string; }): Promise<{ available: boolean; message?: string; dimension?: number }> { return new Promise((resolve, reject) => { post('/api/v1/initialization/embedding/test', modelConfig) @@ -318,7 +319,7 @@ export function testMultimodalFunction(testData: { vlm_base_url: string; vlm_api_key?: string; vlm_interface_type?: string; - storage_type?: 'cos'|'minio'; + storage_type?: 'cos' | 'minio'; // COS optional fields (required only when storage_type === 'cos') cos_secret_id?: string; cos_secret_key?: string; @@ -397,18 +398,18 @@ export function testMultimodalFunction(testData: { headers, body: formData }) - .then(response => response.json()) - .then((data: any) => { - if (data.success) { - resolve(data.data || {}); - } else { - resolve({ success: false, message: data.message || '测试失败' }); - } - }) - .catch((error: any) => { - console.error('多模态测试失败:', error); - reject(error); - }); + .then(response => response.json()) + .then((data: any) => { + if (data.success) { + resolve(data.data || {}); + } else { + resolve({ success: false, message: data.message || '测试失败' }); + } + }) + .catch((error: any) => { + console.error('多模态测试失败:', error); + reject(error); + }); }); } @@ -480,14 +481,14 @@ export function fabriText(request: FabriTextRequest): Promise } export interface FabriTagRequest { - llm_config: LLMConfig; + llm_config: LLMConfig; } export interface FabriTagResponse { tags: string[]; } -// 文本内容生成 +// 标签生成 export function fabriTag(request: FabriTagRequest): Promise { return new Promise((resolve, reject) => { post('/api/v1/initialization/extract/fabri-tag', request) @@ -499,4 +500,30 @@ export function fabriTag(request: FabriTagRequest): Promise { reject(error); }); }); +} + +// 模型厂商信息类型 +export interface ModelProviderOption { + value: string; // provider 标识符 + label: string; // 显示名称 + description: string; // 描述 + defaultUrls: Record; // 按模型类型区分的默认 URL + modelTypes: string[]; // 支持的模型类型 +} + +// 获取模型厂商列表 +export function listModelProviders(modelType?: string): Promise { + 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([]); // 失败时返回空数组,前端可以回退到默认值 + }); + }); } \ No newline at end of file diff --git a/frontend/src/api/model/index.ts b/frontend/src/api/model/index.ts index d1fb9e4b..8b5ffa4c 100644 --- a/frontend/src/api/model/index.ts +++ b/frontend/src/api/model/index.ts @@ -11,12 +11,14 @@ export interface ModelConfig { parameters: { base_url?: string; api_key?: string; + provider?: string; // Provider identifier: openai, aliyun, zhipu, generic embedding_parameters?: { dimension?: number; truncate_prompt_tokens?: number; }; interface_type?: 'ollama' | 'openai'; // VLLM专用 parameter_size?: string; // Ollama模型参数大小 (e.g., "7B", "13B", "70B") + extra_config?: Record; // Provider-specific configuration }; is_default?: boolean; is_builtin?: boolean; diff --git a/frontend/src/components/ModelEditorDialog.vue b/frontend/src/components/ModelEditorDialog.vue index 34ffc6a2..6b289355 100644 --- a/frontend/src/components/ModelEditorDialog.vue +++ b/frontend/src/components/ModelEditorDialog.vue @@ -119,17 +119,39 @@ - -
- - -
-