-
Notifications
You must be signed in to change notification settings - Fork 73
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0196ed1
commit 08e2abb
Showing
6 changed files
with
189 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
...ntial/rag-advanced/src/main/java/com/thomasvitale/ai/spring/RagControllerCompression.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
|
||
} |
45 changes: 45 additions & 0 deletions
45
...sequential/rag-advanced/src/main/java/com/thomasvitale/ai/spring/RagControllerMemory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
|
||
} |
46 changes: 46 additions & 0 deletions
46
...equential/rag-advanced/src/main/java/com/thomasvitale/ai/spring/RagControllerRewrite.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
|
||
} |
27 changes: 0 additions & 27 deletions
27
...ential/rag-advanced/src/main/java/com/thomasvitale/ai/spring/config/HttpClientConfig.java
This file was deleted.
Oops, something went wrong.