|
| 1 | +.. _rag-documentation: |
| 2 | + |
| 3 | +RAG Documentation |
| 4 | +################# |
| 5 | + |
| 6 | +************************************ |
| 7 | +Retrieval-Augmented Generation (RAG) |
| 8 | +************************************ |
| 9 | +RAG is a technique that enhances Large Language Model (LLM) responses by retrieving |
| 10 | +source information from external data stores to augment generated responses. |
| 11 | + |
| 12 | +This package enables Python developers to perform RAG using Neo4j. |
| 13 | + |
| 14 | + |
| 15 | +************************************ |
| 16 | +Overview |
| 17 | +************************************ |
| 18 | + |
| 19 | +.. code:: python |
| 20 | +
|
| 21 | + from neo4j import GraphDatabase |
| 22 | + from neo4j_genai.retrievers import VectorRetriever |
| 23 | + from neo4j_genai.llm import OpenAILLM |
| 24 | + from neo4j_genai.generation import GraphRAG |
| 25 | + from neo4j_genai.embeddings.openai import OpenAIEmbeddings |
| 26 | +
|
| 27 | + URI = "neo4j://localhost:7687" |
| 28 | + AUTH = ("neo4j", "password") |
| 29 | +
|
| 30 | + INDEX_NAME = "index-name" |
| 31 | +
|
| 32 | + # Connect to Neo4j database |
| 33 | + driver = GraphDatabase.driver(URI, auth=AUTH) |
| 34 | +
|
| 35 | + # Create Embedder object |
| 36 | + embedder = OpenAIEmbeddings(model="text-embedding-3-large") |
| 37 | +
|
| 38 | + # Initialize the retriever |
| 39 | + retriever = VectorRetriever(driver, INDEX_NAME, embedder) |
| 40 | +
|
| 41 | + # Initialize the LLM |
| 42 | + # Note: the OPENAI_API_KEY must be in the env vars |
| 43 | + llm = OpenAILLM(model_name="gpt-4o", model_params={"temperature": 0}) |
| 44 | +
|
| 45 | + # Initialize the RAG pipeline |
| 46 | + rag = GraphRAG(retriever=retriever, llm=llm) |
| 47 | +
|
| 48 | + # Query the graph |
| 49 | + query_text = "How do I do similarity search in Neo4j?" |
| 50 | + response = rag.search(query_text=query_text, retriever_config={"top_k": 5}) |
| 51 | + print(response.answer) |
| 52 | +
|
| 53 | +
|
| 54 | +The retriever can be any of the :ref:`supported retrievers<retrievers>`, or any class |
| 55 | +inheriting from the `Retriever` interface. |
| 56 | + |
| 57 | +*************** |
| 58 | +Advanced usage |
| 59 | +*************** |
| 60 | + |
| 61 | + |
| 62 | +Using another LLM |
| 63 | +================== |
| 64 | + |
| 65 | +This package only provide support for OpenAI LLM. If you need to use another LLM, |
| 66 | +you need to subclass the `LLMInterface`: |
| 67 | + |
| 68 | +.. autoclass:: neo4j_genai.llm.LLMInterface |
| 69 | + :members: |
| 70 | + :show-inheritance: |
| 71 | + |
| 72 | +Configuring the prompt |
| 73 | +======================= |
| 74 | + |
| 75 | +Prompt are managed through `PromptTemplate` classes. More |
| 76 | +specifically, the `RAG` pipeline uses a `RagTemplate` with |
| 77 | +a default prompt. You can use another prompt by subclassing |
| 78 | +the `RagTemplate` class and passing it to the `RAG` pipeline |
| 79 | +object during initialization: |
| 80 | + |
| 81 | +.. code:: python |
| 82 | +
|
| 83 | + from neo4j_genai.generation import RagTemplate, GraphRAG |
| 84 | +
|
| 85 | + # ... |
| 86 | +
|
| 87 | + prompt_template = RagTemplate( |
| 88 | + prompt="Answer the question {question} using context {context} and examples {examples}", |
| 89 | + expected_inputs=["context", "question", "examples"] |
| 90 | + ) |
| 91 | + rag = GraphRAG(retriever=vector_retriever, llm=llm, prompt_template=prompt_template) |
| 92 | +
|
| 93 | + # ... |
| 94 | +
|
| 95 | +For more details, see: |
| 96 | + |
| 97 | +.. autoclass:: neo4j_genai.generation.prompts.PromptTemplate |
| 98 | + :members: |
| 99 | + |
| 100 | +and |
| 101 | + |
| 102 | +.. autoclass:: neo4j_genai.generation.prompts.RagTemplate |
| 103 | + :members: |
0 commit comments