0064e946
tangwang
feat: 增量索引服务、租户配置...
|
1
2
|
#!/usr/bin/env python3
"""
|
ff32d894
tangwang
rerank
|
3
|
Translation function test script.
|
0064e946
tangwang
feat: 增量索引服务、租户配置...
|
4
|
|
ff32d894
tangwang
rerank
|
5
6
7
8
9
10
11
|
Test content:
1. Translation prompt configuration loading
2. Synchronous translation (indexing scenario)
3. Asynchronous translation (query scenario)
4. Usage of different prompts
5. Cache functionality
6. DeepL Context parameter usage
|
0064e946
tangwang
feat: 增量索引服务、租户配置...
|
12
13
14
15
16
17
18
19
20
21
|
"""
import sys
import os
from pathlib import Path
# Add parent directory to path
sys.path.insert(0, str(Path(__file__).parent.parent))
from config import ConfigLoader
|
a0a173ae
tangwang
last
|
22
|
from query.qwen_mt_translate import Translator
|
0064e946
tangwang
feat: 增量索引服务、租户配置...
|
23
24
25
26
27
28
29
30
31
32
33
|
import logging
# Configure logging
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s'
)
logger = logging.getLogger(__name__)
def test_config_loading():
|
ff32d894
tangwang
rerank
|
34
|
"""Test configuration loading"""
|
0064e946
tangwang
feat: 增量索引服务、租户配置...
|
35
|
print("\n" + "="*60)
|
ff32d894
tangwang
rerank
|
36
|
print("Test 1: Configuration loading")
|
0064e946
tangwang
feat: 增量索引服务、租户配置...
|
37
38
39
40
41
42
|
print("="*60)
try:
config_loader = ConfigLoader()
config = config_loader.load_config()
|
ff32d894
tangwang
rerank
|
43
44
45
|
print(f"✓ Configuration loaded successfully")
print(f" Translation service: {config.query_config.translation_service}")
print(f" Translation prompt configuration:")
|
0064e946
tangwang
feat: 增量索引服务、租户配置...
|
46
47
48
49
50
|
for key, value in config.query_config.translation_prompts.items():
print(f" {key}: {value[:60]}..." if len(value) > 60 else f" {key}: {value}")
return config
except Exception as e:
|
ff32d894
tangwang
rerank
|
51
|
print(f"✗ Configuration loading failed: {e}")
|
0064e946
tangwang
feat: 增量索引服务、租户配置...
|
52
53
54
55
56
57
|
import traceback
traceback.print_exc()
return None
def test_translator_sync(config):
|
ff32d894
tangwang
rerank
|
58
|
"""Test synchronous translation (indexing scenario)"""
|
0064e946
tangwang
feat: 增量索引服务、租户配置...
|
59
|
print("\n" + "="*60)
|
ff32d894
tangwang
rerank
|
60
|
print("Test 2: Synchronous translation (indexing scenario)")
|
0064e946
tangwang
feat: 增量索引服务、租户配置...
|
61
62
63
|
print("="*60)
if not config:
|
ff32d894
tangwang
rerank
|
64
|
print("✗ Skipped: Configuration not loaded")
|
0064e946
tangwang
feat: 增量索引服务、租户配置...
|
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
|
return None
try:
translator = Translator(
api_key=config.query_config.translation_api_key,
use_cache=True,
glossary_id=config.query_config.translation_glossary_id,
translation_context=config.query_config.translation_context
)
# 测试商品标题翻译(使用product_title提示词)
test_texts = [
("蓝牙耳机", "zh", "en", "product_title"),
("Wireless Headphones", "en", "zh", "product_title"),
]
for text, source_lang, target_lang, prompt_type in test_texts:
if prompt_type == "product_title":
if target_lang == "zh":
prompt = config.query_config.translation_prompts.get('product_title_zh')
else:
prompt = config.query_config.translation_prompts.get('product_title_en')
else:
if target_lang == "zh":
prompt = config.query_config.translation_prompts.get('default_zh')
else:
prompt = config.query_config.translation_prompts.get('default_en')
|
ff32d894
tangwang
rerank
|
93
94
95
96
|
print(f"\nTranslation test:")
print(f" Original text ({source_lang}): {text}")
print(f" Target language: {target_lang}")
print(f" Prompt: {prompt[:50] if prompt else 'None'}...")
|
0064e946
tangwang
feat: 增量索引服务、租户配置...
|
97
98
99
100
101
102
103
104
105
|
result = translator.translate(
text,
target_lang=target_lang,
source_lang=source_lang,
prompt=prompt
)
if result:
|
ff32d894
tangwang
rerank
|
106
107
|
print(f" Result: {result}")
print(f" ✓ Translation successful")
|
0064e946
tangwang
feat: 增量索引服务、租户配置...
|
108
|
else:
|
ff32d894
tangwang
rerank
|
109
|
print(f" ⚠ Translation returned None (possibly mock mode or no API key)")
|
0064e946
tangwang
feat: 增量索引服务、租户配置...
|
110
111
112
113
|
return translator
except Exception as e:
|
ff32d894
tangwang
rerank
|
114
|
print(f"✗ Synchronous translation test failed: {e}")
|
0064e946
tangwang
feat: 增量索引服务、租户配置...
|
115
116
117
118
119
120
|
import traceback
traceback.print_exc()
return None
def test_translator_async(config, translator):
|
ff32d894
tangwang
rerank
|
121
|
"""Test asynchronous translation (query scenario)"""
|
0064e946
tangwang
feat: 增量索引服务、租户配置...
|
122
|
print("\n" + "="*60)
|
ff32d894
tangwang
rerank
|
123
|
print("Test 3: Asynchronous translation (query scenario)")
|
0064e946
tangwang
feat: 增量索引服务、租户配置...
|
124
125
126
|
print("="*60)
if not config or not translator:
|
ff32d894
tangwang
rerank
|
127
|
print("✗ Skipped: Configuration or translator not initialized")
|
0064e946
tangwang
feat: 增量索引服务、租户配置...
|
128
129
130
131
132
133
134
135
136
|
return
try:
query_text = "手机"
target_langs = ['en']
source_lang = 'zh'
query_prompt = config.query_config.translation_prompts.get('query_zh')
|
ff32d894
tangwang
rerank
|
137
138
139
|
print(f"Query text: {query_text}")
print(f"Target languages: {target_langs}")
print(f"Prompt: {query_prompt}")
|
0064e946
tangwang
feat: 增量索引服务、租户配置...
|
140
141
142
143
144
145
146
147
148
149
150
|
# 异步模式(立即返回,后台翻译)
results = translator.translate_multi(
query_text,
target_langs,
source_lang=source_lang,
context=config.query_config.translation_context,
async_mode=True,
prompt=query_prompt
)
|
ff32d894
tangwang
rerank
|
151
|
print(f"\nAsynchronous translation results:")
|
0064e946
tangwang
feat: 增量索引服务、租户配置...
|
152
153
|
for lang, translation in results.items():
if translation:
|
ff32d894
tangwang
rerank
|
154
|
print(f" {lang}: {translation} (cache hit)")
|
0064e946
tangwang
feat: 增量索引服务、租户配置...
|
155
|
else:
|
ff32d894
tangwang
rerank
|
156
|
print(f" {lang}: None (translating in background...)")
|
0064e946
tangwang
feat: 增量索引服务、租户配置...
|
157
158
|
# 同步模式(等待完成)
|
ff32d894
tangwang
rerank
|
159
|
print(f"\nSynchronous translation (waiting for completion):")
|
0064e946
tangwang
feat: 增量索引服务、租户配置...
|
160
161
162
163
164
165
166
167
168
169
170
171
172
|
results_sync = translator.translate_multi(
query_text,
target_langs,
source_lang=source_lang,
context=config.query_config.translation_context,
async_mode=False,
prompt=query_prompt
)
for lang, translation in results_sync.items():
print(f" {lang}: {translation}")
except Exception as e:
|
ff32d894
tangwang
rerank
|
173
|
print(f"✗ Asynchronous translation test failed: {e}")
|
0064e946
tangwang
feat: 增量索引服务、租户配置...
|
174
175
176
177
178
179
180
|
import traceback
traceback.print_exc()
def test_cache():
"""测试缓存功能"""
print("\n" + "="*60)
|
ff32d894
tangwang
rerank
|
181
|
print("Test 4: Cache functionality")
|
0064e946
tangwang
feat: 增量索引服务、租户配置...
|
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
|
print("="*60)
try:
config_loader = ConfigLoader()
config = config_loader.load_config()
translator = Translator(
api_key=config.query_config.translation_api_key,
use_cache=True
)
test_text = "测试文本"
target_lang = "en"
source_lang = "zh"
prompt = config.query_config.translation_prompts.get('default_zh')
|
ff32d894
tangwang
rerank
|
198
|
print(f"First translation (should call API or return mock):")
|
0064e946
tangwang
feat: 增量索引服务、租户配置...
|
199
|
result1 = translator.translate(test_text, target_lang, source_lang, prompt=prompt)
|
ff32d894
tangwang
rerank
|
200
201
202
|
print(f" Result: {result1}")
print(f"\nSecond translation (should use cache):")
|
0064e946
tangwang
feat: 增量索引服务、租户配置...
|
203
|
result2 = translator.translate(test_text, target_lang, source_lang, prompt=prompt)
|
ff32d894
tangwang
rerank
|
204
205
|
print(f" Result: {result2}")
|
0064e946
tangwang
feat: 增量索引服务、租户配置...
|
206
|
if result1 == result2:
|
ff32d894
tangwang
rerank
|
207
|
print(f" ✓ Cache functionality working properly")
|
0064e946
tangwang
feat: 增量索引服务、租户配置...
|
208
|
else:
|
ff32d894
tangwang
rerank
|
209
|
print(f" ⚠ Cache might have issues")
|
0064e946
tangwang
feat: 增量索引服务、租户配置...
|
210
211
|
except Exception as e:
|
ff32d894
tangwang
rerank
|
212
|
print(f"✗ Cache test failed: {e}")
|
0064e946
tangwang
feat: 增量索引服务、租户配置...
|
213
214
215
216
217
|
import traceback
traceback.print_exc()
def test_context_parameter():
|
ff32d894
tangwang
rerank
|
218
|
"""Test DeepL Context parameter usage"""
|
0064e946
tangwang
feat: 增量索引服务、租户配置...
|
219
|
print("\n" + "="*60)
|
ff32d894
tangwang
rerank
|
220
|
print("Test 5: DeepL Context parameter")
|
0064e946
tangwang
feat: 增量索引服务、租户配置...
|
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
|
print("="*60)
try:
config_loader = ConfigLoader()
config = config_loader.load_config()
translator = Translator(
api_key=config.query_config.translation_api_key,
use_cache=False # 禁用缓存以便测试
)
# 测试带context和不带context的翻译
text = "手机"
prompt = config.query_config.translation_prompts.get('query_zh')
|
ff32d894
tangwang
rerank
|
236
237
|
print(f"Test text: {text}")
print(f"Prompt (as context): {prompt}")
|
0064e946
tangwang
feat: 增量索引服务、租户配置...
|
238
239
240
241
242
243
244
245
|
# 带context的翻译
result_with_context = translator.translate(
text,
target_lang='en',
source_lang='zh',
prompt=prompt
)
|
ff32d894
tangwang
rerank
|
246
|
print(f"\nTranslation result with context: {result_with_context}")
|
0064e946
tangwang
feat: 增量索引服务、租户配置...
|
247
248
249
250
251
252
253
254
|
# 不带context的翻译
result_without_context = translator.translate(
text,
target_lang='en',
source_lang='zh',
prompt=None
)
|
ff32d894
tangwang
rerank
|
255
|
print(f"Translation result without context: {result_without_context}")
|
0064e946
tangwang
feat: 增量索引服务、租户配置...
|
256
|
|
ff32d894
tangwang
rerank
|
257
258
|
print(f"\n✓ Context parameter test completed")
print(f" Note: According to DeepL API, context parameter affects translation but does not participate in translation itself")
|
0064e946
tangwang
feat: 增量索引服务、租户配置...
|
259
260
|
except Exception as e:
|
ff32d894
tangwang
rerank
|
261
|
print(f"✗ Context parameter test failed: {e}")
|
0064e946
tangwang
feat: 增量索引服务、租户配置...
|
262
263
264
265
266
|
import traceback
traceback.print_exc()
def main():
|
ff32d894
tangwang
rerank
|
267
|
"""Main test function"""
|
0064e946
tangwang
feat: 增量索引服务、租户配置...
|
268
|
print("="*60)
|
ff32d894
tangwang
rerank
|
269
|
print("Translation function test")
|
0064e946
tangwang
feat: 增量索引服务、租户配置...
|
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
|
print("="*60)
# 测试1: 配置加载
config = test_config_loading()
# 测试2: 同步翻译
translator = test_translator_sync(config)
# 测试3: 异步翻译
test_translator_async(config, translator)
# 测试4: 缓存功能
test_cache()
# 测试5: Context参数
test_context_parameter()
print("\n" + "="*60)
|
ff32d894
tangwang
rerank
|
288
|
print("Test completed")
|
0064e946
tangwang
feat: 增量索引服务、租户配置...
|
289
290
291
292
293
|
print("="*60)
if __name__ == '__main__':
main()
|