YouTip LogoYouTip

Langchain Rag Overview

RAG (Retrieval-Augmented Generation, retrieval-augmented generation) enables AI to answer questions based on your private documents without fine-tuning the model. You only need to vectorize and store the documents, and the Agent can retrieve relevant content to answer questions. * * * ## What is RAG Ordinary large language models can only answer questions based on content in their training data. If your documents are private (company internal documents, personal notes), the model "doesn't know". RAG solves this problem: 1. **Offline phase**: Split documents into chunks β†’ Convert to vectors using Embedding model β†’ Store in vector database 2. **Online phase**: User asks a question β†’ Convert question to vector β†’ Search for most similar content in vector database β†’ Use retrieved content as context for the model β†’ Model answers based on retrieved content * * * ## RAG Workflow β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”β”‚ Offline Phase (Indexing) β”‚β”‚ β”‚β”‚ Document β†’ DocumentLoader β†’ TextSplitter β†’ Embedding β”‚β”‚ ↓ β”‚β”‚ VectorStore β”‚β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”β”‚ Online Phase (Retrieval) β”‚β”‚ β”‚β”‚ User Query β†’ Embedding β†’ Similarity Search β†’ Retrieved Results β”‚β”‚ ↓ β”‚β”‚ Retrieved Results + User Query β†’ Model β†’ Answer β”‚β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ !(https://example.com/wp-content/uploads/2026/05/tutorial-chainlang-rag.png) * * * ## Environment Preparation Install RAG-related dependencies: $ pip install langchain-deepseek langchain-chroma chromadb | Package | Purpose | | --- | --- | | langchain-deepseek | Provides OpenAI Embedding model | | langchain-chroma | LangChain integration for Chroma vector database | | chromadb | Chroma vector database (lightweight, suitable for beginners) | ### Embedding Model Initialization ## Example from dotenv import load_dotenv load_dotenv() from langchain_openai import OpenAIEmbeddings # OpenAI's text embedding model # Convert text to vectors (a group of floating-point numbers) embeddings = OpenAIEmbeddings(model="text-embedding-3-small") # Test: convert a piece of text to vectors text ="Runoob Tutorial TUTORIAL is a programming learning platform" vector = embeddings.embed_query(text) print(f"text: {text}") print(f"vector dimension: {len(vector)}")# text-embedding-3-small is 1536 dimensions print(f"first 5 values of the vector: {vector[:5]}") Running result: text: Runoob Tutorial TUTORIAL is a programming learning platformvector dimension: 1536first 5 values of the vector: [0.0123, -0.0045, 0.0234, -0.0012, 0.0089] * * * ## Create Vector Store ## Example from langchain_openai import OpenAIEmbeddings from langchain_chroma import Chroma # Initialize Embedding model embeddings = OpenAIEmbeddings(model="text-embedding-3-small") # Create Chroma vector store (data saved in local directory) vector_store = Chroma( collection_name="tutorial_docs", embedding_function=embeddings, persist_directory="./chroma_db",# persistence directory ) # Add documents (simplest form: list of texts) texts =[ "Runoob Tutorial (TUTORIAL) is a free programming learning website that provides tutorials on HTML, CSS, JavaScript, Python, etc.", "Python3 Basic Tutorial has 30 Chapters, suitable for zero-based entry, covering environment setup, syntax basics, object-oriented programming, and other Content.", "HTML Basic Tutorial has 25 Chapters, covering HTML tags, forms, multimedia, and other basic knowledge.", ] # add_texts automatically converts texts to vectors and stores them vector_store.add_texts(texts) print(f"added {len(texts)} documents to vector store") ### Semantic Retrieval ## Example # Semantic search - does not rely on keyword matching, but semantic similarity results = vector_store.similarity_search( "I want to learn Python, what tutorials do you recommend?", k=2,# Return the 2 most similar results ) print("Search Result:") for i, doc in enumerate(results): print(f"\\ Result {i+1}:") print(f" Content: {doc.page_content}") print(f" metadata: {doc.metadata}") Running result: Search Result:Result 1: Content: Python3 Basic Tutorial has 30 Chapters, suitable for zero-based entry... metadata: {}Result 2: Content: Runoob Tutorial (TUTORIAL) is a free programming learning website... metadata: {} > Note that the first search result is more relevant than the second - although the first contains the keyword "Python", it is ranked by semantic similarity rather than keyword matching. This is the advantage of vector retrieval. * * * ## Create Retriever Retriever is the standardized interface of Vector Store: ## Example # Create retriever from vector_store retriever = vector_store.as_retriever( search_type="similarity",# similarity search search_kwargs={"k": 3},# return top 3 results ) # Use retriever docs = retriever.invoke("Python learning path") for doc in docs: print(f"- {doc.page_content[:60]}...")
← Langchainrag AgentLangchain Human In The Loop β†’