mirror of
https://github.com/fawney19/Aether.git
synced 2026-09-02 01:10:23 +08:00
feat(normalizer): 稳定请求体字段顺序以提升 prompt cache 命中率
在 FormatNormalizer 基类新增 _reorder_request_keys 方法,OpenAI/OpenAI CLI normalizer 各自定义前缀字段顺序(model, tools, messages / model, instructions, tools, input),确保静态字段前置、动态内容后置。同时调整 OpenAI CLI 中 instructions 字段的构建位置使其在 input 之前。
This commit is contained in:
@@ -160,6 +160,21 @@ class FormatNormalizer(ABC):
|
||||
def _extract_extra(self, payload: dict[str, Any], known_keys: set[str]) -> dict[str, Any]:
|
||||
return {k: v for k, v in payload.items() if k not in known_keys}
|
||||
|
||||
@staticmethod
|
||||
def _reorder_request_keys(
|
||||
payload: dict[str, Any],
|
||||
prefix_keys: tuple[str, ...],
|
||||
) -> dict[str, Any]:
|
||||
"""将指定字段前置,其余保持原序,用于稳定请求体前缀以提升 prompt cache 命中率。"""
|
||||
ordered: dict[str, Any] = {}
|
||||
for key in prefix_keys:
|
||||
if key in payload:
|
||||
ordered[key] = payload[key]
|
||||
for key, value in payload.items():
|
||||
if key not in ordered:
|
||||
ordered[key] = value
|
||||
return ordered
|
||||
|
||||
def _merge_dropped(self, target: dict[str, int], source: dict[str, int]) -> None:
|
||||
for k, v in source.items():
|
||||
target[k] = target.get(k, 0) + int(v)
|
||||
|
||||
@@ -453,7 +453,7 @@ class OpenAINormalizer(FormatNormalizer):
|
||||
if key in _CHAT_PASSTHROUGH_KEYS and key not in result and key not in _HANDLED_KEYS:
|
||||
result[key] = value
|
||||
|
||||
return result
|
||||
return self._reorder_request_prefix_keys(result)
|
||||
|
||||
# =========================
|
||||
# Responses
|
||||
@@ -1401,6 +1401,11 @@ class OpenAINormalizer(FormatNormalizer):
|
||||
joined = "\n\n".join(parts)
|
||||
return joined or None
|
||||
|
||||
_REQUEST_PREFIX_KEYS = ("model", "tools", "messages")
|
||||
|
||||
def _reorder_request_prefix_keys(self, payload: dict[str, Any]) -> dict[str, Any]:
|
||||
return self._reorder_request_keys(payload, self._REQUEST_PREFIX_KEYS)
|
||||
|
||||
def _openai_tools_to_internal(self, tools: Any) -> list[ToolDefinition] | None:
|
||||
if not tools or not isinstance(tools, list):
|
||||
return None
|
||||
|
||||
@@ -262,11 +262,6 @@ class OpenAICliNormalizer(FormatNormalizer):
|
||||
isinstance(request_flags, dict) and request_flags.get("has_stream")
|
||||
)
|
||||
|
||||
result: dict[str, Any] = {
|
||||
"model": internal.model,
|
||||
"input": self._internal_messages_to_input(internal.messages, system_to_developer=False),
|
||||
}
|
||||
|
||||
# 合并 instructions,如果没有则使用 system。
|
||||
# 仅在 internal 中确有内容或原请求显式提供过时输出,
|
||||
# 让 Codex 默认 body_rules 仍可在字段缺失时注入默认 instructions。
|
||||
@@ -275,8 +270,14 @@ class OpenAICliNormalizer(FormatNormalizer):
|
||||
if internal.instructions
|
||||
else internal.system
|
||||
)
|
||||
result: dict[str, Any] = {"model": internal.model}
|
||||
if instructions_text or has_explicit_instructions:
|
||||
result["instructions"] = instructions_text or ""
|
||||
# Keep the stable system prefix ahead of the typically dynamic input payload.
|
||||
result["input"] = self._internal_messages_to_input(
|
||||
internal.messages,
|
||||
system_to_developer=False,
|
||||
)
|
||||
|
||||
if internal.max_tokens is not None:
|
||||
# Responses API 使用 max_output_tokens
|
||||
@@ -402,7 +403,7 @@ class OpenAICliNormalizer(FormatNormalizer):
|
||||
if is_codex_variant and "store" not in result:
|
||||
result["store"] = False
|
||||
|
||||
return result
|
||||
return self._reorder_request_prefix_keys(result)
|
||||
|
||||
# =========================
|
||||
# Responses
|
||||
@@ -2128,6 +2129,11 @@ class OpenAICliNormalizer(FormatNormalizer):
|
||||
joined = "\n\n".join(parts)
|
||||
return joined or None
|
||||
|
||||
_REQUEST_PREFIX_KEYS = ("model", "instructions", "tools", "input")
|
||||
|
||||
def _reorder_request_prefix_keys(self, payload: dict[str, Any]) -> dict[str, Any]:
|
||||
return self._reorder_request_keys(payload, self._REQUEST_PREFIX_KEYS)
|
||||
|
||||
def _error_type_from_value(self, value: str) -> ErrorType:
|
||||
for t in ErrorType:
|
||||
if t.value == value:
|
||||
|
||||
Reference in New Issue
Block a user