mirror of
https://gitee.com/qqtt-ah/ecom-dmb.git
synced 2026-11-24 16:29:19 +08:00
fix: 探测 Responses/store 能力并在无状态时用 Completions 带全量历史
This commit is contained in:
@@ -7,8 +7,8 @@ import json
|
||||
import redis.asyncio as redis
|
||||
|
||||
from config.settings import Settings
|
||||
from domain.models import LlmRuntimeConfig
|
||||
from domain.ports import LlmRuntimeCache
|
||||
from domain.models import LlmProtocolCapability, LlmRuntimeConfig
|
||||
from domain.ports import LlmProtocolCapabilityCache, LlmRuntimeCache
|
||||
from infrastructure.llm.api_style import normalize_api_style
|
||||
|
||||
|
||||
@@ -99,6 +99,53 @@ def get_redis(settings: Settings) -> redis.Redis:
|
||||
return _redis
|
||||
|
||||
|
||||
class RedisLlmProtocolCapabilityCache(LlmProtocolCapabilityCache):
|
||||
"""按 provider_id(优先)或 base_url 缓存 Responses/store 能力。"""
|
||||
|
||||
def __init__(self, client: redis.Redis) -> None:
|
||||
self._r = client
|
||||
|
||||
@staticmethod
|
||||
def _key(*, provider_id: int | None, base_url: str) -> str:
|
||||
if provider_id is not None and int(provider_id) > 0:
|
||||
return f"agent:llm:cap:p:{int(provider_id)}"
|
||||
url = (base_url or "").rstrip("/").lower()
|
||||
return f"agent:llm:cap:u:{url}"
|
||||
|
||||
async def get(
|
||||
self, *, provider_id: int | None, base_url: str
|
||||
) -> LlmProtocolCapability | None:
|
||||
raw = await self._r.get(self._key(provider_id=provider_id, base_url=base_url))
|
||||
if not raw:
|
||||
return None
|
||||
try:
|
||||
data = json.loads(raw)
|
||||
except json.JSONDecodeError:
|
||||
return None
|
||||
return LlmProtocolCapability(
|
||||
responses=data.get("responses"),
|
||||
store=data.get("store"),
|
||||
)
|
||||
|
||||
async def set(
|
||||
self,
|
||||
*,
|
||||
provider_id: int | None,
|
||||
base_url: str,
|
||||
capability: LlmProtocolCapability,
|
||||
ttl_seconds: int,
|
||||
) -> None:
|
||||
payload = {
|
||||
"responses": capability.responses,
|
||||
"store": capability.store,
|
||||
}
|
||||
await self._r.set(
|
||||
self._key(provider_id=provider_id, base_url=base_url),
|
||||
json.dumps(payload),
|
||||
ex=max(60, ttl_seconds),
|
||||
)
|
||||
|
||||
|
||||
async def close_redis() -> None:
|
||||
global _redis
|
||||
if _redis is not None:
|
||||
|
||||
Reference in New Issue
Block a user