forked from kherud/java-llama.cpp
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathJsonParameters.java
More file actions
47 lines (40 loc) · 1.51 KB
/
Copy pathJsonParameters.java
File metadata and controls
47 lines (40 loc) · 1.51 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
package net.ladenthin.llama;
import net.ladenthin.llama.json.ParameterJsonSerializer;
import java.util.HashMap;
import java.util.Map;
/**
* The Java library re-uses most of the llama.cpp server code, which mostly works with JSONs. Thus, the complexity and
* maintainability is much lower if we work with JSONs. This class provides a simple abstraction to easily create
* JSON object strings by filling a <code>Map<String, String></code> with key value pairs.
*/
abstract class JsonParameters {
// We save parameters directly as a String map here, to re-use as much as possible of the (json-based) C++ code.
// The JNI code for a proper Java-typed data object is comparatively too complex and hard to maintain.
final Map<String, String> parameters = new HashMap<>();
/** Serializer for converting Java values to JSON-safe strings. */
protected final ParameterJsonSerializer serializer = new ParameterJsonSerializer();
@Override
public String toString() {
StringBuilder builder = new StringBuilder();
builder.append("{\n");
int i = 0;
for (Map.Entry<String, String> entry : parameters.entrySet()) {
String key = entry.getKey();
String value = entry.getValue();
builder.append("\t\"")
.append(key)
.append("\": ")
.append(value);
if (i++ < parameters.size() - 1) {
builder.append(",");
}
builder.append("\n");
}
builder.append("}");
return builder.toString();
}
String toJsonString(String text) {
if (text == null) return null;
return serializer.toJsonString(text);
}
}