35 private final String response;
40 private final String thinking;
45 private int httpStatusCode;
50 private long responseTime = 0;
53 private String createdAt;
55 private String doneReason;
56 private List<Integer> context;
57 private Long totalDuration;
58 private Long loadDuration;
59 private Integer promptEvalCount;
60 private Long promptEvalDuration;
61 private Integer evalCount;
62 private Long evalDuration;
64 public OllamaResult(String response, String thinking,
long responseTime,
int httpStatusCode) {
65 this.response = response;
66 this.thinking = thinking;
67 this.responseTime = responseTime;
68 this.httpStatusCode = httpStatusCode;
74 Map<String, Object> responseMap =
new HashMap<>();
75 responseMap.put(
"response", this.response);
76 responseMap.put(
"thinking", this.thinking);
77 responseMap.put(
"httpStatusCode", this.httpStatusCode);
78 responseMap.put(
"responseTime", this.responseTime);
79 responseMap.put(
"model", this.model);
80 responseMap.put(
"createdAt", this.createdAt);
81 responseMap.put(
"done", this.done);
82 responseMap.put(
"doneReason", this.doneReason);
83 responseMap.put(
"context", this.context);
84 responseMap.put(
"totalDuration", this.totalDuration);
85 responseMap.put(
"loadDuration", this.loadDuration);
86 responseMap.put(
"promptEvalCount", this.promptEvalCount);
87 responseMap.put(
"promptEvalDuration", this.promptEvalDuration);
88 responseMap.put(
"evalCount", this.evalCount);
89 responseMap.put(
"evalDuration", this.evalDuration);
90 return getObjectMapper()
91 .writerWithDefaultPrettyPrinter()
92 .writeValueAsString(responseMap);
93 }
catch (JsonProcessingException e) {
94 throw new RuntimeException(e);
105 String responseStr = this.getResponse();
106 if (responseStr ==
null || responseStr.trim().isEmpty()) {
107 throw new IllegalArgumentException(
"Response is empty or null");
112 if ((!responseStr.trim().startsWith(
"{") && !responseStr.trim().startsWith(
"["))
113 || (!responseStr.trim().endsWith(
"}") && !responseStr.trim().endsWith(
"]"))) {
114 throw new IllegalArgumentException(
"Response is not a valid JSON object");
117 return getObjectMapper()
118 .readValue(responseStr,
new TypeReference<Map<String, Object>>() {});
119 }
catch (JsonProcessingException e) {
120 throw new IllegalArgumentException(
121 "Failed to parse response as JSON: " + e.getMessage(), e);
134 public <T> T as(Class<T> clazz) {
135 String responseStr = this.getResponse();
136 if (responseStr ==
null || responseStr.trim().isEmpty()) {
137 throw new IllegalArgumentException(
"Response is empty or null");
142 if ((!responseStr.trim().startsWith(
"{") && !responseStr.trim().startsWith(
"["))
143 || (!responseStr.trim().endsWith(
"}") && !responseStr.trim().endsWith(
"]"))) {
144 throw new IllegalArgumentException(
"Response is not a valid JSON object");
146 return getObjectMapper().readValue(responseStr, clazz);
147 }
catch (JsonProcessingException e) {
148 throw new IllegalArgumentException(
149 "Failed to parse response as JSON: " + e.getMessage(), e);