MarkTechPost@AI 01月28日
Building a Retrieval-Augmented Generation (RAG) System with DeepSeek R1: A Step-by-Step Guide
index_new5.html
../../../zaker_core/zaker_tpl_static/wap/tpl_guoji1.html

 

本文介绍了如何使用DeepSeek R1构建检索增强生成(RAG)系统。RAG结合了检索和生成方法的优点,从知识库中检索相关信息,并生成准确且上下文相关的回复。文章详细讲解了环境搭建、Ollama安装、DeepSeek R1模型运行、知识库准备、向量存储创建、检索器设置以及查询处理等步骤,并提供了代码示例。通过这些步骤,用户可以有效地从文档中检索信息,并基于检索到的信息生成准确的回复。本文旨在帮助读者理解和实践如何利用DeepSeek R1构建RAG系统。

🛠️首先,需要安装Ollama,这是一个允许本地运行DeepSeek R1等模型的框架。安装后,通过命令行验证安装是否成功,并运行DeepSeek R1模型。

📚其次,需要准备知识库,可以是文档、文章等文本数据。文章提供了加载文本文件的Python代码示例,并介绍了如何使用FAISS创建向量存储,以便高效检索相关文档。

🔍接着,需要创建一个检索器,根据用户查询获取最相关的文档。文中提供了SimpleRetriever类的示例,展示了如何使用FAISS索引和HuggingFaceEmbeddings模型进行检索。

🤖最后,配置DeepSeek R1模型,使用提示模板指导模型基于检索到的上下文进行回复。文章还展示了如何实现查询处理功能,结合检索和生成来回答用户问题。

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.

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.

Some prerequisites for running the codes in this tutorial are as follows:

Now, let’s look into step-by-step implementation:

Step 1: Install Ollama

First, install Ollama by following the instructions on their website. Once installed, verify the installation by running:

Step 2: Run DeepSeek R1 Model

To start the DeepSeek R1 model, open your terminal and execute:

# bashollama run deepseek-r1:1.5b

This command initializes the 1.5 billion parameter version of DeepSeek R1, which is suitable for various applications.

Step 3: Prepare Your Knowledge Base

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

You can load documents from various sources, such as text files, databases, or web scraping. Here’s an example of loading text files:

# pythonimport osdef 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 documentsdocuments = load_documents('path/to/your/documents')

Step 4: Create a Vector Store for Retrieval

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.

4.1 Install Required Libraries

You may need to install additional libraries for embeddings and FAISS:

# bashpip install faiss-cpu huggingface-hub

4.2 Generate Embeddings and Set Up FAISS

Here’s how to generate embeddings and set up the FAISS vector store:

# pythonfrom huggingface_hub import HuggingFaceEmbeddingsimport faissimport numpy as np# Initialize the embeddings modelembeddings_model = HuggingFaceEmbeddings()# Generate embeddings for all documentsdocument_embeddings = [embeddings_model.embed(doc) for doc in documents]document_embeddings = np.array(document_embeddings).astype('float32')# Create FAISS indexindex = faiss.IndexFlatL2(document_embeddings.shape[1])  # L2 distance metricindex.add(document_embeddings)  # Add document embeddings to the index

Step 5: Set Up the Retriever

You must create a retriever based on user queries to fetch the most relevant documents.

# pythonclass 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

Next, a prompt template will be set up to instruct DeepSeek R1 to respond based on retrieved context.

# pythonfrom ollama import Ollamafrom string import Template# Instantiate the modelllm = Ollama(model="deepseek-r1:1.5b")# Craft the prompt template using string. Template for better readabilityprompt_template = Template("""Use ONLY the context below.If unsure, say "I don't know".Keep answers under 4 sentences.Context: $contextQuestion: $questionAnswer:""")

Step 7: Implement Query Handling Functionality

Now, you can create a function that combines retrieval and generation to answer user queries:

# pythondef 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

You can now test your RAG system by calling the answer_query function with any question about your knowledge base.

# pythonif 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

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.

Sources

The post Building a Retrieval-Augmented Generation (RAG) System with DeepSeek R1: A Step-by-Step Guide appeared first on MarkTechPost.

Fish AI Reader

Fish AI Reader

AI辅助创作,多种专业模板,深度分析,高质量内容生成。从观点提取到深度思考,FishAI为您提供全方位的创作支持。新版本引入自定义参数,让您的创作更加个性化和精准。

FishAI

FishAI

鱼阅,AI 时代的下一个智能信息助手,助你摆脱信息焦虑

联系邮箱 441953276@qq.com

相关标签

DeepSeek R1 RAG系统 Ollama FAISS 向量检索
相关文章