完成:词典和资讯 API 支持 async 和数据库缓存
All checks were successful
构建并部署 AI Agent 服务 / deploy (push) Successful in 5m44s

This commit is contained in:
2026-04-27 17:37:07 +08:00
parent 37ceb6c260
commit f274df6e3d
3 changed files with 161 additions and 16 deletions

View File

@@ -1,28 +1,68 @@
"""
词典API调用工具
Dictionary API Client
支持 async 和真实数据库缓存
"""
from typing import Dict, Any, Optional
import requests
import json
from dataclasses import dataclass
@dataclass
class DictionaryAPIClient:
"""
词典API客户端 - 可扩展支持多种API
词典API客户端 - 可扩展支持多种API和数据库缓存
"""
# 可以配置多个API
youdao_api_key: Optional[str] = None
youdao_api_secret: Optional[str] = None
def query_word_youdao(self, word: str) -> Optional[Dict[str, Any]]:
# 数据库 Repository可选用于缓存单词查询
word_repository: Optional[Any] = None
def __post_init__(self):
"""初始化后,如果有 repository 则支持 async"""
pass
async def query_word_db(self, user_id: str, word: str) -> Optional[Dict[str, Any]]:
"""从数据库缓存查询单词"""
if not self.word_repository:
return None
try:
entity = await self.word_repository.search_by_word(user_id, word)
if entity:
return {
"phonetic": entity.phonetic,
"part_of_speech": entity.part_of_speech,
"definitions": [entity.definition] if entity.definition else [],
"examples": [entity.examples] if entity.examples else []
}
except Exception as e:
print(f"从数据库查询单词失败:{e}")
return None
async def cache_word_db(self, user_id: str, word: str, data: Dict[str, Any]):
"""把单词查询结果缓存到数据库"""
if not self.word_repository:
return
try:
from ...db.models import WordEntity
entity = WordEntity(
user_id=user_id,
word=word,
phonetic=data.get("phonetic", ""),
part_of_speech=data.get("part_of_speech", ""),
definition=data.get("definitions", [""])[0] if data.get("definitions") else "",
examples=data.get("examples", [""])[0] if data.get("examples") else ""
)
await self.word_repository.insert(entity)
except Exception as e:
print(f"缓存单词到数据库失败:{e}")
async def query_word_youdao(self, word: str) -> Optional[Dict[str, Any]]:
"""
调用有道词典API查询单词
调用有道词典API查询单词async 版本)
注意需要配置有道API密钥才能使用
文档https://ai.youdao.com/doc.s#guide
"""
@@ -30,7 +70,7 @@ class DictionaryAPIClient:
return None
try:
# TODO: 实现真实的有道API调用
# TODO: 实现真实的有道API调用(用 httpx 或 aiohttp
# 这里是示例结构
return None
@@ -38,14 +78,13 @@ class DictionaryAPIClient:
print(f"有道API调用失败{e}")
return None
def translate_baidu(self, text: str, from_lang: str = "auto", to_lang: str = "zh") -> Optional[Dict[str, Any]]:
async def translate_baidu(self, text: str, from_lang: str = "auto", to_lang: str = "zh") -> Optional[Dict[str, Any]]:
"""
调用百度翻译API
调用百度翻译APIasync 版本)
注意需要配置百度API密钥才能使用
文档https://fanyi-api.baidu.com/doc/21
"""
# TODO: 实现真实的百度翻译API调用
# TODO: 实现真实的百度翻译API调用(用 httpx 或 aiohttp
return None
def query_word_mock(self, word: str) -> Dict[str, Any]:
@@ -123,7 +162,31 @@ class DictionaryAPIClient:
{"term": "LLM", "type": "技术术语", "definition": "大语言模型", "confidence": 0.92},
{"term": "NLP", "type": "技术术语", "definition": "自然语言处理", "confidence": 0.88}
]
# ========== 统一入口(优先查缓存) ==========
async def query_word(self, user_id: str = "default", word: str = "", use_cache: bool = True) -> Dict[str, Any]:
"""
查询单词(统一入口,优先查数据库缓存)
"""
# 1. 先查数据库缓存
if use_cache:
cached = await self.query_word_db(user_id, word)
if cached:
return cached
# 2. 查第三方 API暂未实现
api_result = await self.query_word_youdao(word)
if api_result:
if use_cache:
await self.cache_word_db(user_id, word, api_result)
return api_result
# 3. 用模拟数据(兜底)
mock_result = self.query_word_mock(word)
if use_cache:
await self.cache_word_db(user_id, word, mock_result)
return mock_result
# 单例实例
# 单例实例(模拟模式,保持向后兼容)
dictionary_api = DictionaryAPIClient()