|
| 1 | +package org.acme.example; |
| 2 | + |
| 3 | +import java.util.Collection; |
| 4 | +import java.util.Collections; |
| 5 | +import java.util.List; |
| 6 | +import java.util.function.Supplier; |
| 7 | + |
| 8 | +import jakarta.inject.Singleton; |
| 9 | + |
| 10 | +import dev.langchain4j.data.message.ChatMessage; |
| 11 | +import dev.langchain4j.data.message.UserMessage; |
| 12 | +import dev.langchain4j.rag.DefaultRetrievalAugmentor; |
| 13 | +import dev.langchain4j.rag.RetrievalAugmentor; |
| 14 | +import dev.langchain4j.rag.content.Content; |
| 15 | +import dev.langchain4j.rag.content.injector.ContentInjector; |
| 16 | +import dev.langchain4j.rag.content.retriever.ContentRetriever; |
| 17 | +import dev.langchain4j.rag.query.Query; |
| 18 | +import dev.langchain4j.rag.query.router.QueryRouter; |
| 19 | +import io.quarkiverse.langchain4j.RegisterAiService; |
| 20 | + |
| 21 | +@RegisterAiService(retrievalAugmentor = AiServiceWithQueryRouterAndContentInjector.QueryRouterAugmentor.class) |
| 22 | +public interface AiServiceWithQueryRouterAndContentInjector { |
| 23 | + |
| 24 | + String chat(String message); |
| 25 | + |
| 26 | + /** |
| 27 | + * Contains a query transformer that transforms the query text to |
| 28 | + * lowercase. If the transformed worked properly, the content retriever |
| 29 | + * will append content saying "The transformer works!". |
| 30 | + */ |
| 31 | + @Singleton |
| 32 | + class QueryRouterAugmentor implements Supplier<RetrievalAugmentor> { |
| 33 | + |
| 34 | + @Override |
| 35 | + public RetrievalAugmentor get() { |
| 36 | + return DefaultRetrievalAugmentor.builder() |
| 37 | + .queryRouter(new QueryRouter() { |
| 38 | + @Override |
| 39 | + public Collection<ContentRetriever> route(Query query) { |
| 40 | + if (query.text().contains("dog")) { |
| 41 | + return Collections.singletonList(dogsRetriever()); |
| 42 | + } else if (query.text().contains("cat")) { |
| 43 | + return Collections.singletonList(catsRetriever()); |
| 44 | + } else { |
| 45 | + return Collections.emptyList(); |
| 46 | + } |
| 47 | + } |
| 48 | + }) |
| 49 | + .contentInjector(new ContentInjector() { |
| 50 | + @Override |
| 51 | + public ChatMessage inject(List<Content> contents, ChatMessage chatMessage) { |
| 52 | + String rewrittenMessage = ((UserMessage) chatMessage).singleText() + " - " |
| 53 | + + contents.get(0).textSegment().text(); |
| 54 | + return UserMessage.userMessage(rewrittenMessage); |
| 55 | + } |
| 56 | + }) |
| 57 | + .build(); |
| 58 | + } |
| 59 | + |
| 60 | + static ContentRetriever dogsRetriever() { |
| 61 | + return new ContentRetriever() { |
| 62 | + @Override |
| 63 | + public List<Content> retrieve(Query query) { |
| 64 | + return Collections.singletonList(Content.from("Dogs bark")); |
| 65 | + } |
| 66 | + }; |
| 67 | + } |
| 68 | + |
| 69 | + static ContentRetriever catsRetriever() { |
| 70 | + return new ContentRetriever() { |
| 71 | + @Override |
| 72 | + public List<Content> retrieve(Query query) { |
| 73 | + return Collections.singletonList(Content.from("Cats meow")); |
| 74 | + } |
| 75 | + }; |
| 76 | + } |
| 77 | + } |
| 78 | +} |
0 commit comments