From ed780c4dec47cbf48dffe988cf3ed08a709437c9 Mon Sep 17 00:00:00 2001 From: Raja Kolli Date: Mon, 27 May 2024 10:56:18 +0530 Subject: [PATCH] upgrade to 1.0.0-SNAPSHOT (#57) * upgrade to 1.0.0-SNAPSHOT Currently upgrade is failing, once file is avaialable it should run * adds sequence diagram --- rag/rag-springai-ollama-llm/ReadMe.md | 20 ++++++++++- rag/rag-springai-ollama-llm/pom.xml | 36 ++++++++++++------- .../service/AIChatService.java | 12 +++---- .../service/DataIndexerService.java | 17 ++++++++- .../src/main/resources/application.properties | 1 + .../TestLlmRagWithSpringAiApplication.java | 6 ++-- 6 files changed, 68 insertions(+), 24 deletions(-) diff --git a/rag/rag-springai-ollama-llm/ReadMe.md b/rag/rag-springai-ollama-llm/ReadMe.md index f7f8a06..4cf64df 100644 --- a/rag/rag-springai-ollama-llm/ReadMe.md +++ b/rag/rag-springai-ollama-llm/ReadMe.md @@ -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 diff --git a/rag/rag-springai-ollama-llm/pom.xml b/rag/rag-springai-ollama-llm/pom.xml index e9570b0..0de8e89 100644 --- a/rag/rag-springai-ollama-llm/pom.xml +++ b/rag/rag-springai-ollama-llm/pom.xml @@ -16,10 +16,10 @@ 17 - 0.8.1 + 1.0.0-SNAPSHOT 2.43.0 - + org.springframework.boot @@ -39,7 +39,7 @@ org.springframework.ai - spring-ai-redis-spring-boot-starter + spring-ai-redis-store-spring-boot-starter org.apache.commons @@ -70,10 +70,10 @@ test - org.springframework.boot - spring-boot-testcontainers - test - + org.springframework.ai + spring-ai-spring-boot-testcontainers + test + org.testcontainers junit-jupiter @@ -125,7 +125,7 @@ - 2.40.0 + 2.47.0 @@ -189,13 +189,23 @@ - spring-milestones - Spring Milestones - https://repo.spring.io/milestone - + spring-snapshots + Spring Snapshots + https://repo.spring.io/snapshot + false - + + + + spring-snapshots + Spring Snapshots + https://repo.spring.io/snapshot + + false + + + diff --git a/rag/rag-springai-ollama-llm/src/main/java/com/learning/ai/llmragwithspringai/service/AIChatService.java b/rag/rag-springai-ollama-llm/src/main/java/com/learning/ai/llmragwithspringai/service/AIChatService.java index 9047990..2ecf1e1 100644 --- a/rag/rag-springai-ollama-llm/src/main/java/com/learning/ai/llmragwithspringai/service/AIChatService.java +++ b/rag/rag-springai-ollama-llm/src/main/java/com/learning/ai/llmragwithspringai/service/AIChatService.java @@ -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; @@ -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; } @@ -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() : ""; diff --git a/rag/rag-springai-ollama-llm/src/main/java/com/learning/ai/llmragwithspringai/service/DataIndexerService.java b/rag/rag-springai-ollama-llm/src/main/java/com/learning/ai/llmragwithspringai/service/DataIndexerService.java index 56f83b3..d07c35a 100644 --- a/rag/rag-springai-ollama-llm/src/main/java/com/learning/ai/llmragwithspringai/service/DataIndexerService.java +++ b/rag/rag-springai-ollama-llm/src/main/java/com/learning/ai/llmragwithspringai/service/DataIndexerService.java @@ -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; @@ -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 apply(List documents) { + documents.forEach(d -> { + Map 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."); } } diff --git a/rag/rag-springai-ollama-llm/src/main/resources/application.properties b/rag/rag-springai-ollama-llm/src/main/resources/application.properties index d2fe47d..0a3789a 100644 --- a/rag/rag-springai-ollama-llm/src/main/resources/application.properties +++ b/rag/rag-springai-ollama-llm/src/main/resources/application.properties @@ -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 diff --git a/rag/rag-springai-ollama-llm/src/test/java/com/learning/ai/llmragwithspringai/TestLlmRagWithSpringAiApplication.java b/rag/rag-springai-ollama-llm/src/test/java/com/learning/ai/llmragwithspringai/TestLlmRagWithSpringAiApplication.java index b79b399..fb13ad9 100644 --- a/rag/rag-springai-ollama-llm/src/test/java/com/learning/ai/llmragwithspringai/TestLlmRagWithSpringAiApplication.java +++ b/rag/rag-springai-ollama-llm/src/test/java/com/learning/ai/llmragwithspringai/TestLlmRagWithSpringAiApplication.java @@ -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; @@ -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