#!/usr/bin/env python3 """ 简单的翻译测试脚本 用于验证 API 连接和翻译功能 """ import asyncio import sys from pathlib import Path # 添加 src 目录到路径 sys.path.insert(0, str(Path(__file__).parent / "src")) from src.llm_client import OpenRouterClient from src.utils import load_config from rich.console import Console async def test_api_connection(): """测试 API 连接和基本翻译功能""" console = Console() try: # 加载配置 config = load_config('config/config.json') # 初始化客户端 client = OpenRouterClient(config) console.print("[green]✓ OpenRouter 客户端初始化成功[/green]") # 测试简单翻译 test_text = "Hello, this is a test sentence for translation." console.print(f"\n[cyan]测试文本:[/cyan] {test_text}") console.print("[yellow]正在翻译...[/yellow]") translation = await client.test_translation(test_text) console.print(f"[green]翻译结果:[/green] {translation}") # 测试更复杂的文本 complex_text = """ Modern technology has transformed the way we live and work. The rapid advancement of artificial intelligence and machine learning has created new opportunities and challenges for society. """ console.print(f"\n[cyan]复杂测试文本:[/cyan] {complex_text.strip()}") console.print("[yellow]正在翻译...[/yellow]") complex_translation = await client.test_translation(complex_text.strip()) console.print(f"[green]翻译结果:[/green] {complex_translation}") # 测试术语表生成 console.print("\n[cyan]测试术语表生成...[/cyan]") sample_texts = [ "Artificial intelligence and machine learning are transforming industries.", "The complexity of modern systems requires innovative solutions.", "Economic growth and environmental sustainability are key challenges." ] terminology = await client.generate_terminology(sample_texts) if terminology: console.print("[green]✓ 术语表生成成功[/green]") for category, terms in terminology.items(): console.print(f"[yellow]{category}:[/yellow]") for en, zh in terms.items(): console.print(f" {en} -> {zh}") else: console.print("[yellow]术语表为空[/yellow]") await client.close() console.print("\n[green]✓ 所有测试完成[/green]") except Exception as e: console.print(f"[red]✗ 测试失败: {e}[/red]") import traceback console.print(traceback.format_exc()) if __name__ == "__main__": asyncio.run(test_api_connection())