Repository navigation
Expand file tree
/
Copy pathChat.java
More file actions
75 lines (72 loc) · 3.33 KB
/
Copy pathChat.java
File metadata and controls
75 lines (72 loc) · 3.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
///usr/bin/env jbang "$0" "$@" ; exit $?
// SPDX-FileCopyrightText: 2026 Bernard Ladenthin <bernard.ladenthin@gmail.com>
//
// SPDX-License-Identifier: MIT
//JAVA 8+
//DEPS net.ladenthin:llama:5.2.0
//DEPS net.ladenthin:llama:5.2.0:cpu-linux-x86-64
//DEPS net.ladenthin:llama:5.2.0:cpu-linux-aarch64
//DEPS net.ladenthin:llama:5.2.0:cpu-linux-s390x
//DEPS net.ladenthin:llama:5.2.0:cpu-windows-x86-64
//DEPS net.ladenthin:llama:5.2.0:cpu-windows-aarch64
//DEPS net.ladenthin:llama:5.2.0:metal-macos-aarch64
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import net.ladenthin.llama.LlamaModel;
import net.ladenthin.llama.Session;
import net.ladenthin.llama.parameters.ModelParameters;
import net.ladenthin.llama.value.LlamaOutput;
/**
* A multi-turn chat on the console that JBang runs with no project and no build:
*
* <pre>
* jbang Chat.java path/to/an-instruct-model.gguf
* </pre>
*
* The {@code //DEPS} lines above are the classes jar and the CPU natives jars of every desktop platform
* -- the jars {@code net.ladenthin:llama-platform} names, listed here one by one because JBang treats a
* {@code pom} dependency as a BOM and puts nothing of it on the classpath. The loader picks the jar of
* this machine; the others are downloaded once and ignored. A GPU backend (for example
* {@code net.ladenthin:llama:5.2.0:cuda13-linux-x86-64}) is one more DEPS line and is tried before the CPU.
*
* <p>A {@link Session} keeps the conversation, so every turn passes only the new message, and the model's
* chat template is applied for it; each reply is streamed as it is generated. An empty line or the end
* of the input (Ctrl+D, on Windows Ctrl+Z) ends the chat.
*/
public class Chat {
public static void main(String... args) throws IOException {
if (args.length != 1) {
System.err.println("usage: jbang Chat.java <model.gguf>");
System.exit(2);
}
BufferedReader console = new BufferedReader(new InputStreamReader(System.in, StandardCharsets.UTF_8));
try (LlamaModel model = new LlamaModel(new ModelParameters().setModel(args[0]).setCtxSize(4096));
// Slot 0 holds this conversation; the customizer applies to every turn.
Session session = new Session(model, 0, "You are a concise, helpful assistant.",
p -> p.withNPredict(512).withTemperature(0.7f))) {
while (true) {
System.out.print("\nYou: ");
String message = console.readLine();
if (message == null || message.trim().isEmpty()) {
return;
}
System.out.print("Assistant: ");
StringBuilder reply = new StringBuilder();
try {
for (LlamaOutput output : session.stream(message)) {
System.out.print(output.text);
reply.append(output.text);
}
} catch (RuntimeException e) {
// Without a committed reply the session would refuse every further turn.
session.cancelStream();
throw e;
}
session.commitStreamedReply(reply.toString());
System.out.println();
}
}
}
}