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 com.fasterxml.jackson.annotation.JsonIgnoreProperties;
4import com.fasterxml.jackson.annotation.JsonProperty;
5import com.fasterxml.jackson.core.JsonProcessingException;
6import com.fasterxml.jackson.core.type.TypeReference;
7import lombok.Data;
8import lombok.Getter;
9import lombok.NoArgsConstructor;
10
11import java.util.List;
12import java.util.Map;
13
14import static io.github.ollama4j.utils.Utils.getObjectMapper;
15
16@Getter
17@SuppressWarnings("unused")
18@Data
19@NoArgsConstructor
20@JsonIgnoreProperties(ignoreUnknown = true)
22 private String response;
23 private String thinking;
24 private int httpStatusCode;
25 private long responseTime = 0;
26 private String model;
27
28 private @JsonProperty("created_at") String createdAt;
29 private boolean done;
30 private @JsonProperty("done_reason") String doneReason;
31 private List<Integer> context;
32 private @JsonProperty("total_duration") Long totalDuration;
33 private @JsonProperty("load_duration") Long loadDuration;
34 private @JsonProperty("prompt_eval_count") Integer promptEvalCount;
35 private @JsonProperty("prompt_eval_duration") Long promptEvalDuration;
36 private @JsonProperty("eval_count") Integer evalCount;
37 private @JsonProperty("eval_duration") Long evalDuration;
38
39 public OllamaStructuredResult(String response, long responseTime, int httpStatusCode) {
40 this.response = response;
41 this.responseTime = responseTime;
42 this.httpStatusCode = httpStatusCode;
43 }
44
45 @Override
46 public String toString() {
47 try {
48 return getObjectMapper().writerWithDefaultPrettyPrinter().writeValueAsString(this);
49 } catch (JsonProcessingException e) {
50 throw new RuntimeException(e);
51 }
52 }
53
59 public Map<String, Object> getStructuredResponse() {
60 try {
61 Map<String, Object> response = getObjectMapper().readValue(this.getResponse(),
62 new TypeReference<Map<String, Object>>() {
63 });
64 return response;
65 } catch (JsonProcessingException e) {
66 throw new RuntimeException(e);
67 }
68 }
69
78 public <T> T getStructuredResponse(Class<T> clazz) {
79 try {
80 return getObjectMapper().readValue(this.getResponse(), clazz);
81 } catch (JsonProcessingException e) {
82 throw new RuntimeException(e);
83 }
84 }
85}
OllamaStructuredResult(String response, long responseTime, int httpStatusCode)