ai extraction

This commit is contained in:
androidlover5842
2026-01-24 17:27:52 +05:30
parent d13ce8c36f
commit b8c9f8dac4
9 changed files with 498 additions and 0 deletions

View File

@@ -0,0 +1,39 @@
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
@Component
class LlamaClient(
private val restTemplate: RestTemplate,
private val objectMapper: ObjectMapper,
@Value("\${ai.llama.baseUrl}")
private val baseUrl: String
) {
fun ask(imageUrl: String, question: String): String {
val payload = mapOf(
"model" to "qwen",
"messages" to listOf(
mapOf(
"role" to "user",
"content" to listOf(
mapOf("type" to "text", "text" to question),
mapOf("type" to "image_url", "image_url" to mapOf("url" to imageUrl))
)
)
)
)
val headers = HttpHeaders()
headers.contentType = MediaType.APPLICATION_JSON
val entity = HttpEntity(payload, headers)
val response = restTemplate.postForEntity(baseUrl, entity, String::class.java)
val body = response.body ?: return ""
val node = objectMapper.readTree(body)
return node.path("choices").path(0).path("message").path("content").asText()
}
}