|
| 1 | +// SPDX-FileCopyrightText: 2026 Bernard Ladenthin <bernard.ladenthin@gmail.com> |
| 2 | +// |
| 3 | +// SPDX-License-Identifier: MIT |
| 4 | + |
| 5 | +package net.ladenthin.llama; |
| 6 | + |
| 7 | +import java.util.ArrayList; |
| 8 | +import java.util.Collections; |
| 9 | +import java.util.List; |
| 10 | +import java.util.function.Consumer; |
| 11 | + |
| 12 | +/** |
| 13 | + * Thin multi-turn conversation wrapper over a {@link LlamaModel} slot. Maintains an |
| 14 | + * accumulating list of {@link ChatMessage} turns and forwards each {@link #send(String)} |
| 15 | + * to the underlying chat-completion API with the full transcript so far. KV-cache state |
| 16 | + * for the bound slot can be persisted via {@link #save(String)} and restored with |
| 17 | + * {@link #restore(String)}, which delegate to {@link LlamaModel#saveSlot(int, String)} |
| 18 | + * and {@link LlamaModel#restoreSlot(int, String)}. |
| 19 | + * <p> |
| 20 | + * This wrapper is intentionally not thread-safe; callers must serialize access to a |
| 21 | + * single {@code Session} instance. Concurrency support is a follow-up (M-effort) item. |
| 22 | + * </p> |
| 23 | + */ |
| 24 | +public final class Session implements AutoCloseable { |
| 25 | + |
| 26 | + private final LlamaModel model; |
| 27 | + private final int slotId; |
| 28 | + private final String systemMessage; |
| 29 | + private final List<Pair<String, String>> turns = new ArrayList<Pair<String, String>>(); |
| 30 | + private final Consumer<InferenceParameters> paramsCustomizer; |
| 31 | + |
| 32 | + /** |
| 33 | + * Create a session bound to a specific slot id, with an optional system prompt |
| 34 | + * applied to every {@link #send(String)} call. |
| 35 | + * |
| 36 | + * @param model the underlying model |
| 37 | + * @param slotId the slot id used by {@link #save(String)} / {@link #restore(String)} |
| 38 | + * @param systemMessage optional system prompt (may be {@code null} or empty) |
| 39 | + */ |
| 40 | + public Session(LlamaModel model, int slotId, String systemMessage) { |
| 41 | + this(model, slotId, systemMessage, null); |
| 42 | + } |
| 43 | + |
| 44 | + /** |
| 45 | + * Create a session with a customizer that gets to mutate the |
| 46 | + * {@link InferenceParameters} for every call (e.g. set temperature, n_predict). |
| 47 | + * |
| 48 | + * @param model the underlying model |
| 49 | + * @param slotId the slot id |
| 50 | + * @param systemMessage optional system prompt |
| 51 | + * @param paramsCustomizer applied to each request's parameters; may be {@code null} |
| 52 | + */ |
| 53 | + public Session(LlamaModel model, int slotId, String systemMessage, |
| 54 | + Consumer<InferenceParameters> paramsCustomizer) { |
| 55 | + this.model = model; |
| 56 | + this.slotId = slotId; |
| 57 | + this.systemMessage = systemMessage; |
| 58 | + this.paramsCustomizer = paramsCustomizer; |
| 59 | + } |
| 60 | + |
| 61 | + /** Send a user message and return the assistant's text reply, appending both to the transcript. */ |
| 62 | + public String send(String userMessage) { |
| 63 | + turns.add(new Pair<String, String>("user", userMessage)); |
| 64 | + InferenceParameters params = buildParams(); |
| 65 | + String reply = model.chatCompleteText(params); |
| 66 | + turns.add(new Pair<String, String>("assistant", reply)); |
| 67 | + return reply; |
| 68 | + } |
| 69 | + |
| 70 | + /** |
| 71 | + * Streaming variant of {@link #send(String)}. The returned iterable yields chunks of |
| 72 | + * the assistant reply; consume it fully (or via try-with-resources) before calling |
| 73 | + * {@link #send(String)} again, because the assistant turn is only appended to the |
| 74 | + * transcript when the caller invokes {@link #commitStreamedReply(String)}. |
| 75 | + */ |
| 76 | + public LlamaIterable stream(String userMessage) { |
| 77 | + turns.add(new Pair<String, String>("user", userMessage)); |
| 78 | + return model.generateChat(buildParams()); |
| 79 | + } |
| 80 | + |
| 81 | + /** |
| 82 | + * Record an assistant reply that was produced by a previous {@link #stream(String)} |
| 83 | + * call. Called by the caller after it has accumulated the streamed text. |
| 84 | + */ |
| 85 | + public void commitStreamedReply(String assistantText) { |
| 86 | + turns.add(new Pair<String, String>("assistant", assistantText)); |
| 87 | + } |
| 88 | + |
| 89 | + /** Save this session's slot KV cache to {@code filepath}. */ |
| 90 | + public String save(String filepath) { |
| 91 | + return model.saveSlot(slotId, filepath); |
| 92 | + } |
| 93 | + |
| 94 | + /** Restore this session's slot KV cache from {@code filepath}. */ |
| 95 | + public String restore(String filepath) { |
| 96 | + return model.restoreSlot(slotId, filepath); |
| 97 | + } |
| 98 | + |
| 99 | + /** The accumulated turns so far, in order. */ |
| 100 | + public List<ChatMessage> getMessages() { |
| 101 | + List<ChatMessage> out = new ArrayList<ChatMessage>(turns.size() + 1); |
| 102 | + if (systemMessage != null && !systemMessage.isEmpty()) { |
| 103 | + out.add(new ChatMessage("system", systemMessage)); |
| 104 | + } |
| 105 | + for (Pair<String, String> p : turns) { |
| 106 | + out.add(new ChatMessage(p.getKey(), p.getValue())); |
| 107 | + } |
| 108 | + return Collections.unmodifiableList(out); |
| 109 | + } |
| 110 | + |
| 111 | + /** Erase the bound slot's KV cache. Does not modify the in-memory transcript. */ |
| 112 | + @Override |
| 113 | + public void close() { |
| 114 | + model.eraseSlot(slotId); |
| 115 | + } |
| 116 | + |
| 117 | + private InferenceParameters buildParams() { |
| 118 | + InferenceParameters params = new InferenceParameters("") |
| 119 | + .setMessages(systemMessage, new ArrayList<Pair<String, String>>(turns)); |
| 120 | + if (paramsCustomizer != null) { |
| 121 | + paramsCustomizer.accept(params); |
| 122 | + } |
| 123 | + return params; |
| 124 | + } |
| 125 | +} |
0 commit comments