Source code for nlp_shap.backends.api.payload
"""Chat-completions request and response helpers."""
import hashlib
import json
from typing import Any
from ...errors import BackendUnavailableError
[docs]
def build_chat_payload(
model_id: str,
messages: list[dict[str, str]],
max_new_tokens: int,
temperature: float,
top_k: int,
) -> dict[str, Any]:
"""Build an OpenAI-style chat-completions JSON body."""
payload: dict[str, Any] = {
"model": model_id,
"messages": messages,
"max_tokens": max_new_tokens,
}
if temperature > 0.0:
payload["temperature"] = float(temperature)
if top_k > 0:
payload["top_p"] = 0.95
else:
payload["temperature"] = 0.0
return payload
[docs]
def payload_cache_key(payload: dict[str, Any]) -> str:
"""Return a stable SHA256 key for deterministic request deduplication."""
canonical = json.dumps(payload, sort_keys=True, separators=(",", ":"))
return hashlib.sha256(canonical.encode("utf-8")).hexdigest()