99 lines
3.9 KiB
Kotlin
99 lines
3.9 KiB
Kotlin
package com.android.trisolarisserver.component
|
|
|
|
import com.fasterxml.jackson.databind.ObjectMapper
|
|
import org.springframework.beans.factory.annotation.Value
|
|
import org.springframework.http.HttpEntity
|
|
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
|
|
class OpenAIVisionClient(
|
|
private val restTemplate: RestTemplate,
|
|
private val objectMapper: ObjectMapper,
|
|
@Value("\${openai.apiKey:}")
|
|
private val apiKey: String,
|
|
@Value("\${openai.baseUrl:https://api.openai.com/v1/responses}")
|
|
private val baseUrl: String,
|
|
@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()) {
|
|
logger.warn("OpenAI fallback skipped: openai.apiKey is blank")
|
|
return null
|
|
}
|
|
|
|
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 16,
|
|
"input" to listOf(
|
|
mapOf(
|
|
"role" to "user",
|
|
"content" to listOf(
|
|
mapOf(
|
|
"type" to "input_text",
|
|
"text" to "Read extremely carefully. Aadhaar number = 12 digits. Reply ONLY the 12 digits or NONE."
|
|
),
|
|
mapOf(
|
|
"type" to "input_image",
|
|
"image_url" to imageUrl,
|
|
"detail" to "high"
|
|
)
|
|
)
|
|
)
|
|
)
|
|
)
|
|
|
|
val headers = HttpHeaders()
|
|
headers.contentType = MediaType.APPLICATION_JSON
|
|
headers.setBearerAuth(apiKey)
|
|
val entity = HttpEntity(payload, headers)
|
|
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()) {
|
|
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()) {
|
|
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)
|
|
null
|
|
}
|
|
}
|
|
}
|