🤖 欧拉玛
Ollama 是一个在本地机器上运行大型语言模型 (LLM) 的框架。它允许您 下载、 运行和与 AI 模型交互,而无需基于云的 API。🔹示例: – 在本地运行 DeepSeek R1。🔹为什么要使用它?免费、私密、快速且可离线工作。ollama run deepseek-r1:1.5b
🔗 朗链
LangChain 是一个Python/JS 框架,用于通过将 LLM 与数据源、API 和内存集成来构建AI 驱动的应用程序。🔹为什么要使用它?它有助于将 LLM连接到聊天机器人、文档处理和 RAG 等现实世界的应用程序 。
📄 RAG (检索增强生成)
RAG 是一种 AI 技术,可检索外部数据(例如 PDF、数据库)并增强LLM 的响应。🔹
为什么要使用它?通过引用实际文档来提高准确性并减少幻觉
。🔹 示例: AI 驱动的PDF 问答系统,可在生成答案之前获取相关文档内容。
⚡ DeepSeek R1
DeepSeek R1 是一个针对推理、解决问题和事实检索进行了优化的开源 AI 模型。🔹为什么要 使用它?强大的逻辑能力,非常适合 RAG 应用程序,并且可以与 Ollama 一起在本地运行 。
🚀 他们如何一起工作?
Ollama在本地运行 DeepSeek R1 。
LangChain将AI模型与外部数据连接起来。
RAG通过检索相关信息来增强响应。
DeepSeek R1生成高质量答案。
💡 示例用例:一个问答系统,允许用户上传 PDF 并提出相关问题,由Ollama上的DeepSeek R1 + RAG + LangChain提供支持!🚀
🎯 为什么要在本地运行 DeepSeek R1?
🛠 步骤 1:安装 Ollama
🔹 下载 Ollama
Ollama 适用于macOS、Linux 和 Windows。请按照以下步骤进行安装:
1️⃣前往 Ollama 官方下载页面
🔗下载 Ollama
2️⃣ 选择你的操作系统(macOS、Linux、Windows)
3️⃣ 单击下载按钮
4️⃣ 按照系统特定的说明进行安装
📸截图:
🛠 第 2 步:在 Ollama 上运行 DeepSeek R1
安装 Ollama 后,您可以运行 DeepSeek R1 模型。
🔹 拉取 DeepSeek R1 模型
要提取DeepSeek R1(1.5B 参数模型),请运行:
ollama pull deepseek-r1:1.5b
这将下载并设置DeepSeek R1 模型。
🔹 运行 DeepSeek R1
下载模型后,您可以通过运行以下命令与其进行交互:
ollama run deepseek-r1:1.5b
它将初始化模型并允许您发送查询。
🛠 步骤 3:使用 Streamlit 设置 RAG 系统
现在您已经运行了DeepSeek R1,让我们使用Streamlit将其集成到检索增强生成 (RAG) 系统中。
🔹 先决条件
在运行 RAG 系统之前,请确保您已:
安装Python
Conda 环境(推荐用于包管理)
所需的 Python 包
pip install -U langchain langchain-community
pip install streamlit
pip install pdfplumber
pip install semantic-chunkers
pip install open-text-embeddings
pip install faiss
pip install ollama
pip install prompt-template
pip install langchain
pip install langchain_experimental
pip install sentence-transformers
pip install faiss-cpu
有关详细设置,请遵循本指南:
🔗为 Python 项目设置 Conda 环境
🛠 步骤 4:运行 RAG 系统
🔹 克隆或创建项目
1️⃣创建新的项目目录
mkdir rag-system && cd rag-system
2️⃣创建 Python 脚本(app.py
)
粘贴以下基于 Streamlit 的脚本:
import streamlit as st
from langchain_community.document_loaders import PDFPlumberLoader
from langchain_experimental.text_splitter import SemanticChunker
from langchain_community.embeddings import HuggingFaceEmbeddings
from langchain_community.vectorstores import FAISS
from langchain_community.llms import Ollama
from langchain.prompts import PromptTemplate
from langchain.chains.llm import LLMChain
from langchain.chains.combine_documents.stuff import StuffDocumentsChain
from langchain.chains import RetrievalQA
# Streamlit UI
st.title("📄 RAG System with DeepSeek R1 & Ollama")
uploaded_file = st.file_uploader("Upload your PDF file here", type="pdf")
if uploaded_file:
with open("temp.pdf", "wb") as f:
f.write(uploaded_file.getvalue())
loader = PDFPlumberLoader("temp.pdf")
docs = loader.load()
text_splitter = SemanticChunker(HuggingFaceEmbeddings())
documents = text_splitter.split_documents(docs)
embedder = HuggingFaceEmbeddings()
vector = FAISS.from_documents(documents, embedder)
retriever = vector.as_retriever(search_type="similarity", search_kwargs={"k": 3})
llm = Ollama(model="deepseek-r1:1.5b")
prompt = """
Use the following context to answer the question.
Context: {context}
Question: {question}
Answer:"""
QA_PROMPT = PromptTemplate.from_template(prompt)
llm_chain = LLMChain(llm=llm, prompt=QA_PROMPT)
combine_documents_chain = StuffDocumentsChain(llm_chain=llm_chain, document_variable_name="context")
qa = RetrievalQA(combine_documents_chain=combine_documents_chain, retriever=retriever)
user_input = st.text_input("Ask a question about your document:")
if user_input:
response = qa(user_input)["result"]
st.write("**Response:**")
st.write(response)
🛠 第 5 步:运行应用程序
脚本准备好后,启动你的Streamlit 应用程序:
streamlit run app.py