mirror of
https://github.com/amithkoujalgi/ollama4j.git
synced 2025-10-14 01:18:58 +02:00
refactor: clean up and deprecate unused methods in OllamaAPI and related classes
- Removed deprecated methods and unused imports from `OllamaAPI`. - Updated method signatures to improve clarity and consistency. - Refactored embedding request handling to utilize `OllamaEmbedRequestModel`. - Adjusted integration tests to reflect changes in method usage and removed obsolete tests. - Enhanced code readability by standardizing formatting and comments across various classes.
This commit is contained in:
parent
44c6236243
commit
656802b343
@ -9,8 +9,6 @@ import io.github.ollama4j.exceptions.ToolNotFoundException;
|
|||||||
import io.github.ollama4j.models.chat.*;
|
import io.github.ollama4j.models.chat.*;
|
||||||
import io.github.ollama4j.models.embeddings.OllamaEmbedRequestModel;
|
import io.github.ollama4j.models.embeddings.OllamaEmbedRequestModel;
|
||||||
import io.github.ollama4j.models.embeddings.OllamaEmbedResponseModel;
|
import io.github.ollama4j.models.embeddings.OllamaEmbedResponseModel;
|
||||||
import io.github.ollama4j.models.embeddings.OllamaEmbeddingResponseModel;
|
|
||||||
import io.github.ollama4j.models.embeddings.OllamaEmbeddingsRequestModel;
|
|
||||||
import io.github.ollama4j.models.generate.OllamaGenerateRequest;
|
import io.github.ollama4j.models.generate.OllamaGenerateRequest;
|
||||||
import io.github.ollama4j.models.generate.OllamaStreamHandler;
|
import io.github.ollama4j.models.generate.OllamaStreamHandler;
|
||||||
import io.github.ollama4j.models.generate.OllamaTokenHandler;
|
import io.github.ollama4j.models.generate.OllamaTokenHandler;
|
||||||
@ -25,10 +23,6 @@ import io.github.ollama4j.utils.Constants;
|
|||||||
import io.github.ollama4j.utils.Options;
|
import io.github.ollama4j.utils.Options;
|
||||||
import io.github.ollama4j.utils.Utils;
|
import io.github.ollama4j.utils.Utils;
|
||||||
import lombok.Setter;
|
import lombok.Setter;
|
||||||
import org.jsoup.Jsoup;
|
|
||||||
import org.jsoup.nodes.Document;
|
|
||||||
import org.jsoup.nodes.Element;
|
|
||||||
import org.jsoup.select.Elements;
|
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
@ -233,182 +227,6 @@ public class OllamaAPI {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Retrieves a list of models from the Ollama library. This method fetches the
|
|
||||||
* available models directly from Ollama
|
|
||||||
* library page, including model details such as the name, pull count, popular
|
|
||||||
* tags, tag count, and the time when model was updated.
|
|
||||||
*
|
|
||||||
* @return A list of {@link LibraryModel} objects representing the models
|
|
||||||
* available in the Ollama library.
|
|
||||||
* @throws OllamaBaseException If the HTTP request fails or the response is not
|
|
||||||
* successful (non-200 status code).
|
|
||||||
* @throws IOException If an I/O error occurs during the HTTP request
|
|
||||||
* or response processing.
|
|
||||||
* @throws InterruptedException If the thread executing the request is
|
|
||||||
* interrupted.
|
|
||||||
* @throws URISyntaxException If there is an error creating the URI for the
|
|
||||||
* HTTP request.
|
|
||||||
*/
|
|
||||||
public List<LibraryModel> listModelsFromLibrary()
|
|
||||||
throws OllamaBaseException, IOException, InterruptedException, URISyntaxException {
|
|
||||||
String url = "https://ollama.com/library";
|
|
||||||
HttpClient httpClient = HttpClient.newHttpClient();
|
|
||||||
HttpRequest httpRequest = getRequestBuilderDefault(new URI(url))
|
|
||||||
.header(Constants.HttpConstants.HEADER_KEY_ACCEPT, Constants.HttpConstants.APPLICATION_JSON)
|
|
||||||
.header(Constants.HttpConstants.HEADER_KEY_CONTENT_TYPE, Constants.HttpConstants.APPLICATION_JSON).GET()
|
|
||||||
.build();
|
|
||||||
HttpResponse<String> response = httpClient.send(httpRequest, HttpResponse.BodyHandlers.ofString());
|
|
||||||
int statusCode = response.statusCode();
|
|
||||||
String responseString = response.body();
|
|
||||||
List<LibraryModel> models = new ArrayList<>();
|
|
||||||
if (statusCode == 200) {
|
|
||||||
Document doc = Jsoup.parse(responseString);
|
|
||||||
Elements modelSections = doc.selectXpath("//*[@id='repo']/ul/li/a");
|
|
||||||
for (Element e : modelSections) {
|
|
||||||
LibraryModel model = new LibraryModel();
|
|
||||||
Elements names = e.select("div > h2 > div > span");
|
|
||||||
Elements desc = e.select("div > p");
|
|
||||||
Elements pullCounts = e.select("div:nth-of-type(2) > p > span:first-of-type > span:first-of-type");
|
|
||||||
Elements popularTags = e.select("div > div > span");
|
|
||||||
Elements totalTags = e.select("div:nth-of-type(2) > p > span:nth-of-type(2) > span:first-of-type");
|
|
||||||
Elements lastUpdatedTime = e
|
|
||||||
.select("div:nth-of-type(2) > p > span:nth-of-type(3) > span:nth-of-type(2)");
|
|
||||||
|
|
||||||
if (names.first() == null || names.isEmpty()) {
|
|
||||||
// if name cannot be extracted, skip.
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
Optional.ofNullable(names.first()).map(Element::text).ifPresent(model::setName);
|
|
||||||
model.setDescription(Optional.ofNullable(desc.first()).map(Element::text).orElse(""));
|
|
||||||
model.setPopularTags(Optional.of(popularTags)
|
|
||||||
.map(tags -> tags.stream().map(Element::text).collect(Collectors.toList()))
|
|
||||||
.orElse(new ArrayList<>()));
|
|
||||||
model.setPullCount(Optional.ofNullable(pullCounts.first()).map(Element::text).orElse(""));
|
|
||||||
model.setTotalTags(
|
|
||||||
Optional.ofNullable(totalTags.first()).map(Element::text).map(Integer::parseInt).orElse(0));
|
|
||||||
model.setLastUpdated(Optional.ofNullable(lastUpdatedTime.first()).map(Element::text).orElse(""));
|
|
||||||
|
|
||||||
models.add(model);
|
|
||||||
}
|
|
||||||
return models;
|
|
||||||
} else {
|
|
||||||
throw new OllamaBaseException(statusCode + " - " + responseString);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Fetches the tags associated with a specific model from Ollama library.
|
|
||||||
* This method fetches the available model tags directly from Ollama library
|
|
||||||
* model page, including model tag name, size and time when model was last
|
|
||||||
* updated
|
|
||||||
* into a list of {@link LibraryModelTag} objects.
|
|
||||||
*
|
|
||||||
* @param libraryModel the {@link LibraryModel} object which contains the name
|
|
||||||
* of the library model
|
|
||||||
* for which the tags need to be fetched.
|
|
||||||
* @return a list of {@link LibraryModelTag} objects containing the extracted
|
|
||||||
* tags and their associated metadata.
|
|
||||||
* @throws OllamaBaseException if the HTTP response status code indicates an
|
|
||||||
* error (i.e., not 200 OK),
|
|
||||||
* or if there is any other issue during the
|
|
||||||
* request or response processing.
|
|
||||||
* @throws IOException if an input/output exception occurs during the
|
|
||||||
* HTTP request or response handling.
|
|
||||||
* @throws InterruptedException if the thread is interrupted while waiting for
|
|
||||||
* the HTTP response.
|
|
||||||
* @throws URISyntaxException if the URI format is incorrect or invalid.
|
|
||||||
*/
|
|
||||||
public LibraryModelDetail getLibraryModelDetails(LibraryModel libraryModel)
|
|
||||||
throws OllamaBaseException, IOException, InterruptedException, URISyntaxException {
|
|
||||||
String url = String.format("https://ollama.com/library/%s/tags", libraryModel.getName());
|
|
||||||
HttpClient httpClient = HttpClient.newHttpClient();
|
|
||||||
HttpRequest httpRequest = getRequestBuilderDefault(new URI(url))
|
|
||||||
.header(Constants.HttpConstants.HEADER_KEY_ACCEPT, Constants.HttpConstants.APPLICATION_JSON)
|
|
||||||
.header(Constants.HttpConstants.HEADER_KEY_CONTENT_TYPE, Constants.HttpConstants.APPLICATION_JSON).GET()
|
|
||||||
.build();
|
|
||||||
HttpResponse<String> response = httpClient.send(httpRequest, HttpResponse.BodyHandlers.ofString());
|
|
||||||
int statusCode = response.statusCode();
|
|
||||||
String responseString = response.body();
|
|
||||||
|
|
||||||
List<LibraryModelTag> libraryModelTags = new ArrayList<>();
|
|
||||||
if (statusCode == 200) {
|
|
||||||
Document doc = Jsoup.parse(responseString);
|
|
||||||
Elements tagSections = doc
|
|
||||||
.select("html > body > main > div > section > div > div > div:nth-child(n+2) > div");
|
|
||||||
for (Element e : tagSections) {
|
|
||||||
Elements tags = e.select("div > a > div");
|
|
||||||
Elements tagsMetas = e.select("div > span");
|
|
||||||
|
|
||||||
LibraryModelTag libraryModelTag = new LibraryModelTag();
|
|
||||||
|
|
||||||
if (tags.first() == null || tags.isEmpty()) {
|
|
||||||
// if tag cannot be extracted, skip.
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
libraryModelTag.setName(libraryModel.getName());
|
|
||||||
Optional.ofNullable(tags.first()).map(Element::text).ifPresent(libraryModelTag::setTag);
|
|
||||||
libraryModelTag.setSize(Optional.ofNullable(tagsMetas.first()).map(element -> element.text().split("•"))
|
|
||||||
.filter(parts -> parts.length > 1).map(parts -> parts[1].trim()).orElse(""));
|
|
||||||
libraryModelTag
|
|
||||||
.setLastUpdated(Optional.ofNullable(tagsMetas.first()).map(element -> element.text().split("•"))
|
|
||||||
.filter(parts -> parts.length > 1).map(parts -> parts[2].trim()).orElse(""));
|
|
||||||
libraryModelTags.add(libraryModelTag);
|
|
||||||
}
|
|
||||||
LibraryModelDetail libraryModelDetail = new LibraryModelDetail();
|
|
||||||
libraryModelDetail.setModel(libraryModel);
|
|
||||||
libraryModelDetail.setTags(libraryModelTags);
|
|
||||||
return libraryModelDetail;
|
|
||||||
} else {
|
|
||||||
throw new OllamaBaseException(statusCode + " - " + responseString);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Finds a specific model using model name and tag from Ollama library.
|
|
||||||
* <p>
|
|
||||||
* <b>Deprecated:</b> This method relies on the HTML structure of the Ollama
|
|
||||||
* website,
|
|
||||||
* which is subject to change at any time. As a result, it is difficult to keep
|
|
||||||
* this API
|
|
||||||
* method consistently updated and reliable. Therefore, this method is
|
|
||||||
* deprecated and
|
|
||||||
* may be removed in future releases.
|
|
||||||
* <p>
|
|
||||||
* This method retrieves the model from the Ollama library by its name, then
|
|
||||||
* fetches its tags.
|
|
||||||
* It searches through the tags of the model to find one that matches the
|
|
||||||
* specified tag name.
|
|
||||||
* If the model or the tag is not found, it throws a
|
|
||||||
* {@link NoSuchElementException}.
|
|
||||||
*
|
|
||||||
* @param modelName The name of the model to search for in the library.
|
|
||||||
* @param tag The tag name to search for within the specified model.
|
|
||||||
* @return The {@link LibraryModelTag} associated with the specified model and
|
|
||||||
* tag.
|
|
||||||
* @throws OllamaBaseException If there is a problem with the Ollama library
|
|
||||||
* operations.
|
|
||||||
* @throws IOException If an I/O error occurs during the operation.
|
|
||||||
* @throws URISyntaxException If there is an error with the URI syntax.
|
|
||||||
* @throws InterruptedException If the operation is interrupted.
|
|
||||||
* @throws NoSuchElementException If the model or the tag is not found.
|
|
||||||
* @deprecated This method relies on the HTML structure of the Ollama website,
|
|
||||||
* which can change at any time and break this API. It is deprecated
|
|
||||||
* and may be removed in the future.
|
|
||||||
*/
|
|
||||||
@Deprecated
|
|
||||||
public LibraryModelTag findModelTagFromLibrary(String modelName, String tag)
|
|
||||||
throws OllamaBaseException, IOException, URISyntaxException, InterruptedException {
|
|
||||||
List<LibraryModel> libraryModels = this.listModelsFromLibrary();
|
|
||||||
LibraryModel libraryModel = libraryModels.stream().filter(model -> model.getName().equals(modelName))
|
|
||||||
.findFirst().orElseThrow(
|
|
||||||
() -> new NoSuchElementException(String.format("Model by name '%s' not found", modelName)));
|
|
||||||
LibraryModelDetail libraryModelDetail = this.getLibraryModelDetails(libraryModel);
|
|
||||||
return libraryModelDetail.getTags().stream().filter(tagName -> tagName.getTag().equals(tag)).findFirst()
|
|
||||||
.orElseThrow(() -> new NoSuchElementException(
|
|
||||||
String.format("Tag '%s' for model '%s' not found", tag, modelName)));
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Pull a model on the Ollama server from the list of <a
|
* Pull a model on the Ollama server from the list of <a
|
||||||
* href="https://ollama.ai/library">available models</a>.
|
* href="https://ollama.ai/library">available models</a>.
|
||||||
@ -584,80 +402,6 @@ public class OllamaAPI {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Create a custom model from a model file. Read more about custom model file
|
|
||||||
* creation <a
|
|
||||||
* href=
|
|
||||||
* "https://github.com/jmorganca/ollama/blob/main/docs/modelfile.md">here</a>.
|
|
||||||
*
|
|
||||||
* @param modelName the name of the custom model to be created.
|
|
||||||
* @param modelFilePath the path to model file that exists on the Ollama server.
|
|
||||||
* @throws OllamaBaseException if the response indicates an error status
|
|
||||||
* @throws IOException if an I/O error occurs during the HTTP request
|
|
||||||
* @throws InterruptedException if the operation is interrupted
|
|
||||||
* @throws URISyntaxException if the URI for the request is malformed
|
|
||||||
*/
|
|
||||||
@Deprecated
|
|
||||||
public void createModelWithFilePath(String modelName, String modelFilePath)
|
|
||||||
throws IOException, InterruptedException, OllamaBaseException, URISyntaxException {
|
|
||||||
String url = this.host + "/api/create";
|
|
||||||
String jsonData = new CustomModelFilePathRequest(modelName, modelFilePath).toString();
|
|
||||||
HttpRequest request = getRequestBuilderDefault(new URI(url))
|
|
||||||
.header(Constants.HttpConstants.HEADER_KEY_ACCEPT, Constants.HttpConstants.APPLICATION_JSON)
|
|
||||||
.header(Constants.HttpConstants.HEADER_KEY_CONTENT_TYPE, Constants.HttpConstants.APPLICATION_JSON)
|
|
||||||
.POST(HttpRequest.BodyPublishers.ofString(jsonData, StandardCharsets.UTF_8)).build();
|
|
||||||
HttpClient client = HttpClient.newHttpClient();
|
|
||||||
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
|
|
||||||
int statusCode = response.statusCode();
|
|
||||||
String responseString = response.body();
|
|
||||||
if (statusCode != 200) {
|
|
||||||
throw new OllamaBaseException(statusCode + " - " + responseString);
|
|
||||||
}
|
|
||||||
// FIXME: Ollama API returns HTTP status code 200 for model creation failure
|
|
||||||
// cases. Correct this
|
|
||||||
// if the issue is fixed in the Ollama API server.
|
|
||||||
if (responseString.contains("error")) {
|
|
||||||
throw new OllamaBaseException(responseString);
|
|
||||||
}
|
|
||||||
LOG.debug(responseString);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Create a custom model from a model file. Read more about custom model file
|
|
||||||
* creation <a
|
|
||||||
* href=
|
|
||||||
* "https://github.com/jmorganca/ollama/blob/main/docs/modelfile.md">here</a>.
|
|
||||||
*
|
|
||||||
* @param modelName the name of the custom model to be created.
|
|
||||||
* @param modelFileContents the path to model file that exists on the Ollama
|
|
||||||
* server.
|
|
||||||
* @throws OllamaBaseException if the response indicates an error status
|
|
||||||
* @throws IOException if an I/O error occurs during the HTTP request
|
|
||||||
* @throws InterruptedException if the operation is interrupted
|
|
||||||
* @throws URISyntaxException if the URI for the request is malformed
|
|
||||||
*/
|
|
||||||
@Deprecated
|
|
||||||
public void createModelWithModelFileContents(String modelName, String modelFileContents)
|
|
||||||
throws IOException, InterruptedException, OllamaBaseException, URISyntaxException {
|
|
||||||
String url = this.host + "/api/create";
|
|
||||||
String jsonData = new CustomModelFileContentsRequest(modelName, modelFileContents).toString();
|
|
||||||
HttpRequest request = getRequestBuilderDefault(new URI(url))
|
|
||||||
.header(Constants.HttpConstants.HEADER_KEY_ACCEPT, Constants.HttpConstants.APPLICATION_JSON)
|
|
||||||
.header(Constants.HttpConstants.HEADER_KEY_CONTENT_TYPE, Constants.HttpConstants.APPLICATION_JSON)
|
|
||||||
.POST(HttpRequest.BodyPublishers.ofString(jsonData, StandardCharsets.UTF_8)).build();
|
|
||||||
HttpClient client = HttpClient.newHttpClient();
|
|
||||||
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
|
|
||||||
int statusCode = response.statusCode();
|
|
||||||
String responseString = response.body();
|
|
||||||
if (statusCode != 200) {
|
|
||||||
throw new OllamaBaseException(statusCode + " - " + responseString);
|
|
||||||
}
|
|
||||||
if (responseString.contains("error")) {
|
|
||||||
throw new OllamaBaseException(responseString);
|
|
||||||
}
|
|
||||||
LOG.debug(responseString);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create a custom model. Read more about custom model creation <a
|
* Create a custom model. Read more about custom model creation <a
|
||||||
* href=
|
* href=
|
||||||
@ -722,70 +466,6 @@ public class OllamaAPI {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Generate embeddings for a given text from a model
|
|
||||||
*
|
|
||||||
* @param model name of model to generate embeddings from
|
|
||||||
* @param prompt text to generate embeddings for
|
|
||||||
* @return embeddings
|
|
||||||
* @throws OllamaBaseException if the response indicates an error status
|
|
||||||
* @throws IOException if an I/O error occurs during the HTTP request
|
|
||||||
* @throws InterruptedException if the operation is interrupted
|
|
||||||
* @deprecated Use {@link #embed(String, List)} instead.
|
|
||||||
*/
|
|
||||||
@Deprecated
|
|
||||||
public List<Double> generateEmbeddings(String model, String prompt)
|
|
||||||
throws IOException, InterruptedException, OllamaBaseException {
|
|
||||||
return generateEmbeddings(new OllamaEmbeddingsRequestModel(model, prompt));
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Generate embeddings using a {@link OllamaEmbeddingsRequestModel}.
|
|
||||||
*
|
|
||||||
* @param modelRequest request for '/api/embeddings' endpoint
|
|
||||||
* @return embeddings
|
|
||||||
* @throws OllamaBaseException if the response indicates an error status
|
|
||||||
* @throws IOException if an I/O error occurs during the HTTP request
|
|
||||||
* @throws InterruptedException if the operation is interrupted
|
|
||||||
* @deprecated Use {@link #embed(OllamaEmbedRequestModel)} instead.
|
|
||||||
*/
|
|
||||||
@Deprecated
|
|
||||||
public List<Double> generateEmbeddings(OllamaEmbeddingsRequestModel modelRequest)
|
|
||||||
throws IOException, InterruptedException, OllamaBaseException {
|
|
||||||
URI uri = URI.create(this.host + "/api/embeddings");
|
|
||||||
String jsonData = modelRequest.toString();
|
|
||||||
HttpClient httpClient = HttpClient.newHttpClient();
|
|
||||||
HttpRequest.Builder requestBuilder = getRequestBuilderDefault(uri)
|
|
||||||
.header(Constants.HttpConstants.HEADER_KEY_ACCEPT, Constants.HttpConstants.APPLICATION_JSON)
|
|
||||||
.POST(HttpRequest.BodyPublishers.ofString(jsonData));
|
|
||||||
HttpRequest request = requestBuilder.build();
|
|
||||||
HttpResponse<String> response = httpClient.send(request, HttpResponse.BodyHandlers.ofString());
|
|
||||||
int statusCode = response.statusCode();
|
|
||||||
String responseBody = response.body();
|
|
||||||
if (statusCode == 200) {
|
|
||||||
OllamaEmbeddingResponseModel embeddingResponse = Utils.getObjectMapper().readValue(responseBody,
|
|
||||||
OllamaEmbeddingResponseModel.class);
|
|
||||||
return embeddingResponse.getEmbedding();
|
|
||||||
} else {
|
|
||||||
throw new OllamaBaseException(statusCode + " - " + responseBody);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Generate embeddings for a given text from a model
|
|
||||||
*
|
|
||||||
* @param model name of model to generate embeddings from
|
|
||||||
* @param inputs text/s to generate embeddings for
|
|
||||||
* @return embeddings
|
|
||||||
* @throws OllamaBaseException if the response indicates an error status
|
|
||||||
* @throws IOException if an I/O error occurs during the HTTP request
|
|
||||||
* @throws InterruptedException if the operation is interrupted
|
|
||||||
*/
|
|
||||||
public OllamaEmbedResponseModel embed(String model, List<String> inputs)
|
|
||||||
throws IOException, InterruptedException, OllamaBaseException {
|
|
||||||
return embed(new OllamaEmbedRequestModel(model, inputs));
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Generate embeddings using a {@link OllamaEmbedRequestModel}.
|
* Generate embeddings using a {@link OllamaEmbedRequestModel}.
|
||||||
*
|
*
|
||||||
@ -1068,7 +748,7 @@ public class OllamaAPI {
|
|||||||
* </p>
|
* </p>
|
||||||
*
|
*
|
||||||
* <pre>{@code
|
* <pre>{@code
|
||||||
* OllamaAsyncResultStreamer resultStreamer = ollamaAPI.generateAsync("gpt-oss:20b", "Who are you", false, true);
|
* OllamaAsyncResultStreamer resultStreamer = ollamaAPI.generate("gpt-oss:20b", "Who are you", false, true);
|
||||||
* int pollIntervalMilliseconds = 1000;
|
* int pollIntervalMilliseconds = 1000;
|
||||||
* while (true) {
|
* while (true) {
|
||||||
* String thinkingTokens = resultStreamer.getThinkingResponseStream().poll();
|
* String thinkingTokens = resultStreamer.getThinkingResponseStream().poll();
|
||||||
@ -1155,86 +835,7 @@ public class OllamaAPI {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Ask a question to a model based on a given message stack (i.e. a chat
|
* Ask a question to a model using an {@link OllamaChatRequest} and set up streaming response. This can be
|
||||||
* history). Creates a synchronous call to the api
|
|
||||||
* 'api/chat'.
|
|
||||||
*
|
|
||||||
* @param model the ollama model to ask the question to
|
|
||||||
* @param messages chat history / message stack to send to the model
|
|
||||||
* @return {@link OllamaChatResult} containing the api response and the message
|
|
||||||
* history including the newly acquired assistant response.
|
|
||||||
* @throws OllamaBaseException any response code than 200 has been returned
|
|
||||||
* @throws IOException in case the responseStream can not be read
|
|
||||||
* @throws InterruptedException in case the server is not reachable or
|
|
||||||
* network
|
|
||||||
* issues happen
|
|
||||||
* @throws OllamaBaseException if the response indicates an error status
|
|
||||||
* @throws IOException if an I/O error occurs during the HTTP
|
|
||||||
* request
|
|
||||||
* @throws InterruptedException if the operation is interrupted
|
|
||||||
* @throws ToolInvocationException if the tool invocation fails
|
|
||||||
*/
|
|
||||||
public OllamaChatResult chat(String model, List<OllamaChatMessage> messages)
|
|
||||||
throws OllamaBaseException, IOException, InterruptedException, ToolInvocationException {
|
|
||||||
OllamaChatRequestBuilder builder = OllamaChatRequestBuilder.getInstance(model);
|
|
||||||
return chat(builder.withMessages(messages).build());
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Ask a question to a model using an {@link OllamaChatRequest}. This can be
|
|
||||||
* constructed using an {@link OllamaChatRequestBuilder}.
|
|
||||||
* <p>
|
|
||||||
* Hint: the OllamaChatRequestModel#getStream() property is not implemented.
|
|
||||||
*
|
|
||||||
* @param request request object to be sent to the server
|
|
||||||
* @return {@link OllamaChatResult}
|
|
||||||
* @throws OllamaBaseException any response code than 200 has been returned
|
|
||||||
* @throws IOException in case the responseStream can not be read
|
|
||||||
* @throws InterruptedException in case the server is not reachable or
|
|
||||||
* network
|
|
||||||
* issues happen
|
|
||||||
* @throws OllamaBaseException if the response indicates an error status
|
|
||||||
* @throws IOException if an I/O error occurs during the HTTP
|
|
||||||
* request
|
|
||||||
* @throws InterruptedException if the operation is interrupted
|
|
||||||
* @throws ToolInvocationException if the tool invocation fails
|
|
||||||
*/
|
|
||||||
public OllamaChatResult chat(OllamaChatRequest request)
|
|
||||||
throws OllamaBaseException, IOException, InterruptedException, ToolInvocationException {
|
|
||||||
return chat(request, null, null);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Ask a question to a model using an {@link OllamaChatRequest}. This can be
|
|
||||||
* constructed using an {@link OllamaChatRequestBuilder}.
|
|
||||||
* <p>
|
|
||||||
* Hint: the OllamaChatRequestModel#getStream() property is not implemented.
|
|
||||||
*
|
|
||||||
* @param request request object to be sent to the server
|
|
||||||
* @param responseStreamHandler callback handler to handle the last message from
|
|
||||||
* stream
|
|
||||||
* @param thinkingStreamHandler callback handler to handle the last thinking
|
|
||||||
* message from stream
|
|
||||||
* @return {@link OllamaChatResult}
|
|
||||||
* @throws OllamaBaseException any response code than 200 has been returned
|
|
||||||
* @throws IOException in case the responseStream can not be read
|
|
||||||
* @throws InterruptedException in case the server is not reachable or
|
|
||||||
* network
|
|
||||||
* issues happen
|
|
||||||
* @throws OllamaBaseException if the response indicates an error status
|
|
||||||
* @throws IOException if an I/O error occurs during the HTTP
|
|
||||||
* request
|
|
||||||
* @throws InterruptedException if the operation is interrupted
|
|
||||||
* @throws ToolInvocationException if the tool invocation fails
|
|
||||||
*/
|
|
||||||
public OllamaChatResult chat(OllamaChatRequest request, OllamaStreamHandler thinkingStreamHandler,
|
|
||||||
OllamaStreamHandler responseStreamHandler)
|
|
||||||
throws OllamaBaseException, IOException, InterruptedException, ToolInvocationException {
|
|
||||||
return chatStreaming(request, new OllamaChatStreamObserver(thinkingStreamHandler, responseStreamHandler));
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Ask a question to a model using an {@link OllamaChatRequest}. This can be
|
|
||||||
* constructed using an {@link OllamaChatRequestBuilder}.
|
* constructed using an {@link OllamaChatRequestBuilder}.
|
||||||
* <p>
|
* <p>
|
||||||
* Hint: the OllamaChatRequestModel#getStream() property is not implemented.
|
* Hint: the OllamaChatRequestModel#getStream() property is not implemented.
|
||||||
@ -1252,7 +853,7 @@ public class OllamaAPI {
|
|||||||
* @throws IOException if an I/O error occurs during the HTTP request
|
* @throws IOException if an I/O error occurs during the HTTP request
|
||||||
* @throws InterruptedException if the operation is interrupted
|
* @throws InterruptedException if the operation is interrupted
|
||||||
*/
|
*/
|
||||||
public OllamaChatResult chatStreaming(OllamaChatRequest request, OllamaTokenHandler tokenHandler)
|
public OllamaChatResult chat(OllamaChatRequest request, OllamaTokenHandler tokenHandler)
|
||||||
throws OllamaBaseException, IOException, InterruptedException, ToolInvocationException {
|
throws OllamaBaseException, IOException, InterruptedException, ToolInvocationException {
|
||||||
OllamaChatEndpointCaller requestCaller = new OllamaChatEndpointCaller(host, auth, requestTimeoutSeconds);
|
OllamaChatEndpointCaller requestCaller = new OllamaChatEndpointCaller(host, auth, requestTimeoutSeconds);
|
||||||
OllamaChatResult result;
|
OllamaChatResult result;
|
||||||
|
@ -38,17 +38,17 @@ public class OllamaChatResult {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Deprecated
|
@Deprecated
|
||||||
public String getResponse(){
|
public String getResponse() {
|
||||||
return responseModel != null ? responseModel.getMessage().getContent() : "";
|
return responseModel != null ? responseModel.getMessage().getContent() : "";
|
||||||
}
|
}
|
||||||
|
|
||||||
@Deprecated
|
@Deprecated
|
||||||
public int getHttpStatusCode(){
|
public int getHttpStatusCode() {
|
||||||
return 200;
|
return 200;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Deprecated
|
@Deprecated
|
||||||
public long getResponseTime(){
|
public long getResponseTime() {
|
||||||
return responseModel != null ? responseModel.getTotalDuration() : 0L;
|
return responseModel != null ? responseModel.getTotalDuration() : 0L;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -12,24 +12,24 @@ public class OllamaEmbedRequestBuilder {
|
|||||||
private final OllamaEmbedRequestModel request;
|
private final OllamaEmbedRequestModel request;
|
||||||
|
|
||||||
private OllamaEmbedRequestBuilder(String model, List<String> input) {
|
private OllamaEmbedRequestBuilder(String model, List<String> input) {
|
||||||
this.request = new OllamaEmbedRequestModel(model,input);
|
this.request = new OllamaEmbedRequestModel(model, input);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static OllamaEmbedRequestBuilder getInstance(String model, String... input){
|
public static OllamaEmbedRequestBuilder getInstance(String model, String... input) {
|
||||||
return new OllamaEmbedRequestBuilder(model, List.of(input));
|
return new OllamaEmbedRequestBuilder(model, List.of(input));
|
||||||
}
|
}
|
||||||
|
|
||||||
public OllamaEmbedRequestBuilder withOptions(Options options){
|
public OllamaEmbedRequestBuilder withOptions(Options options) {
|
||||||
this.request.setOptions(options.getOptionsMap());
|
this.request.setOptions(options.getOptionsMap());
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public OllamaEmbedRequestBuilder withKeepAlive(String keepAlive){
|
public OllamaEmbedRequestBuilder withKeepAlive(String keepAlive) {
|
||||||
this.request.setKeepAlive(keepAlive);
|
this.request.setKeepAlive(keepAlive);
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public OllamaEmbedRequestBuilder withoutTruncate(){
|
public OllamaEmbedRequestBuilder withoutTruncate() {
|
||||||
this.request.setTruncate(false);
|
this.request.setTruncate(false);
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
@ -7,7 +7,7 @@ import java.util.List;
|
|||||||
|
|
||||||
@SuppressWarnings("unused")
|
@SuppressWarnings("unused")
|
||||||
@Data
|
@Data
|
||||||
@Deprecated(since="1.0.90")
|
@Deprecated(since = "1.0.90")
|
||||||
public class OllamaEmbeddingResponseModel {
|
public class OllamaEmbeddingResponseModel {
|
||||||
@JsonProperty("embedding")
|
@JsonProperty("embedding")
|
||||||
private List<Double> embedding;
|
private List<Double> embedding;
|
||||||
|
@ -2,29 +2,29 @@ package io.github.ollama4j.models.embeddings;
|
|||||||
|
|
||||||
import io.github.ollama4j.utils.Options;
|
import io.github.ollama4j.utils.Options;
|
||||||
|
|
||||||
@Deprecated(since="1.0.90")
|
@Deprecated(since = "1.0.90")
|
||||||
public class OllamaEmbeddingsRequestBuilder {
|
public class OllamaEmbeddingsRequestBuilder {
|
||||||
|
|
||||||
private OllamaEmbeddingsRequestBuilder(String model, String prompt){
|
private OllamaEmbeddingsRequestBuilder(String model, String prompt) {
|
||||||
request = new OllamaEmbeddingsRequestModel(model, prompt);
|
request = new OllamaEmbeddingsRequestModel(model, prompt);
|
||||||
}
|
}
|
||||||
|
|
||||||
private OllamaEmbeddingsRequestModel request;
|
private OllamaEmbeddingsRequestModel request;
|
||||||
|
|
||||||
public static OllamaEmbeddingsRequestBuilder getInstance(String model, String prompt){
|
public static OllamaEmbeddingsRequestBuilder getInstance(String model, String prompt) {
|
||||||
return new OllamaEmbeddingsRequestBuilder(model, prompt);
|
return new OllamaEmbeddingsRequestBuilder(model, prompt);
|
||||||
}
|
}
|
||||||
|
|
||||||
public OllamaEmbeddingsRequestModel build(){
|
public OllamaEmbeddingsRequestModel build() {
|
||||||
return request;
|
return request;
|
||||||
}
|
}
|
||||||
|
|
||||||
public OllamaEmbeddingsRequestBuilder withOptions(Options options){
|
public OllamaEmbeddingsRequestBuilder withOptions(Options options) {
|
||||||
this.request.setOptions(options.getOptionsMap());
|
this.request.setOptions(options.getOptionsMap());
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public OllamaEmbeddingsRequestBuilder withKeepAlive(String keepAlive){
|
public OllamaEmbeddingsRequestBuilder withKeepAlive(String keepAlive) {
|
||||||
this.request.setKeepAlive(keepAlive);
|
this.request.setKeepAlive(keepAlive);
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
@ -14,23 +14,23 @@ import static io.github.ollama4j.utils.Utils.getObjectMapper;
|
|||||||
@Data
|
@Data
|
||||||
@RequiredArgsConstructor
|
@RequiredArgsConstructor
|
||||||
@NoArgsConstructor
|
@NoArgsConstructor
|
||||||
@Deprecated(since="1.0.90")
|
@Deprecated(since = "1.0.90")
|
||||||
public class OllamaEmbeddingsRequestModel {
|
public class OllamaEmbeddingsRequestModel {
|
||||||
@NonNull
|
@NonNull
|
||||||
private String model;
|
private String model;
|
||||||
@NonNull
|
@NonNull
|
||||||
private String prompt;
|
private String prompt;
|
||||||
|
|
||||||
protected Map<String, Object> options;
|
protected Map<String, Object> options;
|
||||||
@JsonProperty(value = "keep_alive")
|
@JsonProperty(value = "keep_alive")
|
||||||
private String keepAlive;
|
private String keepAlive;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
try {
|
try {
|
||||||
return getObjectMapper().writerWithDefaultPrettyPrinter().writeValueAsString(this);
|
return getObjectMapper().writerWithDefaultPrettyPrinter().writeValueAsString(this);
|
||||||
} catch (JsonProcessingException e) {
|
} catch (JsonProcessingException e) {
|
||||||
throw new RuntimeException(e);
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -7,7 +7,6 @@ import lombok.Getter;
|
|||||||
import lombok.Setter;
|
import lombok.Setter;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
|
||||||
|
|
||||||
@Getter
|
@Getter
|
||||||
@Setter
|
@Setter
|
||||||
|
@ -8,46 +8,46 @@ import io.github.ollama4j.utils.Options;
|
|||||||
*/
|
*/
|
||||||
public class OllamaGenerateRequestBuilder {
|
public class OllamaGenerateRequestBuilder {
|
||||||
|
|
||||||
private OllamaGenerateRequestBuilder(String model, String prompt){
|
private OllamaGenerateRequestBuilder(String model, String prompt) {
|
||||||
request = new OllamaGenerateRequest(model, prompt);
|
request = new OllamaGenerateRequest(model, prompt);
|
||||||
}
|
}
|
||||||
|
|
||||||
private OllamaGenerateRequest request;
|
private OllamaGenerateRequest request;
|
||||||
|
|
||||||
public static OllamaGenerateRequestBuilder getInstance(String model){
|
public static OllamaGenerateRequestBuilder getInstance(String model) {
|
||||||
return new OllamaGenerateRequestBuilder(model,"");
|
return new OllamaGenerateRequestBuilder(model, "");
|
||||||
}
|
}
|
||||||
|
|
||||||
public OllamaGenerateRequest build(){
|
public OllamaGenerateRequest build() {
|
||||||
return request;
|
return request;
|
||||||
}
|
}
|
||||||
|
|
||||||
public OllamaGenerateRequestBuilder withPrompt(String prompt){
|
public OllamaGenerateRequestBuilder withPrompt(String prompt) {
|
||||||
request.setPrompt(prompt);
|
request.setPrompt(prompt);
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public OllamaGenerateRequestBuilder withGetJsonResponse(){
|
public OllamaGenerateRequestBuilder withGetJsonResponse() {
|
||||||
this.request.setFormat("json");
|
this.request.setFormat("json");
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public OllamaGenerateRequestBuilder withOptions(Options options){
|
public OllamaGenerateRequestBuilder withOptions(Options options) {
|
||||||
this.request.setOptions(options.getOptionsMap());
|
this.request.setOptions(options.getOptionsMap());
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public OllamaGenerateRequestBuilder withTemplate(String template){
|
public OllamaGenerateRequestBuilder withTemplate(String template) {
|
||||||
this.request.setTemplate(template);
|
this.request.setTemplate(template);
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public OllamaGenerateRequestBuilder withStreaming(){
|
public OllamaGenerateRequestBuilder withStreaming() {
|
||||||
this.request.setStream(true);
|
this.request.setStream(true);
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public OllamaGenerateRequestBuilder withKeepAlive(String keepAlive){
|
public OllamaGenerateRequestBuilder withKeepAlive(String keepAlive) {
|
||||||
this.request.setKeepAlive(keepAlive);
|
this.request.setKeepAlive(keepAlive);
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
@ -1,10 +1,10 @@
|
|||||||
package io.github.ollama4j.models.request;
|
package io.github.ollama4j.models.request;
|
||||||
|
|
||||||
public abstract class Auth {
|
public abstract class Auth {
|
||||||
/**
|
/**
|
||||||
* Get authentication header value.
|
* Get authentication header value.
|
||||||
*
|
*
|
||||||
* @return authentication header value
|
* @return authentication header value
|
||||||
*/
|
*/
|
||||||
public abstract String getAuthHeaderValue();
|
public abstract String getAuthHeaderValue();
|
||||||
}
|
}
|
||||||
|
@ -10,16 +10,16 @@ import java.util.Base64;
|
|||||||
@AllArgsConstructor
|
@AllArgsConstructor
|
||||||
@EqualsAndHashCode(callSuper = false)
|
@EqualsAndHashCode(callSuper = false)
|
||||||
public class BasicAuth extends Auth {
|
public class BasicAuth extends Auth {
|
||||||
private String username;
|
private String username;
|
||||||
private String password;
|
private String password;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get basic authentication header value.
|
* Get basic authentication header value.
|
||||||
*
|
*
|
||||||
* @return basic authentication header value (encoded credentials)
|
* @return basic authentication header value (encoded credentials)
|
||||||
*/
|
*/
|
||||||
public String getAuthHeaderValue() {
|
public String getAuthHeaderValue() {
|
||||||
final String credentialsToEncode = this.getUsername() + ":" + this.getPassword();
|
final String credentialsToEncode = this.getUsername() + ":" + this.getPassword();
|
||||||
return "Basic " + Base64.getEncoder().encodeToString(credentialsToEncode.getBytes());
|
return "Basic " + Base64.getEncoder().encodeToString(credentialsToEncode.getBytes());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -9,15 +9,15 @@ import static io.github.ollama4j.utils.Utils.getObjectMapper;
|
|||||||
@Data
|
@Data
|
||||||
@AllArgsConstructor
|
@AllArgsConstructor
|
||||||
public class CustomModelFileContentsRequest {
|
public class CustomModelFileContentsRequest {
|
||||||
private String name;
|
private String name;
|
||||||
private String modelfile;
|
private String modelfile;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
try {
|
try {
|
||||||
return getObjectMapper().writerWithDefaultPrettyPrinter().writeValueAsString(this);
|
return getObjectMapper().writerWithDefaultPrettyPrinter().writeValueAsString(this);
|
||||||
} catch (JsonProcessingException e) {
|
} catch (JsonProcessingException e) {
|
||||||
throw new RuntimeException(e);
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -9,15 +9,15 @@ import static io.github.ollama4j.utils.Utils.getObjectMapper;
|
|||||||
@Data
|
@Data
|
||||||
@AllArgsConstructor
|
@AllArgsConstructor
|
||||||
public class CustomModelFilePathRequest {
|
public class CustomModelFilePathRequest {
|
||||||
private String name;
|
private String name;
|
||||||
private String path;
|
private String path;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
try {
|
try {
|
||||||
return getObjectMapper().writerWithDefaultPrettyPrinter().writeValueAsString(this);
|
return getObjectMapper().writerWithDefaultPrettyPrinter().writeValueAsString(this);
|
||||||
} catch (JsonProcessingException e) {
|
} catch (JsonProcessingException e) {
|
||||||
throw new RuntimeException(e);
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -9,14 +9,14 @@ import static io.github.ollama4j.utils.Utils.getObjectMapper;
|
|||||||
@Data
|
@Data
|
||||||
@AllArgsConstructor
|
@AllArgsConstructor
|
||||||
public class ModelRequest {
|
public class ModelRequest {
|
||||||
private String name;
|
private String name;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
try {
|
try {
|
||||||
return getObjectMapper().writerWithDefaultPrettyPrinter().writeValueAsString(this);
|
return getObjectMapper().writerWithDefaultPrettyPrinter().writeValueAsString(this);
|
||||||
} catch (JsonProcessingException e) {
|
} catch (JsonProcessingException e) {
|
||||||
throw new RuntimeException(e);
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -61,8 +61,7 @@ public class OllamaChatEndpointCaller extends OllamaEndpointCaller {
|
|||||||
if (message != null) {
|
if (message != null) {
|
||||||
if (message.getThinking() != null) {
|
if (message.getThinking() != null) {
|
||||||
thinkingBuffer.append(message.getThinking());
|
thinkingBuffer.append(message.getThinking());
|
||||||
}
|
} else {
|
||||||
else {
|
|
||||||
responseBuffer.append(message.getContent());
|
responseBuffer.append(message.getContent());
|
||||||
}
|
}
|
||||||
if (tokenHandler != null) {
|
if (tokenHandler != null) {
|
||||||
|
@ -3,8 +3,6 @@ package io.github.ollama4j.models.request;
|
|||||||
import com.fasterxml.jackson.annotation.JsonInclude;
|
import com.fasterxml.jackson.annotation.JsonInclude;
|
||||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||||
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
|
|
||||||
import io.github.ollama4j.utils.BooleanToJsonFormatFlagSerializer;
|
|
||||||
import io.github.ollama4j.utils.Utils;
|
import io.github.ollama4j.utils.Utils;
|
||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
|
|
||||||
@ -14,23 +12,23 @@ import java.util.Map;
|
|||||||
@JsonInclude(JsonInclude.Include.NON_NULL)
|
@JsonInclude(JsonInclude.Include.NON_NULL)
|
||||||
public abstract class OllamaCommonRequest {
|
public abstract class OllamaCommonRequest {
|
||||||
|
|
||||||
protected String model;
|
protected String model;
|
||||||
// @JsonSerialize(using = BooleanToJsonFormatFlagSerializer.class)
|
// @JsonSerialize(using = BooleanToJsonFormatFlagSerializer.class)
|
||||||
// this can either be set to format=json or format={"key1": "val1", "key2": "val2"}
|
// this can either be set to format=json or format={"key1": "val1", "key2": "val2"}
|
||||||
@JsonProperty(value = "format", required = false, defaultValue = "json")
|
@JsonProperty(value = "format", required = false, defaultValue = "json")
|
||||||
protected Object format;
|
protected Object format;
|
||||||
protected Map<String, Object> options;
|
protected Map<String, Object> options;
|
||||||
protected String template;
|
protected String template;
|
||||||
protected boolean stream;
|
protected boolean stream;
|
||||||
@JsonProperty(value = "keep_alive")
|
@JsonProperty(value = "keep_alive")
|
||||||
protected String keepAlive;
|
protected String keepAlive;
|
||||||
|
|
||||||
|
|
||||||
public String toString() {
|
public String toString() {
|
||||||
try {
|
try {
|
||||||
return Utils.getObjectMapper().writerWithDefaultPrettyPrinter().writeValueAsString(this);
|
return Utils.getObjectMapper().writerWithDefaultPrettyPrinter().writeValueAsString(this);
|
||||||
} catch (JsonProcessingException e) {
|
} catch (JsonProcessingException e) {
|
||||||
throw new RuntimeException(e);
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -12,43 +12,43 @@ import java.time.OffsetDateTime;
|
|||||||
@JsonIgnoreProperties(ignoreUnknown = true)
|
@JsonIgnoreProperties(ignoreUnknown = true)
|
||||||
public class Model {
|
public class Model {
|
||||||
|
|
||||||
private String name;
|
private String name;
|
||||||
private String model;
|
private String model;
|
||||||
@JsonProperty("modified_at")
|
@JsonProperty("modified_at")
|
||||||
private OffsetDateTime modifiedAt;
|
private OffsetDateTime modifiedAt;
|
||||||
@JsonProperty("expires_at")
|
@JsonProperty("expires_at")
|
||||||
private OffsetDateTime expiresAt;
|
private OffsetDateTime expiresAt;
|
||||||
private String digest;
|
private String digest;
|
||||||
private long size;
|
private long size;
|
||||||
@JsonProperty("details")
|
@JsonProperty("details")
|
||||||
private ModelMeta modelMeta;
|
private ModelMeta modelMeta;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the model name without its version
|
* Returns the model name without its version
|
||||||
*
|
*
|
||||||
* @return model name
|
* @return model name
|
||||||
*/
|
*/
|
||||||
public String getModelName() {
|
public String getModelName() {
|
||||||
return name.split(":")[0];
|
return name.split(":")[0];
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the model version without its name
|
* Returns the model version without its name
|
||||||
*
|
*
|
||||||
* @return model version
|
* @return model version
|
||||||
*/
|
*/
|
||||||
public String getModelVersion() {
|
public String getModelVersion() {
|
||||||
return name.split(":")[1];
|
return name.split(":")[1];
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
try {
|
try {
|
||||||
return Utils.getObjectMapper().writerWithDefaultPrettyPrinter().writeValueAsString(this);
|
return Utils.getObjectMapper().writerWithDefaultPrettyPrinter().writeValueAsString(this);
|
||||||
} catch (JsonProcessingException e) {
|
} catch (JsonProcessingException e) {
|
||||||
throw new RuntimeException(e);
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -9,22 +9,22 @@ import lombok.Data;
|
|||||||
@Data
|
@Data
|
||||||
@JsonIgnoreProperties(ignoreUnknown = true)
|
@JsonIgnoreProperties(ignoreUnknown = true)
|
||||||
public class ModelDetail {
|
public class ModelDetail {
|
||||||
private String license;
|
private String license;
|
||||||
|
|
||||||
@JsonProperty("modelfile")
|
@JsonProperty("modelfile")
|
||||||
private String modelFile;
|
private String modelFile;
|
||||||
|
|
||||||
private String parameters;
|
private String parameters;
|
||||||
private String template;
|
private String template;
|
||||||
private String system;
|
private String system;
|
||||||
private ModelMeta details;
|
private ModelMeta details;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
try {
|
try {
|
||||||
return Utils.getObjectMapper().writerWithDefaultPrettyPrinter().writeValueAsString(this);
|
return Utils.getObjectMapper().writerWithDefaultPrettyPrinter().writeValueAsString(this);
|
||||||
} catch (JsonProcessingException e) {
|
} catch (JsonProcessingException e) {
|
||||||
throw new RuntimeException(e);
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -9,27 +9,27 @@ import lombok.Data;
|
|||||||
@Data
|
@Data
|
||||||
@JsonIgnoreProperties(ignoreUnknown = true)
|
@JsonIgnoreProperties(ignoreUnknown = true)
|
||||||
public class ModelMeta {
|
public class ModelMeta {
|
||||||
@JsonProperty("format")
|
@JsonProperty("format")
|
||||||
private String format;
|
private String format;
|
||||||
|
|
||||||
@JsonProperty("family")
|
@JsonProperty("family")
|
||||||
private String family;
|
private String family;
|
||||||
|
|
||||||
@JsonProperty("families")
|
@JsonProperty("families")
|
||||||
private String[] families;
|
private String[] families;
|
||||||
|
|
||||||
@JsonProperty("parameter_size")
|
@JsonProperty("parameter_size")
|
||||||
private String parameterSize;
|
private String parameterSize;
|
||||||
|
|
||||||
@JsonProperty("quantization_level")
|
@JsonProperty("quantization_level")
|
||||||
private String quantizationLevel;
|
private String quantizationLevel;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
try {
|
try {
|
||||||
return Utils.getObjectMapper().writerWithDefaultPrettyPrinter().writeValueAsString(this);
|
return Utils.getObjectMapper().writerWithDefaultPrettyPrinter().writeValueAsString(this);
|
||||||
} catch (JsonProcessingException e) {
|
} catch (JsonProcessingException e) {
|
||||||
throw new RuntimeException(e);
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -7,5 +7,5 @@ import lombok.Data;
|
|||||||
@JsonIgnoreProperties(ignoreUnknown = true)
|
@JsonIgnoreProperties(ignoreUnknown = true)
|
||||||
public class OllamaErrorResponse {
|
public class OllamaErrorResponse {
|
||||||
|
|
||||||
private String error;
|
private String error;
|
||||||
}
|
}
|
||||||
|
@ -19,67 +19,67 @@ import static io.github.ollama4j.utils.Utils.getObjectMapper;
|
|||||||
@NoArgsConstructor
|
@NoArgsConstructor
|
||||||
@JsonIgnoreProperties(ignoreUnknown = true)
|
@JsonIgnoreProperties(ignoreUnknown = true)
|
||||||
public class OllamaStructuredResult {
|
public class OllamaStructuredResult {
|
||||||
private String response;
|
private String response;
|
||||||
private String thinking;
|
private String thinking;
|
||||||
private int httpStatusCode;
|
private int httpStatusCode;
|
||||||
private long responseTime = 0;
|
private long responseTime = 0;
|
||||||
private String model;
|
private String model;
|
||||||
|
|
||||||
private @JsonProperty("created_at") String createdAt;
|
private @JsonProperty("created_at") String createdAt;
|
||||||
private boolean done;
|
private boolean done;
|
||||||
private @JsonProperty("done_reason") String doneReason;
|
private @JsonProperty("done_reason") String doneReason;
|
||||||
private List<Integer> context;
|
private List<Integer> context;
|
||||||
private @JsonProperty("total_duration") Long totalDuration;
|
private @JsonProperty("total_duration") Long totalDuration;
|
||||||
private @JsonProperty("load_duration") Long loadDuration;
|
private @JsonProperty("load_duration") Long loadDuration;
|
||||||
private @JsonProperty("prompt_eval_count") Integer promptEvalCount;
|
private @JsonProperty("prompt_eval_count") Integer promptEvalCount;
|
||||||
private @JsonProperty("prompt_eval_duration") Long promptEvalDuration;
|
private @JsonProperty("prompt_eval_duration") Long promptEvalDuration;
|
||||||
private @JsonProperty("eval_count") Integer evalCount;
|
private @JsonProperty("eval_count") Integer evalCount;
|
||||||
private @JsonProperty("eval_duration") Long evalDuration;
|
private @JsonProperty("eval_duration") Long evalDuration;
|
||||||
|
|
||||||
public OllamaStructuredResult(String response, long responseTime, int httpStatusCode) {
|
public OllamaStructuredResult(String response, long responseTime, int httpStatusCode) {
|
||||||
this.response = response;
|
this.response = response;
|
||||||
this.responseTime = responseTime;
|
this.responseTime = responseTime;
|
||||||
this.httpStatusCode = httpStatusCode;
|
this.httpStatusCode = httpStatusCode;
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public String toString() {
|
|
||||||
try {
|
|
||||||
return getObjectMapper().writerWithDefaultPrettyPrinter().writeValueAsString(this);
|
|
||||||
} catch (JsonProcessingException e) {
|
|
||||||
throw new RuntimeException(e);
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
@Override
|
||||||
* Get the structured response if the response is a JSON object.
|
public String toString() {
|
||||||
*
|
try {
|
||||||
* @return Map - structured response
|
return getObjectMapper().writerWithDefaultPrettyPrinter().writeValueAsString(this);
|
||||||
*/
|
} catch (JsonProcessingException e) {
|
||||||
public Map<String, Object> getStructuredResponse() {
|
throw new RuntimeException(e);
|
||||||
try {
|
}
|
||||||
Map<String, Object> response = getObjectMapper().readValue(this.getResponse(),
|
|
||||||
new TypeReference<Map<String, Object>>() {
|
|
||||||
});
|
|
||||||
return response;
|
|
||||||
} catch (JsonProcessingException e) {
|
|
||||||
throw new RuntimeException(e);
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get the structured response mapped to a specific class type.
|
* Get the structured response if the response is a JSON object.
|
||||||
*
|
*
|
||||||
* @param <T> The type of class to map the response to
|
* @return Map - structured response
|
||||||
* @param clazz The class to map the response to
|
*/
|
||||||
* @return An instance of the specified class with the response data
|
public Map<String, Object> getStructuredResponse() {
|
||||||
* @throws RuntimeException if there is an error mapping the response
|
try {
|
||||||
*/
|
Map<String, Object> response = getObjectMapper().readValue(this.getResponse(),
|
||||||
public <T> T getStructuredResponse(Class<T> clazz) {
|
new TypeReference<Map<String, Object>>() {
|
||||||
try {
|
});
|
||||||
return getObjectMapper().readValue(this.getResponse(), clazz);
|
return response;
|
||||||
} catch (JsonProcessingException e) {
|
} catch (JsonProcessingException e) {
|
||||||
throw new RuntimeException(e);
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the structured response mapped to a specific class type.
|
||||||
|
*
|
||||||
|
* @param <T> The type of class to map the response to
|
||||||
|
* @param clazz The class to map the response to
|
||||||
|
* @return An instance of the specified class with the response data
|
||||||
|
* @throws RuntimeException if there is an error mapping the response
|
||||||
|
*/
|
||||||
|
public <T> T getStructuredResponse(Class<T> clazz) {
|
||||||
|
try {
|
||||||
|
return getObjectMapper().readValue(this.getResponse(), clazz);
|
||||||
|
} catch (JsonProcessingException e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -11,8 +11,7 @@ import java.util.Map;
|
|||||||
@NoArgsConstructor
|
@NoArgsConstructor
|
||||||
@AllArgsConstructor
|
@AllArgsConstructor
|
||||||
@JsonIgnoreProperties(ignoreUnknown = true)
|
@JsonIgnoreProperties(ignoreUnknown = true)
|
||||||
public class OllamaToolCallsFunction
|
public class OllamaToolCallsFunction {
|
||||||
{
|
|
||||||
private String name;
|
private String name;
|
||||||
private Map<String,Object> arguments;
|
private Map<String, Object> arguments;
|
||||||
}
|
}
|
||||||
|
@ -15,17 +15,17 @@ import java.util.Map;
|
|||||||
@Setter
|
@Setter
|
||||||
@Getter
|
@Getter
|
||||||
@AllArgsConstructor
|
@AllArgsConstructor
|
||||||
public class ReflectionalToolFunction implements ToolFunction{
|
public class ReflectionalToolFunction implements ToolFunction {
|
||||||
|
|
||||||
private Object functionHolder;
|
private Object functionHolder;
|
||||||
private Method function;
|
private Method function;
|
||||||
private LinkedHashMap<String,String> propertyDefinition;
|
private LinkedHashMap<String, String> propertyDefinition;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Object apply(Map<String, Object> arguments) {
|
public Object apply(Map<String, Object> arguments) {
|
||||||
LinkedHashMap<String, Object> argumentsCopy = new LinkedHashMap<>(this.propertyDefinition);
|
LinkedHashMap<String, Object> argumentsCopy = new LinkedHashMap<>(this.propertyDefinition);
|
||||||
for (Map.Entry<String,String> param : this.propertyDefinition.entrySet()){
|
for (Map.Entry<String, String> param : this.propertyDefinition.entrySet()) {
|
||||||
argumentsCopy.replace(param.getKey(),typeCast(arguments.get(param.getKey()),param.getValue()));
|
argumentsCopy.replace(param.getKey(), typeCast(arguments.get(param.getKey()), param.getValue()));
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
return function.invoke(functionHolder, argumentsCopy.values().toArray());
|
return function.invoke(functionHolder, argumentsCopy.values().toArray());
|
||||||
@ -35,7 +35,7 @@ public class ReflectionalToolFunction implements ToolFunction{
|
|||||||
}
|
}
|
||||||
|
|
||||||
private Object typeCast(Object inputValue, String className) {
|
private Object typeCast(Object inputValue, String className) {
|
||||||
if(className == null || inputValue == null) {
|
if (className == null || inputValue == null) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
String inputValueString = inputValue.toString();
|
String inputValueString = inputValue.toString();
|
||||||
|
@ -17,12 +17,12 @@ public interface OllamaRequestBody {
|
|||||||
* @return JSON representation of a OllamaRequest
|
* @return JSON representation of a OllamaRequest
|
||||||
*/
|
*/
|
||||||
@JsonIgnore
|
@JsonIgnore
|
||||||
default BodyPublisher getBodyPublisher(){
|
default BodyPublisher getBodyPublisher() {
|
||||||
try {
|
try {
|
||||||
return BodyPublishers.ofString(
|
return BodyPublishers.ofString(
|
||||||
Utils.getObjectMapper().writeValueAsString(this));
|
Utils.getObjectMapper().writeValueAsString(this));
|
||||||
} catch (JsonProcessingException e) {
|
} catch (JsonProcessingException e) {
|
||||||
throw new IllegalArgumentException("Request not Body convertible.",e);
|
throw new IllegalArgumentException("Request not Body convertible.", e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -4,9 +4,11 @@ import lombok.Data;
|
|||||||
|
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
/** Class for options for Ollama model. */
|
/**
|
||||||
|
* Class for options for Ollama model.
|
||||||
|
*/
|
||||||
@Data
|
@Data
|
||||||
public class Options {
|
public class Options {
|
||||||
|
|
||||||
private final Map<String, Object> optionsMap;
|
private final Map<String, Object> optionsMap;
|
||||||
}
|
}
|
||||||
|
@ -2,247 +2,251 @@ package io.github.ollama4j.utils;
|
|||||||
|
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
|
|
||||||
/** Builder class for creating options for Ollama model. */
|
/**
|
||||||
|
* Builder class for creating options for Ollama model.
|
||||||
|
*/
|
||||||
public class OptionsBuilder {
|
public class OptionsBuilder {
|
||||||
|
|
||||||
private final Options options;
|
private final Options options;
|
||||||
|
|
||||||
/** Constructs a new OptionsBuilder with an empty options map. */
|
/**
|
||||||
public OptionsBuilder() {
|
* Constructs a new OptionsBuilder with an empty options map.
|
||||||
this.options = new Options(new HashMap<>());
|
*/
|
||||||
}
|
public OptionsBuilder() {
|
||||||
|
this.options = new Options(new HashMap<>());
|
||||||
/**
|
}
|
||||||
* Enable Mirostat sampling for controlling perplexity. (default: 0, 0 = disabled, 1 = Mirostat, 2
|
|
||||||
* = Mirostat 2.0)
|
/**
|
||||||
*
|
* Enable Mirostat sampling for controlling perplexity. (default: 0, 0 = disabled, 1 = Mirostat, 2
|
||||||
* @param value The value for the "mirostat" parameter.
|
* = Mirostat 2.0)
|
||||||
* @return The updated OptionsBuilder.
|
*
|
||||||
*/
|
* @param value The value for the "mirostat" parameter.
|
||||||
public OptionsBuilder setMirostat(int value) {
|
* @return The updated OptionsBuilder.
|
||||||
options.getOptionsMap().put("mirostat", value);
|
*/
|
||||||
return this;
|
public OptionsBuilder setMirostat(int value) {
|
||||||
}
|
options.getOptionsMap().put("mirostat", value);
|
||||||
|
return this;
|
||||||
/**
|
}
|
||||||
* Influences how quickly the algorithm responds to feedback from the generated text. A lower
|
|
||||||
* learning rate will result in slower adjustments, while a higher learning rate will make the
|
/**
|
||||||
* algorithm more responsive. (Default: 0.1)
|
* Influences how quickly the algorithm responds to feedback from the generated text. A lower
|
||||||
*
|
* learning rate will result in slower adjustments, while a higher learning rate will make the
|
||||||
* @param value The value for the "mirostat_eta" parameter.
|
* algorithm more responsive. (Default: 0.1)
|
||||||
* @return The updated OptionsBuilder.
|
*
|
||||||
*/
|
* @param value The value for the "mirostat_eta" parameter.
|
||||||
public OptionsBuilder setMirostatEta(float value) {
|
* @return The updated OptionsBuilder.
|
||||||
options.getOptionsMap().put("mirostat_eta", value);
|
*/
|
||||||
return this;
|
public OptionsBuilder setMirostatEta(float value) {
|
||||||
}
|
options.getOptionsMap().put("mirostat_eta", value);
|
||||||
|
return this;
|
||||||
/**
|
}
|
||||||
* Controls the balance between coherence and diversity of the output. A lower value will result
|
|
||||||
* in more focused and coherent text. (Default: 5.0)
|
/**
|
||||||
*
|
* Controls the balance between coherence and diversity of the output. A lower value will result
|
||||||
* @param value The value for the "mirostat_tau" parameter.
|
* in more focused and coherent text. (Default: 5.0)
|
||||||
* @return The updated OptionsBuilder.
|
*
|
||||||
*/
|
* @param value The value for the "mirostat_tau" parameter.
|
||||||
public OptionsBuilder setMirostatTau(float value) {
|
* @return The updated OptionsBuilder.
|
||||||
options.getOptionsMap().put("mirostat_tau", value);
|
*/
|
||||||
return this;
|
public OptionsBuilder setMirostatTau(float value) {
|
||||||
}
|
options.getOptionsMap().put("mirostat_tau", value);
|
||||||
|
return this;
|
||||||
/**
|
}
|
||||||
* Sets the size of the context window used to generate the next token. (Default: 2048)
|
|
||||||
*
|
/**
|
||||||
* @param value The value for the "num_ctx" parameter.
|
* Sets the size of the context window used to generate the next token. (Default: 2048)
|
||||||
* @return The updated OptionsBuilder.
|
*
|
||||||
*/
|
* @param value The value for the "num_ctx" parameter.
|
||||||
public OptionsBuilder setNumCtx(int value) {
|
* @return The updated OptionsBuilder.
|
||||||
options.getOptionsMap().put("num_ctx", value);
|
*/
|
||||||
return this;
|
public OptionsBuilder setNumCtx(int value) {
|
||||||
}
|
options.getOptionsMap().put("num_ctx", value);
|
||||||
|
return this;
|
||||||
/**
|
}
|
||||||
* The number of GQA groups in the transformer layer. Required for some models, for example, it is
|
|
||||||
* 8 for llama2:70b.
|
/**
|
||||||
*
|
* The number of GQA groups in the transformer layer. Required for some models, for example, it is
|
||||||
* @param value The value for the "num_gqa" parameter.
|
* 8 for llama2:70b.
|
||||||
* @return The updated OptionsBuilder.
|
*
|
||||||
*/
|
* @param value The value for the "num_gqa" parameter.
|
||||||
public OptionsBuilder setNumGqa(int value) {
|
* @return The updated OptionsBuilder.
|
||||||
options.getOptionsMap().put("num_gqa", value);
|
*/
|
||||||
return this;
|
public OptionsBuilder setNumGqa(int value) {
|
||||||
}
|
options.getOptionsMap().put("num_gqa", value);
|
||||||
|
return this;
|
||||||
/**
|
}
|
||||||
* The number of layers to send to the GPU(s). On macOS it defaults to 1 to enable metal support,
|
|
||||||
* 0 to disable.
|
/**
|
||||||
*
|
* The number of layers to send to the GPU(s). On macOS it defaults to 1 to enable metal support,
|
||||||
* @param value The value for the "num_gpu" parameter.
|
* 0 to disable.
|
||||||
* @return The updated OptionsBuilder.
|
*
|
||||||
*/
|
* @param value The value for the "num_gpu" parameter.
|
||||||
public OptionsBuilder setNumGpu(int value) {
|
* @return The updated OptionsBuilder.
|
||||||
options.getOptionsMap().put("num_gpu", value);
|
*/
|
||||||
return this;
|
public OptionsBuilder setNumGpu(int value) {
|
||||||
}
|
options.getOptionsMap().put("num_gpu", value);
|
||||||
|
return this;
|
||||||
/**
|
}
|
||||||
* Sets the number of threads to use during computation. By default, Ollama will detect this for
|
|
||||||
* optimal performance. It is recommended to set this value to the number of physical CPU cores
|
/**
|
||||||
* your system has (as opposed to the logical number of cores).
|
* Sets the number of threads to use during computation. By default, Ollama will detect this for
|
||||||
*
|
* optimal performance. It is recommended to set this value to the number of physical CPU cores
|
||||||
* @param value The value for the "num_thread" parameter.
|
* your system has (as opposed to the logical number of cores).
|
||||||
* @return The updated OptionsBuilder.
|
*
|
||||||
*/
|
* @param value The value for the "num_thread" parameter.
|
||||||
public OptionsBuilder setNumThread(int value) {
|
* @return The updated OptionsBuilder.
|
||||||
options.getOptionsMap().put("num_thread", value);
|
*/
|
||||||
return this;
|
public OptionsBuilder setNumThread(int value) {
|
||||||
}
|
options.getOptionsMap().put("num_thread", value);
|
||||||
|
return this;
|
||||||
/**
|
}
|
||||||
* Sets how far back for the model to look back to prevent repetition. (Default: 64, 0 = disabled,
|
|
||||||
* -1 = num_ctx)
|
/**
|
||||||
*
|
* Sets how far back for the model to look back to prevent repetition. (Default: 64, 0 = disabled,
|
||||||
* @param value The value for the "repeat_last_n" parameter.
|
* -1 = num_ctx)
|
||||||
* @return The updated OptionsBuilder.
|
*
|
||||||
*/
|
* @param value The value for the "repeat_last_n" parameter.
|
||||||
public OptionsBuilder setRepeatLastN(int value) {
|
* @return The updated OptionsBuilder.
|
||||||
options.getOptionsMap().put("repeat_last_n", value);
|
*/
|
||||||
return this;
|
public OptionsBuilder setRepeatLastN(int value) {
|
||||||
}
|
options.getOptionsMap().put("repeat_last_n", value);
|
||||||
|
return this;
|
||||||
/**
|
}
|
||||||
* Sets how strongly to penalize repetitions. A higher value (e.g., 1.5) will penalize repetitions
|
|
||||||
* more strongly, while a lower value (e.g., 0.9) will be more lenient. (Default: 1.1)
|
/**
|
||||||
*
|
* Sets how strongly to penalize repetitions. A higher value (e.g., 1.5) will penalize repetitions
|
||||||
* @param value The value for the "repeat_penalty" parameter.
|
* more strongly, while a lower value (e.g., 0.9) will be more lenient. (Default: 1.1)
|
||||||
* @return The updated OptionsBuilder.
|
*
|
||||||
*/
|
* @param value The value for the "repeat_penalty" parameter.
|
||||||
public OptionsBuilder setRepeatPenalty(float value) {
|
* @return The updated OptionsBuilder.
|
||||||
options.getOptionsMap().put("repeat_penalty", value);
|
*/
|
||||||
return this;
|
public OptionsBuilder setRepeatPenalty(float value) {
|
||||||
}
|
options.getOptionsMap().put("repeat_penalty", value);
|
||||||
|
return this;
|
||||||
/**
|
}
|
||||||
* The temperature of the model. Increasing the temperature will make the model answer more
|
|
||||||
* creatively. (Default: 0.8)
|
/**
|
||||||
*
|
* The temperature of the model. Increasing the temperature will make the model answer more
|
||||||
* @param value The value for the "temperature" parameter.
|
* creatively. (Default: 0.8)
|
||||||
* @return The updated OptionsBuilder.
|
*
|
||||||
*/
|
* @param value The value for the "temperature" parameter.
|
||||||
public OptionsBuilder setTemperature(float value) {
|
* @return The updated OptionsBuilder.
|
||||||
options.getOptionsMap().put("temperature", value);
|
*/
|
||||||
return this;
|
public OptionsBuilder setTemperature(float value) {
|
||||||
}
|
options.getOptionsMap().put("temperature", value);
|
||||||
|
return this;
|
||||||
/**
|
}
|
||||||
* Sets the random number seed to use for generation. Setting this to a specific number will make
|
|
||||||
* the model generate the same text for the same prompt. (Default: 0)
|
/**
|
||||||
*
|
* Sets the random number seed to use for generation. Setting this to a specific number will make
|
||||||
* @param value The value for the "seed" parameter.
|
* the model generate the same text for the same prompt. (Default: 0)
|
||||||
* @return The updated OptionsBuilder.
|
*
|
||||||
*/
|
* @param value The value for the "seed" parameter.
|
||||||
public OptionsBuilder setSeed(int value) {
|
* @return The updated OptionsBuilder.
|
||||||
options.getOptionsMap().put("seed", value);
|
*/
|
||||||
return this;
|
public OptionsBuilder setSeed(int value) {
|
||||||
}
|
options.getOptionsMap().put("seed", value);
|
||||||
|
return this;
|
||||||
/**
|
}
|
||||||
* Sets the stop sequences to use. When this pattern is encountered the LLM will stop generating
|
|
||||||
* text and return. Multiple stop patterns may be set by specifying multiple separate `stop`
|
/**
|
||||||
* parameters in a modelfile.
|
* Sets the stop sequences to use. When this pattern is encountered the LLM will stop generating
|
||||||
*
|
* text and return. Multiple stop patterns may be set by specifying multiple separate `stop`
|
||||||
* @param value The value for the "stop" parameter.
|
* parameters in a modelfile.
|
||||||
* @return The updated OptionsBuilder.
|
*
|
||||||
*/
|
* @param value The value for the "stop" parameter.
|
||||||
public OptionsBuilder setStop(String value) {
|
* @return The updated OptionsBuilder.
|
||||||
options.getOptionsMap().put("stop", value);
|
*/
|
||||||
return this;
|
public OptionsBuilder setStop(String value) {
|
||||||
}
|
options.getOptionsMap().put("stop", value);
|
||||||
|
return this;
|
||||||
/**
|
}
|
||||||
* Tail free sampling is used to reduce the impact of less probable tokens from the output. A
|
|
||||||
* higher value (e.g., 2.0) will reduce the impact more, while a value of 1.0 disables this
|
/**
|
||||||
* setting. (default: 1)
|
* Tail free sampling is used to reduce the impact of less probable tokens from the output. A
|
||||||
*
|
* higher value (e.g., 2.0) will reduce the impact more, while a value of 1.0 disables this
|
||||||
* @param value The value for the "tfs_z" parameter.
|
* setting. (default: 1)
|
||||||
* @return The updated OptionsBuilder.
|
*
|
||||||
*/
|
* @param value The value for the "tfs_z" parameter.
|
||||||
public OptionsBuilder setTfsZ(float value) {
|
* @return The updated OptionsBuilder.
|
||||||
options.getOptionsMap().put("tfs_z", value);
|
*/
|
||||||
return this;
|
public OptionsBuilder setTfsZ(float value) {
|
||||||
}
|
options.getOptionsMap().put("tfs_z", value);
|
||||||
|
return this;
|
||||||
/**
|
}
|
||||||
* Maximum number of tokens to predict when generating text. (Default: 128, -1 = infinite
|
|
||||||
* generation, -2 = fill context)
|
/**
|
||||||
*
|
* Maximum number of tokens to predict when generating text. (Default: 128, -1 = infinite
|
||||||
* @param value The value for the "num_predict" parameter.
|
* generation, -2 = fill context)
|
||||||
* @return The updated OptionsBuilder.
|
*
|
||||||
*/
|
* @param value The value for the "num_predict" parameter.
|
||||||
public OptionsBuilder setNumPredict(int value) {
|
* @return The updated OptionsBuilder.
|
||||||
options.getOptionsMap().put("num_predict", value);
|
*/
|
||||||
return this;
|
public OptionsBuilder setNumPredict(int value) {
|
||||||
}
|
options.getOptionsMap().put("num_predict", value);
|
||||||
|
return this;
|
||||||
/**
|
}
|
||||||
* Reduces the probability of generating nonsense. A higher value (e.g. 100) will give more
|
|
||||||
* diverse answers, while a lower value (e.g. 10) will be more conservative. (Default: 40)
|
/**
|
||||||
*
|
* Reduces the probability of generating nonsense. A higher value (e.g. 100) will give more
|
||||||
* @param value The value for the "top_k" parameter.
|
* diverse answers, while a lower value (e.g. 10) will be more conservative. (Default: 40)
|
||||||
* @return The updated OptionsBuilder.
|
*
|
||||||
*/
|
* @param value The value for the "top_k" parameter.
|
||||||
public OptionsBuilder setTopK(int value) {
|
* @return The updated OptionsBuilder.
|
||||||
options.getOptionsMap().put("top_k", value);
|
*/
|
||||||
return this;
|
public OptionsBuilder setTopK(int value) {
|
||||||
}
|
options.getOptionsMap().put("top_k", value);
|
||||||
|
return this;
|
||||||
/**
|
}
|
||||||
* Works together with top-k. A higher value (e.g., 0.95) will lead to more diverse text, while a
|
|
||||||
* lower value (e.g., 0.5) will generate more focused and conservative text. (Default: 0.9)
|
/**
|
||||||
*
|
* Works together with top-k. A higher value (e.g., 0.95) will lead to more diverse text, while a
|
||||||
* @param value The value for the "top_p" parameter.
|
* lower value (e.g., 0.5) will generate more focused and conservative text. (Default: 0.9)
|
||||||
* @return The updated OptionsBuilder.
|
*
|
||||||
*/
|
* @param value The value for the "top_p" parameter.
|
||||||
public OptionsBuilder setTopP(float value) {
|
* @return The updated OptionsBuilder.
|
||||||
options.getOptionsMap().put("top_p", value);
|
*/
|
||||||
return this;
|
public OptionsBuilder setTopP(float value) {
|
||||||
}
|
options.getOptionsMap().put("top_p", value);
|
||||||
|
return this;
|
||||||
/**
|
}
|
||||||
* Alternative to the top_p, and aims to ensure a balance of qualityand variety. The parameter p
|
|
||||||
* represents the minimum probability for a token to be considered, relative to the probability
|
/**
|
||||||
* of the most likely token. For example, with p=0.05 and the most likely token having a
|
* Alternative to the top_p, and aims to ensure a balance of qualityand variety. The parameter p
|
||||||
* probability of 0.9, logits with a value less than 0.045 are filtered out. (Default: 0.0)
|
* represents the minimum probability for a token to be considered, relative to the probability
|
||||||
*/
|
* of the most likely token. For example, with p=0.05 and the most likely token having a
|
||||||
public OptionsBuilder setMinP(float value) {
|
* probability of 0.9, logits with a value less than 0.045 are filtered out. (Default: 0.0)
|
||||||
options.getOptionsMap().put("min_p", value);
|
*/
|
||||||
return this;
|
public OptionsBuilder setMinP(float value) {
|
||||||
}
|
options.getOptionsMap().put("min_p", value);
|
||||||
|
return this;
|
||||||
/**
|
}
|
||||||
* Allows passing an option not formally supported by the library
|
|
||||||
* @param name The option name for the parameter.
|
/**
|
||||||
* @param value The value for the "{name}" parameter.
|
* Allows passing an option not formally supported by the library
|
||||||
* @return The updated OptionsBuilder.
|
*
|
||||||
* @throws IllegalArgumentException if parameter has an unsupported type
|
* @param name The option name for the parameter.
|
||||||
*/
|
* @param value The value for the "{name}" parameter.
|
||||||
public OptionsBuilder setCustomOption(String name, Object value) throws IllegalArgumentException {
|
* @return The updated OptionsBuilder.
|
||||||
if (!(value instanceof Integer || value instanceof Float || value instanceof String)) {
|
* @throws IllegalArgumentException if parameter has an unsupported type
|
||||||
throw new IllegalArgumentException("Invalid type for parameter. Allowed types are: Integer, Float, or String.");
|
*/
|
||||||
|
public OptionsBuilder setCustomOption(String name, Object value) throws IllegalArgumentException {
|
||||||
|
if (!(value instanceof Integer || value instanceof Float || value instanceof String)) {
|
||||||
|
throw new IllegalArgumentException("Invalid type for parameter. Allowed types are: Integer, Float, or String.");
|
||||||
|
}
|
||||||
|
options.getOptionsMap().put(name, value);
|
||||||
|
return this;
|
||||||
}
|
}
|
||||||
options.getOptionsMap().put(name, value);
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
/**
|
* Builds the options map.
|
||||||
* Builds the options map.
|
*
|
||||||
*
|
* @return The populated options map.
|
||||||
* @return The populated options map.
|
*/
|
||||||
*/
|
public Options build() {
|
||||||
public Options build() {
|
return options;
|
||||||
return options;
|
}
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -18,52 +18,54 @@ package io.github.ollama4j.utils;
|
|||||||
*/
|
*/
|
||||||
public class PromptBuilder {
|
public class PromptBuilder {
|
||||||
|
|
||||||
private final StringBuilder prompt;
|
private final StringBuilder prompt;
|
||||||
|
|
||||||
/** Constructs a new {@code PromptBuilder} with an empty prompt. */
|
/**
|
||||||
public PromptBuilder() {
|
* Constructs a new {@code PromptBuilder} with an empty prompt.
|
||||||
this.prompt = new StringBuilder();
|
*/
|
||||||
}
|
public PromptBuilder() {
|
||||||
|
this.prompt = new StringBuilder();
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Appends the specified text to the prompt.
|
* Appends the specified text to the prompt.
|
||||||
*
|
*
|
||||||
* @param text the text to be added to the prompt
|
* @param text the text to be added to the prompt
|
||||||
* @return a reference to this {@code PromptBuilder} instance for method chaining
|
* @return a reference to this {@code PromptBuilder} instance for method chaining
|
||||||
*/
|
*/
|
||||||
public PromptBuilder add(String text) {
|
public PromptBuilder add(String text) {
|
||||||
prompt.append(text);
|
prompt.append(text);
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Appends the specified text followed by a newline character to the prompt.
|
* Appends the specified text followed by a newline character to the prompt.
|
||||||
*
|
*
|
||||||
* @param text the text to be added as a line to the prompt
|
* @param text the text to be added as a line to the prompt
|
||||||
* @return a reference to this {@code PromptBuilder} instance for method chaining
|
* @return a reference to this {@code PromptBuilder} instance for method chaining
|
||||||
*/
|
*/
|
||||||
public PromptBuilder addLine(String text) {
|
public PromptBuilder addLine(String text) {
|
||||||
prompt.append(text).append("\n");
|
prompt.append(text).append("\n");
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Appends a separator line to the prompt. The separator is a newline followed by a line of
|
* Appends a separator line to the prompt. The separator is a newline followed by a line of
|
||||||
* dashes.
|
* dashes.
|
||||||
*
|
*
|
||||||
* @return a reference to this {@code PromptBuilder} instance for method chaining
|
* @return a reference to this {@code PromptBuilder} instance for method chaining
|
||||||
*/
|
*/
|
||||||
public PromptBuilder addSeparator() {
|
public PromptBuilder addSeparator() {
|
||||||
prompt.append("\n--------------------------------------------------\n");
|
prompt.append("\n--------------------------------------------------\n");
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Builds and returns the final prompt as a string.
|
* Builds and returns the final prompt as a string.
|
||||||
*
|
*
|
||||||
* @return the final prompt as a string
|
* @return the final prompt as a string
|
||||||
*/
|
*/
|
||||||
public String build() {
|
public String build() {
|
||||||
return prompt.toString();
|
return prompt.toString();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -4,8 +4,8 @@ import io.github.ollama4j.OllamaAPI;
|
|||||||
import io.github.ollama4j.exceptions.OllamaBaseException;
|
import io.github.ollama4j.exceptions.OllamaBaseException;
|
||||||
import io.github.ollama4j.exceptions.ToolInvocationException;
|
import io.github.ollama4j.exceptions.ToolInvocationException;
|
||||||
import io.github.ollama4j.models.chat.*;
|
import io.github.ollama4j.models.chat.*;
|
||||||
|
import io.github.ollama4j.models.embeddings.OllamaEmbedRequestModel;
|
||||||
import io.github.ollama4j.models.embeddings.OllamaEmbedResponseModel;
|
import io.github.ollama4j.models.embeddings.OllamaEmbedResponseModel;
|
||||||
import io.github.ollama4j.models.response.LibraryModel;
|
|
||||||
import io.github.ollama4j.models.response.Model;
|
import io.github.ollama4j.models.response.Model;
|
||||||
import io.github.ollama4j.models.response.ModelDetail;
|
import io.github.ollama4j.models.response.ModelDetail;
|
||||||
import io.github.ollama4j.models.response.OllamaResult;
|
import io.github.ollama4j.models.response.OllamaResult;
|
||||||
@ -113,15 +113,6 @@ class OllamaAPIIntegrationTest {
|
|||||||
assertTrue(models.size() >= 0, "Models list should not be empty");
|
assertTrue(models.size() >= 0, "Models list should not be empty");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
|
||||||
@Order(2)
|
|
||||||
void testListModelsFromLibrary()
|
|
||||||
throws OllamaBaseException, IOException, URISyntaxException, InterruptedException {
|
|
||||||
List<LibraryModel> models = api.listModelsFromLibrary();
|
|
||||||
assertNotNull(models);
|
|
||||||
assertFalse(models.isEmpty());
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@Order(3)
|
@Order(3)
|
||||||
void testPullModelAPI() throws URISyntaxException, IOException, OllamaBaseException, InterruptedException {
|
void testPullModelAPI() throws URISyntaxException, IOException, OllamaBaseException, InterruptedException {
|
||||||
@ -144,8 +135,10 @@ class OllamaAPIIntegrationTest {
|
|||||||
@Order(5)
|
@Order(5)
|
||||||
void testEmbeddings() throws Exception {
|
void testEmbeddings() throws Exception {
|
||||||
api.pullModel(EMBEDDING_MODEL);
|
api.pullModel(EMBEDDING_MODEL);
|
||||||
OllamaEmbedResponseModel embeddings = api.embed(EMBEDDING_MODEL,
|
OllamaEmbedRequestModel m = new OllamaEmbedRequestModel();
|
||||||
Arrays.asList("Why is the sky blue?", "Why is the grass green?"));
|
m.setModel(EMBEDDING_MODEL);
|
||||||
|
m.setInput(Arrays.asList("Why is the sky blue?", "Why is the grass green?"));
|
||||||
|
OllamaEmbedResponseModel embeddings = api.embed(m);
|
||||||
assertNotNull(embeddings, "Embeddings should not be null");
|
assertNotNull(embeddings, "Embeddings should not be null");
|
||||||
assertFalse(embeddings.getEmbeddings().isEmpty(), "Embeddings should not be empty");
|
assertFalse(embeddings.getEmbeddings().isEmpty(), "Embeddings should not be empty");
|
||||||
}
|
}
|
||||||
@ -228,7 +221,7 @@ class OllamaAPIIntegrationTest {
|
|||||||
requestModel = builder.withMessages(requestModel.getMessages())
|
requestModel = builder.withMessages(requestModel.getMessages())
|
||||||
.withMessage(OllamaChatMessageRole.USER, "Give me a cool name")
|
.withMessage(OllamaChatMessageRole.USER, "Give me a cool name")
|
||||||
.withOptions(new OptionsBuilder().setTemperature(0.5f).build()).build();
|
.withOptions(new OptionsBuilder().setTemperature(0.5f).build()).build();
|
||||||
OllamaChatResult chatResult = api.chat(requestModel);
|
OllamaChatResult chatResult = api.chat(requestModel, null);
|
||||||
|
|
||||||
assertNotNull(chatResult);
|
assertNotNull(chatResult);
|
||||||
assertNotNull(chatResult.getResponseModel());
|
assertNotNull(chatResult.getResponseModel());
|
||||||
@ -249,7 +242,7 @@ class OllamaAPIIntegrationTest {
|
|||||||
expectedResponse)).withMessage(OllamaChatMessageRole.USER, "Who are you?")
|
expectedResponse)).withMessage(OllamaChatMessageRole.USER, "Who are you?")
|
||||||
.withOptions(new OptionsBuilder().setTemperature(0.0f).build()).build();
|
.withOptions(new OptionsBuilder().setTemperature(0.0f).build()).build();
|
||||||
|
|
||||||
OllamaChatResult chatResult = api.chat(requestModel);
|
OllamaChatResult chatResult = api.chat(requestModel, null);
|
||||||
assertNotNull(chatResult);
|
assertNotNull(chatResult);
|
||||||
assertNotNull(chatResult.getResponseModel());
|
assertNotNull(chatResult.getResponseModel());
|
||||||
assertNotNull(chatResult.getResponseModel().getMessage());
|
assertNotNull(chatResult.getResponseModel().getMessage());
|
||||||
@ -270,7 +263,7 @@ class OllamaAPIIntegrationTest {
|
|||||||
.build();
|
.build();
|
||||||
|
|
||||||
// Start conversation with model
|
// Start conversation with model
|
||||||
OllamaChatResult chatResult = api.chat(requestModel);
|
OllamaChatResult chatResult = api.chat(requestModel, null);
|
||||||
|
|
||||||
assertTrue(chatResult.getChatHistory().stream().anyMatch(chat -> chat.getContent().contains("2")),
|
assertTrue(chatResult.getChatHistory().stream().anyMatch(chat -> chat.getContent().contains("2")),
|
||||||
"Expected chat history to contain '2'");
|
"Expected chat history to contain '2'");
|
||||||
@ -279,7 +272,7 @@ class OllamaAPIIntegrationTest {
|
|||||||
.withMessage(OllamaChatMessageRole.USER, "And what is its squared value?").build();
|
.withMessage(OllamaChatMessageRole.USER, "And what is its squared value?").build();
|
||||||
|
|
||||||
// Continue conversation with model
|
// Continue conversation with model
|
||||||
chatResult = api.chat(requestModel);
|
chatResult = api.chat(requestModel, null);
|
||||||
|
|
||||||
assertTrue(chatResult.getChatHistory().stream().anyMatch(chat -> chat.getContent().contains("4")),
|
assertTrue(chatResult.getChatHistory().stream().anyMatch(chat -> chat.getContent().contains("4")),
|
||||||
"Expected chat history to contain '4'");
|
"Expected chat history to contain '4'");
|
||||||
@ -289,7 +282,7 @@ class OllamaAPIIntegrationTest {
|
|||||||
"What is the largest value between 2, 4 and 6?").build();
|
"What is the largest value between 2, 4 and 6?").build();
|
||||||
|
|
||||||
// Continue conversation with the model for the third question
|
// Continue conversation with the model for the third question
|
||||||
chatResult = api.chat(requestModel);
|
chatResult = api.chat(requestModel, null);
|
||||||
|
|
||||||
// verify the result
|
// verify the result
|
||||||
assertNotNull(chatResult, "Chat result should not be null");
|
assertNotNull(chatResult, "Chat result should not be null");
|
||||||
@ -315,7 +308,7 @@ class OllamaAPIIntegrationTest {
|
|||||||
"Give me the ID and address of the employee Rahul Kumar.").build();
|
"Give me the ID and address of the employee Rahul Kumar.").build();
|
||||||
requestModel.setOptions(new OptionsBuilder().setTemperature(0.9f).build().getOptionsMap());
|
requestModel.setOptions(new OptionsBuilder().setTemperature(0.9f).build().getOptionsMap());
|
||||||
|
|
||||||
OllamaChatResult chatResult = api.chat(requestModel);
|
OllamaChatResult chatResult = api.chat(requestModel, null);
|
||||||
|
|
||||||
assertNotNull(chatResult, "chatResult should not be null");
|
assertNotNull(chatResult, "chatResult should not be null");
|
||||||
assertNotNull(chatResult.getResponseModel(), "Response model should not be null");
|
assertNotNull(chatResult.getResponseModel(), "Response model should not be null");
|
||||||
@ -357,7 +350,7 @@ class OllamaAPIIntegrationTest {
|
|||||||
.build();
|
.build();
|
||||||
requestModel.setOptions(new OptionsBuilder().setTemperature(0.9f).build().getOptionsMap());
|
requestModel.setOptions(new OptionsBuilder().setTemperature(0.9f).build().getOptionsMap());
|
||||||
|
|
||||||
OllamaChatResult chatResult = api.chat(requestModel);
|
OllamaChatResult chatResult = api.chat(requestModel, null);
|
||||||
|
|
||||||
assertNotNull(chatResult, "chatResult should not be null");
|
assertNotNull(chatResult, "chatResult should not be null");
|
||||||
assertNotNull(chatResult.getResponseModel(), "Response model should not be null");
|
assertNotNull(chatResult.getResponseModel(), "Response model should not be null");
|
||||||
@ -405,11 +398,11 @@ class OllamaAPIIntegrationTest {
|
|||||||
.withKeepAlive("0m").withOptions(new OptionsBuilder().setTemperature(0.9f).build())
|
.withKeepAlive("0m").withOptions(new OptionsBuilder().setTemperature(0.9f).build())
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
OllamaChatResult chatResult = api.chat(requestModel, (s) -> {
|
OllamaChatResult chatResult = api.chat(requestModel, new OllamaChatStreamObserver((s) -> {
|
||||||
LOG.info(s.toUpperCase());
|
LOG.info(s.toUpperCase());
|
||||||
}, (s) -> {
|
}, (s) -> {
|
||||||
LOG.info(s.toLowerCase());
|
LOG.info(s.toLowerCase());
|
||||||
});
|
}));
|
||||||
|
|
||||||
assertNotNull(chatResult, "chatResult should not be null");
|
assertNotNull(chatResult, "chatResult should not be null");
|
||||||
assertNotNull(chatResult.getResponseModel(), "Response model should not be null");
|
assertNotNull(chatResult.getResponseModel(), "Response model should not be null");
|
||||||
@ -447,7 +440,7 @@ class OllamaAPIIntegrationTest {
|
|||||||
"Compute the most important constant in the world using 5 digits")
|
"Compute the most important constant in the world using 5 digits")
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
OllamaChatResult chatResult = api.chat(requestModel);
|
OllamaChatResult chatResult = api.chat(requestModel, null);
|
||||||
assertNotNull(chatResult);
|
assertNotNull(chatResult);
|
||||||
assertNotNull(chatResult.getResponseModel());
|
assertNotNull(chatResult.getResponseModel());
|
||||||
assertNotNull(chatResult.getResponseModel().getMessage());
|
assertNotNull(chatResult.getResponseModel().getMessage());
|
||||||
@ -480,7 +473,7 @@ class OllamaAPIIntegrationTest {
|
|||||||
"Greet Rahul with a lot of hearts and respond to me with count of emojis that have been in used in the greeting")
|
"Greet Rahul with a lot of hearts and respond to me with count of emojis that have been in used in the greeting")
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
OllamaChatResult chatResult = api.chat(requestModel);
|
OllamaChatResult chatResult = api.chat(requestModel, null);
|
||||||
assertNotNull(chatResult);
|
assertNotNull(chatResult);
|
||||||
assertNotNull(chatResult.getResponseModel());
|
assertNotNull(chatResult.getResponseModel());
|
||||||
assertNotNull(chatResult.getResponseModel().getMessage());
|
assertNotNull(chatResult.getResponseModel().getMessage());
|
||||||
@ -515,13 +508,11 @@ class OllamaAPIIntegrationTest {
|
|||||||
requestModel.setThink(false);
|
requestModel.setThink(false);
|
||||||
StringBuffer sb = new StringBuffer();
|
StringBuffer sb = new StringBuffer();
|
||||||
|
|
||||||
OllamaChatResult chatResult = api.chat(requestModel, (s) -> {
|
OllamaChatResult chatResult = api.chat(requestModel, new OllamaChatStreamObserver((s) -> {
|
||||||
LOG.info(s.toUpperCase());
|
LOG.info(s.toUpperCase());
|
||||||
sb.append(s);
|
|
||||||
}, (s) -> {
|
}, (s) -> {
|
||||||
LOG.info(s.toLowerCase());
|
LOG.info(s.toLowerCase());
|
||||||
sb.append(s);
|
}));
|
||||||
});
|
|
||||||
assertNotNull(chatResult);
|
assertNotNull(chatResult);
|
||||||
assertNotNull(chatResult.getResponseModel());
|
assertNotNull(chatResult.getResponseModel());
|
||||||
assertNotNull(chatResult.getResponseModel().getMessage());
|
assertNotNull(chatResult.getResponseModel().getMessage());
|
||||||
@ -540,13 +531,11 @@ class OllamaAPIIntegrationTest {
|
|||||||
.withThinking(true).withKeepAlive("0m").build();
|
.withThinking(true).withKeepAlive("0m").build();
|
||||||
StringBuffer sb = new StringBuffer();
|
StringBuffer sb = new StringBuffer();
|
||||||
|
|
||||||
OllamaChatResult chatResult = api.chat(requestModel, (s) -> {
|
OllamaChatResult chatResult = api.chat(requestModel, new OllamaChatStreamObserver((s) -> {
|
||||||
sb.append(s);
|
|
||||||
LOG.info(s.toUpperCase());
|
LOG.info(s.toUpperCase());
|
||||||
}, (s) -> {
|
}, (s) -> {
|
||||||
sb.append(s);
|
|
||||||
LOG.info(s.toLowerCase());
|
LOG.info(s.toLowerCase());
|
||||||
});
|
}));
|
||||||
|
|
||||||
assertNotNull(chatResult);
|
assertNotNull(chatResult);
|
||||||
assertNotNull(chatResult.getResponseModel());
|
assertNotNull(chatResult.getResponseModel());
|
||||||
@ -569,7 +558,7 @@ class OllamaAPIIntegrationTest {
|
|||||||
.build();
|
.build();
|
||||||
api.registerAnnotatedTools(new OllamaAPIIntegrationTest());
|
api.registerAnnotatedTools(new OllamaAPIIntegrationTest());
|
||||||
|
|
||||||
OllamaChatResult chatResult = api.chat(requestModel);
|
OllamaChatResult chatResult = api.chat(requestModel, null);
|
||||||
assertNotNull(chatResult);
|
assertNotNull(chatResult);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -583,7 +572,7 @@ class OllamaAPIIntegrationTest {
|
|||||||
"What's in the picture?", Collections.emptyList(),
|
"What's in the picture?", Collections.emptyList(),
|
||||||
List.of(getImageFileFromClasspath("emoji-smile.jpeg"))).build();
|
List.of(getImageFileFromClasspath("emoji-smile.jpeg"))).build();
|
||||||
|
|
||||||
OllamaChatResult chatResult = api.chat(requestModel);
|
OllamaChatResult chatResult = api.chat(requestModel, null);
|
||||||
assertNotNull(chatResult);
|
assertNotNull(chatResult);
|
||||||
assertNotNull(chatResult.getResponseModel());
|
assertNotNull(chatResult.getResponseModel());
|
||||||
builder.reset();
|
builder.reset();
|
||||||
@ -591,7 +580,7 @@ class OllamaAPIIntegrationTest {
|
|||||||
requestModel = builder.withMessages(chatResult.getChatHistory())
|
requestModel = builder.withMessages(chatResult.getChatHistory())
|
||||||
.withMessage(OllamaChatMessageRole.USER, "What's the color?").build();
|
.withMessage(OllamaChatMessageRole.USER, "What's the color?").build();
|
||||||
|
|
||||||
chatResult = api.chat(requestModel);
|
chatResult = api.chat(requestModel, null);
|
||||||
assertNotNull(chatResult);
|
assertNotNull(chatResult);
|
||||||
assertNotNull(chatResult.getResponseModel());
|
assertNotNull(chatResult.getResponseModel());
|
||||||
}
|
}
|
||||||
|
@ -4,7 +4,8 @@ import io.github.ollama4j.models.request.BasicAuth;
|
|||||||
import io.github.ollama4j.models.request.BearerAuth;
|
import io.github.ollama4j.models.request.BearerAuth;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
import static org.junit.jupiter.api.Assertions.*;
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||||
|
|
||||||
class TestAuth {
|
class TestAuth {
|
||||||
|
|
||||||
|
@ -96,9 +96,12 @@ class TestMockedAPIs {
|
|||||||
String model = OllamaModelType.LLAMA2;
|
String model = OllamaModelType.LLAMA2;
|
||||||
String prompt = "some prompt text";
|
String prompt = "some prompt text";
|
||||||
try {
|
try {
|
||||||
when(ollamaAPI.generateEmbeddings(model, prompt)).thenReturn(new ArrayList<>());
|
OllamaEmbedRequestModel m = new OllamaEmbedRequestModel();
|
||||||
ollamaAPI.generateEmbeddings(model, prompt);
|
m.setModel(model);
|
||||||
verify(ollamaAPI, times(1)).generateEmbeddings(model, prompt);
|
m.setInput(List.of(prompt));
|
||||||
|
when(ollamaAPI.embed(m)).thenReturn(new OllamaEmbedResponseModel());
|
||||||
|
ollamaAPI.embed(m);
|
||||||
|
verify(ollamaAPI, times(1)).embed(m);
|
||||||
} catch (IOException | OllamaBaseException | InterruptedException e) {
|
} catch (IOException | OllamaBaseException | InterruptedException e) {
|
||||||
throw new RuntimeException(e);
|
throw new RuntimeException(e);
|
||||||
}
|
}
|
||||||
@ -110,9 +113,10 @@ class TestMockedAPIs {
|
|||||||
String model = OllamaModelType.LLAMA2;
|
String model = OllamaModelType.LLAMA2;
|
||||||
List<String> inputs = List.of("some prompt text");
|
List<String> inputs = List.of("some prompt text");
|
||||||
try {
|
try {
|
||||||
when(ollamaAPI.embed(model, inputs)).thenReturn(new OllamaEmbedResponseModel());
|
OllamaEmbedRequestModel m = new OllamaEmbedRequestModel(model, inputs);
|
||||||
ollamaAPI.embed(model, inputs);
|
when(ollamaAPI.embed(m)).thenReturn(new OllamaEmbedResponseModel());
|
||||||
verify(ollamaAPI, times(1)).embed(model, inputs);
|
ollamaAPI.embed(m);
|
||||||
|
verify(ollamaAPI, times(1)).embed(m);
|
||||||
} catch (IOException | OllamaBaseException | InterruptedException e) {
|
} catch (IOException | OllamaBaseException | InterruptedException e) {
|
||||||
throw new RuntimeException(e);
|
throw new RuntimeException(e);
|
||||||
}
|
}
|
||||||
|
@ -5,7 +5,8 @@ import io.github.ollama4j.models.chat.OllamaChatMessageRole;
|
|||||||
import org.json.JSONObject;
|
import org.json.JSONObject;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
import static org.junit.jupiter.api.Assertions.*;
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||||
|
|
||||||
class TestOllamaChatMessage {
|
class TestOllamaChatMessage {
|
||||||
|
|
||||||
|
@ -7,7 +7,7 @@ import org.junit.jupiter.api.Test;
|
|||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
import static org.junit.jupiter.api.Assertions.*;
|
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||||
|
|
||||||
class TestToolsPromptBuilder {
|
class TestToolsPromptBuilder {
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user