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


相关推荐
Cosolar1 小时前
Nanobot:超轻量个人 AI 智能体
chatgpt·langchain·llm
中草药z2 小时前
【LangChain】快速上手 + 聊天模型详解
langchain
@atweiwei7 小时前
用 Rust 构建agent的 LLM 应用的高性能框架
开发语言·后端·rust·langchain·eclipse·llm·agent
abigale039 小时前
Langchain入门到实战开发智能体教程(LLM+RAG+OpenAI+Agent)-下
langchain·prompt
Csvn11 小时前
🌟 LangChain 30 天保姆级教程 · Day 16|文档加载器大合集!PDF、Word、网页、数据库一键读取,构建你的知识库!
python·langchain
前进的李工11 小时前
智能Agent实战指南:从入门到精通(工具)
开发语言·人工智能·架构·langchain·agent·tool·agentexecutor
chaors11 小时前
LangGraph 入门到精通0x00:HelloLangGraph
langchain·llm·agent
老王熬夜敲代码12 小时前
LAngChain工具接入
langchain
怕浪猫13 小时前
第12章 工具(Tools)与函数调用(LangChain实战)
langchain·aigc·ai编程
老王熬夜敲代码13 小时前
接入工具代码讲解
langchain