forked from Mirror/ollama4j
This update modifies the OllamaAPI class to enhance support for embedding models by renaming related classes and introducing new request and response models. The OllamaEmbedRequestModel and OllamaEmbedResponseModel classes have been added, along with their corresponding builder class. Additionally, the tool registration process has been improved with the introduction of annotations for automatic tool discovery. Deprecated methods and commented-out code have been removed for clarity, and Javadoc comments have been updated for consistency across the API.
64 lines
1.9 KiB
Java
64 lines
1.9 KiB
Java
/*
|
|
* Ollama4j - Java library for interacting with Ollama server.
|
|
* Copyright (c) 2025 Amith Koujalgi and contributors.
|
|
*
|
|
* Licensed under the MIT License (the "License");
|
|
* you may not use this file except in compliance with the License.
|
|
*
|
|
*/
|
|
package io.github.ollama4j.models.request;
|
|
|
|
import io.github.ollama4j.utils.Constants;
|
|
import java.net.URI;
|
|
import java.net.http.HttpRequest;
|
|
import java.time.Duration;
|
|
import lombok.Getter;
|
|
|
|
/**
|
|
* Abstract helper class to call the ollama api server.
|
|
*/
|
|
@Getter
|
|
public abstract class OllamaEndpointCaller {
|
|
|
|
private final String host;
|
|
private final Auth auth;
|
|
private final long requestTimeoutSeconds;
|
|
|
|
protected OllamaEndpointCaller(String host, Auth auth, long requestTimeoutSeconds) {
|
|
this.host = host;
|
|
this.auth = auth;
|
|
this.requestTimeoutSeconds = requestTimeoutSeconds;
|
|
}
|
|
|
|
protected abstract boolean parseResponseAndAddToBuffer(
|
|
String line, StringBuilder responseBuffer, StringBuilder thinkingBuffer);
|
|
|
|
/**
|
|
* Get default request builder.
|
|
*
|
|
* @param uri URI to get a HttpRequest.Builder
|
|
* @return HttpRequest.Builder
|
|
*/
|
|
protected HttpRequest.Builder getRequestBuilderDefault(URI uri) {
|
|
HttpRequest.Builder requestBuilder =
|
|
HttpRequest.newBuilder(uri)
|
|
.header(
|
|
Constants.HttpConstants.HEADER_KEY_CONTENT_TYPE,
|
|
Constants.HttpConstants.APPLICATION_JSON)
|
|
.timeout(Duration.ofSeconds(this.requestTimeoutSeconds));
|
|
if (isAuthCredentialsSet()) {
|
|
requestBuilder.header("Authorization", this.auth.getAuthHeaderValue());
|
|
}
|
|
return requestBuilder;
|
|
}
|
|
|
|
/**
|
|
* Check if Auth credentials set.
|
|
*
|
|
* @return true when Auth credentials set
|
|
*/
|
|
protected boolean isAuthCredentialsSet() {
|
|
return this.auth != null;
|
|
}
|
|
}
|