Improve OpenAI fallback diagnostics
All checks were successful
build-and-deploy / build-deploy (push) Successful in 32s

This commit is contained in:
androidlover5842
2026-01-31 05:21:11 +05:30
parent cf82446641
commit f743d50d7f

View File

@@ -7,6 +7,7 @@ import org.springframework.http.HttpHeaders
import org.springframework.http.MediaType
import org.springframework.stereotype.Component
import org.springframework.web.client.RestTemplate
import org.springframework.web.client.HttpStatusCodeException
import org.slf4j.LoggerFactory
@Component
@@ -30,6 +31,10 @@ class OpenAIVisionClient(
val payload = mapOf(
"model" to model,
"instructions" to "Read extremely carefully. Reply ONLY the 12 digits or NONE. No extra text.",
"temperature" to 0.0,
"top_p" to 1.0,
"max_output_tokens" to 12,
"input" to listOf(
mapOf(
"role" to "user",
@@ -40,7 +45,10 @@ class OpenAIVisionClient(
),
mapOf(
"type" to "input_image",
"image_url" to imageUrl
"image_url" to mapOf(
"url" to imageUrl,
"detail" to "high"
)
)
)
)
@@ -58,16 +66,31 @@ class OpenAIVisionClient(
val node = objectMapper.readTree(body)
val outputText = node.path("output_text").asText()
if (outputText.isNotBlank()) return outputText
if (outputText.isNotBlank()) {
logger.info("OpenAI fallback output_text length={}", outputText.trim().length)
return outputText
}
val outputArray = node.path("output")
if (outputArray.isArray && outputArray.size() > 0) {
val content = outputArray[0].path("content")
if (content.isArray && content.size() > 0) {
val text = content[0].path("text").asText()
if (text.isNotBlank()) return text
if (text.isNotBlank()) {
logger.info("OpenAI fallback content text length={}", text.trim().length)
return text
}
}
}
val errorNode = node.path("error")
if (!errorNode.isMissingNode) {
val code = errorNode.path("code").asText()
val type = errorNode.path("type").asText()
logger.warn("OpenAI fallback error code={} type={}", code, type)
}
null
} catch (e: HttpStatusCodeException) {
logger.warn("OpenAI fallback HTTP error status={} body={}", e.statusCode.value(), e.responseBodyAsString.take(200))
null
} catch (e: Exception) {
logger.warn("OpenAI fallback failed: {}", e.message)