Ollama4j
A Java library (wrapper/binding) for Ollama server.
Loading...
Searching...
No Matches
OllamaResult.java
Go to the documentation of this file.
1package io.github.ollama4j.models.response;
2
3import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
4import com.fasterxml.jackson.core.JsonProcessingException;
5import com.fasterxml.jackson.core.type.TypeReference;
6import lombok.Data;
7import lombok.Getter;
8
9import java.util.HashMap;
10import java.util.List;
11import java.util.Map;
12
13import static io.github.ollama4j.utils.Utils.getObjectMapper;
14
18@Getter
19@SuppressWarnings("unused")
20@Data
21@JsonIgnoreProperties(ignoreUnknown = true)
22public class OllamaResult {
26 private final String response;
30 private final String thinking;
34 private int httpStatusCode;
38 private long responseTime = 0;
39
40 private String model;
41 private String createdAt;
42 private boolean done;
43 private String doneReason;
44 private List<Integer> context;
45 private Long totalDuration;
46 private Long loadDuration;
47 private Integer promptEvalCount;
48 private Long promptEvalDuration;
49 private Integer evalCount;
50 private Long evalDuration;
51
52 public OllamaResult(String response, String thinking, long responseTime, int httpStatusCode) {
53 this.response = response;
54 this.thinking = thinking;
55 this.responseTime = responseTime;
56 this.httpStatusCode = httpStatusCode;
57 }
58
59 @Override
60 public String toString() {
61 try {
62 Map<String, Object> responseMap = new HashMap<>();
63 responseMap.put("response", this.response);
64 responseMap.put("thinking", this.thinking);
65 responseMap.put("httpStatusCode", this.httpStatusCode);
66 responseMap.put("responseTime", this.responseTime);
67 responseMap.put("model", this.model);
68 responseMap.put("createdAt", this.createdAt);
69 responseMap.put("done", this.done);
70 responseMap.put("doneReason", this.doneReason);
71 responseMap.put("context", this.context);
72 responseMap.put("totalDuration", this.totalDuration);
73 responseMap.put("loadDuration", this.loadDuration);
74 responseMap.put("promptEvalCount", this.promptEvalCount);
75 responseMap.put("promptEvalDuration", this.promptEvalDuration);
76 responseMap.put("evalCount", this.evalCount);
77 responseMap.put("evalDuration", this.evalDuration);
78 return getObjectMapper().writerWithDefaultPrettyPrinter().writeValueAsString(responseMap);
79 } catch (JsonProcessingException e) {
80 throw new RuntimeException(e);
81 }
82 }
83
90 public Map<String, Object> getStructuredResponse() {
91 String responseStr = this.getResponse();
92 if (responseStr == null || responseStr.trim().isEmpty()) {
93 throw new IllegalArgumentException("Response is empty or null");
94 }
95
96 try {
97 // Check if the response is a valid JSON
98 if ((!responseStr.trim().startsWith("{") && !responseStr.trim().startsWith("[")) ||
99 (!responseStr.trim().endsWith("}") && !responseStr.trim().endsWith("]"))) {
100 throw new IllegalArgumentException("Response is not a valid JSON object");
101 }
102
103 Map<String, Object> response = getObjectMapper().readValue(responseStr,
104 new TypeReference<Map<String, Object>>() {
105 });
106 return response;
107 } catch (JsonProcessingException e) {
108 throw new IllegalArgumentException("Failed to parse response as JSON: " + e.getMessage(), e);
109 }
110 }
111
121 public <T> T as(Class<T> clazz) {
122 String responseStr = this.getResponse();
123 if (responseStr == null || responseStr.trim().isEmpty()) {
124 throw new IllegalArgumentException("Response is empty or null");
125 }
126
127 try {
128 // Check if the response is a valid JSON
129 if ((!responseStr.trim().startsWith("{") && !responseStr.trim().startsWith("[")) ||
130 (!responseStr.trim().endsWith("}") && !responseStr.trim().endsWith("]"))) {
131 throw new IllegalArgumentException("Response is not a valid JSON object");
132 }
133 return getObjectMapper().readValue(responseStr, clazz);
134 } catch (JsonProcessingException e) {
135 throw new IllegalArgumentException("Failed to parse response as JSON: " + e.getMessage(), e);
136 }
137 }
138}
OllamaResult(String response, String thinking, long responseTime, int httpStatusCode)