Add initial project with simple chat using openchat-3.5-0106.Q4_K_M.gguf model

This commit is contained in:
2025-06-27 08:16:41 +02:00
commit 2aad09df7d
13 changed files with 630 additions and 0 deletions

View File

@@ -0,0 +1,52 @@
package com.pablotj.ia.chat.boot;
import de.kherud.llama.InferenceParameters;
import de.kherud.llama.LlamaModel;
import de.kherud.llama.ModelParameters;
import de.kherud.llama.LlamaOutput;
import jakarta.annotation.PostConstruct;
import org.springframework.stereotype.Service;
@Service
public class LlamaService implements AutoCloseable {
private LlamaModel model;
@PostConstruct
public void init() {
try {
ModelParameters params = new ModelParameters()
.setModelFilePath("models/openchat-3.5-0106.Q4_K_M.gguf");
model = new LlamaModel(params);
} catch (Exception e) {
throw new RuntimeException("Error cargando el modelo", e);
}
}
public String chat(String prompt) {
PromptBuilder chat = new PromptBuilder("You are a helpful assistant");
// Historial previo
// chat.user("Pregunta");
// chat.assistant("Respuesta");
chat.user(prompt);
String finalPrompt = chat.build();
InferenceParameters inf = new InferenceParameters(finalPrompt)
.setTemperature(0.7f)
.setTopP(0.9f)
.setTopK(40);
StringBuilder sb = new StringBuilder();
for (LlamaOutput out : model.generate(inf)) {
sb.append(out.text);
}
return sb.toString().replace("<|end_of_turn|>", "").trim();
}
@Override
public void close() {
if (model != null) model.close();
}
}