add a debug info
This commit is contained in:
@@ -1,10 +1,14 @@
|
|||||||
"""AI-backed sentence explanations for the mobile clients."""
|
"""AI-backed sentence explanations for the mobile clients."""
|
||||||
|
|
||||||
from dataclasses import dataclass
|
from dataclasses import dataclass
|
||||||
|
import logging
|
||||||
|
|
||||||
import requests
|
import requests
|
||||||
|
|
||||||
|
|
||||||
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
class AITutorError(RuntimeError):
|
class AITutorError(RuntimeError):
|
||||||
"""Raised when the configured AI service cannot answer a request."""
|
"""Raised when the configured AI service cannot answer a request."""
|
||||||
|
|
||||||
@@ -61,15 +65,37 @@ class AITutor:
|
|||||||
timeout=self.timeout_seconds,
|
timeout=self.timeout_seconds,
|
||||||
)
|
)
|
||||||
except requests.RequestException as exc:
|
except requests.RequestException as exc:
|
||||||
|
logger.exception(
|
||||||
|
"AI explanation request failed: endpoint=%s model=%s",
|
||||||
|
self.endpoint,
|
||||||
|
self.model,
|
||||||
|
)
|
||||||
raise AITutorError("AI service request failed.") from exc
|
raise AITutorError("AI service request failed.") from exc
|
||||||
|
|
||||||
if response.status_code < 200 or response.status_code >= 300:
|
if response.status_code < 200 or response.status_code >= 300:
|
||||||
|
logger.warning(
|
||||||
|
"AI explanation returned HTTP %s: endpoint=%s model=%s body=%s",
|
||||||
|
response.status_code,
|
||||||
|
self.endpoint,
|
||||||
|
self.model,
|
||||||
|
response.text[:1000],
|
||||||
|
)
|
||||||
raise AITutorError(f"AI service returned HTTP {response.status_code}.")
|
raise AITutorError(f"AI service returned HTTP {response.status_code}.")
|
||||||
try:
|
try:
|
||||||
body = response.json()
|
body = response.json()
|
||||||
explanation = body["choices"][0]["message"]["content"]
|
explanation = body["choices"][0]["message"]["content"]
|
||||||
except (KeyError, IndexError, TypeError, ValueError) as exc:
|
except (KeyError, IndexError, TypeError, ValueError) as exc:
|
||||||
|
logger.warning(
|
||||||
|
"AI explanation returned an unexpected body: endpoint=%s body=%s",
|
||||||
|
self.endpoint,
|
||||||
|
response.text[:1000],
|
||||||
|
)
|
||||||
raise AITutorError("AI service returned an unexpected response.") from exc
|
raise AITutorError("AI service returned an unexpected response.") from exc
|
||||||
if not isinstance(explanation, str) or not explanation.strip():
|
if not isinstance(explanation, str) or not explanation.strip():
|
||||||
|
logger.warning(
|
||||||
|
"AI explanation returned empty content: endpoint=%s body=%s",
|
||||||
|
self.endpoint,
|
||||||
|
response.text[:1000],
|
||||||
|
)
|
||||||
raise AITutorError("AI service returned an empty explanation.")
|
raise AITutorError("AI service returned an empty explanation.")
|
||||||
return explanation.strip()
|
return explanation.strip()
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ from sentence_api.ai_tutor import AITutor, AITutorError
|
|||||||
class FakeResponse:
|
class FakeResponse:
|
||||||
def __init__(self, status_code=200, body=None):
|
def __init__(self, status_code=200, body=None):
|
||||||
self.status_code = status_code
|
self.status_code = status_code
|
||||||
|
self.text = str(body)
|
||||||
self._body = body or {}
|
self._body = body or {}
|
||||||
|
|
||||||
def json(self):
|
def json(self):
|
||||||
|
|||||||
Reference in New Issue
Block a user