Files
condado-newsletter/backend/src/main/kotlin/com/condado/newsletter/service/LlamaPreviewService.kt
Gabriel Sancho ebcea643c4 feat(backend): persist tasks and generated message history
- add EntityTask domain and CRUD API backed by PostgreSQL

- relate generated messages directly to tasks and delete on task removal

- move preview generation to backend Llama endpoint

- migrate frontend task APIs from localStorage to backend endpoints

- update tests and CLAUDE rules for backend-owned LLM/persistence
2026-03-27 02:46:56 -03:00

48 lines
1.5 KiB
Kotlin

package com.condado.newsletter.service
import com.fasterxml.jackson.annotation.JsonIgnoreProperties
import com.fasterxml.jackson.annotation.JsonProperty
import org.springframework.beans.factory.annotation.Value
import org.springframework.stereotype.Service
import org.springframework.web.client.RestClient
/** Calls a local Llama/Ollama endpoint to generate test task messages. */
@Service
class LlamaPreviewService(
private val restClient: RestClient,
@Value("\${llama.base-url:http://localhost:11434}") private val baseUrl: String,
@Value("\${llama.model:gemma3:4b}") private val model: String
) {
/**
* Generates one message from the provided prompt.
*/
fun generate(prompt: String): String {
val response = restClient.post()
.uri("${baseUrl.trimEnd('/')}/api/generate")
.body(LlamaGenerateRequest(model = model, prompt = prompt, stream = false))
.retrieve()
.body(LlamaGenerateResponse::class.java)
?: throw IllegalStateException("Llama returned an empty response")
val text = response.response?.trim().orEmpty()
if (text.isBlank()) {
throw IllegalStateException("Llama returned an empty message")
}
return text
}
private data class LlamaGenerateRequest(
val model: String,
val prompt: String,
val stream: Boolean
)
@JsonIgnoreProperties(ignoreUnknown = true)
private data class LlamaGenerateResponse(
@JsonProperty("response")
val response: String?
)
}