fix: address phase 1 ai review

This commit is contained in:
QiuSW
2026-07-02 14:47:22 +08:00
parent 2d9c233683
commit 2f93374193
18 changed files with 499 additions and 56 deletions
+45 -2
View File
@@ -28,6 +28,35 @@ from .utils import (
)
SAFE_PARAMETER_KEYS = frozenset(
{
"temperature",
"top_p",
"topP",
"top_k",
"topK",
"max_tokens",
"max_output_tokens",
"maxOutputTokens",
"presence_penalty",
"presencePenalty",
"frequency_penalty",
"frequencyPenalty",
"seed",
"stop",
}
)
SAFE_GENERATION_CONFIG_KEYS = frozenset(
{
"temperature",
"topP",
"topK",
"maxOutputTokens",
"stopSequences",
}
)
class BaseHttpProvider:
def __init__(self, session: requests.Session | None = None):
self.session = session or requests.Session()
@@ -371,6 +400,20 @@ def apply_extra_body(
model: ResolvedModel,
parameters: Mapping[str, Any] | None = None,
) -> None:
payload.update(model.extra_body)
apply_safe_parameters(payload, model.extra_body)
if parameters:
payload.update(dict(parameters))
apply_safe_parameters(payload, parameters)
def apply_safe_parameters(payload: dict[str, Any], values: Mapping[str, Any]) -> None:
for key, value in values.items():
if key == "generationConfig" and isinstance(value, Mapping):
generation_config = payload.setdefault("generationConfig", {})
if not isinstance(generation_config, dict):
continue
for config_key, config_value in value.items():
if config_key in SAFE_GENERATION_CONFIG_KEYS:
generation_config[config_key] = config_value
continue
if key in SAFE_PARAMETER_KEYS:
payload[key] = value
+3 -2
View File
@@ -110,14 +110,15 @@ def decode_image_data_url(data_url: str) -> bytes:
def resolution_to_size(resolution: str) -> str:
key = str(resolution).strip().upper()
mapping = {
"512": "512x512",
"512px": "512x512",
"512PX": "512x512",
"1K": "1024x1024",
"2K": "2048x2048",
"4K": "4096x4096",
}
return mapping.get(str(resolution), str(resolution))
return mapping.get(key, str(resolution).strip())
def extract_image_from_response(