Blame view

query/test_translation.py 7.51 KB
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
  """
  
  import sys
  import os
  from pathlib import Path
d4cadc13   tangwang   翻译重构
17
  from concurrent.futures import ThreadPoolExecutor
0064e946   tangwang   feat: 增量索引服务、租户配置...
18
19
20
21
22
  
  # Add parent directory to path
  sys.path.insert(0, str(Path(__file__).parent.parent))
  
  from config import ConfigLoader
a0a173ae   tangwang   last
23
  from query.qwen_mt_translate import Translator
0064e946   tangwang   feat: 增量索引服务、租户配置...
24
25
26
27
28
29
30
31
32
33
34
  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
35
      """Test configuration loading"""
0064e946   tangwang   feat: 增量索引服务、租户配置...
36
      print("\n" + "="*60)
ff32d894   tangwang   rerank
37
      print("Test 1: Configuration loading")
0064e946   tangwang   feat: 增量索引服务、租户配置...
38
39
40
41
42
43
      print("="*60)
      
      try:
          config_loader = ConfigLoader()
          config = config_loader.load_config()
          
ff32d894   tangwang   rerank
44
45
          print(f"✓ Configuration loaded successfully")
          print(f"  Translation service: {config.query_config.translation_service}")
0064e946   tangwang   feat: 增量索引服务、租户配置...
46
47
48
          
          return config
      except Exception as e:
ff32d894   tangwang   rerank
49
          print(f"✗ Configuration loading failed: {e}")
0064e946   tangwang   feat: 增量索引服务、租户配置...
50
51
52
53
54
55
          import traceback
          traceback.print_exc()
          return None
  
  
  def test_translator_sync(config):
ff32d894   tangwang   rerank
56
      """Test synchronous translation (indexing scenario)"""
0064e946   tangwang   feat: 增量索引服务、租户配置...
57
      print("\n" + "="*60)
ff32d894   tangwang   rerank
58
      print("Test 2: Synchronous translation (indexing scenario)")
0064e946   tangwang   feat: 增量索引服务、租户配置...
59
60
61
      print("="*60)
      
      if not config:
ff32d894   tangwang   rerank
62
          print("✗ Skipped: Configuration not loaded")
0064e946   tangwang   feat: 增量索引服务、租户配置...
63
64
65
66
67
68
69
70
71
72
          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
          )
          
d4cadc13   tangwang   翻译重构
73
          # 测试商品标题翻译(使用sku_name提示词)
0064e946   tangwang   feat: 增量索引服务、租户配置...
74
          test_texts = [
d4cadc13   tangwang   翻译重构
75
76
              ("蓝牙耳机", "zh", "en", "sku_name"),
              ("Wireless Headphones", "en", "zh", "sku_name"),
0064e946   tangwang   feat: 增量索引服务、租户配置...
77
78
          ]
          
d4cadc13   tangwang   翻译重构
79
          for text, source_lang, target_lang, scene in test_texts:
ff32d894   tangwang   rerank
80
81
82
              print(f"\nTranslation test:")
              print(f"  Original text ({source_lang}): {text}")
              print(f"  Target language: {target_lang}")
d4cadc13   tangwang   翻译重构
83
              print(f"  Scene: {scene}")
0064e946   tangwang   feat: 增量索引服务、租户配置...
84
85
86
87
88
              
              result = translator.translate(
                  text,
                  target_lang=target_lang,
                  source_lang=source_lang,
d4cadc13   tangwang   翻译重构
89
                  context=scene,
0064e946   tangwang   feat: 增量索引服务、租户配置...
90
91
92
              )
              
              if result:
ff32d894   tangwang   rerank
93
94
                  print(f"  Result: {result}")
                  print(f"  ✓ Translation successful")
0064e946   tangwang   feat: 增量索引服务、租户配置...
95
              else:
ff32d894   tangwang   rerank
96
                  print(f"  ⚠ Translation returned None (possibly mock mode or no API key)")
0064e946   tangwang   feat: 增量索引服务、租户配置...
97
98
99
100
          
          return translator
          
      except Exception as e:
ff32d894   tangwang   rerank
101
          print(f"✗ Synchronous translation test failed: {e}")
0064e946   tangwang   feat: 增量索引服务、租户配置...
102
103
104
105
106
107
          import traceback
          traceback.print_exc()
          return None
  
  
  def test_translator_async(config, translator):
ff32d894   tangwang   rerank
108
      """Test asynchronous translation (query scenario)"""
0064e946   tangwang   feat: 增量索引服务、租户配置...
109
      print("\n" + "="*60)
ff32d894   tangwang   rerank
110
      print("Test 3: Asynchronous translation (query scenario)")
0064e946   tangwang   feat: 增量索引服务、租户配置...
111
112
113
      print("="*60)
      
      if not config or not translator:
ff32d894   tangwang   rerank
114
          print("✗ Skipped: Configuration or translator not initialized")
0064e946   tangwang   feat: 增量索引服务、租户配置...
115
116
117
118
119
120
          return
      
      try:
          query_text = "手机"
          target_langs = ['en']
          source_lang = 'zh'
d4cadc13   tangwang   翻译重构
121
  
ff32d894   tangwang   rerank
122
123
          print(f"Query text: {query_text}")
          print(f"Target languages: {target_langs}")
d4cadc13   tangwang   翻译重构
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
          print("Scene: ecommerce_search_query")
          
          print(f"\nConcurrent translation via generic translate():")
          with ThreadPoolExecutor(max_workers=len(target_langs)) as executor:
              futures = {
                  lang: executor.submit(
                      translator.translate,
                      query_text,
                      lang,
                      source_lang,
                      "ecommerce_search_query",
                  )
                  for lang in target_langs
              }
              for lang, future in futures.items():
                  print(f"  {lang}: {future.result()}")
0064e946   tangwang   feat: 增量索引服务、租户配置...
140
141
          
      except Exception as e:
ff32d894   tangwang   rerank
142
          print(f"✗ Asynchronous translation test failed: {e}")
0064e946   tangwang   feat: 增量索引服务、租户配置...
143
144
145
146
147
148
149
          import traceback
          traceback.print_exc()
  
  
  def test_cache():
      """测试缓存功能"""
      print("\n" + "="*60)
ff32d894   tangwang   rerank
150
      print("Test 4: Cache functionality")
0064e946   tangwang   feat: 增量索引服务、租户配置...
151
152
153
154
155
156
157
158
159
160
161
162
163
164
      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"
0064e946   tangwang   feat: 增量索引服务、租户配置...
165
          
ff32d894   tangwang   rerank
166
          print(f"First translation (should call API or return mock):")
d4cadc13   tangwang   翻译重构
167
          result1 = translator.translate(test_text, target_lang, source_lang, context="default")
ff32d894   tangwang   rerank
168
169
170
          print(f"  Result: {result1}")
  
          print(f"\nSecond translation (should use cache):")
d4cadc13   tangwang   翻译重构
171
          result2 = translator.translate(test_text, target_lang, source_lang, context="default")
ff32d894   tangwang   rerank
172
173
          print(f"  Result: {result2}")
  
0064e946   tangwang   feat: 增量索引服务、租户配置...
174
          if result1 == result2:
ff32d894   tangwang   rerank
175
              print(f"  ✓ Cache functionality working properly")
0064e946   tangwang   feat: 增量索引服务、租户配置...
176
          else:
ff32d894   tangwang   rerank
177
              print(f"  ⚠ Cache might have issues")
0064e946   tangwang   feat: 增量索引服务、租户配置...
178
179
          
      except Exception as e:
ff32d894   tangwang   rerank
180
          print(f"✗ Cache test failed: {e}")
0064e946   tangwang   feat: 增量索引服务、租户配置...
181
182
183
184
185
          import traceback
          traceback.print_exc()
  
  
  def test_context_parameter():
ff32d894   tangwang   rerank
186
      """Test DeepL Context parameter usage"""
0064e946   tangwang   feat: 增量索引服务、租户配置...
187
      print("\n" + "="*60)
ff32d894   tangwang   rerank
188
      print("Test 5: DeepL Context parameter")
0064e946   tangwang   feat: 增量索引服务、租户配置...
189
190
191
192
193
194
195
196
197
198
199
200
201
      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 = "手机"
0064e946   tangwang   feat: 增量索引服务、租户配置...
202
          
ff32d894   tangwang   rerank
203
          print(f"Test text: {text}")
d4cadc13   tangwang   翻译重构
204
          print("Scene: ecommerce_search_query")
0064e946   tangwang   feat: 增量索引服务、租户配置...
205
206
207
208
209
210
          
          # 带context的翻译
          result_with_context = translator.translate(
              text,
              target_lang='en',
              source_lang='zh',
d4cadc13   tangwang   翻译重构
211
              context="ecommerce_search_query",
0064e946   tangwang   feat: 增量索引服务、租户配置...
212
          )
ff32d894   tangwang   rerank
213
          print(f"\nTranslation result with context: {result_with_context}")
0064e946   tangwang   feat: 增量索引服务、租户配置...
214
215
216
217
218
219
220
221
          
          # 不带context的翻译
          result_without_context = translator.translate(
              text,
              target_lang='en',
              source_lang='zh',
              prompt=None
          )
ff32d894   tangwang   rerank
222
          print(f"Translation result without context: {result_without_context}")
0064e946   tangwang   feat: 增量索引服务、租户配置...
223
          
ff32d894   tangwang   rerank
224
225
          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: 增量索引服务、租户配置...
226
227
          
      except Exception as e:
ff32d894   tangwang   rerank
228
          print(f"✗ Context parameter test failed: {e}")
0064e946   tangwang   feat: 增量索引服务、租户配置...
229
230
231
232
233
          import traceback
          traceback.print_exc()
  
  
  def main():
ff32d894   tangwang   rerank
234
      """Main test function"""
0064e946   tangwang   feat: 增量索引服务、租户配置...
235
      print("="*60)
ff32d894   tangwang   rerank
236
      print("Translation function test")
0064e946   tangwang   feat: 增量索引服务、租户配置...
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
      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
255
      print("Test completed")
0064e946   tangwang   feat: 增量索引服务、租户配置...
256
257
258
259
260
      print("="*60)
  
  
  if __name__ == '__main__':
      main()