Ollama4j
A Java library (wrapper/binding) for Ollama server.
Loading...
Searching...
No Matches
OllamaStructuredResult.java
Go to the documentation of this file.
1package io.github.ollama4j.models.response;
2
3import static io.github.ollama4j.utils.Utils.getObjectMapper;
4
5import java.util.Map;
6
7import com.fasterxml.jackson.annotation.JsonCreator;
8import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
9import com.fasterxml.jackson.annotation.JsonProperty;
10import com.fasterxml.jackson.core.JsonProcessingException;
11import com.fasterxml.jackson.core.type.TypeReference;
12
13import lombok.Data;
14import lombok.Getter;
15import lombok.NoArgsConstructor;
16
17@Getter
18@SuppressWarnings("unused")
19@Data
20@NoArgsConstructor
21@JsonIgnoreProperties(ignoreUnknown = true)
23 private String response;
24
25 private int httpStatusCode;
26
27 private long responseTime = 0;
28
29 private String model;
30
31 public OllamaStructuredResult(String response, long responseTime, int httpStatusCode) {
32 this.response = response;
33 this.responseTime = responseTime;
34 this.httpStatusCode = httpStatusCode;
35 }
36
37 @Override
38 public String toString() {
39 try {
40 return getObjectMapper().writerWithDefaultPrettyPrinter().writeValueAsString(this);
41 } catch (JsonProcessingException e) {
42 throw new RuntimeException(e);
43 }
44 }
45
51 public Map<String, Object> getStructuredResponse() {
52 try {
53 Map<String, Object> response = getObjectMapper().readValue(this.getResponse(),
54 new TypeReference<Map<String, Object>>() {
55 });
56 return response;
57 } catch (JsonProcessingException e) {
58 throw new RuntimeException(e);
59 }
60 }
61
70 public <T> T getStructuredResponse(Class<T> clazz) {
71 try {
72 return getObjectMapper().readValue(this.getResponse(), clazz);
73 } catch (JsonProcessingException e) {
74 throw new RuntimeException(e);
75 }
76 }
77}
OllamaStructuredResult(String response, long responseTime, int httpStatusCode)