feat: refine metadata catalog workflows
This commit is contained in:
@@ -265,6 +265,36 @@ def _response_content(response: object) -> str:
|
||||
return content
|
||||
|
||||
|
||||
def _response_usage(response: object) -> dict[str, int] | None:
|
||||
"""Normalize LiteLLM usage, tolerating providers that omit it."""
|
||||
try:
|
||||
usage = _field(response, "usage")
|
||||
prompt = _field(usage, "prompt_tokens")
|
||||
output = _field(usage, "completion_tokens")
|
||||
if type(prompt) is not int or prompt < 0 or type(output) is not int or output < 0:
|
||||
return None
|
||||
cached = 0
|
||||
for source, name in ((usage, "cache_read_input_tokens"), (usage, "cached_tokens")):
|
||||
try:
|
||||
value = _field(source, name)
|
||||
except _InvalidResponse:
|
||||
continue
|
||||
if type(value) is int and value >= 0:
|
||||
cached = value
|
||||
break
|
||||
try:
|
||||
details = _field(usage, "prompt_tokens_details")
|
||||
value = _field(details, "cached_tokens")
|
||||
if type(value) is int and value >= 0:
|
||||
cached = value
|
||||
except _InvalidResponse:
|
||||
pass
|
||||
cached = min(cached, prompt)
|
||||
return {"input": prompt - cached, "cacheRead": cached, "output": output}
|
||||
except _InvalidResponse:
|
||||
return None
|
||||
|
||||
|
||||
def handle_request(request: object, *, completion: Completion | None = None) -> Result:
|
||||
"""Validate one request and normalize one injected or LiteLLM completion."""
|
||||
try:
|
||||
@@ -284,7 +314,11 @@ def handle_request(request: object, *, completion: Completion | None = None) ->
|
||||
content = _response_content(response)
|
||||
except Exception: # noqa: BLE001 - arbitrary provider objects are untrusted response data
|
||||
return {"ok": False, "error": "invalid_response"}
|
||||
return {"ok": True, "content": content}
|
||||
result: dict[str, object] = {"ok": True, "content": content}
|
||||
usage = _response_usage(response)
|
||||
if usage is not None:
|
||||
result["usage"] = usage
|
||||
return result
|
||||
|
||||
|
||||
def _write_json(payload: Mapping[str, object]) -> None:
|
||||
|
||||
Reference in New Issue
Block a user