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...
- Intuitive API client: Set up and interact with Ollama in just a few lines of code.
✓
- Support for various Ollama operations: Including streaming completions (chatting), listing local models, pulling new
models, show model information, creating new models, copying models, deleting models, pushing models, and generating
embeddings.
✗
- Real-time streaming: Stream responses directly to your application.
✗
- Progress reporting: Get real-time progress feedback on tasks like model pulling.
✗
<repositories>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
</repositories>
<dependency>
<groupId>com.github.Asedem</groupId>
<artifactId>OllamaJavaAPI</artifactId>
<version>master-SNAPSHOT</version>
</dependency>
repositories {
maven { url 'https://jitpack.io' }
}
dependencies {
implementation 'com.github.Asedem:OllamaJavaAPI:master-SNAPSHOT'
}
These examples use poor error handling for simplicity, but you should handle errors properly in your code.
// By default, it will connect to localhost:11434
Ollama ollama = Ollama.initDefault();
// For custom values
Ollama ollama = Ollama.init("http://localhost", 11434);
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...
List<Model> models = ollama.listModels();
Returns a List
of Model
objects.
ModelInfo modelInfo = ollama.showInfo("llama2:latest");
Returns a ModelInfo
object.
boolean success = ollama.copy("llama2:latest", "llama2-backup");
Returns true
if the copy process was successfully.
boolean success = ollama.delete("llama2-backup");
Returns true
if the copy process was successfully.
Structure of the readme is inspired from Ollama Sharp and ollama-rs.
Icon and name were reused from the amazing Ollama project.