feat: add cache_salt dimension to LLM response cache

This commit is contained in:
2026-07-16 05:23:58 -04:00
parent c5c11ea81b
commit 564c92a043
6 changed files with 57 additions and 14 deletions
+8 -2
View File
@@ -274,6 +274,7 @@ class GovernedLLMClient:
*,
session_id: str | None = None,
parent_call_id: str | None = None,
cache_salt: str | None = None,
) -> LLMResponse:
"""发起 LLM 调用,经四层治理栈:熔断 → 缓存 → 重试+流式 → 遥测。
@@ -281,6 +282,7 @@ class GovernedLLMClient:
messages: OpenAI 格式消息列表。
session_id: 会话 ID(传递到遥测)。
parent_call_id: 父调用 ID(传递到遥测)。
cache_salt: 可选缓存盐,透传到 Redis 缓存键(如跨 epoch 重采样)。
返回:
LLMResponse 统一响应。
@@ -296,7 +298,11 @@ class GovernedLLMClient:
raise CircuitOpenError(f"熔断器已开启,拒绝调用 provider={self._provider}")
# ② 缓存查询(cache 为 None 时跳过)— call_id 在缓存路径独立生成
cached = await self._cache.get(self._model, messages) if self._cache is not None else None
cached = (
await self._cache.get(self._model, messages, cache_salt)
if self._cache is not None
else None
)
if cached is not None:
cache_call_id = str(uuid4())
response = LLMResponse(
@@ -370,7 +376,7 @@ class GovernedLLMClient:
# ④ 写缓存(cache 为 None 时跳过)
if self._cache is not None:
await self._cache.set(self._model, messages, response)
await self._cache.set(self._model, messages, response, cache_salt)
# ⑤ 遥测
await self._telemetry.record_llm_call(