Update backend api
This commit is contained in:
409
api/docs/asr.md
Normal file
409
api/docs/asr.md
Normal file
@@ -0,0 +1,409 @@
|
||||
# 语音识别 (ASR Model) API
|
||||
|
||||
语音识别 API 用于管理语音识别模型的配置和调用。
|
||||
|
||||
## 基础信息
|
||||
|
||||
| 项目 | 值 |
|
||||
|------|-----|
|
||||
| Base URL | `/api/v1/asr` |
|
||||
| 认证方式 | Bearer Token (预留) |
|
||||
|
||||
---
|
||||
|
||||
## 数据模型
|
||||
|
||||
### ASRModel
|
||||
|
||||
```typescript
|
||||
interface ASRModel {
|
||||
id: string; // 模型唯一标识 (8位UUID)
|
||||
user_id: number; // 所属用户ID
|
||||
name: string; // 模型显示名称
|
||||
vendor: string; // 供应商: "OpenAI" | "SiliconFlow" | "Paraformer" | 等
|
||||
language: string; // 识别语言: "zh" | "en" | "Multi-lingual"
|
||||
base_url: string; // API Base URL
|
||||
api_key: string; // API Key
|
||||
model_name?: string; // 模型名称,如 "whisper-1" | "paraformer-v2"
|
||||
hotwords?: string[]; // 热词列表
|
||||
enable_punctuation: boolean; // 是否启用标点
|
||||
enable_normalization: boolean; // 是否启用文本规范化
|
||||
enabled: boolean; // 是否启用
|
||||
created_at: string;
|
||||
}
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## API 端点
|
||||
|
||||
### 1. 获取 ASR 模型列表
|
||||
|
||||
```http
|
||||
GET /api/v1/asr
|
||||
```
|
||||
|
||||
**Query Parameters:**
|
||||
|
||||
| 参数 | 类型 | 必填 | 默认值 | 说明 |
|
||||
|------|------|------|--------|------|
|
||||
| language | string | 否 | - | 过滤语言: "zh" \| "en" \| "Multi-lingual" |
|
||||
| enabled | boolean | 否 | - | 过滤启用状态 |
|
||||
| page | int | 否 | 1 | 页码 |
|
||||
| limit | int | 否 | 50 | 每页数量 |
|
||||
|
||||
**Response:**
|
||||
|
||||
```json
|
||||
{
|
||||
"total": 3,
|
||||
"page": 1,
|
||||
"limit": 50,
|
||||
"list": [
|
||||
{
|
||||
"id": "abc12345",
|
||||
"user_id": 1,
|
||||
"name": "Whisper 多语种识别",
|
||||
"vendor": "OpenAI",
|
||||
"language": "Multi-lingual",
|
||||
"base_url": "https://api.openai.com/v1",
|
||||
"api_key": "sk-***",
|
||||
"model_name": "whisper-1",
|
||||
"enable_punctuation": true,
|
||||
"enable_normalization": true,
|
||||
"enabled": true,
|
||||
"created_at": "2024-01-15T10:30:00Z"
|
||||
},
|
||||
{
|
||||
"id": "def67890",
|
||||
"user_id": 1,
|
||||
"name": "SenseVoice 中文识别",
|
||||
"vendor": "SiliconFlow",
|
||||
"language": "zh",
|
||||
"base_url": "https://api.siliconflow.cn/v1",
|
||||
"api_key": "sf-***",
|
||||
"model_name": "paraformer-v2",
|
||||
"hotwords": ["小助手", "帮我"],
|
||||
"enable_punctuation": true,
|
||||
"enable_normalization": true,
|
||||
"enabled": true,
|
||||
"created_at": "2024-01-15T10:30:00Z"
|
||||
}
|
||||
]
|
||||
}
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
### 2. 获取单个 ASR 模型详情
|
||||
|
||||
```http
|
||||
GET /api/v1/asr/{id}
|
||||
```
|
||||
|
||||
**Path Parameters:**
|
||||
|
||||
| 参数 | 类型 | 说明 |
|
||||
|------|------|------|
|
||||
| id | string | 模型ID |
|
||||
|
||||
**Response:**
|
||||
|
||||
```json
|
||||
{
|
||||
"id": "abc12345",
|
||||
"user_id": 1,
|
||||
"name": "Whisper 多语种识别",
|
||||
"vendor": "OpenAI",
|
||||
"language": "Multi-lingual",
|
||||
"base_url": "https://api.openai.com/v1",
|
||||
"api_key": "sk-***",
|
||||
"model_name": "whisper-1",
|
||||
"hotwords": [],
|
||||
"enable_punctuation": true,
|
||||
"enable_normalization": true,
|
||||
"enabled": true,
|
||||
"created_at": "2024-01-15T10:30:00Z"
|
||||
}
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
### 3. 创建 ASR 模型
|
||||
|
||||
```http
|
||||
POST /api/v1/asr
|
||||
```
|
||||
|
||||
**Request Body:**
|
||||
|
||||
```json
|
||||
{
|
||||
"name": "SenseVoice 中文识别",
|
||||
"vendor": "SiliconFlow",
|
||||
"language": "zh",
|
||||
"base_url": "https://api.siliconflow.cn/v1",
|
||||
"api_key": "sk-your-api-key",
|
||||
"model_name": "paraformer-v2",
|
||||
"hotwords": ["小助手", "帮我"],
|
||||
"enable_punctuation": true,
|
||||
"enable_normalization": true,
|
||||
"enabled": true
|
||||
}
|
||||
```
|
||||
|
||||
**Fields 说明:**
|
||||
|
||||
| 字段 | 类型 | 必填 | 说明 |
|
||||
|------|------|------|------|
|
||||
| name | string | 是 | 模型显示名称 |
|
||||
| vendor | string | 是 | 供应商: "OpenAI" / "SiliconFlow" / "Paraformer" |
|
||||
| language | string | 是 | 语言: "zh" / "en" / "Multi-lingual" |
|
||||
| base_url | string | 是 | API Base URL |
|
||||
| api_key | string | 是 | API Key |
|
||||
| model_name | string | 否 | 模型名称 |
|
||||
| hotwords | string[] | 否 | 热词列表,提升识别准确率 |
|
||||
| enable_punctuation | boolean | 否 | 是否输出标点,默认 true |
|
||||
| enable_normalization | boolean | 否 | 是否文本规范化,默认 true |
|
||||
| enabled | boolean | 否 | 是否启用,默认 true |
|
||||
| id | string | 否 | 指定模型ID,默认自动生成 |
|
||||
|
||||
---
|
||||
|
||||
### 4. 更新 ASR 模型
|
||||
|
||||
```http
|
||||
PUT /api/v1/asr/{id}
|
||||
```
|
||||
|
||||
**Request Body:** (部分更新)
|
||||
|
||||
```json
|
||||
{
|
||||
"name": "Whisper-1 优化版",
|
||||
"language": "zh",
|
||||
"enable_punctuation": true,
|
||||
"hotwords": ["新词1", "新词2"]
|
||||
}
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
### 5. 删除 ASR 模型
|
||||
|
||||
```http
|
||||
DELETE /api/v1/asr/{id}
|
||||
```
|
||||
|
||||
**Response:**
|
||||
|
||||
```json
|
||||
{
|
||||
"message": "Deleted successfully"
|
||||
}
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
### 6. 测试 ASR 模型
|
||||
|
||||
```http
|
||||
POST /api/v1/asr/{id}/test
|
||||
```
|
||||
|
||||
**Request Body:**
|
||||
|
||||
```json
|
||||
{
|
||||
"audio_url": "https://example.com/test-audio.wav"
|
||||
}
|
||||
```
|
||||
|
||||
或使用 Base64 编码的音频数据:
|
||||
|
||||
```json
|
||||
{
|
||||
"audio_data": "UklGRi..."
|
||||
}
|
||||
```
|
||||
|
||||
**Response (成功):**
|
||||
|
||||
```json
|
||||
{
|
||||
"success": true,
|
||||
"transcript": "您好,请问有什么可以帮助您?",
|
||||
"language": "zh",
|
||||
"confidence": 0.95,
|
||||
"latency_ms": 500
|
||||
}
|
||||
```
|
||||
|
||||
**Response (失败):**
|
||||
|
||||
```json
|
||||
{
|
||||
"success": false,
|
||||
"error": "HTTP Error: 401 - Unauthorized"
|
||||
}
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
### 7. 转写音频
|
||||
|
||||
```http
|
||||
POST /api/v1/asr/{id}/transcribe
|
||||
```
|
||||
|
||||
**Query Parameters:**
|
||||
|
||||
| 参数 | 类型 | 必填 | 说明 |
|
||||
|------|------|------|------|
|
||||
| audio_url | string | 否* | 音频文件URL |
|
||||
| audio_data | string | 否* | Base64编码的音频数据 |
|
||||
| hotwords | string[] | 否 | 热词列表 |
|
||||
|
||||
*二选一,至少提供一个
|
||||
|
||||
**Response:**
|
||||
|
||||
```json
|
||||
{
|
||||
"success": true,
|
||||
"transcript": "您好,请问有什么可以帮助您?",
|
||||
"language": "zh",
|
||||
"confidence": 0.95
|
||||
}
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## Schema 定义
|
||||
|
||||
```python
|
||||
from enum import Enum
|
||||
from pydantic import BaseModel
|
||||
from typing import Optional, List
|
||||
from datetime import datetime
|
||||
|
||||
class ASRLanguage(str, Enum):
|
||||
ZH = "zh"
|
||||
EN = "en"
|
||||
MULTILINGUAL = "Multi-lingual"
|
||||
|
||||
class ASRModelBase(BaseModel):
|
||||
name: str
|
||||
vendor: str
|
||||
language: str # "zh" | "en" | "Multi-lingual"
|
||||
base_url: str
|
||||
api_key: str
|
||||
model_name: Optional[str] = None
|
||||
hotwords: List[str] = []
|
||||
enable_punctuation: bool = True
|
||||
enable_normalization: bool = True
|
||||
enabled: bool = True
|
||||
|
||||
class ASRModelCreate(ASRModelBase):
|
||||
id: Optional[str] = None
|
||||
|
||||
class ASRModelUpdate(BaseModel):
|
||||
name: Optional[str] = None
|
||||
language: Optional[str] = None
|
||||
base_url: Optional[str] = None
|
||||
api_key: Optional[str] = None
|
||||
model_name: Optional[str] = None
|
||||
hotwords: Optional[List[str]] = None
|
||||
enable_punctuation: Optional[bool] = None
|
||||
enable_normalization: Optional[bool] = None
|
||||
enabled: Optional[bool] = None
|
||||
|
||||
class ASRModelOut(ASRModelBase):
|
||||
id: str
|
||||
user_id: int
|
||||
created_at: datetime
|
||||
|
||||
class Config:
|
||||
from_attributes = True
|
||||
|
||||
class ASRTestRequest(BaseModel):
|
||||
audio_url: Optional[str] = None
|
||||
audio_data: Optional[str] = None # base64 encoded
|
||||
|
||||
class ASRTestResponse(BaseModel):
|
||||
success: bool
|
||||
transcript: Optional[str] = None
|
||||
language: Optional[str] = None
|
||||
confidence: Optional[float] = None
|
||||
latency_ms: Optional[int] = None
|
||||
error: Optional[str] = None
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 供应商配置示例
|
||||
|
||||
### OpenAI Whisper
|
||||
|
||||
```json
|
||||
{
|
||||
"vendor": "OpenAI",
|
||||
"base_url": "https://api.openai.com/v1",
|
||||
"api_key": "sk-xxx",
|
||||
"model_name": "whisper-1",
|
||||
"language": "Multi-lingual",
|
||||
"enable_punctuation": true,
|
||||
"enable_normalization": true
|
||||
}
|
||||
```
|
||||
|
||||
### SiliconFlow Paraformer
|
||||
|
||||
```json
|
||||
{
|
||||
"vendor": "SiliconFlow",
|
||||
"base_url": "https://api.siliconflow.cn/v1",
|
||||
"api_key": "sf-xxx",
|
||||
"model_name": "paraformer-v2",
|
||||
"language": "zh",
|
||||
"hotwords": ["产品名称", "公司名"],
|
||||
"enable_punctuation": true,
|
||||
"enable_normalization": true
|
||||
}
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 单元测试
|
||||
|
||||
项目包含完整的单元测试,位于 `api/tests/test_asr.py`。
|
||||
|
||||
### 测试用例概览
|
||||
|
||||
| 测试方法 | 说明 |
|
||||
|----------|------|
|
||||
| test_get_asr_models_empty | 空数据库获取测试 |
|
||||
| test_create_asr_model | 创建模型测试 |
|
||||
| test_create_asr_model_minimal | 最小数据创建测试 |
|
||||
| test_get_asr_model_by_id | 获取单个模型测试 |
|
||||
| test_get_asr_model_not_found | 获取不存在模型测试 |
|
||||
| test_update_asr_model | 更新模型测试 |
|
||||
| test_delete_asr_model | 删除模型测试 |
|
||||
| test_list_asr_models_with_pagination | 分页测试 |
|
||||
| test_filter_asr_models_by_language | 按语言过滤测试 |
|
||||
| test_filter_asr_models_by_enabled | 按启用状态过滤测试 |
|
||||
| test_create_asr_model_with_hotwords | 热词配置测试 |
|
||||
| test_test_asr_model_siliconflow | SiliconFlow 供应商测试 |
|
||||
| test_test_asr_model_openai | OpenAI 供应商测试 |
|
||||
| test_different_asr_languages | 多语言测试 |
|
||||
| test_different_asr_vendors | 多供应商测试 |
|
||||
|
||||
### 运行测试
|
||||
|
||||
```bash
|
||||
# 运行 ASR 相关测试
|
||||
pytest api/tests/test_asr.py -v
|
||||
|
||||
# 运行所有测试
|
||||
pytest api/tests/ -v
|
||||
```
|
||||
@@ -7,9 +7,9 @@
|
||||
| 模块 | 文件 | 说明 |
|
||||
|------|------|------|
|
||||
| 小助手 | [assistant.md](./assistant.md) | AI 助手管理 |
|
||||
| 模型接入 | [model-access.md](./model-access.md) | LLM/ASR/TTS 模型配置 |
|
||||
| 语音识别 | [speech-recognition.md](./speech-recognition.md) | ASR 模型配置 |
|
||||
| 声音资源 | [voice-resources.md](./voice-resources.md) | TTS 声音库管理 |
|
||||
| LLM 模型 | [llm.md](./llm.md) | LLM 模型配置与管理 |
|
||||
| ASR 模型 | [asr.md](./asr.md) | 语音识别模型配置 |
|
||||
| 工具与测试 | [tools.md](./tools.md) | 工具列表与自动测试 |
|
||||
| 历史记录 | [history-records.md](./history-records.md) | 通话记录和转写 |
|
||||
|
||||
---
|
||||
|
||||
401
api/docs/llm.md
Normal file
401
api/docs/llm.md
Normal file
@@ -0,0 +1,401 @@
|
||||
# LLM 模型 (LLM Model) API
|
||||
|
||||
LLM 模型 API 用于管理大语言模型的配置和调用。
|
||||
|
||||
## 基础信息
|
||||
|
||||
| 项目 | 值 |
|
||||
|------|-----|
|
||||
| Base URL | `/api/v1/llm` |
|
||||
| 认证方式 | Bearer Token (预留) |
|
||||
|
||||
---
|
||||
|
||||
## 数据模型
|
||||
|
||||
### LLMModel
|
||||
|
||||
```typescript
|
||||
interface LLMModel {
|
||||
id: string; // 模型唯一标识 (8位UUID)
|
||||
user_id: number; // 所属用户ID
|
||||
name: string; // 模型显示名称
|
||||
vendor: string; // 供应商: "OpenAI" | "SiliconFlow" | "Dify" | "FastGPT" | 等
|
||||
type: string; // 类型: "text" | "embedding" | "rerank"
|
||||
base_url: string; // API Base URL
|
||||
api_key: string; // API Key
|
||||
model_name?: string; // 实际模型名称,如 "gpt-4o"
|
||||
temperature?: number; // 温度参数 (0-2)
|
||||
context_length?: int; // 上下文长度
|
||||
enabled: boolean; // 是否启用
|
||||
created_at: string;
|
||||
updated_at: string;
|
||||
}
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## API 端点
|
||||
|
||||
### 1. 获取 LLM 模型列表
|
||||
|
||||
```http
|
||||
GET /api/v1/llm
|
||||
```
|
||||
|
||||
**Query Parameters:**
|
||||
|
||||
| 参数 | 类型 | 必填 | 默认值 | 说明 |
|
||||
|------|------|------|--------|------|
|
||||
| model_type | string | 否 | - | 过滤类型: "text" \| "embedding" \| "rerank" |
|
||||
| enabled | boolean | 否 | - | 过滤启用状态 |
|
||||
| page | int | 否 | 1 | 页码 |
|
||||
| limit | int | 否 | 50 | 每页数量 |
|
||||
|
||||
**Response:**
|
||||
|
||||
```json
|
||||
{
|
||||
"total": 5,
|
||||
"page": 1,
|
||||
"limit": 50,
|
||||
"list": [
|
||||
{
|
||||
"id": "abc12345",
|
||||
"user_id": 1,
|
||||
"name": "GPT-4o",
|
||||
"vendor": "OpenAI",
|
||||
"type": "text",
|
||||
"base_url": "https://api.openai.com/v1",
|
||||
"api_key": "sk-***",
|
||||
"model_name": "gpt-4o",
|
||||
"temperature": 0.7,
|
||||
"context_length": 128000,
|
||||
"enabled": true,
|
||||
"created_at": "2024-01-15T10:30:00Z",
|
||||
"updated_at": "2024-01-15T10:30:00Z"
|
||||
},
|
||||
{
|
||||
"id": "def67890",
|
||||
"user_id": 1,
|
||||
"name": "Embedding-3-Small",
|
||||
"vendor": "OpenAI",
|
||||
"type": "embedding",
|
||||
"base_url": "https://api.openai.com/v1",
|
||||
"api_key": "sk-***",
|
||||
"model_name": "text-embedding-3-small",
|
||||
"enabled": true
|
||||
}
|
||||
]
|
||||
}
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
### 2. 获取单个 LLM 模型详情
|
||||
|
||||
```http
|
||||
GET /api/v1/llm/{id}
|
||||
```
|
||||
|
||||
**Path Parameters:**
|
||||
|
||||
| 参数 | 类型 | 说明 |
|
||||
|------|------|------|
|
||||
| id | string | 模型ID |
|
||||
|
||||
**Response:**
|
||||
|
||||
```json
|
||||
{
|
||||
"id": "abc12345",
|
||||
"user_id": 1,
|
||||
"name": "GPT-4o",
|
||||
"vendor": "OpenAI",
|
||||
"type": "text",
|
||||
"base_url": "https://api.openai.com/v1",
|
||||
"api_key": "sk-***",
|
||||
"model_name": "gpt-4o",
|
||||
"temperature": 0.7,
|
||||
"context_length": 128000,
|
||||
"enabled": true,
|
||||
"created_at": "2024-01-15T10:30:00Z",
|
||||
"updated_at": "2024-01-15T10:30:00Z"
|
||||
}
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
### 3. 创建 LLM 模型
|
||||
|
||||
```http
|
||||
POST /api/v1/llm
|
||||
```
|
||||
|
||||
**Request Body:**
|
||||
|
||||
```json
|
||||
{
|
||||
"name": "GPT-4o",
|
||||
"vendor": "OpenAI",
|
||||
"type": "text",
|
||||
"base_url": "https://api.openai.com/v1",
|
||||
"api_key": "sk-your-api-key",
|
||||
"model_name": "gpt-4o",
|
||||
"temperature": 0.7,
|
||||
"context_length": 128000,
|
||||
"enabled": true
|
||||
}
|
||||
```
|
||||
|
||||
**Fields 说明:**
|
||||
|
||||
| 字段 | 类型 | 必填 | 说明 |
|
||||
|------|------|------|------|
|
||||
| name | string | 是 | 模型显示名称 |
|
||||
| vendor | string | 是 | 供应商名称 |
|
||||
| type | string | 是 | 模型类型: "text" / "embedding" / "rerank" |
|
||||
| base_url | string | 是 | API Base URL |
|
||||
| api_key | string | 是 | API Key |
|
||||
| model_name | string | 否 | 实际模型名称 |
|
||||
| temperature | number | 否 | 温度参数,默认 0.7 |
|
||||
| context_length | int | 否 | 上下文长度 |
|
||||
| enabled | boolean | 否 | 是否启用,默认 true |
|
||||
| id | string | 否 | 指定模型ID,默认自动生成 |
|
||||
|
||||
---
|
||||
|
||||
### 4. 更新 LLM 模型
|
||||
|
||||
```http
|
||||
PUT /api/v1/llm/{id}
|
||||
```
|
||||
|
||||
**Request Body:** (部分更新)
|
||||
|
||||
```json
|
||||
{
|
||||
"name": "GPT-4o-Updated",
|
||||
"temperature": 0.8,
|
||||
"enabled": false
|
||||
}
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
### 5. 删除 LLM 模型
|
||||
|
||||
```http
|
||||
DELETE /api/v1/llm/{id}
|
||||
```
|
||||
|
||||
**Response:**
|
||||
|
||||
```json
|
||||
{
|
||||
"message": "Deleted successfully"
|
||||
}
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
### 6. 测试 LLM 模型连接
|
||||
|
||||
```http
|
||||
POST /api/v1/llm/{id}/test
|
||||
```
|
||||
|
||||
**Response:**
|
||||
|
||||
```json
|
||||
{
|
||||
"success": true,
|
||||
"latency_ms": 150,
|
||||
"message": "Connection successful"
|
||||
}
|
||||
```
|
||||
|
||||
**错误响应:**
|
||||
|
||||
```json
|
||||
{
|
||||
"success": false,
|
||||
"latency_ms": 200,
|
||||
"message": "HTTP Error: 401 - Unauthorized"
|
||||
}
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
### 7. 与 LLM 模型对话
|
||||
|
||||
```http
|
||||
POST /api/v1/llm/{id}/chat
|
||||
```
|
||||
|
||||
**Query Parameters:**
|
||||
|
||||
| 参数 | 类型 | 必填 | 默认值 | 说明 |
|
||||
|------|------|------|--------|------|
|
||||
| message | string | 是 | - | 用户消息 |
|
||||
| system_prompt | string | 否 | - | 系统提示词 |
|
||||
| max_tokens | int | 否 | 1000 | 最大生成token数 |
|
||||
| temperature | number | 否 | 模型配置 | 温度参数 |
|
||||
|
||||
**Response:**
|
||||
|
||||
```json
|
||||
{
|
||||
"success": true,
|
||||
"reply": "您好!有什么可以帮助您的?",
|
||||
"usage": {
|
||||
"prompt_tokens": 20,
|
||||
"completion_tokens": 15,
|
||||
"total_tokens": 35
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## Schema 定义
|
||||
|
||||
```python
|
||||
from enum import Enum
|
||||
from pydantic import BaseModel
|
||||
from typing import Optional
|
||||
from datetime import datetime
|
||||
|
||||
class LLMModelType(str, Enum):
|
||||
TEXT = "text"
|
||||
EMBEDDING = "embedding"
|
||||
RERANK = "rerank"
|
||||
|
||||
class LLMModelBase(BaseModel):
|
||||
name: str
|
||||
vendor: str
|
||||
type: LLMModelType
|
||||
base_url: str
|
||||
api_key: str
|
||||
model_name: Optional[str] = None
|
||||
temperature: Optional[float] = None
|
||||
context_length: Optional[int] = None
|
||||
enabled: bool = True
|
||||
|
||||
class LLMModelCreate(LLMModelBase):
|
||||
id: Optional[str] = None
|
||||
|
||||
class LLMModelUpdate(BaseModel):
|
||||
name: Optional[str] = None
|
||||
vendor: Optional[str] = None
|
||||
base_url: Optional[str] = None
|
||||
api_key: Optional[str] = None
|
||||
model_name: Optional[str] = None
|
||||
temperature: Optional[float] = None
|
||||
context_length: Optional[int] = None
|
||||
enabled: Optional[bool] = None
|
||||
|
||||
class LLMModelOut(LLMModelBase):
|
||||
id: str
|
||||
user_id: int
|
||||
created_at: datetime
|
||||
updated_at: datetime
|
||||
|
||||
class Config:
|
||||
from_attributes = True
|
||||
|
||||
class LLMModelTestResponse(BaseModel):
|
||||
success: bool
|
||||
latency_ms: int
|
||||
message: str
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 供应商配置示例
|
||||
|
||||
### OpenAI
|
||||
|
||||
```json
|
||||
{
|
||||
"vendor": "OpenAI",
|
||||
"base_url": "https://api.openai.com/v1",
|
||||
"api_key": "sk-xxx",
|
||||
"model_name": "gpt-4o",
|
||||
"type": "text",
|
||||
"temperature": 0.7
|
||||
}
|
||||
```
|
||||
|
||||
### SiliconFlow
|
||||
|
||||
```json
|
||||
{
|
||||
"vendor": "SiliconFlow",
|
||||
"base_url": "https://api.siliconflow.com/v1",
|
||||
"api_key": "sf-xxx",
|
||||
"model_name": "deepseek-v3",
|
||||
"type": "text",
|
||||
"temperature": 0.7
|
||||
}
|
||||
```
|
||||
|
||||
### Dify
|
||||
|
||||
```json
|
||||
{
|
||||
"vendor": "Dify",
|
||||
"base_url": "https://your-dify.domain.com/v1",
|
||||
"api_key": "app-xxx",
|
||||
"model_name": "gpt-4",
|
||||
"type": "text"
|
||||
}
|
||||
```
|
||||
|
||||
### Embedding 模型
|
||||
|
||||
```json
|
||||
{
|
||||
"vendor": "OpenAI",
|
||||
"base_url": "https://api.openai.com/v1",
|
||||
"api_key": "sk-xxx",
|
||||
"model_name": "text-embedding-3-small",
|
||||
"type": "embedding"
|
||||
}
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 单元测试
|
||||
|
||||
项目包含完整的单元测试,位于 `api/tests/test_llm.py`。
|
||||
|
||||
### 测试用例概览
|
||||
|
||||
| 测试方法 | 说明 |
|
||||
|----------|------|
|
||||
| test_get_llm_models_empty | 空数据库获取测试 |
|
||||
| test_create_llm_model | 创建模型测试 |
|
||||
| test_create_llm_model_minimal | 最小数据创建测试 |
|
||||
| test_get_llm_model_by_id | 获取单个模型测试 |
|
||||
| test_get_llm_model_not_found | 获取不存在模型测试 |
|
||||
| test_update_llm_model | 更新模型测试 |
|
||||
| test_delete_llm_model | 删除模型测试 |
|
||||
| test_list_llm_models_with_pagination | 分页测试 |
|
||||
| test_filter_llm_models_by_type | 按类型过滤测试 |
|
||||
| test_filter_llm_models_by_enabled | 按启用状态过滤测试 |
|
||||
| test_create_llm_model_with_all_fields | 全字段创建测试 |
|
||||
| test_test_llm_model_success | 测试连接成功测试 |
|
||||
| test_test_llm_model_failure | 测试连接失败测试 |
|
||||
| test_different_llm_vendors | 多供应商测试 |
|
||||
| test_embedding_llm_model | Embedding 模型测试 |
|
||||
|
||||
### 运行测试
|
||||
|
||||
```bash
|
||||
# 运行 LLM 相关测试
|
||||
pytest api/tests/test_llm.py -v
|
||||
|
||||
# 运行所有测试
|
||||
pytest api/tests/ -v
|
||||
```
|
||||
445
api/docs/tools.md
Normal file
445
api/docs/tools.md
Normal file
@@ -0,0 +1,445 @@
|
||||
# 工具与自动测试 (Tools & Autotest) API
|
||||
|
||||
工具与自动测试 API 用于管理可用工具列表和自动测试功能。
|
||||
|
||||
## 基础信息
|
||||
|
||||
| 项目 | 值 |
|
||||
|------|-----|
|
||||
| Base URL | `/api/v1/tools` |
|
||||
| 认证方式 | Bearer Token (预留) |
|
||||
|
||||
---
|
||||
|
||||
## 可用工具 (Tool Registry)
|
||||
|
||||
系统内置以下工具:
|
||||
|
||||
| 工具ID | 名称 | 说明 |
|
||||
|--------|------|------|
|
||||
| search | 网络搜索 | 搜索互联网获取最新信息 |
|
||||
| calculator | 计算器 | 执行数学计算 |
|
||||
| weather | 天气查询 | 查询指定城市的天气 |
|
||||
| translate | 翻译 | 翻译文本到指定语言 |
|
||||
| knowledge | 知识库查询 | 从知识库中检索相关信息 |
|
||||
| code_interpreter | 代码执行 | 安全地执行Python代码 |
|
||||
|
||||
---
|
||||
|
||||
## API 端点
|
||||
|
||||
### 1. 获取可用工具列表
|
||||
|
||||
```http
|
||||
GET /api/v1/tools/list
|
||||
```
|
||||
|
||||
**Response:**
|
||||
|
||||
```json
|
||||
{
|
||||
"tools": {
|
||||
"search": {
|
||||
"name": "网络搜索",
|
||||
"description": "搜索互联网获取最新信息",
|
||||
"parameters": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"query": {"type": "string", "description": "搜索关键词"}
|
||||
},
|
||||
"required": ["query"]
|
||||
}
|
||||
},
|
||||
"calculator": {
|
||||
"name": "计算器",
|
||||
"description": "执行数学计算",
|
||||
"parameters": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"expression": {"type": "string", "description": "数学表达式,如: 2 + 3 * 4"}
|
||||
},
|
||||
"required": ["expression"]
|
||||
}
|
||||
},
|
||||
"weather": {
|
||||
"name": "天气查询",
|
||||
"description": "查询指定城市的天气",
|
||||
"parameters": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"city": {"type": "string", "description": "城市名称"}
|
||||
},
|
||||
"required": ["city"]
|
||||
}
|
||||
},
|
||||
"translate": {
|
||||
"name": "翻译",
|
||||
"description": "翻译文本到指定语言",
|
||||
"parameters": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"text": {"type": "string", "description": "要翻译的文本"},
|
||||
"target_lang": {"type": "string", "description": "目标语言,如: en, ja, ko"}
|
||||
},
|
||||
"required": ["text", "target_lang"]
|
||||
}
|
||||
},
|
||||
"knowledge": {
|
||||
"name": "知识库查询",
|
||||
"description": "从知识库中检索相关信息",
|
||||
"parameters": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"query": {"type": "string", "description": "查询内容"},
|
||||
"kb_id": {"type": "string", "description": "知识库ID"}
|
||||
},
|
||||
"required": ["query"]
|
||||
}
|
||||
},
|
||||
"code_interpreter": {
|
||||
"name": "代码执行",
|
||||
"description": "安全地执行Python代码",
|
||||
"parameters": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"code": {"type": "string", "description": "要执行的Python代码"}
|
||||
},
|
||||
"required": ["code"]
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
### 2. 获取工具详情
|
||||
|
||||
```http
|
||||
GET /api/v1/tools/list/{tool_id}
|
||||
```
|
||||
|
||||
**Path Parameters:**
|
||||
|
||||
| 参数 | 类型 | 说明 |
|
||||
|------|------|------|
|
||||
| tool_id | string | 工具ID |
|
||||
|
||||
**Response:**
|
||||
|
||||
```json
|
||||
{
|
||||
"name": "计算器",
|
||||
"description": "执行数学计算",
|
||||
"parameters": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"expression": {"type": "string", "description": "数学表达式,如: 2 + 3 * 4"}
|
||||
},
|
||||
"required": ["expression"]
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
**错误响应 (工具不存在):**
|
||||
|
||||
```json
|
||||
{
|
||||
"detail": "Tool not found"
|
||||
}
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
### 3. 健康检查
|
||||
|
||||
```http
|
||||
GET /api/v1/tools/health
|
||||
```
|
||||
|
||||
**Response:**
|
||||
|
||||
```json
|
||||
{
|
||||
"status": "healthy",
|
||||
"timestamp": 1705315200.123,
|
||||
"tools": ["search", "calculator", "weather", "translate", "knowledge", "code_interpreter"]
|
||||
}
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 自动测试 (Autotest)
|
||||
|
||||
### 4. 运行完整自动测试
|
||||
|
||||
```http
|
||||
POST /api/v1/tools/autotest
|
||||
```
|
||||
|
||||
**Query Parameters:**
|
||||
|
||||
| 参数 | 类型 | 必填 | 默认值 | 说明 |
|
||||
|------|------|------|--------|------|
|
||||
| llm_model_id | string | 否 | - | LLM 模型ID |
|
||||
| asr_model_id | string | 否 | - | ASR 模型ID |
|
||||
| test_llm | boolean | 否 | true | 是否测试LLM |
|
||||
| test_asr | boolean | 否 | true | 是否测试ASR |
|
||||
|
||||
**Response:**
|
||||
|
||||
```json
|
||||
{
|
||||
"id": "abc12345",
|
||||
"started_at": 1705315200.0,
|
||||
"duration_ms": 2500,
|
||||
"tests": [
|
||||
{
|
||||
"name": "Model Existence",
|
||||
"passed": true,
|
||||
"message": "Found model: GPT-4o",
|
||||
"duration_ms": 15
|
||||
},
|
||||
{
|
||||
"name": "API Connection",
|
||||
"passed": true,
|
||||
"message": "Latency: 150ms",
|
||||
"duration_ms": 150
|
||||
},
|
||||
{
|
||||
"name": "Temperature Setting",
|
||||
"passed": true,
|
||||
"message": "temperature=0.7"
|
||||
},
|
||||
{
|
||||
"name": "Streaming Support",
|
||||
"passed": true,
|
||||
"message": "Received 15 chunks",
|
||||
"duration_ms": 800
|
||||
}
|
||||
],
|
||||
"summary": {
|
||||
"passed": 4,
|
||||
"failed": 0,
|
||||
"total": 4
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
### 5. 测试单个 LLM 模型
|
||||
|
||||
```http
|
||||
POST /api/v1/tools/autotest/llm/{model_id}
|
||||
```
|
||||
|
||||
**Path Parameters:**
|
||||
|
||||
| 参数 | 类型 | 说明 |
|
||||
|------|------|------|
|
||||
| model_id | string | LLM 模型ID |
|
||||
|
||||
**Response:**
|
||||
|
||||
```json
|
||||
{
|
||||
"id": "llm_test_001",
|
||||
"started_at": 1705315200.0,
|
||||
"duration_ms": 1200,
|
||||
"tests": [
|
||||
{
|
||||
"name": "Model Existence",
|
||||
"passed": true,
|
||||
"message": "Found model: GPT-4o",
|
||||
"duration_ms": 10
|
||||
},
|
||||
{
|
||||
"name": "API Connection",
|
||||
"passed": true,
|
||||
"message": "Latency: 180ms",
|
||||
"duration_ms": 180
|
||||
},
|
||||
{
|
||||
"name": "Temperature Setting",
|
||||
"passed": true,
|
||||
"message": "temperature=0.7"
|
||||
},
|
||||
{
|
||||
"name": "Streaming Support",
|
||||
"passed": true,
|
||||
"message": "Received 12 chunks",
|
||||
"duration_ms": 650
|
||||
}
|
||||
],
|
||||
"summary": {
|
||||
"passed": 4,
|
||||
"failed": 0,
|
||||
"total": 4
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
### 6. 测试单个 ASR 模型
|
||||
|
||||
```http
|
||||
POST /api/v1/tools/autotest/asr/{model_id}
|
||||
```
|
||||
|
||||
**Path Parameters:**
|
||||
|
||||
| 参数 | 类型 | 说明 |
|
||||
|------|------|------|
|
||||
| model_id | string | ASR 模型ID |
|
||||
|
||||
**Response:**
|
||||
|
||||
```json
|
||||
{
|
||||
"id": "asr_test_001",
|
||||
"started_at": 1705315200.0,
|
||||
"duration_ms": 800,
|
||||
"tests": [
|
||||
{
|
||||
"name": "Model Existence",
|
||||
"passed": true,
|
||||
"message": "Found model: Whisper-1",
|
||||
"duration_ms": 8
|
||||
},
|
||||
{
|
||||
"name": "Hotwords Config",
|
||||
"passed": true,
|
||||
"message": "Hotwords: 3 words"
|
||||
},
|
||||
{
|
||||
"name": "API Availability",
|
||||
"passed": true,
|
||||
"message": "Status: 200",
|
||||
"duration_ms": 250
|
||||
},
|
||||
{
|
||||
"name": "Language Config",
|
||||
"passed": true,
|
||||
"message": "Language: zh"
|
||||
}
|
||||
],
|
||||
"summary": {
|
||||
"passed": 4,
|
||||
"failed": 0,
|
||||
"total": 4
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
### 7. 发送测试消息
|
||||
|
||||
```http
|
||||
POST /api/v1/tools/test-message
|
||||
```
|
||||
|
||||
**Query Parameters:**
|
||||
|
||||
| 参数 | 类型 | 必填 | 默认值 | 说明 |
|
||||
|------|------|------|--------|------|
|
||||
| llm_model_id | string | 是 | - | LLM 模型ID |
|
||||
| message | string | 否 | "Hello, this is a test message." | 测试消息 |
|
||||
|
||||
**Response:**
|
||||
|
||||
```json
|
||||
{
|
||||
"success": true,
|
||||
"reply": "Hello! This is a test reply from GPT-4o.",
|
||||
"usage": {
|
||||
"prompt_tokens": 15,
|
||||
"completion_tokens": 12,
|
||||
"total_tokens": 27
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
**错误响应 (模型不存在):**
|
||||
|
||||
```json
|
||||
{
|
||||
"detail": "LLM Model not found"
|
||||
}
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 测试结果结构
|
||||
|
||||
### AutotestResult
|
||||
|
||||
```typescript
|
||||
interface AutotestResult {
|
||||
id: string; // 测试ID
|
||||
started_at: number; // 开始时间戳
|
||||
duration_ms: number; // 总耗时(毫秒)
|
||||
tests: TestCase[]; // 测试用例列表
|
||||
summary: TestSummary; // 测试摘要
|
||||
}
|
||||
|
||||
interface TestCase {
|
||||
name: string; // 测试名称
|
||||
passed: boolean; // 是否通过
|
||||
message: string; // 测试消息
|
||||
duration_ms: number; // 耗时(毫秒)
|
||||
}
|
||||
|
||||
interface TestSummary {
|
||||
passed: number; // 通过数量
|
||||
failed: number; // 失败数量
|
||||
total: number; // 总数量
|
||||
}
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 测试项目说明
|
||||
|
||||
### LLM 模型测试项目
|
||||
|
||||
| 测试名称 | 说明 |
|
||||
|----------|------|
|
||||
| Model Existence | 检查模型是否存在于数据库 |
|
||||
| API Connection | 测试 API 连接并测量延迟 |
|
||||
| Temperature Setting | 检查温度配置 |
|
||||
| Streaming Support | 测试流式响应支持 |
|
||||
|
||||
### ASR 模型测试项目
|
||||
|
||||
| 测试名称 | 说明 |
|
||||
|----------|------|
|
||||
| Model Existence | 检查模型是否存在于数据库 |
|
||||
| Hotwords Config | 检查热词配置 |
|
||||
| API Availability | 测试 API 可用性 |
|
||||
| Language Config | 检查语言配置 |
|
||||
|
||||
---
|
||||
|
||||
## 单元测试
|
||||
|
||||
项目包含完整的单元测试,位于 `api/tests/test_tools.py`。
|
||||
|
||||
### 测试用例概览
|
||||
|
||||
| 测试类 | 说明 |
|
||||
|--------|------|
|
||||
| TestToolsAPI | 工具列表、健康检查等基础功能测试 |
|
||||
| TestAutotestAPI | 自动测试功能完整测试 |
|
||||
|
||||
### 运行测试
|
||||
|
||||
```bash
|
||||
# 运行工具相关测试
|
||||
pytest api/tests/test_tools.py -v
|
||||
|
||||
# 运行所有测试
|
||||
pytest api/tests/ -v
|
||||
```
|
||||
Reference in New Issue
Block a user