42e3aea6
tangwang
tidy
|
1
|
# 翻译模块
|
3cd09b3b
tangwang
翻译接口改为调用qwen-mt-f...
|
2
|
|
42e3aea6
tangwang
tidy
|
3
|
**快速上手**:见 `docs/QUICKSTART.md` 第 3.4 节。
|
3cd09b3b
tangwang
翻译接口改为调用qwen-mt-f...
|
4
|
|
42e3aea6
tangwang
tidy
|
5
|
## 环境变量
|
3cd09b3b
tangwang
翻译接口改为调用qwen-mt-f...
|
6
|
|
42e3aea6
tangwang
tidy
|
7
8
|
```bash
# Qwen(默认)
|
3cd09b3b
tangwang
翻译接口改为调用qwen-mt-f...
|
9
10
11
12
13
|
DASHSCOPE_API_KEY=sk-xxx
# DeepL
DEEPL_AUTH_KEY=xxx
|
42e3aea6
tangwang
tidy
|
14
|
# 可选
|
5e4dc8e4
tangwang
翻译架构按“一个翻译服务 +
|
15
16
17
|
TRANSLATION_SERVICE_URL=http://127.0.0.1:6006
TRANSLATION_MODEL=llm # 默认能力;也可传 qwen-mt / deepl
TRANSLATION_SCENE=general
|
3cd09b3b
tangwang
翻译接口改为调用qwen-mt-f...
|
18
19
|
```
|
985752f5
tangwang
1. 前端调试功能
|
20
21
22
23
24
25
|
> **重要限速说明(Qwen 机翻)**
> 当前默认的 Qwen 翻译后端使用 `qwen-mt-flash` 云端模型,**官方限速较低,约 RPM=60(每分钟约 60 请求)**。
> - 推荐通过 Redis 翻译缓存复用结果,避免对相同文本重复打云端
> - 高并发场景需要在调用端做限流 / 去抖,或改为离线批量翻译
> - 如需更高吞吐,可考虑 DeepL 或自建翻译服务
|
5e4dc8e4
tangwang
翻译架构按“一个翻译服务 +
|
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
|
## 配置模型
翻译已改为“一个翻译服务 + 多种翻译能力”的结构:
- 业务侧(`QueryParser` / indexer)统一调用 `http://127.0.0.1:6006`
- 服务内按 `services.translation.capabilities` 加载并管理各翻译能力
- 每种能力独立配置 `enabled`、`model`、`timeout` 等参数
- 外部接口通过 `model + scene` 指定本次使用哪种能力、哪个场景
配置入口在 `config/config.yaml -> services.translation`,核心字段示例:
```yaml
services:
translation:
service_url: "http://127.0.0.1:6006"
default_model: "llm"
default_scene: "general"
timeout_sec: 10.0
capabilities:
qwen-mt:
enabled: true
model: "qwen-mt-flash"
llm:
enabled: true
model: "qwen-flash"
deepl:
enabled: false
```
|
6f7840cf
tangwang
refactor: rename ...
|
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
|
## HTTP 接口契约(translator service,端口 6006)
服务默认监听 `http://localhost:6006`,提供:
- `POST /translate`: 文本翻译(支持 `qwen/qwen-mt`、`deepl`、`llm`)
- `GET /health`: 健康检查
### `POST /translate`
**请求体**:
```json
{
"text": "商品名称",
"target_lang": "en",
"source_lang": "zh",
|
5e4dc8e4
tangwang
翻译架构按“一个翻译服务 +
|
71
72
|
"model": "qwen-mt",
"scene": "sku_name",
|
6f7840cf
tangwang
refactor: rename ...
|
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
|
"prompt": null
}
```
- `text` 支持两种形式:
- 单条:`string`
- 批量:`string[]`(等长返回,顺序对应)
**响应体**(单条):
```json
{
"text": "商品名称",
"target_lang": "en",
"source_lang": "zh",
"translated_text": "Product name",
"status": "success",
|
5e4dc8e4
tangwang
翻译架构按“一个翻译服务 +
|
90
91
|
"model": "qwen-mt",
"scene": "sku_name"
|
6f7840cf
tangwang
refactor: rename ...
|
92
93
94
95
96
97
98
99
100
101
102
103
|
}
```
**响应体**(批量):
```json
{
"text": ["商品名称1", "商品名称2"],
"target_lang": "en",
"source_lang": "zh",
"translated_text": ["Product name 1", null],
"status": "success",
|
5e4dc8e4
tangwang
翻译架构按“一个翻译服务 +
|
104
105
|
"model": "qwen-mt",
"scene": "sku_name"
|
6f7840cf
tangwang
refactor: rename ...
|
106
107
108
109
110
|
}
```
批量模式下,**单条失败用 `null` 占位**(即 `translated_text[i] = null`),保证长度与顺序一一对应,避免部分失败导致整批报错。
|
5e4dc8e4
tangwang
翻译架构按“一个翻译服务 +
|
111
112
113
114
115
116
|
说明:
- `scene` 是标准字段,`context` 仅保留为兼容别名
- `model` 只能选择已在 `services.translation.capabilities` 中启用的能力
- `/health` 会返回 `default_model`、`default_scene` 与 `enabled_capabilities`
|
6f7840cf
tangwang
refactor: rename ...
|
117
118
|
---
|
5e4dc8e4
tangwang
翻译架构按“一个翻译服务 +
|
119
|
## 开发者接口约定(代码调用)
|
6f7840cf
tangwang
refactor: rename ...
|
120
|
|
5e4dc8e4
tangwang
翻译架构按“一个翻译服务 +
|
121
|
代码侧(如 query/indexer)仍通过 `providers.translation.create_translation_provider()` 获取实例并调用 `translate()`,但该实例现在固定是 **translator service client**,不再在业务侧做翻译 provider 选择。
|
6f7840cf
tangwang
refactor: rename ...
|
122
123
124
125
126
127
128
129
130
131
|
### 输入输出形状(Shape)
- `translate(text=...)` 支持:
- **单条**:`text: str` → 返回 `Optional[str]`
- **批量**:`text: List[str]` → 返回 `List[Optional[str]]`
- **批量语义**:返回列表必须与输入 **等长且顺序对应**;某条翻译失败时,对应位置为 `None`(HTTP JSON 中表现为 `null`)。
### 批量能力标识(supports_batch)
|
5e4dc8e4
tangwang
翻译架构按“一个翻译服务 +
|
132
|
服务客户端与服务内后端都可以暴露 `supports_batch`。若后端不支持批量,服务端会逐条拆分并保持 shape。
|
6f7840cf
tangwang
refactor: rename ...
|
133
134
135
136
|
为便于上层(如 `api/translator_app.py`)做最优调用,provider 可暴露:
- `supports_batch: bool`(property)
|