|
| 1 | +import dev.langchain4j.community.chain.RetrievalQAChain; |
| 2 | +import dev.langchain4j.community.store.embedding.neo4j.Neo4jEmbeddingStoreIngestor; |
| 3 | +import dev.langchain4j.community.store.embedding.neo4j.ParentChildGraphIngestor; |
| 4 | +import dev.langchain4j.data.document.Document; |
| 5 | +import dev.langchain4j.data.document.DocumentSplitter; |
| 6 | +import dev.langchain4j.data.document.loader.FileSystemDocumentLoader; |
| 7 | +import dev.langchain4j.data.document.splitter.DocumentByRegexSplitter; |
| 8 | +import dev.langchain4j.model.embedding.onnx.allminilml6v2q.AllMiniLmL6V2QuantizedEmbeddingModel; |
| 9 | +import dev.langchain4j.model.openai.OpenAiChatModel; |
| 10 | +import dev.langchain4j.agent.tool.Tool; |
| 11 | +import dev.langchain4j.rag.content.retriever.ContentRetriever; |
| 12 | +import dev.langchain4j.rag.content.retriever.EmbeddingStoreContentRetriever; |
| 13 | +import dev.langchain4j.rag.query.Query; |
| 14 | +import dev.langchain4j.service.AiServices; |
| 15 | +import org.neo4j.driver.AuthTokens; |
| 16 | +import org.neo4j.driver.Driver; |
| 17 | +import org.neo4j.driver.GraphDatabase; |
| 18 | +import org.testcontainers.containers.Neo4jContainer; |
| 19 | +import util.Utils; |
| 20 | + |
| 21 | +import java.time.Duration; |
| 22 | + |
| 23 | +import static dev.langchain4j.model.openai.OpenAiChatModelName.GPT_4_O_MINI; |
| 24 | + |
| 25 | +public class Neo4jRagAsAToolExample { |
| 26 | + |
| 27 | + public static class RagTools { |
| 28 | + |
| 29 | + private final Neo4jEmbeddingStoreIngestor ingestor; |
| 30 | + private final RetrievalQAChain qaChain; |
| 31 | + |
| 32 | + public RagTools(Neo4jEmbeddingStoreIngestor ingestor, RetrievalQAChain qaChain) { |
| 33 | + this.ingestor = ingestor; |
| 34 | + this.qaChain = qaChain; |
| 35 | + } |
| 36 | + |
| 37 | + @Tool("Ingest from document") |
| 38 | + public String ingest(String text) { |
| 39 | + Document document = FileSystemDocumentLoader.loadDocument(Utils.toPath(text)); |
| 40 | + |
| 41 | + ingestor.ingest(document); |
| 42 | + return "Document ingested"; |
| 43 | + } |
| 44 | + |
| 45 | + @Tool("Answer the question based only on the context provided from the ingested documents.") |
| 46 | + public String ask(String question) { |
| 47 | + return qaChain.execute(Query.from(question)); |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + public static void main(String[] args) { |
| 52 | + try (Neo4jContainer<?> neo4j = new Neo4jContainer<>("neo4j:5.26").withAdminPassword("pass1234")) { |
| 53 | + neo4j.start(); |
| 54 | + // Setup OpenAI chat model |
| 55 | + OpenAiChatModel chatModel = OpenAiChatModel.builder() |
| 56 | + .baseUrl(System.getenv("OPENAI_BASE_URL")) |
| 57 | + .apiKey(System.getenv("OPENAI_API_KEY")) |
| 58 | + .modelName(GPT_4_O_MINI) |
| 59 | + .timeout(Duration.ofSeconds(60)) |
| 60 | + .build(); |
| 61 | + final AllMiniLmL6V2QuantizedEmbeddingModel embeddingModel = new AllMiniLmL6V2QuantizedEmbeddingModel(); |
| 62 | + |
| 63 | + // MainDoc splitter splits on paragraphs (double newlines) |
| 64 | + final String expectedQuery = "\\n\\n"; |
| 65 | + int maxSegmentSize = 250; |
| 66 | + DocumentSplitter parentSplitter = new DocumentByRegexSplitter(expectedQuery, expectedQuery, maxSegmentSize, 0); |
| 67 | + |
| 68 | + // Child splitter splits on periods (sentences) |
| 69 | + final String expectedQueryChild = "\\. "; |
| 70 | + DocumentSplitter childSplitter = |
| 71 | + new DocumentByRegexSplitter(expectedQueryChild, expectedQuery, maxSegmentSize, 0); |
| 72 | + |
| 73 | + final Driver driver = GraphDatabase.driver(neo4j.getBoltUrl(), AuthTokens.basic("neo4j", "pass1234")); |
| 74 | + |
| 75 | + Neo4jEmbeddingStoreIngestor ingestor = ParentChildGraphIngestor.builder() |
| 76 | + .driver(driver) |
| 77 | + .documentSplitter(parentSplitter) |
| 78 | + .documentSplitter(childSplitter) |
| 79 | + .embeddingModel(embeddingModel) |
| 80 | + .build(); |
| 81 | + |
| 82 | + // Retriever from Neo4j embeddings |
| 83 | + ContentRetriever retriever = EmbeddingStoreContentRetriever.builder() |
| 84 | + .embeddingStore(ingestor.getEmbeddingStore()) |
| 85 | + .embeddingModel(embeddingModel) |
| 86 | + .maxResults(5) |
| 87 | + .minScore(0.4) |
| 88 | + .build(); |
| 89 | + |
| 90 | + // Retrieval QA chain using retriever and LLM |
| 91 | + RetrievalQAChain retrievalQAChain = RetrievalQAChain.builder() |
| 92 | + .contentRetriever(retriever) |
| 93 | + .chatModel(chatModel) |
| 94 | + .build(); |
| 95 | + |
| 96 | + |
| 97 | + RagTools tools = new RagTools(ingestor, retrievalQAChain); |
| 98 | + |
| 99 | + // Build assistant with ingestion tool and retrieval QA tool |
| 100 | + Utils.Assistant assistant = AiServices.builder(Utils.Assistant.class) |
| 101 | + .tools(tools) |
| 102 | + .chatModel(chatModel) |
| 103 | + .build(); |
| 104 | + |
| 105 | + |
| 106 | + // Ask a question answered by retrieval QA chain |
| 107 | + String chat = assistant.chat(""" |
| 108 | + Ingest from document 'myname.txt', and then return the answer for the question 'What is the cancellation policy?'"""); |
| 109 | + System.out.println("ANSWER: " + chat); |
| 110 | + // example output: |
| 111 | + // `ANSWER: John Doe is a Super Saiyan` |
| 112 | + } |
| 113 | + } |
| 114 | +} |
0 commit comments