Add initial project with simple chat using openchat-3.5-0106.Q4_K_M.gguf model
This commit is contained in:
21
src/main/java/com/pablotj/ia/chat/boot/ChatController.java
Normal file
21
src/main/java/com/pablotj/ia/chat/boot/ChatController.java
Normal file
@@ -0,0 +1,21 @@
|
||||
package com.pablotj.ia.chat.boot;
|
||||
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/chat")
|
||||
public class ChatController {
|
||||
private final LlamaService llamaService;
|
||||
|
||||
public ChatController(LlamaService llamaService) {
|
||||
this.llamaService = llamaService;
|
||||
}
|
||||
|
||||
@GetMapping
|
||||
public String chat(@RequestParam String prompt) {
|
||||
return llamaService.chat(prompt);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
package com.pablotj.ia.chat.boot;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
@SpringBootApplication
|
||||
public class IAChatBootApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(IAChatBootApplication.class, args);
|
||||
}
|
||||
|
||||
}
|
||||
52
src/main/java/com/pablotj/ia/chat/boot/LlamaService.java
Normal file
52
src/main/java/com/pablotj/ia/chat/boot/LlamaService.java
Normal 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();
|
||||
}
|
||||
}
|
||||
33
src/main/java/com/pablotj/ia/chat/boot/PromptBuilder.java
Normal file
33
src/main/java/com/pablotj/ia/chat/boot/PromptBuilder.java
Normal file
@@ -0,0 +1,33 @@
|
||||
package com.pablotj.ia.chat.boot;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class PromptBuilder {
|
||||
|
||||
private final String systemPrompt;
|
||||
private final List<String> turns = new ArrayList<>();
|
||||
|
||||
public PromptBuilder(String systemPrompt) {
|
||||
this.systemPrompt = systemPrompt;
|
||||
}
|
||||
|
||||
public void user(String message) {
|
||||
turns.add("GPT4 Correct User: " + message + "<|end_of_turn|>");
|
||||
}
|
||||
|
||||
public void assistant(String message) {
|
||||
turns.add("GPT4 Correct Assistant: " + message + "<|end_of_turn|>");
|
||||
}
|
||||
|
||||
public String build() {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sb.append(systemPrompt).append("<|end_of_turn|>\n");
|
||||
for (String turn : turns) {
|
||||
sb.append(turn).append("\n");
|
||||
}
|
||||
// Deja el último turno preparado para que el modelo continúe como "Assistant"
|
||||
sb.append("GPT4 Correct Assistant: ");
|
||||
return sb.toString();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user