mirror of
https://gitee.com/qqtt-ah/ecom-dmb.git
synced 2026-11-24 03:19:18 +08:00
feat: 贯通 context_window 配置并按 token 预算裁剪对话历史
This commit is contained in:
@@ -29,6 +29,7 @@ class RedisLlmRuntimeCache(LlmRuntimeCache):
|
||||
api_key=data["api_key"],
|
||||
model=data["model"],
|
||||
extra_headers=data.get("extra_headers") or {},
|
||||
context_window=_optional_int(data.get("context_window")),
|
||||
)
|
||||
|
||||
async def set(self, company_id: int, config: LlmRuntimeConfig, ttl_seconds: int) -> None:
|
||||
@@ -37,6 +38,7 @@ class RedisLlmRuntimeCache(LlmRuntimeCache):
|
||||
"api_key": config.api_key,
|
||||
"model": config.model,
|
||||
"extra_headers": config.extra_headers,
|
||||
"context_window": config.context_window,
|
||||
}
|
||||
await self._r.set(self._key(company_id), json.dumps(payload), ex=ttl_seconds)
|
||||
|
||||
@@ -47,6 +49,16 @@ class RedisLlmRuntimeCache(LlmRuntimeCache):
|
||||
_redis: redis.Redis | None = None
|
||||
|
||||
|
||||
def _optional_int(v) -> int | None:
|
||||
if v is None:
|
||||
return None
|
||||
try:
|
||||
n = int(v)
|
||||
except (TypeError, ValueError):
|
||||
return None
|
||||
return n if n > 0 else None
|
||||
|
||||
|
||||
def get_redis(settings: Settings) -> redis.Redis:
|
||||
global _redis
|
||||
if _redis is None:
|
||||
|
||||
Reference in New Issue
Block a user