Skip to content
Closed
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
25 changes: 17 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,15 @@ Let's make your `@Beans` intelligent!
:warning:

### Breaking Changes

December 20, 2023 Update

Refactor the Ollama client and related classes and package names

- Replace the org.springframework.ai.ollama.client.OllamaClient by org.springframework.ai.ollama.OllamaChatClient.
- The OllamaChatClient method signatures have changed.
- Rename the org.springframework.ai.autoconfigure.ollama.OllamaProperties into org.springframework.ai.autoconfigure.ollama.OllamaChatProperties and change the suffix to: `spring.ai.ollama.chat`. Some of the properties have changed as well.

December 19, 2023 Update

Renaming of AiClient and related classes and packagenames
Expand All @@ -22,7 +31,7 @@ Renaming of AiClient and related classes and packagenames
* Rename AiStreamClient to StreamingChatClient
* Rename package org.sf.ai.client to org.sf.ai.chat

Rename artifact ID of
Rename artifact ID of

* `transformers-embedding` to `spring-ai-transformers`

Expand Down Expand Up @@ -144,7 +153,7 @@ Following vector stores are supported:

```xml
<dependency>
<groupId>org.springframework.ai</groupId>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-azure-vector-store-spring-boot-starter</artifactId>
<version>0.8.0-SNAPSHOT</version>
</dependency>
Expand All @@ -154,7 +163,7 @@ Following vector stores are supported:

```xml
<dependency>
<groupId>org.springframework.ai</groupId>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-chroma-store-spring-boot-starter</artifactId>
<version>0.8.0-SNAPSHOT</version>
</dependency>
Expand All @@ -163,7 +172,7 @@ Following vector stores are supported:

```xml
<dependency>
<groupId>org.springframework.ai</groupId>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-milvus-store-spring-boot-starter</artifactId>
<version>0.8.0-SNAPSHOT</version>
</dependency>
Expand All @@ -173,7 +182,7 @@ Following vector stores are supported:

```xml
<dependency>
<groupId>org.springframework.ai</groupId>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-pgvector-store-spring-boot-starter</artifactId>
<version>0.8.0-SNAPSHOT</version>
</dependency>
Expand All @@ -182,7 +191,7 @@ Following vector stores are supported:

```xml
<dependency>
<groupId>org.springframework.ai</groupId>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-pinecone-store-spring-boot-starter</artifactId>
<version>0.8.0-SNAPSHOT</version>
</dependency>
Expand All @@ -191,7 +200,7 @@ Following vector stores are supported:

```xml
<dependency>
<groupId>org.springframework.ai</groupId>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-weaviate-store-spring-boot-starter</artifactId>
<version>0.8.0-SNAPSHOT</version>
</dependency>
Expand All @@ -200,7 +209,7 @@ Following vector stores are supported:

```xml
<dependency>
<groupId>org.springframework.ai</groupId>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-neo4j-store-spring-boot-starter</artifactId>
<version>0.8.0-SNAPSHOT</version>
</dependency>
Expand Down
120 changes: 120 additions & 0 deletions models/spring-ai-ollama/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
# 1. Ollama Chat and Embedding

## 1.1 OllamaApi

[OllamaApi](./src/main/java/org/springframework/ai/ollama/api/OllamaApi.java) provides is lightweight Java client for [Ollama models](https://ollama.ai/).

The OllamaApi provides the Chat completion as well as Embedding endpoints.

Following class diagram illustrates the OllamaApi interface and building blocks for chat completion:

![OllamaApi Class Diagram](./src/test/resources/doc/Ollama%20Chat%20API.jpg)

The OllamaApi can supports all [Ollama Models](https://ollama.ai/library) providing synchronous chat completion, streaming chat completion and embedding:

```java
ChatResponse chat(ChatRequest chatRequest)

Flux<ChatResponse> streamingChat(ChatRequest chatRequest)

EmbeddingResponse embeddings(EmbeddingRequest embeddingRequest)
```

> NOTE: OllamaApi expose also the Ollama `generation` endpoint but later if inferior compared to the Ollama `chat` endpoint.

The `OllamaApiOptions` is helper class used as type-safe option builder. It provides `toMap` to convert the content into `Map<String, Object>`.

Here is a simple snippet how to use the OllamaApi programmatically:

```java
var request = ChatRequest.builder("orca-mini")
.withStream(false)
.withMessages(List.of(Message.builder(Role.user)
.withContent("What is the capital of Bulgaria and what is the size? " + "What it the national anthem?")
.build()))
.withOptions(Options.builder().withTemperature(0.9f).build())
.build();

ChatResponse response = ollamaApi.chat(request);
```

```java
var request = ChatRequest.builder("orca-mini")
.withStream(true)
.withMessages(List.of(Message.builder(Role.user)
.withContent("What is the capital of Bulgaria and what is the size? " + "What it the national anthem?")
.build()))
.withOptions(Options.builder().withTemperature(0.9f).build().toMap())
.build();

Flux<ChatResponse> response = ollamaApi.streamingChat(request);

List<ChatResponse> responses = response.collectList().block();
```

```java
EmbeddingRequest request = new EmbeddingRequest("orca-mini", "I like to eat apples");

EmbeddingResponse response = ollamaApi.embeddings(request);
```

## 1.2 OllamaChatClient and OllamaEmbeddingClient

The [OllamaChatClient](./src/main/java/org/springframework/ai/ollama/OllamaChatClient.java) implements the Spring-Ai `ChatClient` and `StreamingChatClient` interfaces.

The [OllamaEmbeddingClient](./src/main/java/org/springframework/ai/ollama/OllamaEmbeddingClient.java) implements the Spring AI `EmbeddingClient` interface.

Both the OllamaChatClient and the OllamaEmbeddingClient leverage the `OllamaApi`.

You can configure the clients like this:

```java
@Bean
public OllamaApi ollamaApi() {
return new OllamaApi(baseUrl);
}

@Bean
public OllamaChatClient ollamaChat(OllamaApi ollamaApi) {
return new OllamaChatClient(ollamaApi).withModel(MODEL)
.withOptions(OllamaApiOptions.Options.builder().withTemperature(0.9f).build());
}

@Bean
public OllamaEmbeddingClient ollamaEmbedding(OllamaApi ollamaApi) {
return new OllamaEmbeddingClient(ollamaApi).withModel("orca-mini");
}

```

or you can leverage the `spring-ai-ollama-spring-boot-starter` Spring Boot starter.
For this add the following dependency:

```xml
<dependency>
<artifactId>spring-ai-ollama-spring-boot-starter</artifactId>
<groupId>org.springframework.ai</groupId>
<version>0.8.0-SNAPSHOT</version>
</dependency>
```

Use the `OllamaChatProperties` to configure the Ollama Chat client:

| Property | Description | Default |
| ------------- | ------------- | ------------- |
| spring.ai.ollama.chat.model | Model to use. | llama2 |
| spring.ai.ollama.chat.base-url | The base url of the Ollama server. | http://localhost:11434 |
| spring.ai.ollama.chat.enabled | Allows you to disable the Ollama Chat autoconfiguration. | true |
| spring.ai.ollama.chat.temperature | Controls the randomness of the output. Values can range over [0.0,1.0] | 0.8 |
| spring.ai.ollama.chat.topP | The maximum cumulative probability of tokens to consider when sampling. | - |
| spring.ai.ollama.chat.topK | Max number or responses to generate. | - |
| spring.ai.options.chat.options (WIP) | A Map<String,Object> used to configure the Chat client. | - |

and `OllamaEmbeddingProperties` to configure the Ollama Embedding client:

| Property | Description | Default |
| ------------- | ------------- | ------------- |
| spring.ai.ollama.embedding.model | Model to use. | llama2 |
| spring.ai.ollama.embedding.base-url | The base url of the Ollama server. | http://localhost:11434 |
| spring.ai.ollama.embedding.enabled | Allows you to disable the Ollama embedding autoconfiguration. | true |
| spring.ai.options.embedding.options (WIP) | A Map<String,Object> used to configure the embedding client. | - |
32 changes: 30 additions & 2 deletions models/spring-ai-ollama/pom.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.ai</groupId>
Expand All @@ -28,6 +27,23 @@
<version>${project.parent.version}</version>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webflux</artifactId>
</dependency>

<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>${jackson.version}</version>
</dependency>

<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-jsr310</artifactId>
<version>${jackson.version}</version>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
Expand All @@ -39,5 +55,17 @@
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-testcontainers</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>junit-jupiter</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Loading