mirror of
https://github.com/amithkoujalgi/ollama4j.git
synced 2025-05-15 03:47:13 +02:00
Support bearer token
May be use as follows: ``` ollamaAPI.setBasicAuth(new BasicAuth() { @Override public String getBasicAuthHeaderValue() { return "Bearer [sometext]"; } }); ``` Signed-off-by: Sven Strickroth <email@cs-ware.de>
This commit is contained in:
parent
7ef859bba5
commit
3a792090e2
@ -109,6 +109,10 @@ public class OllamaAPI {
|
||||
this.basicAuth = new BasicAuth(username, password);
|
||||
}
|
||||
|
||||
public void setBasicAuth(BasicAuth basicAuth) {
|
||||
this.basicAuth = basicAuth;
|
||||
}
|
||||
|
||||
/**
|
||||
* API to check the reachability of Ollama server.
|
||||
*
|
||||
@ -1083,21 +1087,11 @@ public class OllamaAPI {
|
||||
private HttpRequest.Builder getRequestBuilderDefault(URI uri) {
|
||||
HttpRequest.Builder requestBuilder = HttpRequest.newBuilder(uri).header("Content-Type", "application/json").timeout(Duration.ofSeconds(requestTimeoutSeconds));
|
||||
if (isBasicAuthCredentialsSet()) {
|
||||
requestBuilder.header("Authorization", getBasicAuthHeaderValue());
|
||||
requestBuilder.header("Authorization", basicAuth.getBasicAuthHeaderValue());
|
||||
}
|
||||
return requestBuilder;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get basic authentication header value.
|
||||
*
|
||||
* @return basic authentication header value (encoded credentials)
|
||||
*/
|
||||
private String getBasicAuthHeaderValue() {
|
||||
String credentialsToEncode = basicAuth.getUsername() + ":" + basicAuth.getPassword();
|
||||
return "Basic " + Base64.getEncoder().encodeToString(credentialsToEncode.getBytes());
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if Basic Auth credentials set.
|
||||
*
|
||||
|
@ -1,5 +1,7 @@
|
||||
package io.github.ollama4j.models.request;
|
||||
|
||||
import java.util.Base64;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
@ -10,4 +12,14 @@ import lombok.NoArgsConstructor;
|
||||
public class BasicAuth {
|
||||
private String username;
|
||||
private String password;
|
||||
|
||||
/**
|
||||
* Get basic authentication header value.
|
||||
*
|
||||
* @return basic authentication header value (encoded credentials)
|
||||
*/
|
||||
public String getBasicAuthHeaderValue() {
|
||||
final String credentialsToEncode = this.getUsername() + ":" + this.getPassword();
|
||||
return "Basic " + Base64.getEncoder().encodeToString(credentialsToEncode.getBytes());
|
||||
}
|
||||
}
|
||||
|
@ -1,26 +1,14 @@
|
||||
package io.github.ollama4j.models.request;
|
||||
|
||||
import io.github.ollama4j.OllamaAPI;
|
||||
import io.github.ollama4j.exceptions.OllamaBaseException;
|
||||
import io.github.ollama4j.models.response.OllamaErrorResponse;
|
||||
import io.github.ollama4j.models.response.OllamaResult;
|
||||
import io.github.ollama4j.utils.OllamaRequestBody;
|
||||
import io.github.ollama4j.utils.Utils;
|
||||
import lombok.Getter;
|
||||
import java.net.URI;
|
||||
import java.net.http.HttpRequest;
|
||||
import java.time.Duration;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.net.URI;
|
||||
import java.net.http.HttpClient;
|
||||
import java.net.http.HttpRequest;
|
||||
import java.net.http.HttpResponse;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.time.Duration;
|
||||
import java.util.Base64;
|
||||
import io.github.ollama4j.OllamaAPI;
|
||||
import lombok.Getter;
|
||||
|
||||
/**
|
||||
* Abstract helperclass to call the ollama api server.
|
||||
@ -59,21 +47,11 @@ public abstract class OllamaEndpointCaller {
|
||||
.header("Content-Type", "application/json")
|
||||
.timeout(Duration.ofSeconds(this.requestTimeoutSeconds));
|
||||
if (isBasicAuthCredentialsSet()) {
|
||||
requestBuilder.header("Authorization", getBasicAuthHeaderValue());
|
||||
requestBuilder.header("Authorization", this.basicAuth.getBasicAuthHeaderValue());
|
||||
}
|
||||
return requestBuilder;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get basic authentication header value.
|
||||
*
|
||||
* @return basic authentication header value (encoded credentials)
|
||||
*/
|
||||
protected String getBasicAuthHeaderValue() {
|
||||
String credentialsToEncode = this.basicAuth.getUsername() + ":" + this.basicAuth.getPassword();
|
||||
return "Basic " + Base64.getEncoder().encodeToString(credentialsToEncode.getBytes());
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if Basic Auth credentials set.
|
||||
*
|
||||
|
Loading…
x
Reference in New Issue
Block a user