跳转至

2025-01-30T070522-0700Building a Retrieval-Augmented Generation (RAG) System with DeepSeek R1 A Step-by-Step Guide

  • 分类: Clippings
  • 创建: 2025-01-30
  • 标签: DeepSeek R1, RAG, AI, 检索增强生成, 开源模型, 文档检索, 嵌入生成

Building a Retrieval-Augmented Generation (RAG) System with DeepSeek R1: A Step-by-Step Guide

摘要

随着 DeepSeek R1 的发布,AI 社区引起了轰动。该开源模型在多个指标上表现出色,甚至与一些专有模型相当。本文探讨如何使用 DeepSeek R1 实现检索增强生成(RAG)系统,包括环境设置、知识库准备和运行查询等步骤。

要点

  1. DeepSeek R1 是一个开源模型,表现优异。
  2. RAG 系统结合了检索和生成的优势。
  3. 需要安装 Python 和 Ollama。
  4. 使用 FAISS 创建向量存储以提高检索效率。
  5. 代码示例涵盖了文档加载、嵌入生成和查询处理。

正文

With the release of DeepSeek R1, there is a buzz in the AI community. The open-source model offers some best-in-class performance across many metrics, even at par with state-of-the-art proprietary models in many cases. Such huge success invites attention and curiosity to learn more about it. In this article, we will look into implementing a  Retrieval-Augmented Generation (RAG) system using DeepSeek R1. We will cover everything from setting up your environment to running queries with additional explanations and code snippets.
随着 DeepSeek R1 的发布,AI 社区引起了轰动。开源模型在许多指标上提供了一些一流的性能,在许多情况下甚至与最先进的专有模型相当。如此巨大的成功引起了人们的关注和好奇心,以了解更多信息。在本文中,我们将研究使用 DeepSeek R1 实现检索增强生成 (RAG) 系统。我们将涵盖从设置环境到运行查询的所有内容,并提供额外的解释和代码片段。

As already widespread, RAG combines the strengths of retrieval-based and generation-based approaches. It retrieves relevant information from a knowledge base and uses it to generate accurate and contextually relevant responses to user queries.
正如已经广泛采用的那样,RAG 结合了基于检索和基于生成的方法的优势。它从知识库中检索相关信息,并使用它为用户查询生成准确且与上下文相关的响应。

Some prerequisites for running the codes in this tutorial are as follows:
运行本教程中的代码的一些先决条件如下:

  • Python installed (preferably version 3.7 or higher).
    已安装 Python(最好是 3.7 或更高版本)。
  • Ollama installed: This framework allows running models like DeepSeek R1 locally.
    已安装 Ollama:此框架允许在本地运行 DeepSeek R1 等模型。

Now, let’s look into step-by-step implementation:
现在,让我们看看逐步的实现:

Step 1: Install Ollama  第 1 步:安装 Ollama

First, install Ollama by following the instructions on their website. Once installed, verify the installation by running:
首先,按照 Ollama 网站上的说明安装 Ollama。安装后,通过运行以下命令验证安装:

Step 2: Run DeepSeek R1 Model
第 2 步:运行 DeepSeek R1 模型

To start the DeepSeek R1 model, open your terminal and execute:
要启动 DeepSeek R1 模型,请打开您的终端并执行:

# bash
ollama run deepseek-r1:1.5b

This command initializes the 1.5 billion parameter version of DeepSeek R1, which is suitable for various applications.
此命令初始化 DeepSeek R1 的 15 亿参数版本,适用于各种应用。

Step 3: Prepare Your Knowledge Base
第 3 步:准备知识库

A retrieval system requires a knowledge base from which it can pull information. This can be a collection of documents, articles, or any text data relevant to your domain.
检索系统需要一个知识库,它可以从中提取信息。这可以是与您的域相关的文档、文章或任何文本数据的集合。

3.1 Load Your Documents  3.1 加载您的文档

You can load documents from various sources, such as text files, databases, or web scraping. Here’s an example of loading text files:
您可以从各种来源加载文档,例如文本文件、数据库或 Web 抓取。以下是加载文本文件的示例:

# python
import os

def load_documents(directory):
    documents = []
    for filename in os.listdir(directory):
        if filename.endswith('.txt'):
            with open(os.path.join(directory, filename), 'r') as file:
                documents.append(file.read())
    return documents

documents = load_documents('path/to/your/documents')

Step 4: Create a Vector Store for Retrieval
第 4 步:创建用于检索的 Vector Store

To enable efficient retrieval of relevant documents, you can use a vector store like FAISS (Facebook AI Similarity Search). This involves generating embeddings for your documents.
为了能够有效地检索相关文档,您可以使用像 FAISS (Facebook AI Similarity Search) 这样的向量存储。这涉及为您的文档生成嵌入。

4.1 Install Required Libraries
4.1 安装所需的库

You may need to install additional libraries for embeddings and FAISS:
您可能需要安装用于嵌入和 FAISS 的其他库:

# bash
pip install faiss-cpu huggingface-hub

4.2 Generate Embeddings and Set Up FAISS
4.2 生成嵌入并设置 FAISS

Here’s how to generate embeddings and set up the FAISS vector store:
以下是生成嵌入和设置 FAISS 向量存储的方法:

# python
from huggingface_hub import HuggingFaceEmbeddings
import faiss
import numpy as np

# Initialize the embeddings model
embeddings_model = HuggingFaceEmbeddings()

# Generate embeddings for all documents
document_embeddings = [embeddings_model.embed(doc) for doc in documents]
document_embeddings = np.array(document_embeddings).astype('float32')

# Create FAISS index
index = faiss.IndexFlatL2(document_embeddings.shape[1])  # L2 distance metric
index.add(document_embeddings)  # Add document embeddings to the index

Step 5: Set Up the Retriever
第 5 步:设置检索器

You must create a retriever based on user queries to fetch the most relevant documents.
您必须根据用户查询创建检索器,才能获取最相关的文档。

# python
class SimpleRetriever:
    def __init__(self, index, embeddings_model):
        self.index = index
        self.embeddings_model = embeddings_model

    def retrieve(self, query, k=3):
        query_embedding = self.embeddings_model.embed(query)
        distances, indices = self.index.search(np.array([query_embedding]).astype('float32'), k)
        return [documents[i] for i in indices[0]]

retriever = SimpleRetriever(index, embeddings_model)

Step 6: Configure DeepSeek R1 for RAG
第 6 步:为 RAG 配置 DeepSeek R1

Next, a prompt template will be set up to instruct DeepSeek R1 to respond based on retrieved context.
接下来,将设置一个提示模板,以指示 DeepSeek R1 根据检索到的上下文进行响应。

# python
from ollama import Ollama
from string import Template

# Instantiate the model
llm = Ollama(model="deepseek-r1:1.5b")

# Craft the prompt template using string. Template for better readability
prompt_template = Template("""
Use ONLY the context below.
If unsure, say "I don't know".
Keep answers under 4 sentences.

Context: $context
Question: $question
Answer:
""")

Step 7: Implement Query Handling Functionality
步骤 7:实现查询处理功能

Now, you can create a function that combines retrieval and generation to answer user queries:
现在,您可以创建一个将检索和生成相结合的函数来回答用户查询:

# python
def answer_query(question):
    # Retrieve relevant context from the knowledge base
    context = retriever.retrieve(question)

    # Combine retrieved contexts into a single string (if multiple)
    combined_context = "n".join(context)

    # Generate an answer using DeepSeek R1 with the combined context
    response = llm.generate(prompt_template.substitute(context=combined_context, question=question))

    return response.strip()

Step 8: Running Your RAG System
第 8 步:运行您的 RAG 系统

You can now test your RAG system by calling the `answer_query` function with any question about your knowledge base.
现在,您可以通过调用“answer_query”函数来测试您的 RAG 系统,并询问有关您的知识库的任何问题。

# python
if __name__ == "__main__":
    user_question = "What are the key features of DeepSeek R1?"
    answer = answer_query(user_question)
    print("Answer:", answer)

Access the Colab Notebook with the Complete code
使用完整代码访问 Colab Notebook

In conclusion, following these steps, you can successfully implement a Retrieval-Augmented Generation (RAG) system using DeepSeek R1. This setup allows you to retrieve information from your documents effectively and generate accurate responses based on that information. Also, explore the potential of the DeepSeek R1 model for your specific use case through this.
总之,按照这些步骤,您可以使用 DeepSeek R1 成功实现检索增强生成 (RAG) 系统。此设置允许您有效地从文档中检索信息,并根据该信息生成准确的响应。此外,通过此探索 DeepSeek R1 模型在您的特定用例中的潜力。

Sources  来源

Asif Razzaq  阿西夫·拉扎克

Asif Razzaq is the CEO of Marktechpost Media Inc.. As a visionary entrepreneur and engineer, Asif is committed to harnessing the potential of Artificial Intelligence for social good. His most recent endeavor is the launch of an Artificial Intelligence Media Platform, Marktechpost, which stands out for its in-depth coverage of machine learning and deep learning news that is both technically sound and easily understandable by a wide audience. The platform boasts of over 2 million monthly views, illustrating its popularity among audiences.
Asif Razzaq 是 Marktechpost Media Inc. 的首席执行官。作为一名有远见的企业家和工程师,Asif 致力于利用人工智能的潜力为社会公益服务。他最近的努力是推出了人工智能媒体平台 Marktechpost,该平台以其对机器学习和深度学习新闻的深入报道而著称,这些新闻在技术上既合理又易于广大受众理解。该平台每月浏览量超过 200 万次,说明它在观众中的受欢迎程度。