![]() |
Ollama4j
A Java library (wrapper/binding) for Ollama server.
|
The main API class for interacting with the Ollama server.
This class provides methods for model management, chat, embeddings, tool registration, and more.
Definition at line 56 of file Ollama.java.
io.github.ollama4j.Ollama.Ollama | ( | ) |
Instantiates the Ollama API with the default Ollama host: http://localhost:11434
Definition at line 110 of file Ollama.java.
io.github.ollama4j.Ollama.Ollama | ( | String | host | ) |
Instantiates the Ollama API with a specified Ollama host address.
host | the host address of the Ollama server |
Definition at line 119 of file Ollama.java.
OllamaChatMessageRole io.github.ollama4j.Ollama.addCustomRole | ( | String | roleName | ) |
Adds a custom role.
roleName | the name of the custom role to be added |
Definition at line 1078 of file Ollama.java.
OllamaChatResult io.github.ollama4j.Ollama.chat | ( | OllamaChatRequest | request, |
OllamaChatTokenHandler | tokenHandler ) throws OllamaException |
Sends a chat request to a model using an OllamaChatRequest
and sets up streaming response. This can be constructed using an OllamaChatRequestBuilder
.
Note: the OllamaChatRequestModel#getStream() property is not implemented.
request | request object to be sent to the server |
tokenHandler | callback handler to handle the last token from stream (caution: the previous tokens from stream will not be concatenated) |
OllamaChatResult
OllamaException | if the response indicates an error status |
Definition at line 870 of file Ollama.java.
void io.github.ollama4j.Ollama.createModel | ( | CustomModelRequest | customModelRequest | ) | throws OllamaException |
Creates a custom model. Read more about custom model creation here.
customModelRequest | custom model spec |
OllamaException | if the response indicates an error status |
Definition at line 544 of file Ollama.java.
void io.github.ollama4j.Ollama.deleteModel | ( | String | modelName, |
boolean | ignoreIfNotPresent ) throws OllamaException |
Deletes a model from the Ollama server.
modelName | the name of the model to be deleted |
ignoreIfNotPresent | ignore errors if the specified model is not present on the Ollama server |
OllamaException | if the response indicates an error status |
Definition at line 608 of file Ollama.java.
void io.github.ollama4j.Ollama.deregisterTools | ( | ) |
Deregisters all tools from the tool registry. This method removes all registered tools, effectively clearing the registry.
Definition at line 971 of file Ollama.java.
OllamaEmbedResult io.github.ollama4j.Ollama.embed | ( | OllamaEmbedRequest | modelRequest | ) | throws OllamaException |
Generate embeddings using a OllamaEmbedRequest
.
modelRequest | request for '/api/embed' endpoint |
OllamaException | if the response indicates an error status |
Definition at line 722 of file Ollama.java.
OllamaResult io.github.ollama4j.Ollama.generate | ( | OllamaGenerateRequest | request, |
OllamaGenerateStreamObserver | streamObserver ) throws OllamaException |
Generates a response from a model using the specified parameters and stream observer. If streamObserver
is provided, streaming is enabled; otherwise, a synchronous call is made.
request | the generation request |
streamObserver | the stream observer for streaming responses, or null for synchronous |
OllamaException | if the request fails |
Definition at line 766 of file Ollama.java.
OllamaAsyncResultStreamer io.github.ollama4j.Ollama.generateAsync | ( | String | model, |
String | prompt, | ||
boolean | raw, | ||
boolean | think ) throws OllamaException |
Generates a response from a model asynchronously, returning a streamer for results.
model | the model name |
prompt | the prompt to send |
raw | whether to use raw mode |
think | whether to use "think" mode |
OllamaException | if the request fails |
Definition at line 833 of file Ollama.java.
ModelDetail io.github.ollama4j.Ollama.getModelDetails | ( | String | modelName | ) | throws OllamaException |
Gets model details from the Ollama server.
modelName | the model name |
OllamaException | if the response indicates an error status |
Definition at line 499 of file Ollama.java.
OllamaChatMessageRole io.github.ollama4j.Ollama.getRole | ( | String | roleName | ) | throws RoleNotFoundException |
Retrieves a specific role by name.
roleName | the name of the role to retrieve |
RoleNotFoundException | if the role with the specified name does not exist |
Definition at line 1098 of file Ollama.java.
String io.github.ollama4j.Ollama.getVersion | ( | ) | throws OllamaException |
Gets the Ollama server version.
OllamaException | if the request fails |
Definition at line 410 of file Ollama.java.
List< Model > io.github.ollama4j.Ollama.listModels | ( | ) | throws OllamaException |
Lists available models from the Ollama server.
OllamaException | if the response indicates an error status |
Definition at line 241 of file Ollama.java.
List< OllamaChatMessageRole > io.github.ollama4j.Ollama.listRoles | ( | ) |
Lists all available roles.
Definition at line 1087 of file Ollama.java.
boolean io.github.ollama4j.Ollama.ping | ( | ) | throws OllamaException |
Checks the reachability of the Ollama server.
OllamaException | if the ping fails |
Definition at line 153 of file Ollama.java.
ModelProcessesResult io.github.ollama4j.Ollama.ps | ( | ) | throws OllamaException |
Provides a list of running models and details about each model currently loaded into memory.
OllamaException | if the response indicates an error status |
Definition at line 192 of file Ollama.java.
void io.github.ollama4j.Ollama.pullModel | ( | String | modelName | ) | throws OllamaException |
Pulls a model using the specified Ollama library model tag. The model is identified by a name and a tag, which are combined into a single identifier in the format "name:tag" to pull the corresponding model.
modelName | the name/tag of the model to be pulled. Ex: llama3:latest |
OllamaException | if the response indicates an error status |
Definition at line 457 of file Ollama.java.
void io.github.ollama4j.Ollama.registerAnnotatedTools | ( | ) | throws OllamaException |
Registers tools based on the annotations found on the methods of the caller's class and its providers. This method scans the caller's class for the OllamaToolService
annotation and recursively registers annotated tools from all the providers specified in the annotation.
OllamaException | if the caller's class is not annotated with OllamaToolService or if reflection-based instantiation or invocation fails |
Definition at line 984 of file Ollama.java.
void io.github.ollama4j.Ollama.registerAnnotatedTools | ( | Object | object | ) |
Registers tools based on the annotations found on the methods of the provided object. This method scans the methods of the given object and registers tools using the ToolSpec
annotation and associated ToolProperty
annotations. It constructs tool specifications and stores them in a tool registry.
object | the object whose methods are to be inspected for annotated tools |
RuntimeException | if any reflection-based instantiation or invocation fails |
Definition at line 1022 of file Ollama.java.
void io.github.ollama4j.Ollama.registerTool | ( | Tools.Tool | tool | ) |
Registers a single tool in the tool registry.
tool | the tool to register. Contains the tool's specification and function. |
Definition at line 952 of file Ollama.java.
void io.github.ollama4j.Ollama.registerTools | ( | List< Tools.Tool > | tools | ) |
Registers multiple tools in the tool registry.
tools | a list of Tools.Tool objects to register. Each tool contains its specification and function. |
Definition at line 963 of file Ollama.java.
void io.github.ollama4j.Ollama.setBasicAuth | ( | String | username, |
String | password ) |
Set basic authentication for accessing an Ollama server that's behind a reverse-proxy/gateway.
username | the username |
password | the password |
Definition at line 134 of file Ollama.java.
void io.github.ollama4j.Ollama.setBearerAuth | ( | String | bearerToken | ) |
Set Bearer authentication for accessing an Ollama server that's behind a reverse-proxy/gateway.
bearerToken | the Bearer authentication token to provide |
Definition at line 143 of file Ollama.java.
void io.github.ollama4j.Ollama.unloadModel | ( | String | modelName | ) | throws OllamaException |
Unloads a model from memory.
If an empty prompt is provided and the keep_alive parameter is set to 0, a model will be unloaded from memory.
modelName | the name of the model to unload |
OllamaException | if the response indicates an error status |
Definition at line 662 of file Ollama.java.