feat: LLM/Embedding 重试次数纳入 system.yaml 统一配置
- system.yaml: llm.max_retries(死配置) 改为 llm.max_attempts;embedding 段新增 max_attempts - llm/client.py: LLMConfig 新增 max_attempts(默认 3 兜底),load_llm_config 读取配置 - llm/extractor.py: translate_and_extract(_async) max_attempts=None 时取 config.max_attempts - embedding/client.py: load_embedding_config 读取 embedding.max_attempts - scheduler/reporter.py: _call_llm_simple max_retries=None 时取 max_attempts-1 - 新增 2 个配置读取测试;已同步 pi5 验证
This commit is contained in:
@@ -25,6 +25,9 @@ _QWEN_DEFAULT_BASE = "https://dashscope.aliyuncs.com/compatible-mode/v1"
|
||||
_DEEPSEEK_DEFAULT_MODEL = "deepseek-chat"
|
||||
_QWEN_DEFAULT_MODEL = "qwen-plus"
|
||||
|
||||
# 默认单次调用总尝试次数(含首次;system.yaml llm.max_attempts 未配置时兜底)
|
||||
_DEFAULT_MAX_ATTEMPTS = 3
|
||||
|
||||
|
||||
def _load_system_config() -> dict:
|
||||
"""加载 configs/system.yaml 中 llm 段配置。"""
|
||||
@@ -50,6 +53,7 @@ class LLMConfig:
|
||||
timeout_sec: float = 60.0
|
||||
temperature: float = 0.1
|
||||
max_tokens: int = 8192
|
||||
max_attempts: int = _DEFAULT_MAX_ATTEMPTS # 单次调用总尝试次数(含首次)
|
||||
|
||||
def __post_init__(self) -> None:
|
||||
if not self.api_key:
|
||||
@@ -93,6 +97,7 @@ def load_llm_config(
|
||||
timeout = float(config.get("timeout_sec", 60.0))
|
||||
temperature = float(config.get("temperature", 0.1))
|
||||
max_tokens = int(config.get("max_tokens", 8192))
|
||||
max_attempts = int(config.get("max_attempts", _DEFAULT_MAX_ATTEMPTS))
|
||||
|
||||
return LLMConfig(
|
||||
provider=p,
|
||||
@@ -102,6 +107,7 @@ def load_llm_config(
|
||||
timeout_sec=timeout,
|
||||
temperature=temperature,
|
||||
max_tokens=max_tokens,
|
||||
max_attempts=max_attempts,
|
||||
)
|
||||
|
||||
|
||||
|
||||
+8
-3
@@ -226,7 +226,7 @@ def translate_and_extract(
|
||||
article: ProcessedArticle,
|
||||
*,
|
||||
template: PromptTemplate | None = None,
|
||||
max_attempts: int = DEFAULT_MAX_ATTEMPTS,
|
||||
max_attempts: int | None = None,
|
||||
) -> EnTranslatedArticle:
|
||||
"""同步翻译 + 事件抽取(单篇文章,带重试)。
|
||||
|
||||
@@ -235,7 +235,8 @@ def translate_and_extract(
|
||||
config: LLM 配置
|
||||
article: 待处理的英文新闻
|
||||
template: Prompt 模板,默认加载 prompts/translation_and_extraction.md
|
||||
max_attempts: 最大重试次数
|
||||
max_attempts: 最大尝试次数(含首次);None 时取 config.max_attempts
|
||||
(来自 system.yaml llm.max_attempts)
|
||||
|
||||
Returns:
|
||||
EnTranslatedArticle 含双语内容 + 事件
|
||||
@@ -243,6 +244,8 @@ def translate_and_extract(
|
||||
Raises:
|
||||
LLMCallError: 所有重试均失败
|
||||
"""
|
||||
if max_attempts is None:
|
||||
max_attempts = config.max_attempts
|
||||
tpl = template or PromptTemplate()
|
||||
system_prompt, user_prompt = tpl.render(article)
|
||||
|
||||
@@ -305,10 +308,12 @@ async def translate_and_extract_async(
|
||||
article: ProcessedArticle,
|
||||
*,
|
||||
template: PromptTemplate | None = None,
|
||||
max_attempts: int = DEFAULT_MAX_ATTEMPTS,
|
||||
max_attempts: int | None = None,
|
||||
semaphore: asyncio.Semaphore | None = None,
|
||||
) -> EnTranslatedArticle:
|
||||
"""异步翻译 + 事件抽取(批处理用),与同步版逻辑等价。"""
|
||||
if max_attempts is None:
|
||||
max_attempts = config.max_attempts
|
||||
tpl = template or PromptTemplate()
|
||||
system_prompt, user_prompt = tpl.render(article)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user