Ollama4j
A Java library (wrapper/binding) for Ollama server.
Loading...
Searching...
No Matches
OllamaEndpointCaller.java
Go to the documentation of this file.
1package io.github.ollama4j.models.request;
2
3import io.github.ollama4j.OllamaAPI;
4import io.github.ollama4j.exceptions.OllamaBaseException;
5import io.github.ollama4j.models.response.OllamaErrorResponse;
6import io.github.ollama4j.models.response.OllamaResult;
7import io.github.ollama4j.utils.OllamaRequestBody;
8import io.github.ollama4j.utils.Utils;
9import lombok.Getter;
10import org.slf4j.Logger;
11import org.slf4j.LoggerFactory;
12
13import java.io.BufferedReader;
14import java.io.IOException;
15import java.io.InputStream;
16import java.io.InputStreamReader;
17import java.net.URI;
18import java.net.http.HttpClient;
19import java.net.http.HttpRequest;
20import java.net.http.HttpResponse;
21import java.nio.charset.StandardCharsets;
22import java.time.Duration;
23import java.util.Base64;
24
28@Getter
29public abstract class OllamaEndpointCaller {
30
31 private static final Logger LOG = LoggerFactory.getLogger(OllamaAPI.class);
32
33 private final String host;
34 private final BasicAuth basicAuth;
35 private final long requestTimeoutSeconds;
36 private final boolean verbose;
37
38 public OllamaEndpointCaller(String host, BasicAuth basicAuth, long requestTimeoutSeconds, boolean verbose) {
39 this.host = host;
40 this.basicAuth = basicAuth;
41 this.requestTimeoutSeconds = requestTimeoutSeconds;
42 this.verbose = verbose;
43 }
44
45 protected abstract String getEndpointSuffix();
46
47 protected abstract boolean parseResponseAndAddToBuffer(String line, StringBuilder responseBuffer);
48
49
56 protected HttpRequest.Builder getRequestBuilderDefault(URI uri) {
57 HttpRequest.Builder requestBuilder =
58 HttpRequest.newBuilder(uri)
59 .header("Content-Type", "application/json")
60 .timeout(Duration.ofSeconds(this.requestTimeoutSeconds));
62 requestBuilder.header("Authorization", getBasicAuthHeaderValue());
63 }
64 return requestBuilder;
65 }
66
72 protected String getBasicAuthHeaderValue() {
73 String credentialsToEncode = this.basicAuth.getUsername() + ":" + this.basicAuth.getPassword();
74 return "Basic " + Base64.getEncoder().encodeToString(credentialsToEncode.getBytes());
75 }
76
82 protected boolean isBasicAuthCredentialsSet() {
83 return this.basicAuth != null;
84 }
85
86}
OllamaEndpointCaller(String host, BasicAuth basicAuth, long requestTimeoutSeconds, boolean verbose)
abstract boolean parseResponseAndAddToBuffer(String line, StringBuilder responseBuffer)