-
Notifications
You must be signed in to change notification settings - Fork 4
/
_63_AdvancedRag_AugmentWithMetadata.java
56 lines (43 loc) · 2.12 KB
/
_63_AdvancedRag_AugmentWithMetadata.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package devoxx.demo._6_rag;
import dev.langchain4j.memory.chat.MessageWindowChatMemory;
import dev.langchain4j.rag.DefaultRetrievalAugmentor;
import dev.langchain4j.rag.RetrievalAugmentor;
import dev.langchain4j.rag.content.injector.DefaultContentInjector;
import dev.langchain4j.rag.content.retriever.ContentRetriever;
import dev.langchain4j.rag.content.retriever.EmbeddingStoreContentRetriever;
import dev.langchain4j.service.AiServices;
import dev.langchain4j.store.embedding.astradb.AstraDbEmbeddingStore;
import devoxx.demo.utils.AbstractDevoxxTestSupport;
import devoxx.demo.utils.Assistant;
import org.junit.jupiter.api.Test;
import static java.util.Arrays.asList;
import static java.util.stream.Collectors.joining;
public class _63_AdvancedRag_AugmentWithMetadata extends AbstractDevoxxTestSupport {
@Test
public void shouldRetrieveDocument() {
ContentRetriever contentRetriever = EmbeddingStoreContentRetriever.builder()
.embeddingStore(new AstraDbEmbeddingStore(getCollectionRAG()))
.embeddingModel(getEmbeddingModelGecko())
.maxResults(2)
.minScore(0.5)
.build();
// Enhance the content retriever to add meta data in the prompt
RetrievalAugmentor retrievalAugmentor = DefaultRetrievalAugmentor.builder()
.contentRetriever(contentRetriever)
// Query Transformation
.contentInjector(DefaultContentInjector
.builder()
.metadataKeysToInclude(asList("document_format", "md5"))
.build())
.build();
// configuring it to use the components we've created above.
Assistant ai = AiServices.builder(Assistant.class)
//.contentRetriever(contentRetriever)
.retrievalAugmentor(retrievalAugmentor)
.chatLanguageModel(getChatLanguageModelChatBison())
.chatMemory(MessageWindowChatMemory.withMaxMessages(10))
.build();
String response = ai.answer("Who is Johnny?");
System.out.println(response);
}
}