Skip to content

Commit

Permalink
upgrade to 1.0.0-SNAPSHOT (#57)
Browse files Browse the repository at this point in the history
* upgrade to 1.0.0-SNAPSHOT

Currently upgrade is failing, once file is avaialable it should run

* adds sequence diagram
  • Loading branch information
rajadilipkolli authored May 27, 2024
1 parent 9c08ba5 commit ed780c4
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 24 deletions.
20 changes: 19 additions & 1 deletion rag/rag-springai-ollama-llm/ReadMe.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,25 @@

The use of the pgvector store with the ollama model is not feasible due to the generation of 4096 dimensions by the latter, which exceeds pg vector's indexing support limit of less than 2000 dimensions. This limitation hinders the ease of querying the embedding store due to the absence of indexing.

As LLMs needs heavy computational power, it needs GPU to be enabled for quickly processing using the complex Math operations behind the scenens.
As LLMs need heavy computational power, they need GPU to be enabled for quick processing using complex Math operations behind the scenes.

## Sequence Diagram

```mermaid
sequenceDiagram
participant User
participant AIChatService
participant ChatClient.Builder
participant Redis
User->>AIChatService: Send chat request
AIChatService->>ChatClient.Builder: Initialize chat client
ChatClient.Builder-->>AIChatService: Return chat client instance
AIChatService->>Redis: Enrich and load document metadata
Redis-->>AIChatService: Confirm metadata loaded
AIChatService-->>User: Return chat response
```


### Testcontainers support

Expand Down
36 changes: 23 additions & 13 deletions rag/rag-springai-ollama-llm/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@

<properties>
<java.version>17</java.version>
<spring-ai.version>0.8.1</spring-ai.version>
<spring-ai.version>1.0.0-SNAPSHOT</spring-ai.version>
<spotless.version>2.43.0</spotless.version>
</properties>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
Expand All @@ -39,7 +39,7 @@
</dependency>
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-redis-spring-boot-starter</artifactId>
<artifactId>spring-ai-redis-store-spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
Expand Down Expand Up @@ -70,10 +70,10 @@
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-testcontainers</artifactId>
<scope>test</scope>
</dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-spring-boot-testcontainers</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>junit-jupiter</artifactId>
Expand Down Expand Up @@ -125,7 +125,7 @@
<configuration>
<java>
<palantirJavaFormat>
<version>2.40.0</version>
<version>2.47.0</version>
</palantirJavaFormat>
<importOrder />
<removeUnusedImports />
Expand Down Expand Up @@ -189,13 +189,23 @@

<repositories>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
<snapshots>
<id>spring-snapshots</id>
<name>Spring Snapshots</name>
<url>https://repo.spring.io/snapshot</url>
<releases>
<enabled>false</enabled>
</snapshots>
</releases>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>spring-snapshots</id>
<name>Spring Snapshots</name>
<url>https://repo.spring.io/snapshot</url>
<releases>
<enabled>false</enabled>
</releases>
</pluginRepository>
</pluginRepositories>

</project>
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
import java.util.stream.Collectors;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.ai.chat.ChatClient;
import org.springframework.ai.chat.ChatResponse;
import org.springframework.ai.chat.Generation;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.messages.Message;
import org.springframework.ai.chat.messages.UserMessage;
import org.springframework.ai.chat.model.ChatResponse;
import org.springframework.ai.chat.model.Generation;
import org.springframework.ai.chat.prompt.Prompt;
import org.springframework.ai.chat.prompt.SystemPromptTemplate;
import org.springframework.ai.document.Document;
Expand All @@ -35,8 +35,8 @@ public class AIChatService {
private final ChatClient aiClient;
private final VectorStore vectorStore;

public AIChatService(ChatClient aiClient, VectorStore vectorStore) {
this.aiClient = aiClient;
public AIChatService(ChatClient.Builder builder, VectorStore vectorStore) {
this.aiClient = builder.build();
this.vectorStore = vectorStore;
}

Expand All @@ -54,7 +54,7 @@ public String chat(String query) {
UserMessage userMessage = new UserMessage(query);
Prompt prompt = new Prompt(List.of(systemMessage, userMessage));
LOGGER.info("Calling ai with prompt :{}", prompt);
ChatResponse aiResponse = aiClient.call(prompt);
ChatResponse aiResponse = aiClient.prompt(prompt).call().chatResponse();
LOGGER.info("Response received from call :{}", aiResponse);
Generation generation = aiResponse.getResult();
return (generation != null) ? generation.getOutput().getContent() : "";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
package com.learning.ai.llmragwithspringai.service;

import java.util.List;
import java.util.Map;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.ai.chat.prompt.transformer.TransformerContentType;
import org.springframework.ai.document.Document;
import org.springframework.ai.document.DocumentReader;
import org.springframework.ai.document.DocumentTransformer;
import org.springframework.ai.reader.ExtractedTextFormatter;
import org.springframework.ai.reader.JsonReader;
import org.springframework.ai.reader.TextReader;
Expand Down Expand Up @@ -48,7 +53,17 @@ public void loadData(Resource documentResource) {
}
if (documentReader != null) {
LOGGER.info("Loading text document to redis vector database");
vectorStore.accept(tokenTextSplitter.apply(documentReader.get()));
var metadataEnricher = new DocumentTransformer() {
@Override
public List<Document> apply(List<Document> documents) {
documents.forEach(d -> {
Map<String, Object> metadata = d.getMetadata();
metadata.put(TransformerContentType.EXTERNAL_KNOWLEDGE, "true");
});
return documents;
}
};
vectorStore.accept(metadataEnricher.apply(tokenTextSplitter.apply(documentReader.get())));
LOGGER.info("Loaded document to redis vector database.");
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ spring.ai.ollama.embedding.options.model=llama3

spring.ai.vectorstore.redis.index=vector_store
spring.ai.vectorstore.redis.prefix=ai
spring.ai.vectorstore.redis.initializeSchema=true

spring.ai.ollama.baseUrl=http://localhost:11434

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.redis.testcontainers.RedisStackContainer;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.test.context.TestConfiguration;
import org.springframework.boot.testcontainers.service.connection.ServiceConnection;
import org.springframework.context.annotation.Bean;
import org.springframework.test.context.DynamicPropertyRegistry;
import org.testcontainers.ollama.OllamaContainer;
Expand All @@ -15,13 +16,12 @@
public class TestLlmRagWithSpringAiApplication {

@Bean
@ServiceConnection
OllamaContainer ollama(DynamicPropertyRegistry properties) {
// The model name to use (e.g., "orca-mini", "mistral", "llama2", "codellama", "phi", or
// "tinyllama")
OllamaContainer ollama = new OllamaContainer(
return new OllamaContainer(
DockerImageName.parse("langchain4j/ollama-llama3:latest").asCompatibleSubstituteFor("ollama/ollama"));
properties.add("spring.ai.ollama.base-url", ollama::getEndpoint);
return ollama;
}

@Bean
Expand Down

0 comments on commit ed780c4

Please sign in to comment.