iT邦幫忙

2026 iThome 鐵人賽

DAY 9
0
AI Engineering

從 LLM 到 AI Agent:30 天打造 vLLM × RAG × LangChain 智慧推薦系統系列 第 10

Day 10: Building Our First RAG Application with LangChain

  • 分享至 

  • xImage
  •  

In the previous days, we built a RAG system step by step by working directely with embeddings, FAISS, Top-K retrieval, and a locally served Qwen model throught vLLM. That manual approach helped us understand what happends inside a retrieval pipeline, but as the application grows, connecting and managing every component by hand becomes harder. In Day 10, we will introduce LangChain as a structured way to organize these components and rebuild our RAG workflow using reusable abstractions for documents, retrievers, prompts, and LLMs.

  1. Why LangChain after building RAG manually?

Building RAG manually is useful because it helps us understand what happens inside the pipeline. In the previous days, we created embeddings, stored them in FAISS, searched for the most relevant vectors, constructed a prompt, and then sent that prompt to the LLM. By implementing each step ourselves, we can clearly see how retrieval and generation work togethe

Manual RAG

Documents                                   
   ↓
Chunking
   ↓
Embedding Model
   ↓
FAISS
   ↓
Similarity Search
   ↓
Top-K Documents
   ↓
Build Prompt
   ↓
LLM

Here's after using LangChain to implement and become structured:

Documents
   ↓
LangChain Components
   ↓
Vector Store / Retriever
   ↓
Prompt
   ↓
LLM

After briefly talking about the theoretic part: we are heading to the practical session.

  1. Install the packages:

       pip install -U \
       langchain \
       langchain-community \
       langchain-huggingface \
       langchain-openai \
       sentence-transformers \
       faiss-cpu
    
  2. Start Qwen with vLLM:

     vllm serve Qwen/Qwen2.5-7B-Instruct \
         --host 127.0.0.1 \
         --port 8000 \
         --max-model-len 8192 \
         --gpu-memory-utilization 0.90
    
  3. Create langchain_rag.py:

    nano langchain_rag.py

     from langchain_core.documents import Document
     from langchain_core.prompts import ChatPromptTemplate
     from langchain_core.output_parsers import StrOutputParser
    
     from langchain_huggingface import HuggingFaceEmbeddings
     from langchain_community.vectorstores import FAISS
     from langchain_openai import ChatOpenAI
    

    python3 langchain_rag.py

https://ithelp.ithome.com.tw/upload/images/20260919/20184040ROy8az4g1O.jpg

https://ithelp.ithome.com.tw/upload/images/20260919/20184040RTaWC7aZ6x.jpg

Conclusion:

Day 10 takes the RAG system built manually in the previous days and reorganises it into a modular LangChain application: local documents are loaded and chunked, embedded with a Hugging Face model, indexed in FAISS, retrieved through LangChain's retriever interface, injected into a prompt, and finally sent to Qwen2.5-7B-Instruct running behind a local vLLM server. This is a classic 2-step RAG architecture: retrieval always occurs before generation, which LangChain characterises as straightforward and predictable. LangChain does not replace embeddings, FAISS, or vLLM; its primary value here is providing standard interfaces that make those components easier to connect, swap, test, and maintain.

The result is therefore not inherently a “smarter” RAG system than the manual implementation—it is a better-structured foundation on which later features such as routing, tools, agents, tracing, and evaluation can be added. LangChain's current documentation explicitly treats document loaders, splitters, embedding models, vector stores, and retrievers as modular retrieval building blocks.

Reference:

  1. LangChain — Retrieval
  2. LangChain — Vector Store Integrations
    3. LangChain — Hugging Face Embeddings Integration
    4.Sentence Transformers
    5.FAISS — Official Project
    6.LangGraph — Overview
    7.LangSmith — Observability
    8.LangSmith — Evaluation

上一篇
LangChain, LangGraph, and LangSmith: Building, Orchestrating, and Observing AI Agents
下一篇
Day 11: Building a Stateful AI Workflow with LangGraph
系列文
從 LLM 到 AI Agent:30 天打造 vLLM × RAG × LangChain 智慧推薦系統15
圖片
  熱門推薦
圖片
{{ item.channelVendor }} | {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言