Merge pull request #30 from gabrii/29-request-failed
Fix default mechanism for azure settings, and improve reporting of azure errors
This commit is contained in:
@@ -2,11 +2,14 @@
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
import re
|
||||
from typing import Optional
|
||||
|
||||
import requests
|
||||
from flask import Request, Response
|
||||
|
||||
from ..common.logging import console
|
||||
from ..common.recording import record_payload
|
||||
|
||||
# Local adapters
|
||||
@@ -53,5 +56,47 @@ class AzureAdapter:
|
||||
|
||||
# Perform upstream request with kwargs directly (no long-lived session)
|
||||
resp = requests.request(**request_kwargs)
|
||||
if resp.status_code != 200:
|
||||
return self._handle_azure_error(resp, request_kwargs)
|
||||
|
||||
return self.response_adapter.adapt(resp)
|
||||
|
||||
def _handle_azure_error(self, resp: Response, request_kwargs) -> Response:
|
||||
|
||||
try:
|
||||
resp_content = resp.json()
|
||||
except ValueError:
|
||||
resp_content = resp.content
|
||||
|
||||
body = request_kwargs.get("json", {})
|
||||
if "instructions" in body:
|
||||
body["instructions"] = body["instructions"][:7] + "..."
|
||||
|
||||
if "tools" in body:
|
||||
body["tools"] = f"...redacted {len(body['tools'])} tools..."
|
||||
|
||||
if "input" in body:
|
||||
body["input"] = f"...redacted {len(body['input'])} input items..."
|
||||
|
||||
if "prompt_cache_key" in body:
|
||||
body["prompt_cache_key"] = re.sub(
|
||||
r"(...)(.*)(...)", "\\1***\\3", body["prompt_cache_key"]
|
||||
)
|
||||
report = {
|
||||
"endpoint": re.sub(
|
||||
r"(//.)(.*?)(.\.)", "\\1***\\3", request_kwargs.get("url")
|
||||
),
|
||||
"azure_response": resp_content,
|
||||
"request_body": body,
|
||||
}
|
||||
error_message = f"""\nCheck "azure_response" for the error details:
|
||||
\t{json.dumps(report, indent=4).replace("\n", "\n\t")}
|
||||
If the issue persists, report it to:
|
||||
\thttps://github.com/gabrii/Cursor-Azure-GPT-5/issues
|
||||
Including all the details above"""
|
||||
console.rule(f"[red]Request failed with status code {resp.status_code}[/red]")
|
||||
console.print(error_message)
|
||||
return Response(
|
||||
error_message,
|
||||
status=resp.status_code,
|
||||
)
|
||||
|
||||
+4
-4
@@ -19,11 +19,11 @@ SERVICE_API_KEY = env.str("SERVICE_API_KEY", "change-me")
|
||||
|
||||
AZURE_BASE_URL = env.str("AZURE_BASE_URL", "change_me").rstrip("/")
|
||||
AZURE_API_KEY = env.str("AZURE_API_KEY", "change_me")
|
||||
AZURE_DEPLOYMENT = env.str("AZURE_DEPLOYMENT", "gpt-5")
|
||||
AZURE_DEPLOYMENT = env.str("AZURE_DEPLOYMENT") or "gpt-5"
|
||||
|
||||
AZURE_API_VERSION = env.str("AZURE_API_VERSION", "2025-04-01-preview")
|
||||
AZURE_SUMMARY_LEVEL = env.str("AZURE_SUMMARY_LEVEL", "detailed")
|
||||
AZURE_TRUNCATION = env.str("AZURE_TRUNCATION", "auto")
|
||||
AZURE_API_VERSION = env.str("AZURE_API_VERSION") or "2025-04-01-preview"
|
||||
AZURE_SUMMARY_LEVEL = env.str("AZURE_SUMMARY_LEVEL") or "detailed"
|
||||
AZURE_TRUNCATION = env.str("AZURE_TRUNCATION") or "auto"
|
||||
|
||||
AZURE_RESPONSES_API_URL = (
|
||||
f"{AZURE_BASE_URL}/openai/responses?api-version={AZURE_API_VERSION}"
|
||||
|
||||
Reference in New Issue
Block a user