Ollama4j
A Java library (wrapper/binding) for Ollama server.
Loading...
Searching...
No Matches
io.github.ollama4j.Ollama Class Reference

Public Member Functions

 Ollama ()
 
 Ollama (String host)
 
void setBasicAuth (String username, String password)
 
void setBearerAuth (String bearerToken)
 
boolean ping () throws OllamaException
 
ModelProcessesResult ps () throws OllamaException
 
List< ModellistModels () throws OllamaException
 
String getVersion () throws OllamaException
 
void pullModel (String modelName) throws OllamaException
 
ModelDetail getModelDetails (String modelName) throws OllamaException
 
void createModel (CustomModelRequest customModelRequest) throws OllamaException
 
void deleteModel (String modelName, boolean ignoreIfNotPresent) throws OllamaException
 
void unloadModel (String modelName) throws OllamaException
 
OllamaEmbedResult embed (OllamaEmbedRequest modelRequest) throws OllamaException
 
OllamaResult generate (OllamaGenerateRequest request, OllamaGenerateStreamObserver streamObserver) throws OllamaException
 
OllamaAsyncResultStreamer generateAsync (String model, String prompt, boolean raw, boolean think) throws OllamaException
 
OllamaChatResult chat (OllamaChatRequest request, OllamaChatTokenHandler tokenHandler) throws OllamaException
 
void registerTool (Tools.Tool tool)
 
void registerTools (List< Tools.Tool > tools)
 
void deregisterTools ()
 
void registerAnnotatedTools () throws OllamaException
 
void registerAnnotatedTools (Object object)
 
OllamaChatMessageRole addCustomRole (String roleName)
 
List< OllamaChatMessageRolelistRoles ()
 
OllamaChatMessageRole getRole (String roleName) throws RoleNotFoundException
 

Detailed Description

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.

Constructor & Destructor Documentation

◆ Ollama() [1/2]

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.

◆ Ollama() [2/2]

io.github.ollama4j.Ollama.Ollama ( String host)

Instantiates the Ollama API with a specified Ollama host address.

Parameters
hostthe host address of the Ollama server

Definition at line 119 of file Ollama.java.

Member Function Documentation

◆ addCustomRole()

OllamaChatMessageRole io.github.ollama4j.Ollama.addCustomRole ( String roleName)

Adds a custom role.

Parameters
roleNamethe name of the custom role to be added
Returns
the newly created OllamaChatMessageRole

Definition at line 1078 of file Ollama.java.

◆ chat()

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.

Parameters
requestrequest object to be sent to the server
tokenHandlercallback handler to handle the last token from stream (caution: the previous tokens from stream will not be concatenated)
Returns
OllamaChatResult
Exceptions
OllamaExceptionif the response indicates an error status

Definition at line 870 of file Ollama.java.

◆ createModel()

void io.github.ollama4j.Ollama.createModel ( CustomModelRequest customModelRequest) throws OllamaException

Creates a custom model. Read more about custom model creation here.

Parameters
customModelRequestcustom model spec
Exceptions
OllamaExceptionif the response indicates an error status

Definition at line 544 of file Ollama.java.

◆ deleteModel()

void io.github.ollama4j.Ollama.deleteModel ( String modelName,
boolean ignoreIfNotPresent ) throws OllamaException

Deletes a model from the Ollama server.

Parameters
modelNamethe name of the model to be deleted
ignoreIfNotPresentignore errors if the specified model is not present on the Ollama server
Exceptions
OllamaExceptionif the response indicates an error status

Definition at line 608 of file Ollama.java.

◆ deregisterTools()

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.

◆ embed()

OllamaEmbedResult io.github.ollama4j.Ollama.embed ( OllamaEmbedRequest modelRequest) throws OllamaException

Generate embeddings using a OllamaEmbedRequest.

Parameters
modelRequestrequest for '/api/embed' endpoint
Returns
embeddings
Exceptions
OllamaExceptionif the response indicates an error status

Definition at line 722 of file Ollama.java.

◆ generate()

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.

Parameters
requestthe generation request
streamObserverthe stream observer for streaming responses, or null for synchronous
Returns
the result of the generation
Exceptions
OllamaExceptionif the request fails

Definition at line 766 of file Ollama.java.

◆ generateAsync()

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.

Parameters
modelthe model name
promptthe prompt to send
rawwhether to use raw mode
thinkwhether to use "think" mode
Returns
an OllamaAsyncResultStreamer for streaming results
Exceptions
OllamaExceptionif the request fails

Definition at line 833 of file Ollama.java.

◆ getModelDetails()

ModelDetail io.github.ollama4j.Ollama.getModelDetails ( String modelName) throws OllamaException

Gets model details from the Ollama server.

Parameters
modelNamethe model name
Returns
the model details
Exceptions
OllamaExceptionif the response indicates an error status

Definition at line 499 of file Ollama.java.

◆ getRole()

OllamaChatMessageRole io.github.ollama4j.Ollama.getRole ( String roleName) throws RoleNotFoundException

Retrieves a specific role by name.

Parameters
roleNamethe name of the role to retrieve
Returns
the OllamaChatMessageRole associated with the given name
Exceptions
RoleNotFoundExceptionif the role with the specified name does not exist

Definition at line 1098 of file Ollama.java.

◆ getVersion()

String io.github.ollama4j.Ollama.getVersion ( ) throws OllamaException

Gets the Ollama server version.

Returns
the version string
Exceptions
OllamaExceptionif the request fails

Definition at line 410 of file Ollama.java.

◆ listModels()

List< Model > io.github.ollama4j.Ollama.listModels ( ) throws OllamaException

Lists available models from the Ollama server.

Returns
a list of models available on the server
Exceptions
OllamaExceptionif the response indicates an error status

Definition at line 241 of file Ollama.java.

◆ listRoles()

List< OllamaChatMessageRole > io.github.ollama4j.Ollama.listRoles ( )

Lists all available roles.

Returns
a list of available OllamaChatMessageRole objects

Definition at line 1087 of file Ollama.java.

◆ ping()

boolean io.github.ollama4j.Ollama.ping ( ) throws OllamaException

Checks the reachability of the Ollama server.

Returns
true if the server is reachable, false otherwise
Exceptions
OllamaExceptionif the ping fails

Definition at line 153 of file Ollama.java.

◆ ps()

ModelProcessesResult io.github.ollama4j.Ollama.ps ( ) throws OllamaException

Provides a list of running models and details about each model currently loaded into memory.

Returns
ModelsProcessResult containing details about the running models
Exceptions
OllamaExceptionif the response indicates an error status

Definition at line 192 of file Ollama.java.

◆ pullModel()

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.

Parameters
modelNamethe name/tag of the model to be pulled. Ex: llama3:latest
Exceptions
OllamaExceptionif the response indicates an error status

Definition at line 457 of file Ollama.java.

◆ registerAnnotatedTools() [1/2]

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.

Exceptions
OllamaExceptionif 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.

◆ registerAnnotatedTools() [2/2]

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.

Parameters
objectthe object whose methods are to be inspected for annotated tools
Exceptions
RuntimeExceptionif any reflection-based instantiation or invocation fails

Definition at line 1022 of file Ollama.java.

◆ registerTool()

void io.github.ollama4j.Ollama.registerTool ( Tools.Tool tool)

Registers a single tool in the tool registry.

Parameters
toolthe tool to register. Contains the tool's specification and function.

Definition at line 952 of file Ollama.java.

◆ registerTools()

void io.github.ollama4j.Ollama.registerTools ( List< Tools.Tool > tools)

Registers multiple tools in the tool registry.

Parameters
toolsa list of Tools.Tool objects to register. Each tool contains its specification and function.

Definition at line 963 of file Ollama.java.

◆ setBasicAuth()

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.

Parameters
usernamethe username
passwordthe password

Definition at line 134 of file Ollama.java.

◆ setBearerAuth()

void io.github.ollama4j.Ollama.setBearerAuth ( String bearerToken)

Set Bearer authentication for accessing an Ollama server that's behind a reverse-proxy/gateway.

Parameters
bearerTokenthe Bearer authentication token to provide

Definition at line 143 of file Ollama.java.

◆ unloadModel()

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.

Parameters
modelNamethe name of the model to unload
Exceptions
OllamaExceptionif the response indicates an error status

Definition at line 662 of file Ollama.java.


The documentation for this class was generated from the following file: