Ollama4j
A Java library (wrapper/binding) for Ollama server.
Loading...
Searching...
No Matches
OllamaAsyncResultStreamer.java
Go to the documentation of this file.
1package io.github.ollama4j.models.response;
2
3import io.github.ollama4j.exceptions.OllamaBaseException;
4import io.github.ollama4j.models.generate.OllamaGenerateRequest;
5import io.github.ollama4j.models.generate.OllamaGenerateResponseModel;
6import io.github.ollama4j.utils.Utils;
7import lombok.Data;
8import lombok.EqualsAndHashCode;
9import lombok.Getter;
10import lombok.Setter;
11
12import java.io.BufferedReader;
13import java.io.IOException;
14import java.io.InputStream;
15import java.io.InputStreamReader;
16import java.net.http.HttpClient;
17import java.net.http.HttpRequest;
18import java.net.http.HttpResponse;
19import java.nio.charset.StandardCharsets;
20import java.time.Duration;
21
22@Data
23@EqualsAndHashCode(callSuper = true)
24@SuppressWarnings("unused")
25public class OllamaAsyncResultStreamer extends Thread {
26 private final HttpRequest.Builder requestBuilder;
27 private final OllamaGenerateRequest ollamaRequestModel;
28 private final OllamaResultStream stream = new OllamaResultStream();
29 private String completeResponse;
30
31
37 @Getter
38 private boolean succeeded;
39
40 @Setter
41 private long requestTimeoutSeconds;
42
47 @Getter
48 private int httpStatusCode;
49
53 @Getter
54 private long responseTime = 0;
55
57 HttpRequest.Builder requestBuilder,
58 OllamaGenerateRequest ollamaRequestModel,
59 long requestTimeoutSeconds) {
60 this.requestBuilder = requestBuilder;
61 this.ollamaRequestModel = ollamaRequestModel;
62 this.completeResponse = "";
63 this.stream.add("");
64 this.requestTimeoutSeconds = requestTimeoutSeconds;
65 }
66
67 @Override
68 public void run() {
69 ollamaRequestModel.setStream(true);
70 HttpClient httpClient = HttpClient.newHttpClient();
71 try {
72 long startTime = System.currentTimeMillis();
73 HttpRequest request =
74 requestBuilder
75 .POST(
76 HttpRequest.BodyPublishers.ofString(
77 Utils.getObjectMapper().writeValueAsString(ollamaRequestModel)))
78 .header("Content-Type", "application/json")
79 .timeout(Duration.ofSeconds(requestTimeoutSeconds))
80 .build();
81 HttpResponse<InputStream> response =
82 httpClient.send(request, HttpResponse.BodyHandlers.ofInputStream());
83 int statusCode = response.statusCode();
84 this.httpStatusCode = statusCode;
85
86 InputStream responseBodyStream = response.body();
87 try (BufferedReader reader =
88 new BufferedReader(new InputStreamReader(responseBodyStream, StandardCharsets.UTF_8))) {
89 String line;
90 StringBuilder responseBuffer = new StringBuilder();
91 while ((line = reader.readLine()) != null) {
92 if (statusCode == 404) {
93 OllamaErrorResponse ollamaResponseModel =
94 Utils.getObjectMapper().readValue(line, OllamaErrorResponse.class);
95 stream.add(ollamaResponseModel.getError());
96 responseBuffer.append(ollamaResponseModel.getError());
97 } else {
98 OllamaGenerateResponseModel ollamaResponseModel =
99 Utils.getObjectMapper().readValue(line, OllamaGenerateResponseModel.class);
100 String res = ollamaResponseModel.getResponse();
101 stream.add(res);
102 if (!ollamaResponseModel.isDone()) {
103 responseBuffer.append(res);
104 }
105 }
106 }
107
108 this.succeeded = true;
109 this.completeResponse = responseBuffer.toString();
110 long endTime = System.currentTimeMillis();
111 responseTime = endTime - startTime;
112 }
113 if (statusCode != 200) {
114 throw new OllamaBaseException(this.completeResponse);
115 }
116 } catch (IOException | InterruptedException | OllamaBaseException e) {
117 this.succeeded = false;
118 this.completeResponse = "[FAILED] " + e.getMessage();
119 }
120 }
121
122}
123
OllamaAsyncResultStreamer(HttpRequest.Builder requestBuilder, OllamaGenerateRequest ollamaRequestModel, long requestTimeoutSeconds)
static ObjectMapper getObjectMapper()
Definition Utils.java:17