feat: route codex patch repair through responses api
This commit is contained in:
@@ -239,6 +239,58 @@ def call_openai(model_name, prompt, role):
|
||||
}
|
||||
|
||||
|
||||
def parse_responses_payload(payload):
|
||||
try:
|
||||
text = str(payload.get("output_text") or "").strip()
|
||||
if not text:
|
||||
text = "".join(
|
||||
str(part.get("text") or "")
|
||||
for item in payload.get("output", []) if isinstance(item, dict)
|
||||
for part in item.get("content", []) if isinstance(part, dict) and part.get("type") == "output_text"
|
||||
).strip()
|
||||
usage = payload["usage"]
|
||||
input_tokens = int(usage["input_tokens"])
|
||||
output_tokens = int(usage["output_tokens"])
|
||||
if not text:
|
||||
raise ValueError("output text missing")
|
||||
except (KeyError, TypeError, ValueError) as exc:
|
||||
fail(f"provider_usage_invalid openai-responses {type(exc).__name__}: {str(exc)[:80]}")
|
||||
return text, input_tokens, output_tokens
|
||||
|
||||
|
||||
def call_openai_responses(model_name, prompt, role):
|
||||
"""Use Responses API for Codex models that are not Chat Completions routes."""
|
||||
host = "api.openai.com"
|
||||
if host not in ALLOWED_CLOUD:
|
||||
fail(f"endpoint_not_allowed openai host={host}")
|
||||
key = os.environ["OPENAI_API_KEY"]
|
||||
body = {
|
||||
"model": model_name,
|
||||
"input": prompt,
|
||||
"max_output_tokens": generation_max_tokens(role),
|
||||
"store": False,
|
||||
}
|
||||
data = json.dumps(body).encode()
|
||||
req = urllib.request.Request(
|
||||
f"https://{host}/v1/responses", data=data,
|
||||
headers={"Content-Type": "application/json", "Authorization": f"Bearer {key}"},
|
||||
)
|
||||
t0 = time.time()
|
||||
try:
|
||||
with urllib.request.urlopen(req, timeout=REQUEST_TIMEOUT) as resp:
|
||||
payload = json.loads(resp.read().decode())
|
||||
except Exception as exc:
|
||||
fail(f"backend_unreachable {type(exc).__name__}: {str(exc)[:120]}")
|
||||
latency_ms = int((time.time() - t0) * 1000)
|
||||
text, input_tokens, output_tokens = parse_responses_payload(payload)
|
||||
return {
|
||||
"text": text,
|
||||
"input_tokens": input_tokens,
|
||||
"output_tokens": output_tokens,
|
||||
"latency_ms": latency_ms,
|
||||
}
|
||||
|
||||
|
||||
def openai_compatible_url():
|
||||
"""Return a vetted OpenAI-compatible chat-completions endpoint.
|
||||
|
||||
@@ -412,7 +464,7 @@ def main():
|
||||
if backend == "ollama":
|
||||
result = call_ollama(model_name, prompt, args.role)
|
||||
elif backend == "openai":
|
||||
result = call_openai(model_name, prompt, args.role)
|
||||
result = call_openai_responses(model_name, prompt, args.role) if model_name.endswith("-codex") else call_openai(model_name, prompt, args.role)
|
||||
elif backend == "openai-compatible":
|
||||
result = call_openai_compatible(model_name, prompt, args.role)
|
||||
else: # anthropic
|
||||
|
||||
Reference in New Issue
Block a user