Ollama4j
A Java library (wrapper/binding) for Ollama server.
Loading...
Searching...
No Matches
OllamaResult.java
Go to the documentation of this file.
1/*
2 * Ollama4j - Java library for interacting with Ollama server.
3 * Copyright (c) 2025 Amith Koujalgi and contributors.
4 *
5 * Licensed under the MIT License (the "License");
6 * you may not use this file except in compliance with the License.
7 *
8*/
9package io.github.ollama4j.models.response;
10
11import static io.github.ollama4j.utils.Utils.getObjectMapper;
12
13import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
14import com.fasterxml.jackson.core.JsonProcessingException;
15import com.fasterxml.jackson.core.type.TypeReference;
16import java.util.HashMap;
17import java.util.List;
18import java.util.Map;
19import lombok.Data;
20import lombok.Getter;
21import lombok.Setter;
22
26@Getter
27@Setter
28@SuppressWarnings("unused")
29@Data
30@JsonIgnoreProperties(ignoreUnknown = true)
31public class OllamaResult {
35 private final String response;
36
40 private final String thinking;
41
45 private int httpStatusCode;
46
50 private long responseTime = 0;
51
52 private String model;
53 private String createdAt;
54 private boolean done;
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;
63
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;
69 }
70
71 @Override
72 public String toString() {
73 try {
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);
95 }
96 }
97
104 public Map<String, Object> getStructuredResponse() {
105 String responseStr = this.getResponse();
106 if (responseStr == null || responseStr.trim().isEmpty()) {
107 throw new IllegalArgumentException("Response is empty or null");
108 }
109
110 try {
111 // Check if the response is a valid JSON
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");
115 }
116
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);
122 }
123 }
124
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");
138 }
139
140 try {
141 // Check if the response is a valid JSON
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");
145 }
146 return getObjectMapper().readValue(responseStr, clazz);
147 } catch (JsonProcessingException e) {
148 throw new IllegalArgumentException(
149 "Failed to parse response as JSON: " + e.getMessage(), e);
150 }
151 }
152}
OllamaResult(String response, String thinking, long responseTime, int httpStatusCode)