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;
6
7import lombok.Data;
8import lombok.Getter;
9
10import static io.github.ollama4j.utils.Utils.getObjectMapper;
11
12import java.util.HashMap;
13import java.util.Map;
14
16@Getter
17@SuppressWarnings("unused")
18@Data
19@JsonIgnoreProperties(ignoreUnknown = true)
20public class OllamaResult {
27 private final String response;
28
35 private int httpStatusCode;
36
43 private long responseTime = 0;
44
45 public OllamaResult(String response, long responseTime, int httpStatusCode) {
46 this.response = response;
47 this.responseTime = responseTime;
48 this.httpStatusCode = httpStatusCode;
49 }
50
51 @Override
52 public String toString() {
53 try {
54 Map<String, Object> responseMap = new HashMap<>();
55 responseMap.put("response", this.response);
56 responseMap.put("httpStatusCode", this.httpStatusCode);
57 responseMap.put("responseTime", this.responseTime);
58 return getObjectMapper().writerWithDefaultPrettyPrinter().writeValueAsString(responseMap);
59 } catch (JsonProcessingException e) {
60 throw new RuntimeException(e);
61 }
62 }
63
70 public Map<String, Object> getStructuredResponse() {
71 String responseStr = this.getResponse();
72 if (responseStr == null || responseStr.trim().isEmpty()) {
73 throw new IllegalArgumentException("Response is empty or null");
74 }
75
76 try {
77 // Check if the response is a valid JSON
78 if ((!responseStr.trim().startsWith("{") && !responseStr.trim().startsWith("[")) ||
79 (!responseStr.trim().endsWith("}") && !responseStr.trim().endsWith("]"))) {
80 throw new IllegalArgumentException("Response is not a valid JSON object");
81 }
82
83 Map<String, Object> response = getObjectMapper().readValue(responseStr,
84 new TypeReference<Map<String, Object>>() {
85 });
86 return response;
87 } catch (JsonProcessingException e) {
88 throw new IllegalArgumentException("Failed to parse response as JSON: " + e.getMessage(), e);
89 }
90 }
91
101 public <T> T as(Class<T> clazz) {
102 String responseStr = this.getResponse();
103 if (responseStr == null || responseStr.trim().isEmpty()) {
104 throw new IllegalArgumentException("Response is empty or null");
105 }
106
107 try {
108 // Check if the response is a valid JSON
109 if ((!responseStr.trim().startsWith("{") && !responseStr.trim().startsWith("[")) ||
110 (!responseStr.trim().endsWith("}") && !responseStr.trim().endsWith("]"))) {
111 throw new IllegalArgumentException("Response is not a valid JSON object");
112 }
113 return getObjectMapper().readValue(responseStr, clazz);
114 } catch (JsonProcessingException e) {
115 throw new IllegalArgumentException("Failed to parse response as JSON: " + e.getMessage(), e);
116 }
117 }
118}
OllamaResult(String response, long responseTime, int httpStatusCode)