Class OllamaAPI
- java.lang.Object
-
- io.github.amithkoujalgi.ollama4j.core.OllamaAPI
-
public class OllamaAPI extends Object
The base Ollama API class.
-
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description OllamaResultask(String model, String prompt)Ask a question to a model running on Ollama server.OllamaResultask(String model, String prompt, Options options)Ask a question to a model running on Ollama server.OllamaAsyncResultCallbackaskAsync(String model, String prompt)Ask a question to a model running on Ollama server and get a callback handle that can be used to check for status and get the response from the model later.OllamaResultaskWithImageFiles(String model, String prompt, List<File> imageFiles)With one or more image files, ask a question to a model running on Ollama server.OllamaResultaskWithImageURLs(String model, String prompt, List<String> imageURLs)With one or more image URLs, ask a question to a model running on Ollama server.voidcreateModelWithFilePath(String modelName, String modelFilePath)Create a custom model from a model file.voidcreateModelWithModelFileContents(String modelName, String modelFileContents)Create a custom model from a model file.voiddeleteModel(String modelName, boolean ignoreIfNotPresent)Delete a model from Ollama server.List<Double>generateEmbeddings(String model, String prompt)Generate embeddings for a given text from a modelModelDetailgetModelDetails(String modelName)Gets model details from the Ollama server.List<Model>listModels()List available models from Ollama server.booleanping()API to check the reachability of Ollama server.voidpullModel(String modelName)Pull a model on the Ollama server from the list of available models.voidsetBasicAuth(String username, String password)Set basic authentication for accessing Ollama server that's behind a reverse-proxy/gateway.voidsetRequestTimeoutSeconds(long requestTimeoutSeconds)Set request timeout in seconds.voidsetVerbose(boolean verbose)Set/unset logging of responses
-
-
-
Constructor Detail
-
OllamaAPI
public OllamaAPI(String host)
Instantiates the Ollama API.- Parameters:
host- the host address of Ollama server
-
-
Method Detail
-
setRequestTimeoutSeconds
public void setRequestTimeoutSeconds(long requestTimeoutSeconds)
Set request timeout in seconds. Default is 3 seconds.- Parameters:
requestTimeoutSeconds- the request timeout in seconds
-
setVerbose
public void setVerbose(boolean verbose)
Set/unset logging of responses- Parameters:
verbose- true/false
-
setBasicAuth
public void setBasicAuth(String username, String password)
Set basic authentication for accessing Ollama server that's behind a reverse-proxy/gateway.- Parameters:
username- the usernamepassword- the password
-
ping
public boolean ping()
API to check the reachability of Ollama server.- Returns:
- true if the server is reachable, false otherwise.
-
listModels
public List<Model> listModels() throws OllamaBaseException, IOException, InterruptedException, URISyntaxException
List available models from Ollama server.- Returns:
- the list
- Throws:
OllamaBaseExceptionIOExceptionInterruptedExceptionURISyntaxException
-
pullModel
public void pullModel(String modelName) throws OllamaBaseException, IOException, URISyntaxException, InterruptedException
Pull a model on the Ollama server from the list of available models.- Parameters:
modelName- the name of the model- Throws:
OllamaBaseExceptionIOExceptionURISyntaxExceptionInterruptedException
-
getModelDetails
public ModelDetail getModelDetails(String modelName) throws IOException, OllamaBaseException, InterruptedException, URISyntaxException
Gets model details from the Ollama server.- Parameters:
modelName- the model- Returns:
- the model details
- Throws:
IOExceptionOllamaBaseExceptionInterruptedExceptionURISyntaxException
-
createModelWithFilePath
public void createModelWithFilePath(String modelName, String modelFilePath) throws IOException, InterruptedException, OllamaBaseException, URISyntaxException
Create a custom model from a model file. Read more about custom model file creation here.- Parameters:
modelName- the name of the custom model to be created.modelFilePath- the path to model file that exists on the Ollama server.- Throws:
IOExceptionInterruptedExceptionOllamaBaseExceptionURISyntaxException
-
createModelWithModelFileContents
public void createModelWithModelFileContents(String modelName, String modelFileContents) throws IOException, InterruptedException, OllamaBaseException, URISyntaxException
Create a custom model from a model file. Read more about custom model file creation here.- Parameters:
modelName- the name of the custom model to be created.modelFileContents- the path to model file that exists on the Ollama server.- Throws:
IOExceptionInterruptedExceptionOllamaBaseExceptionURISyntaxException
-
deleteModel
public void deleteModel(String modelName, boolean ignoreIfNotPresent) throws IOException, InterruptedException, OllamaBaseException, URISyntaxException
Delete a model from Ollama server.- Parameters:
modelName- the name of the model to be deleted.ignoreIfNotPresent- ignore errors if the specified model is not present on Ollama server.- Throws:
IOExceptionInterruptedExceptionOllamaBaseExceptionURISyntaxException
-
generateEmbeddings
public List<Double> generateEmbeddings(String model, String prompt) throws IOException, InterruptedException, OllamaBaseException
Generate embeddings for a given text from a model- Parameters:
model- name of model to generate embeddings fromprompt- text to generate embeddings for- Returns:
- embeddings
- Throws:
IOExceptionInterruptedExceptionOllamaBaseException
-
ask
public OllamaResult ask(String model, String prompt) throws OllamaBaseException, IOException, InterruptedException
Ask a question to a model running on Ollama server. This is a sync/blocking call.- Parameters:
model- the ollama model to ask the question toprompt- the prompt/question text- Returns:
- OllamaResult that includes response text and time taken for response
- Throws:
OllamaBaseExceptionIOExceptionInterruptedException
-
ask
public OllamaResult ask(String model, String prompt, Options options) throws OllamaBaseException, IOException, InterruptedException
Ask a question to a model running on Ollama server. This is a sync/blocking call.- Parameters:
model- the ollama model to ask the question toprompt- the prompt/question textoptions- Options like for example temperature or top_p- Returns:
- OllamaResult that includes response text and time taken for response
- Throws:
OllamaBaseExceptionIOExceptionInterruptedException
-
askAsync
public OllamaAsyncResultCallback askAsync(String model, String prompt)
Ask a question to a model running on Ollama server and get a callback handle that can be used to check for status and get the response from the model later. This would be an async/non-blocking call.- Parameters:
model- the ollama model to ask the question toprompt- the prompt/question text- Returns:
- the ollama async result callback handle
-
askWithImageFiles
public OllamaResult askWithImageFiles(String model, String prompt, List<File> imageFiles) throws OllamaBaseException, IOException, InterruptedException
With one or more image files, ask a question to a model running on Ollama server. This is a sync/blocking call.- Parameters:
model- the ollama model to ask the question toprompt- the prompt/question textimageFiles- the list of image files to use for the question- Returns:
- OllamaResult that includes response text and time taken for response
- Throws:
OllamaBaseExceptionIOExceptionInterruptedException
-
askWithImageURLs
public OllamaResult askWithImageURLs(String model, String prompt, List<String> imageURLs) throws OllamaBaseException, IOException, InterruptedException, URISyntaxException
With one or more image URLs, ask a question to a model running on Ollama server. This is a sync/blocking call.- Parameters:
model- the ollama model to ask the question toprompt- the prompt/question textimageURLs- the list of image URLs to use for the question- Returns:
- OllamaResult that includes response text and time taken for response
- Throws:
OllamaBaseExceptionIOExceptionInterruptedExceptionURISyntaxException
-
-