Ollama4j
A Java library (wrapper/binding) for Ollama server.
Loading...
Searching...
No Matches
OllamaStructuredResult.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.annotation.JsonProperty;
15import com.fasterxml.jackson.core.JsonProcessingException;
16import com.fasterxml.jackson.core.type.TypeReference;
17import java.util.List;
18import java.util.Map;
19import lombok.Data;
20import lombok.Getter;
21import lombok.NoArgsConstructor;
22
23@Getter
24@SuppressWarnings("unused")
25@Data
26@NoArgsConstructor
27@JsonIgnoreProperties(ignoreUnknown = true)
29 private String response;
30 private String thinking;
31 private int httpStatusCode;
32 private long responseTime = 0;
33 private String model;
34
35 private @JsonProperty("created_at") String createdAt;
36 private boolean done;
37 private @JsonProperty("done_reason") String doneReason;
38 private List<Integer> context;
39 private @JsonProperty("total_duration") Long totalDuration;
40 private @JsonProperty("load_duration") Long loadDuration;
41 private @JsonProperty("prompt_eval_count") Integer promptEvalCount;
42 private @JsonProperty("prompt_eval_duration") Long promptEvalDuration;
43 private @JsonProperty("eval_count") Integer evalCount;
44 private @JsonProperty("eval_duration") Long evalDuration;
45
46 public OllamaStructuredResult(String response, long responseTime, int httpStatusCode) {
47 this.response = response;
48 this.responseTime = responseTime;
49 this.httpStatusCode = httpStatusCode;
50 }
51
52 @Override
53 public String toString() {
54 try {
55 return getObjectMapper().writerWithDefaultPrettyPrinter().writeValueAsString(this);
56 } catch (JsonProcessingException e) {
57 throw new RuntimeException(e);
58 }
59 }
60
66 public Map<String, Object> getStructuredResponse() {
67 try {
68 return getObjectMapper()
69 .readValue(this.getResponse(), new TypeReference<Map<String, Object>>() {});
70 } catch (JsonProcessingException e) {
71 throw new RuntimeException(e);
72 }
73 }
74
83 public <T> T getStructuredResponse(Class<T> clazz) {
84 try {
85 return getObjectMapper().readValue(this.getResponse(), clazz);
86 } catch (JsonProcessingException e) {
87 throw new RuntimeException(e);
88 }
89 }
90}
OllamaStructuredResult(String response, long responseTime, int httpStatusCode)