尝试修复/v1/responses后端没有命中缓存的情况
This commit is contained in:
parent
cb7350b100
commit
251437a760
3 changed files with 22 additions and 0 deletions
|
|
@ -7,6 +7,7 @@ SSE 消息拼装逻辑,避免 `chat.py` 和 `responses.py` 各自维护重复
|
|||
from __future__ import annotations
|
||||
|
||||
from dataclasses import dataclass
|
||||
import hashlib
|
||||
import json
|
||||
import logging
|
||||
from typing import Any
|
||||
|
|
@ -218,6 +219,23 @@ def inject_instructions_anthropic(payload: dict[str, Any], instructions: str, po
|
|||
# ─── Body / Header 修改 ──────────────────────────
|
||||
|
||||
|
||||
def ensure_prompt_cache_key(payload: dict[str, Any]) -> dict[str, Any]:
|
||||
"""确保 Responses 请求携带 prompt_cache_key 以启用上游提示缓存。
|
||||
|
||||
上游(如 sub2api)对原生 /v1/responses 请求不会自动生成 prompt_cache_key,
|
||||
导致提示缓存无法命中。这里根据模型名 + instructions 生成稳定的 cache key,
|
||||
使得相同模型和系统提示的对话可以共享缓存前缀。
|
||||
"""
|
||||
if payload.get('prompt_cache_key'):
|
||||
return payload
|
||||
|
||||
model = payload.get('model', '')
|
||||
instructions = payload.get('instructions', '')
|
||||
seed = f'{model}|{instructions}'
|
||||
payload['prompt_cache_key'] = hashlib.sha256(seed.encode()).hexdigest()[:32]
|
||||
return payload
|
||||
|
||||
|
||||
def apply_body_modifications(payload: dict[str, Any], modifications: dict[str, Any]) -> dict[str, Any]:
|
||||
"""对转发请求体应用字段级修改。
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue