Add logging for OpenAI fallback
All checks were successful
build-and-deploy / build-deploy (push) Successful in 32s
All checks were successful
build-and-deploy / build-deploy (push) Successful in 32s
This commit is contained in:
@@ -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.slf4j.LoggerFactory
|
||||
|
||||
@Component
|
||||
class OpenAIVisionClient(
|
||||
@@ -19,8 +20,13 @@ class OpenAIVisionClient(
|
||||
@Value("\${openai.model:gpt-5-mini}")
|
||||
private val model: String
|
||||
) {
|
||||
private val logger = LoggerFactory.getLogger(OpenAIVisionClient::class.java)
|
||||
|
||||
fun extractAadhaarNumber(imageUrl: String): String? {
|
||||
if (apiKey.isBlank()) return null
|
||||
if (apiKey.isBlank()) {
|
||||
logger.warn("OpenAI fallback skipped: openai.apiKey is blank")
|
||||
return null
|
||||
}
|
||||
|
||||
val payload = mapOf(
|
||||
"model" to model,
|
||||
@@ -45,21 +51,27 @@ class OpenAIVisionClient(
|
||||
headers.contentType = MediaType.APPLICATION_JSON
|
||||
headers.setBearerAuth(apiKey)
|
||||
val entity = HttpEntity(payload, headers)
|
||||
val response = restTemplate.postForEntity(baseUrl, entity, String::class.java)
|
||||
val body = response.body ?: return null
|
||||
val node = objectMapper.readTree(body)
|
||||
return try {
|
||||
logger.info("OpenAI fallback request model={} url={}", model, baseUrl)
|
||||
val response = restTemplate.postForEntity(baseUrl, entity, String::class.java)
|
||||
val body = response.body ?: return null
|
||||
val node = objectMapper.readTree(body)
|
||||
|
||||
val outputText = node.path("output_text").asText()
|
||||
if (outputText.isNotBlank()) return outputText
|
||||
val outputText = node.path("output_text").asText()
|
||||
if (outputText.isNotBlank()) 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
|
||||
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
|
||||
}
|
||||
}
|
||||
null
|
||||
} catch (e: Exception) {
|
||||
logger.warn("OpenAI fallback failed: {}", e.message)
|
||||
null
|
||||
}
|
||||
return null
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user