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

Classes

class  MCPToolConfig
 
class  MCPToolInfo
 
class  MCPToolsConfig
 
class  OllamaMCPTool
 
class  OllamaMCPToolMatchResponse
 

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
 
void pullModel (String modelName, ModelPullListener listener) throws OllamaException
 
ModelDetail getModelDetails (String modelName) throws OllamaException
 
void createModel (CustomModelRequest customModelRequest) throws OllamaException
 
void createModel (CustomModelRequest customModelRequest, ModelPullListener listener) 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
 
OllamaImageResult generateImage (OllamaGenerateImageRequest request) throws OllamaException
 
OllamaAsyncResultStreamer generateAsync (String model, String prompt, boolean raw, ThinkMode think) throws OllamaException
 
OllamaChatResult chat (OllamaChatRequest request, OllamaChatTokenHandler tokenHandler) throws OllamaException
 
void registerTool (Tools.Tool tool)
 
void registerTools (List< Tools.Tool > tools)
 
List< Tools.Tool > getRegisteredTools ()
 
void deregisterTools ()
 
void registerAnnotatedTools () throws OllamaException
 
void registerAnnotatedTools (Object object)
 
OllamaChatMessageRole addCustomRole (String roleName)
 
List< OllamaChatMessageRolelistRoles ()
 
OllamaChatMessageRole getRole (String roleName) throws RoleNotFoundException
 
void loadMCPToolsFromJson (String mcpConfigJsonFilePath) throws IOException
 

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 69 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 126 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 135 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 1339 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 OllamaChatRequest#builder().

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 1126 of file Ollama.java.

◆ createModel() [1/2]

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 637 of file Ollama.java.

◆ createModel() [2/2]

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

Creates a custom model and notifies the listener of progress.

Parameters
customModelRequestcustom model spec
listenerthe listener for progress updates
Exceptions
OllamaExceptionif the response indicates an error status

Definition at line 648 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 729 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 1232 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 861 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 914 of file Ollama.java.

◆ generateAsync()

OllamaAsyncResultStreamer io.github.ollama4j.Ollama.generateAsync ( String model,
String prompt,
boolean raw,
ThinkMode 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 1089 of file Ollama.java.

◆ generateImage()

OllamaImageResult io.github.ollama4j.Ollama.generateImage ( OllamaGenerateImageRequest request) throws OllamaException

Generates an image using a OllamaGenerateImageRequest.

Note: Image generation is experimental and may change in future versions as mentioned in the Ollama documentation

Parameters
requestthe image generation request
Returns
the image generation result
Exceptions
OllamaExceptionif the request fails

Definition at line 949 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 583 of file Ollama.java.

◆ getRegisteredTools()

List< Tools.Tool > io.github.ollama4j.Ollama.getRegisteredTools ( )

Definition at line 1224 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 1359 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 473 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 275 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 1348 of file Ollama.java.

◆ loadMCPToolsFromJson()

void io.github.ollama4j.Ollama.loadMCPToolsFromJson ( String mcpConfigJsonFilePath) throws IOException

Definition at line 1363 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 169 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 217 of file Ollama.java.

◆ pullModel() [1/2]

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 529 of file Ollama.java.

◆ pullModel() [2/2]

void io.github.ollama4j.Ollama.pullModel ( String modelName,
ModelPullListener listener ) throws OllamaException

Pulls a model using the specified Ollama library model tag and notifies the listener of progress.

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

Definition at line 541 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 1245 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 1283 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 1209 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 1220 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 150 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 159 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 792 of file Ollama.java.


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