Skip to content

Latest commit

 

History

History
184 lines (122 loc) · 4.02 KB

File metadata and controls

184 lines (122 loc) · 4.02 KB

OllamaJavaAPI

Coverage

OllamaJavaAPI is a Java binding for the Ollama API, making it easy to interact with Ollama using you favourite Java variation.

This API is by far not finished and many features are missing by now...

1 Features

  • Intuitive API client: Set up and interact with Ollama in just a few lines of code.
  • Support for various Ollama operations: Including completions, chatting, listing local models, listing running models, showing model information, creating new models, copying models, deleting models, pulling models, pushing models, generating embeddings, and retrieving the server version.
  • Real-time streaming: Stream responses directly to your application.
  • Progress reporting: Get real-time progress feedback on tasks like model pulling.

2 Installation

Maven

Repository

<repositories>
    <repository>
        <id>jitpack.io</id>
        <url>https://jitpack.io</url>
    </repository>
</repositories>

Dependency

<dependency>
    <groupId>com.github.Asedem</groupId>
    <artifactId>OllamaJavaAPI</artifactId>
    <version>master-SNAPSHOT</version>
</dependency>

Gradle

Repository

repositories {
    maven { url 'https://jitpack.io' }
}

Dependency

dependencies {
    implementation 'com.github.Asedem:OllamaJavaAPI:master-SNAPSHOT'
}

3 Usage

These examples use poor error handling for simplicity, but you should handle errors properly in your code.

2.1 Initialize Ollama

// By default, it will connect to localhost:11434
Ollama ollama = Ollama.initDefault();

// For custom values
Ollama ollama = Ollama.init("http://localhost", 11434);

2.2 Completion generation

String model = "llama2:latest";
String prompt = "Why is the sky blue?";

GenerationResponse response = ollama.generate(new GenerationRequest(model, prompt));

System.out.println(response.response());

OUTPUTS: The sky appears blue because of a phenomenon called Rayleigh scattering...

2.3 List local models

List<Model> models = ollama.listModels();

Returns a List of Model objects.

2.4 Show model information

ModelInfo modelInfo = ollama.showInfo("llama2:latest");

Returns a ModelInfo object.

2.5 Copy a model

boolean success = ollama.copy("llama2:latest", "llama2-backup");

Returns true if the copy process was successful.

2.6 Delete a model

boolean success = ollama.delete("llama2-backup");

Returns true if the deletion was successful.

2.7 Chat with a model

import de.asedem.model.*;

List<Message> messages = List.of(new Message("user", "Why is the sky blue?", null, null, null, null));
ChatResponse response = ollama.chat(new ChatRequest("llama3.2", messages));

System.out.println(response.message().content());

2.8 Show model information

// with optional verbose flag
ModelInfo modelInfo = ollama.showInfo("llava", true);

2.9 Create a model

CreateResponse response = ollama.create(new CreateRequest("mario", "llama3.2"));

2.10 Pull a model

PullResponse response = ollama.pull(new PullRequest("llama3.2"));

2.11 Push a model

PushResponse response = ollama.push(new PushRequest("mattw/pygmalion:latest"));

2.12 Generate embeddings

EmbedResponse response = ollama.embed(new EmbedRequest("all-minilm", "Why is the sky blue?"));

The input may also be a List<String> for multiple inputs.

2.13 List running models

List<ProcessModel> models = ollama.runningModels();

2.14 Server version

String version = ollama.version().version();

4 Credits

Structure of the readme is inspired from Ollama Sharp and ollama-rs.

Icon and name were reused from the amazing Ollama project.