π Retrieval Augmented Generation (RAG)
Descriptionβ
< What is it? >β
Retrieval Augmented Generation (RAG) is a technique that combines the power of large language models (LLMs) with external knowledge sources to improve the quality and relevance of generated content. By retrieving relevant information from a knowledge base or database, RAG enhances the model's ability to generate accurate and contextually appropriate responses.
Langchain helps to implement RAG by providing tools and frameworks for integrating LLMs with external data sources, enabling developers to build applications that leverage both the generative capabilities of LLMs and the structured information from databases.
< Main advantages >β
- Up-to-date knowledge β you don't need to retrain the LLM when documents change.
- Private knowledge β company documents can be supplied at query time.
- Reduced hallucination β the model can ground its answer in retrieved documents.
- Citations/sources β you can tell the user which documents support the answer.
Key pointsβ
- Data Preparation
- Load docs (.txt, .pdf, csv, html, etc.)
- Split into passages (e.g., 100β500 words)
- Semantic splitting (e.g., using sentence embeddings) is preferred over naive splitting (e.g., by 100 tokens or 1
paragraph).
- Semantic Chunking for RAG (by Plaban Nayak)
-
# Instantiate an OpenAI embeddings modelembedding_model = OpenAIEmbeddings(api_key="<OPENAI_API_TOKEN>", model='text-embedding-3-small')# Create the semantic text splitter with desired parameterssemantic_splitter = SemanticChunker(embeddings=embedding_model, breakpoint_threshold_type="gradient", breakpoint_threshold_amount=0.8)# Split the documentchunks = semantic_splitter.split_documents(document)
- Semantic splitting (e.g., using sentence embeddings) is preferred over naive splitting (e.g., by 100 tokens or 1
paragraph).
- Split into passages (e.g., 100β500 words)
- Embed each passage into a vector space (e.g., OpenAI embeddings)
- Store the embeddings in a vector database (e.g., Pinecone, Weaviate, Milvus, Chroma, FAISS)
- Load docs (.txt, .pdf, csv, html, etc.)
- Graph Database
- Neo4j is a graph database that can be used to store and query relationships between entities in the knowledge base. It allows for efficient retrieval of relevant information based on the relationships between entities.
- LLMGraphTransformer
- LangChain Cypher search
- RAG evaluation
- Evaluate a RAG application (by langsmith)
- Application-specific evaluation approaches (by langsmith)
- Evaluating RAG pipelines with Ragas + LangSmith (by The LangChain Team)
- Intro to AI Evaluation with Langsmith (by AdriΓ‘n JimΓ©nez)
- APIs
- langchain doc loaders
- langchain text splitters
- langchain SemanticChunker
- Evaluating RAG pipelines
Architectureβ
RAG architectureβ

Neo4j graph databaseβ