Skip to content

Commit

Permalink
Add more Sequential RAG examples
Browse files Browse the repository at this point in the history
  • Loading branch information
ThomasVitale committed Dec 24, 2024
1 parent 0196ed1 commit 08e2abb
Show file tree
Hide file tree
Showing 6 changed files with 189 additions and 27 deletions.
36 changes: 36 additions & 0 deletions rag/rag-sequential/rag-advanced/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,42 @@ You can also explore metrics in "Explore > Metrics" and logs in "Explore > Logs"
Call the application that will use a chat model to answer your questions.

### Query Transformation: Compression

Without compression:

```shell
http --raw "Who are the characters going on an adventure in the North Pole?" :8080/rag/memory/007 -b --pretty none
```

```shell
http --raw "What places do they visit?" :8080/rag/memory/007 -b --pretty none
```

With compression:

```shell
http --raw "Who are the characters going on an adventure in the North Pole?" :8080/rag/compression/007 -b --pretty none
```

```shell
http --raw "What places do they visit?" :8080/rag/compression/007 -b --pretty none
```

### Query Transformation: Rewrite

Without rewrite:

```shell
http --raw "Where are the main characters going on an adventure?" :8080/rag/basic -b --pretty none
```

With rewrite:

```shell
http --raw "Where are the main characters going on an adventure?" :8080/rag/rewrite -b --pretty none
```

### Query Transformation: Translation

Without translation:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import io.opentelemetry.api.OpenTelemetry;
import io.opentelemetry.instrumentation.logback.appender.v1_0.OpenTelemetryAppender;
import org.springframework.ai.chat.memory.ChatMemory;
import org.springframework.ai.chat.memory.InMemoryChatMemory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.event.ApplicationReadyEvent;
Expand All @@ -20,4 +22,9 @@ ApplicationListener<ApplicationReadyEvent> logbackOtelAppenderInitializer(OpenTe
return _ -> OpenTelemetryAppender.install(openTelemetry);
}

@Bean
ChatMemory chatMemory() {
return new InMemoryChatMemory();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package com.thomasvitale.ai.spring;

import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.client.advisor.AbstractChatMemoryAdvisor;
import org.springframework.ai.chat.client.advisor.MessageChatMemoryAdvisor;
import org.springframework.ai.chat.client.advisor.RetrievalAugmentationAdvisor;
import org.springframework.ai.chat.memory.ChatMemory;
import org.springframework.ai.rag.preretrieval.query.transformation.CompressionQueryTransformer;
import org.springframework.ai.rag.retrieval.search.VectorStoreDocumentRetriever;
import org.springframework.ai.vectorstore.VectorStore;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class RagControllerCompression {

private final ChatClient chatClient;
private final MessageChatMemoryAdvisor chatMemoryAdvisor;
private final RetrievalAugmentationAdvisor retrievalAugmentationAdvisor;

public RagControllerCompression(ChatClient.Builder chatClientBuilder, ChatMemory chatMemory, VectorStore vectorStore) {
this.chatClient = chatClientBuilder.build();

this.chatMemoryAdvisor = MessageChatMemoryAdvisor.builder(chatMemory)
.build();

var documentRetriever = VectorStoreDocumentRetriever.builder()
.vectorStore(vectorStore)
.similarityThreshold(0.50)
.build();

var queryTransformer = CompressionQueryTransformer.builder()
.chatClientBuilder(chatClientBuilder.build().mutate())
.build();

this.retrievalAugmentationAdvisor = RetrievalAugmentationAdvisor.builder()
.documentRetriever(documentRetriever)
.queryTransformers(queryTransformer)
.build();
}

@PostMapping("/rag/compression/{conversationId}")
String rag(@RequestBody String input, @PathVariable String conversationId) {
return chatClient.prompt()
.advisors(chatMemoryAdvisor, retrievalAugmentationAdvisor)
.advisors(advisors -> advisors.param(
AbstractChatMemoryAdvisor.CHAT_MEMORY_CONVERSATION_ID_KEY, conversationId))
.user(input)
.call()
.content();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package com.thomasvitale.ai.spring;

import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.client.advisor.AbstractChatMemoryAdvisor;
import org.springframework.ai.chat.client.advisor.MessageChatMemoryAdvisor;
import org.springframework.ai.chat.client.advisor.RetrievalAugmentationAdvisor;
import org.springframework.ai.chat.memory.ChatMemory;
import org.springframework.ai.rag.retrieval.search.VectorStoreDocumentRetriever;
import org.springframework.ai.vectorstore.VectorStore;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
class RagControllerMemory {

private final ChatClient chatClient;
private final MessageChatMemoryAdvisor chatMemoryAdvisor;
private final RetrievalAugmentationAdvisor retrievalAugmentationAdvisor;

RagControllerMemory(ChatClient.Builder chatClientBuilder, ChatMemory chatMemory, VectorStore vectorStore) {
this.chatClient = chatClientBuilder.build();
this.chatMemoryAdvisor = MessageChatMemoryAdvisor.builder(chatMemory)
.build();
this.retrievalAugmentationAdvisor = RetrievalAugmentationAdvisor.builder()
.documentRetriever(VectorStoreDocumentRetriever.builder()
.similarityThreshold(0.50)
.vectorStore(vectorStore)
.build())
.build();
}

@PostMapping("/rag/memory/{conversationId}")
String chatWithDocument(@RequestBody String question, @PathVariable String conversationId) {
return chatClient.prompt()
.advisors(chatMemoryAdvisor, retrievalAugmentationAdvisor)
.advisors(advisors -> advisors.param(
AbstractChatMemoryAdvisor.CHAT_MEMORY_CONVERSATION_ID_KEY, conversationId))
.user(question)
.call()
.content();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package com.thomasvitale.ai.spring;

import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.client.advisor.RetrievalAugmentationAdvisor;
import org.springframework.ai.rag.preretrieval.query.transformation.RewriteQueryTransformer;
import org.springframework.ai.rag.retrieval.search.VectorStoreDocumentRetriever;
import org.springframework.ai.vectorstore.VectorStore;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class RagControllerRewrite {

private final ChatClient chatClient;
private final RetrievalAugmentationAdvisor retrievalAugmentationAdvisor;

public RagControllerRewrite(ChatClient.Builder chatClientBuilder, VectorStore vectorStore) {
this.chatClient = chatClientBuilder.build();

var documentRetriever = VectorStoreDocumentRetriever.builder()
.vectorStore(vectorStore)
.similarityThreshold(0.50)
.build();

var queryTransformer = RewriteQueryTransformer.builder()
.chatClientBuilder(chatClientBuilder.build().mutate())
.targetSearchSystem("vector store")
.build();

this.retrievalAugmentationAdvisor = RetrievalAugmentationAdvisor.builder()
.documentRetriever(documentRetriever)
.queryTransformers(queryTransformer)
.build();
}

@PostMapping("/rag/rewrite")
String rag(@RequestBody String input) {
return chatClient.prompt()
.advisors(retrievalAugmentationAdvisor)
.user(input)
.call()
.content();
}

}

This file was deleted.

0 comments on commit 08e2abb

Please sign in to comment.