从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 后自行修改)


相关推荐
Boop_wu1 小时前
[LangGraph]工作流常见模式
langchain
王国强20093 小时前
通过 Harness Engineering 改进 Deep Agents
langchain
Boop_wu4 小时前
[LangGraph] 案例 3 : RAG 系统 检索流程
langchain
kill5224 小时前
面试题:说说你理解的 Agent 六层架构
langchain
蓝悦无人机1 天前
LangChain v1.0 系列教程——第2章 工具系统
langchain·json·装饰器模式·pydantic
艾醒(AiXing-w)1 天前
LangChain 1.0 入门(九):Agent 核心概念与技术架构
人工智能·chatgpt·langchain
多学一分钟1 天前
讲清 Agent 记忆与上下文管理:长短期记忆、多轮对话、上下文压缩
langchain·agent
聪明蛋子哟1 天前
从LangChain到LangGraph:Python与Java双栈Agent开发实战对比
java·ai·langchain
小叶肥辉1 天前
LangChain链和LangGraph图的学习笔记【四】——(1)调用方法:invoke(2)提示语模板:PromptTemplate
python·langchain·prompt
梦在远山后2 天前
从需求到架构:我的企业研发 Agent 整体设计
langchain·agent