Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 60 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ 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 streaming completions (chatting), listing local models, pulling new
models, show model information, creating new models, copying models, deleting models, pushing models, and generating
embeddings. ``
- 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. `✗`

Expand Down Expand Up @@ -83,9 +83,7 @@ String prompt = "Why is the sky blue?";

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

System.out.

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

**OUTPUTS:** The sky appears blue because of a phenomenon called Rayleigh scattering...
Expand Down Expand Up @@ -122,6 +120,62 @@ boolean success = ollama.delete("llama2-backup");

*Returns* `true` *if the deletion was successful.*

### 2.7 Chat with a model

```java
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

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

### 2.9 Create a model

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

### 2.10 Pull a model

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

### 2.11 Push a model

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

### 2.12 Generate embeddings

```java
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

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

### 2.14 Server version

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

## 4 Credits

Structure of the readme is inspired from [Ollama Sharp](https://github.com/awaescher/OllamaSharp)
Expand Down
17 changes: 13 additions & 4 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,14 @@

<dependencies>
<dependency>
<groupId>com.github.Asedem</groupId>
<artifactId>ConnectionAPI</artifactId>
<version>5ffcfef027</version>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.14.2</version>
</dependency>
<dependency>
<groupId>org.jetbrains</groupId>
<artifactId>annotations</artifactId>
<version>24.0.1</version>
</dependency>

<dependency>
Expand All @@ -48,10 +53,14 @@
<artifactId>maven-compiler-plugin</artifactId>
<version>3.13.0</version>
</plugin>

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.5.0</version>
<configuration>
<argLine>@{argLine} -Dnet.bytebuddy.experimental=true</argLine>
</configuration>
</plugin>

<plugin>
Expand All @@ -73,7 +82,7 @@
</execution>
<execution>
<id>check</id>
<phase>test</phase>
<phase>verify</phase>
<goals>
<goal>check</goal>
</goals>
Expand Down
63 changes: 62 additions & 1 deletion src/main/java/de/asedem/Ollama.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,21 @@
package de.asedem;

import de.asedem.model.ChatRequest;
import de.asedem.model.ChatResponse;
import de.asedem.model.CreateRequest;
import de.asedem.model.CreateResponse;
import de.asedem.model.EmbedRequest;
import de.asedem.model.EmbedResponse;
import de.asedem.model.GenerationRequest;
import de.asedem.model.GenerationResponse;
import de.asedem.model.Model;
import de.asedem.model.ModelInfo;
import de.asedem.model.ProcessModel;
import de.asedem.model.PullRequest;
import de.asedem.model.PullResponse;
import de.asedem.model.PushRequest;
import de.asedem.model.PushResponse;
import de.asedem.model.VersionResponse;
import de.asedem.service.*;
import org.jetbrains.annotations.NotNull;

Expand All @@ -20,7 +32,14 @@ public record Ollama(
GenerateService,
ShowInfoService,
CopyService,
DeleteService {
DeleteService,
ChatService,
CreateService,
PullService,
PushService,
EmbedService,
RunningModelsService,
VersionService {

public static Ollama initDefault() {
return new Ollama("http://127.0.0.1", 11434);
Expand Down Expand Up @@ -61,4 +80,46 @@ public boolean copy(@NotNull String source, @NotNull String destination) {
public boolean delete(@NotNull String modelName) {
return DeleteService.super.delete(this, modelName);
}

@NotNull
@Override
public ChatResponse chat(@NotNull ChatRequest request) {
return ChatService.super.chat(this, request);
}

@NotNull
@Override
public CreateResponse create(@NotNull CreateRequest request) {
return CreateService.super.create(this, request);
}

@NotNull
@Override
public PullResponse pull(@NotNull PullRequest request) {
return PullService.super.pull(this, request);
}

@NotNull
@Override
public PushResponse push(@NotNull PushRequest request) {
return PushService.super.push(this, request);
}

@NotNull
@Override
public EmbedResponse embed(@NotNull EmbedRequest request) {
return EmbedService.super.embed(this, request);
}

@NotNull
@Override
public List<ProcessModel> runningModels() {
return RunningModelsService.super.runningModels(this);
}

@NotNull
@Override
public VersionResponse version() {
return VersionService.super.version(this);
}
}
23 changes: 23 additions & 0 deletions src/main/java/de/asedem/model/ChatRequest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package de.asedem.model;

import com.fasterxml.jackson.annotation.JsonInclude;

import java.util.List;
import java.util.Map;

@JsonInclude(JsonInclude.Include.NON_NULL)
public record ChatRequest(
String model,
List<Message> messages,
boolean stream,
Object format,
Map<String, Object> options,
Object think,
Object keep_alive,
List<Tool> tools
) {

public ChatRequest(String model, List<Message> messages) {
this(model, messages, false, null, null, null, null, null);
}
}
26 changes: 26 additions & 0 deletions src/main/java/de/asedem/model/ChatResponse.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package de.asedem.model;

import com.fasterxml.jackson.annotation.JsonProperty;

public record ChatResponse(
String model,
String created_at,
Message message,
boolean done,
@JsonProperty("done_reason")
String doneReason,
int[] context,
@JsonProperty("total_duration")
long totalDuration,
@JsonProperty("load_duration")
long loadDuration,
@JsonProperty("prompt_eval_count")
long promptEvalCount,
@JsonProperty("prompt_eval_duration")
long promptEvalDuration,
@JsonProperty("eval_count")
long evalCount,
@JsonProperty("eval_duration")
long evalDuration
) {
}
28 changes: 28 additions & 0 deletions src/main/java/de/asedem/model/CreateRequest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package de.asedem.model;

import com.fasterxml.jackson.annotation.JsonInclude;

import java.util.List;
import java.util.Map;

@JsonInclude(JsonInclude.Include.NON_NULL)
public record CreateRequest(
String model,
String from,
Map<String, String> files,
Map<String, String> adapters,
String template,
String renderer,
String parser,
Object license,
String system,
Map<String, Object> parameters,
List<Message> messages,
boolean stream,
String quantize
) {

public CreateRequest(String model, String from) {
this(model, from, null, null, null, null, null, null, null, null, null, false, null);
}
}
6 changes: 6 additions & 0 deletions src/main/java/de/asedem/model/CreateResponse.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package de.asedem.model;

public record CreateResponse(
String status
) {
}
21 changes: 21 additions & 0 deletions src/main/java/de/asedem/model/EmbedRequest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package de.asedem.model;

import com.fasterxml.jackson.annotation.JsonInclude;

import java.util.List;
import java.util.Map;

@JsonInclude(JsonInclude.Include.NON_NULL)
public record EmbedRequest(
String model,
Object input,
Boolean truncate,
Integer dimensions,
Object keep_alive,
Map<String, Object> options
) {

public EmbedRequest(String model, Object input) {
this(model, input, null, null, null, null);
}
}
17 changes: 17 additions & 0 deletions src/main/java/de/asedem/model/EmbedResponse.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package de.asedem.model;

import com.fasterxml.jackson.annotation.JsonProperty;

import java.util.List;

public record EmbedResponse(
String model,
List<List<Double>> embeddings,
@JsonProperty("total_duration")
long totalDuration,
@JsonProperty("load_duration")
long loadDuration,
@JsonProperty("prompt_eval_count")
long promptEvalCount
) {
}
17 changes: 17 additions & 0 deletions src/main/java/de/asedem/model/Message.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package de.asedem.model;

import com.fasterxml.jackson.annotation.JsonInclude;

import java.util.List;
import java.util.Map;

@JsonInclude(JsonInclude.Include.NON_NULL)
public record Message(
String role,
String content,
List<String> images,
String thinking,
List<ToolCall> tool_calls,
String tool_name
) {
}
11 changes: 10 additions & 1 deletion src/main/java/de/asedem/model/ModelInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,20 @@

import com.fasterxml.jackson.annotation.JsonProperty;

import java.util.List;
import java.util.Map;

public record ModelInfo(
String license,
@JsonProperty("modelfile")
String modelFile,
String parameters,
String template
String template,
Model.ModelDetails details,
@JsonProperty("model_info")
Map<String, Object> modelInfo,
List<String> capabilities,
@JsonProperty("projector_info")
Map<String, Object> projectorInfo
) {
}
16 changes: 16 additions & 0 deletions src/main/java/de/asedem/model/ProcessModel.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package de.asedem.model;

import com.fasterxml.jackson.annotation.JsonProperty;

public record ProcessModel(
String name,
String model,
long size,
String digest,
Model.ModelDetails details,
@JsonProperty("expires_at")
String expiresAt,
@JsonProperty("size_vram")
long sizeVram
) {
}
Loading
Loading