forked from AdamBien/lightmetal
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlmprompt
More file actions
executable file
·79 lines (71 loc) · 2.9 KB
/
Copy pathlmprompt
File metadata and controls
executable file
·79 lines (71 loc) · 2.9 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
76
77
78
79
#!/usr/bin/env -S java --enable-native-access=ALL-UNNAMED --source 25 -cp ./zbo/lightmetal.jar:./lightmetal.jar
import lm.catalog.boundary.ModelCatalog;
import lm.configuration.control.ZCfg;
import lm.configuration.entity.GenerationConfig;
import lm.generation.boundary.LightMetal;
import lm.generation.entity.Tps;
String name = MethodHandles.lookup().lookupClass().getName();
String version = "2026-06-10.1";
String DIM = "\033[2m";
String RESET = "\033[0m";
void main(String... args) throws Exception {
IO.println(name + " " + version);
ZCfg.load("lightmetal");
var fileName = args.length > 0 ? matchModel(args[0]) : ZCfg.string("model");
if (fileName == null) {
System.err.println(name + ": no model configured (set model=<file> in ~/.lightmetal/app.properties)");
System.exit(1);
}
var modelPath = ModelCatalog.resolve(fileName);
if (!Files.exists(modelPath)) {
System.err.println(name + ": model not found at " + modelPath);
System.exit(1);
}
var label = fileName.replace(".gguf", "") + "> ";
var prompt = System.console().readLine(label);
if (prompt == null || prompt.isBlank()) {
System.err.println(name + ": empty prompt");
System.exit(1);
}
var count = new AtomicLong();
var thoughtCount = new AtomicLong();
var firstTokenNanos = new AtomicLong();
try (var lm = LightMetal.load(modelPath)) {
var modelName = lm.metadata().name().orElse(modelPath.getFileName().toString());
try (var stream = lm.generate(prompt, GenerationConfig.fromProperties())) {
stream.forEach(t -> {
firstTokenNanos.compareAndSet(0L, System.nanoTime());
count.incrementAndGet();
if ("thought".equals(t.channel())) {
thoughtCount.incrementAndGet();
System.err.print(DIM + t.text() + RESET);
System.err.flush();
return;
}
IO.print(t.text());
System.out.flush();
});
}
IO.println("");
if (count.get() < 2) {
System.err.println(name + ": got " + count.get() + " tokens, need at least 2 for tps");
System.exit(1);
}
var thought = thoughtCount.get() > 0 ? ", " + thoughtCount.get() + " thought" : "";
IO.println("[" + modelName + ": " + Tps.measure(count.get(), firstTokenNanos.get()) + thought + "]");
}
}
String matchModel(String fragment) {
var matches = ModelCatalog.search(fragment);
if (matches.size() == 1) {
return matches.getFirst();
}
if (matches.isEmpty()) {
System.err.println(name + ": no model matches \"" + fragment + "\"");
} else {
System.err.println(name + ": " + matches.size() + " models match \"" + fragment + "\":");
matches.forEach(m -> System.err.println(" " + m));
}
System.exit(1);
return null;
}