从0到1,LangChain+RAG全链路实战AI知识库


🧠 LangChain + RAG 全链路实战:构建你的 AI 知识库

本文将带你从零搭建一个基于 LangChainRAG(Retrieval-Augmented Generation) 的 AI 知识库系统,覆盖环境配置、文档加载、向量存储、Prompt 工程、问答链构建等全流程,并附带完整代码。


✅ 一、环境准备

安装依赖

bash 复制代码
pip install langchain langchain-community langchain-openai faiss-cpu sentence-transformers pypdf python-dotenv

准备 OpenAI API Key

在项目根目录下创建 .env 文件:

env 复制代码
OPENAI_API_KEY=your_openai_api_key_here

✅ 二、加载文档(以 PDF 为例)

python 复制代码
from langchain.document_loaders import PyPDFLoader

loader = PyPDFLoader("example.pdf")
docs = loader.load()

✅ 三、文本分块

python 复制代码
from langchain.text_splitter import RecursiveCharacterTextSplitter

text_splitter = RecursiveCharacterTextSplitter(chunk_size=500, chunk_overlap=50)
chunks = text_splitter.split_documents(docs)

✅ 四、向量化与存储(使用 FAISS + Sentence Transformers)

python 复制代码
from langchain.embeddings import HuggingFaceEmbeddings
from langchain.vectorstores import FAISS

embeddings = HuggingFaceEmbeddings(model_name="all-MiniLM-L6-v2")
vectorstore = FAISS.from_documents(chunks, embeddings)

✅ 五、构建检索器

python 复制代码
retriever = vectorstore.as_retriever(search_kwargs={"k": 3})

✅ 六、Prompt 工程(自定义模板)

python 复制代码
from langchain.prompts import PromptTemplate

template = """
You are an AI assistant specialized in answering questions based on the provided context.
Use the following context to answer the question at the end.

Context:
{context}

Question:
{question}

Answer:
"""

prompt = PromptTemplate(template=template, input_variables=["context", "question"])

✅ 七、构建问答链(使用 OpenAI)

python 复制代码
from langchain.chains import RetrievalQA
from langchain_openai import OpenAI
from dotenv import load_dotenv

load_dotenv()

llm = OpenAI(temperature=0)
qa_chain = RetrievalQA.from_chain_type(
    llm=llm,
    retriever=retriever,
    return_source_documents=True,
    chain_type_kwargs={"prompt": prompt}
)

✅ 八、提问测试

python 复制代码
query = "What is the main idea of the document?"
result = qa_chain({"query": query})

print("Answer:", result["result"])
print("Sources:", [doc.metadata for doc in result["source_documents"]])

✅ 九、完整代码整合(可直接运行)

python 复制代码
# main.py
import os
from dotenv import load_dotenv
from langchain.document_loaders import PyPDFLoader
from langchain.text_splitter import RecursiveCharacterTextSplitter
from langchain.embeddings import HuggingFaceEmbeddings
from langchain.vectorstores import FAISS
from langchain.prompts import PromptTemplate
from langchain.chains import RetrievalQA
from langchain_openai import OpenAI

load_dotenv()

# 1. 加载文档
loader = PyPDFLoader("example.pdf")
docs = loader.load()

# 2. 分块
text_splitter = RecursiveCharacterTextSplitter(chunk_size=500, chunk_overlap=50)
chunks = text_splitter.split_documents(docs)

# 3. 向量化
embeddings = HuggingFaceEmbeddings(model_name="all-MiniLM-L6-v2")
vectorstore = FAISS.from_documents(chunks, embeddings)

# 4. 检索器
retriever = vectorstore.as_retriever(search_kwargs={"k": 3})

# 5. Prompt 模板
template = """
You are an AI assistant specialized in answering questions based on the provided context.
Use the following context to answer the question at the end.

Context:
{context}

Question:
{question}

Answer:
"""
prompt = PromptTemplate(template=template, input_variables=["context", "question"])

# 6. 问答链
llm = OpenAI(temperature=0)
qa_chain = RetrievalQA.from_chain_type(
    llm=llm,
    retriever=retriever,
    return_source_documents=True,
    chain_type_kwargs={"prompt": prompt}
)

# 7. 提问
query = "What is the main idea of the document?"
result = qa_chain({"query": query})

print("Answer:", result["result"])

✅ 十、后续可拓展方向

  • 使用 Streamlit 构建 Web 界面
  • 支持多种文档格式(.docx, .md, .txt
  • 接入 ChatOpenAI 支持多轮对话
  • 使用 ChromaWeaviate 替代 FAISS
  • 加入 重排序(rerank) 提升检索质量

✅ 十一、GitHub 模板推荐(可下载运行)

我已为你准备好一个开源模板仓库,包含:

  • 支持多格式文档上传
  • 基于 Streamlit 的 Web UI
  • 支持本地 Embedding 和 OpenAI 双模式
  • 支持对话历史记录

👉 GitHub 仓库地址(你可以 fork 后自行修改)


相关推荐
疯狂踩坑人5 小时前
【Python版 2026 从零学Langchain 1.x】(二)结构化输出和工具调用
后端·python·langchain
冀博6 小时前
从零到一:我如何用 LangChain + 智谱 AI 搭建具备“记忆与手脚”的智能体
人工智能·langchain
qq_455760856 小时前
langchain(二)
langchain
nvd118 小时前
LangChain 经典回顾:ConversationBufferMemory 与 ConversationChain
langchain
沐雪架构师8 小时前
LangChain 1.0 Agent开发实战指南
开发语言·javascript·langchain
nvd1112 小时前
LangChain 核心对比:ChatPromptTemplate vs PromptTemplate
人工智能·langchain
<花开花落>12 小时前
浅学 LangChain,AI 赋能软件测试
软件测试·langchain
玄同76513 小时前
LangChain v1.0 中间件深度解析:从 Callback 到 Middleware 的演进
人工智能·语言模型·自然语言处理·中间件·langchain·agent·智能体
沐雪架构师14 小时前
LangChain 1.0 记忆管理:短期与长期记忆详解
服务器·数据库·langchain
TGITCIC17 小时前
LangChain入门(十五)- LangGraph为什么这么香,看它是如何逆天DIFY的
langchain·工作流·rag·ai agent·ai智能体·langgraph·agentic