generated from kgod/ai-review-template
feat: improve resume optimization and import reliability
This commit is contained in:
@@ -203,6 +203,8 @@ class OpenAICompatibleStructuredClient:
|
||||
schema_name: str,
|
||||
system_prompt: str,
|
||||
payload: dict[str, Any],
|
||||
timeout_seconds: float | None = None,
|
||||
max_attempts: int | None = None,
|
||||
) -> SchemaT:
|
||||
trace_id = f"ai_{uuid4().hex}"
|
||||
response_format: dict[str, Any]
|
||||
@@ -226,13 +228,22 @@ class OpenAICompatibleStructuredClient:
|
||||
"input": request_payload,
|
||||
"output_json_schema": schema.model_json_schema(),
|
||||
}
|
||||
if max_attempts is not None and max_attempts < 1:
|
||||
raise ValueError("max_attempts must be positive")
|
||||
attempts = max_attempts if max_attempts is not None else self.settings.structured_output_retries + 1
|
||||
request_timeout = timeout_seconds if timeout_seconds is not None else self.settings.openai_timeout_seconds
|
||||
request_client = self.client
|
||||
if timeout_seconds is not None or max_attempts is not None:
|
||||
with_options = getattr(request_client, "with_options", None)
|
||||
if callable(with_options):
|
||||
request_client = with_options(timeout=request_timeout, max_retries=0)
|
||||
failure_summary = "unknown_error"
|
||||
failure_reason = "llm_unknown_error"
|
||||
total_started = time.perf_counter()
|
||||
for attempt in range(1, self.settings.structured_output_retries + 2):
|
||||
for attempt in range(1, attempts + 1):
|
||||
attempt_started = time.perf_counter()
|
||||
try:
|
||||
response = self.client.chat.completions.create(
|
||||
response = request_client.chat.completions.create(
|
||||
model=self.settings.openai_model,
|
||||
messages=[
|
||||
{"role": "system", "content": request_system_prompt},
|
||||
@@ -242,7 +253,7 @@ class OpenAICompatibleStructuredClient:
|
||||
},
|
||||
],
|
||||
response_format=response_format,
|
||||
timeout=self.settings.openai_timeout_seconds,
|
||||
timeout=request_timeout,
|
||||
)
|
||||
if not getattr(response, "choices", None):
|
||||
raise LLMServiceError(
|
||||
@@ -296,7 +307,7 @@ class OpenAICompatibleStructuredClient:
|
||||
trace_id=trace_id,
|
||||
schema=schema_name,
|
||||
model=self.settings.openai_model,
|
||||
attempts=self.settings.structured_output_retries + 1,
|
||||
attempts=attempts,
|
||||
reason_code=failure_reason,
|
||||
duration_ms=round((time.perf_counter() - total_started) * 1000),
|
||||
exception=failure_summary,
|
||||
|
||||
Reference in New Issue
Block a user