Files
epub_bilingual_translator/archive/v0.09/test_specific_failure_cases.py
T
谭凯 7a93c52b42 feat: Release v0.10 - Modular Architecture & External Config
- Refactor codebase into src/ (preprocessing, translation, assembly)
- Add pipeline/ scripts for individual stages
- Externalize configuration to config/config.yaml
- Fix Cover Image preservation
- Update documentation and manuals
2026-01-31 22:49:44 +08:00

125 lines
4.1 KiB
Python

#!/usr/bin/env python3
"""
针对性测试脚本:复现用户报告的占位符丢失问题
直接读取 Manifest 中的特定失败 Item (p_00006, p_00011 等)
调用真实 LLM 进行翻译,并打印完整的 Prompt 和 Response
"""
import sys
import asyncio
import json
from pathlib import Path
from loguru import logger
# 添加 src 到路径
sys.path.insert(0, '.')
from src.manifest_manager import ManifestManager
from src.llm_client import LLMClient
from src.utils import load_config
# 配置日志
logger.remove()
logger.add(sys.stdout, level="DEBUG")
async def run_test():
print("=== 1. 加载配置 ===")
try:
config = load_config()
# 自动选择配置好的 provider
if 'v3' in config['providers'] and config['providers']['v3'].get('api_key'):
config['llm'] = config['providers']['v3']
print("Using Provider: v3")
elif 'openrouter' in config['providers']:
config['llm'] = config['providers']['openrouter']
print("Using Provider: openrouter")
else:
print("No valid provider found with API key in config!")
return
except Exception as e:
print(f"Config load failed: {e}")
return
print("\n=== 2. 加载真实 Manifest ===")
manifest_dir = Path("cache/manifests")
manifest_files = list(manifest_dir.glob("*.json"))
if not manifest_files:
print("Error: No manifest file found")
return
# 优先选择包含 "OpenAI" 的那个文件(用户截图)
target_manifest = next((f for f in manifest_files if "OpenAI" in f.name), manifest_files[0])
print(f"Loading: {target_manifest}")
manifest = ManifestManager(str(target_manifest))
if not manifest.load():
print("Failed to load manifest")
return
# 提取目标失败案例
target_ids = ["p_00006", "p_00009", "p_00011", "p_00013"]
# 也包括上下文以免错位 (p_00003 - p_00006)
context_ids = ["p_00003", "p_00004", "p_00005", "p_00006"]
items_to_test = []
# 测试组 1: 上下文错位测试
print("\n=== 准备测试组 1: 上下文错位及占位符 (p_00003-00006) ===")
group1 = []
for uid in context_ids:
item = manifest._items_by_id.get(uid)
if item:
# 强制清空旧翻译,模拟重新翻译
item.translation = None
item.translation_with_placeholders = None
group1.append(item)
print(f"Added {uid}: {item.text_with_placeholders}")
# 测试组 2: 独立行占位符丢失测试 (p_00009, p_00011)
print("\n=== 准备测试组 2: 独立行占位符 (p_00009, p_00011) ===")
group2 = []
for uid in ["p_00009", "p_00011"]:
item = manifest._items_by_id.get(uid)
if item:
item.translation = None
group2.append(item)
print(f"Added {uid}: {item.text_with_placeholders}")
client = LLMClient(config)
# 执行测试 1
if group1:
print("\n\n>>> 执行 Group 1 测试 (Context Alignment) <<<")
# 打印 Prompt 预览
prompt = client._build_prompt(group1, mode="chinese")
print("\n[PROMPT PREVIEW]:")
print("-" * 20)
print(prompt)
print("-" * 20)
# 调用 LLM
print("\n[CALLING LLM]...")
results = await client.translate_chunk(group1, mode="chinese")
print("\n[RESULTS Group 1]:")
for uid, trans in results.items():
print(f"{uid}: {trans}")
if uid == "p_00006":
print(f" > Contains φ1φ? {'φ1φ' in trans}")
# 执行测试 2
if group2:
print("\n\n>>> 执行 Group 2 测试 (Isolated Placeholders) <<<")
prompt = client._build_prompt(group2, mode="chinese")
print("\n[PROMPT PREVIEW]:")
print(prompt)
print("\n[CALLING LLM]...")
results = await client.translate_chunk(group2, mode="chinese")
print("\n[RESULTS Group 2]:")
for uid, trans in results.items():
print(f"{uid}: {trans}")
if __name__ == "__main__":
asyncio.run(run_test())