With the launch of Deepseek R1There is a buzz in the community of ai. The open source model offers a better performance in class in many metrics, even at the same time with the latest patented models in many cases. Such great success invites attention and curiosity to learn more about it. In this article, we will analyze the implementation of a recovery generation system (RAG) using DEPEEEK R1. We will cover everything from configuring your environment to execution consultations with additional explanations and code fragments.
As already extended, RAG combines the strengths of recovery -based approaches and based on generation. Recover relevant information from a knowledge base and use it to generate precise and contextually relevant responses to user consultations.
Some previous requirements to execute the codes in this tutorial are the following:
- Python installed (preferably version 3.7 or higher).
- Ollama installed: This framework allows you to execute models such as Deepseek R1 locally.
Now, let's look at the step by step:
Step 1: Install Ollama
First, install Ollama following the instructions on your website. Once installed, check the installation running:
Step 2: Execute the Deepseek R1 model
To start the Deepseek R1 model, open its terminal and run:
# bash
ollama run deepseek-r1:1.5b
This command initializes the 1.5 billion version of Deepseek R1 parameters, which is suitable for several applications.
Step 3: Prepare your knowledge base
A recovery system requires a knowledge base from which you can extract information. This may be a collection of documents, articles or any relevant text data for their domain.
3.1 Load your documents
You can load documents from various sources, such as text files, databases or web scraping. Here is an example of text file load:
# 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 recovery
To enable the efficient recovery of relevant documents, you can use a vector store such as FAISS (search for similarity of facebook). This implies generating inlays for your documents.
4.1 Install required libraries
It is possible that you should install additional libraries for inlays and faiss:
# bash
pip install faiss-cpu huggingface-hub
4.2 Generate inlays and configure FAISS
Here we show you how to generate inlays and configure the Faiss Vector store:
# 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: Configure the retriever
You must create a retriever based on user consultations to obtain 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: Deepseek R1 configure for rag
Next, a quick template will be established to instruct Deepseek R1 that responds based on the recovered context.
# 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 consultation management functionality
Now, you can create a function that combines recovery and generation to answer user consultations:
# 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
You can now try your RAG system by calling the `answer_query` function with any questions about your knowledge basis.
# python
if __name__ == "__main__":
user_question = "What are the key features of DeepSeek R1?"
answer = answer_query(user_question)
print("Answer:", answer)
Access the collab notebook with the complete code
In conclusion, after these steps, you can successfully implement a recovery generation system (RAG) using Deepseek R1. This configuration allows you to recover information from your documents effectively and generate precise answers based on that information. In addition, explore the potential of the Deepseek R1 model for your specific use case through this.
Sources
Asif Razzaq is the CEO of Marktechpost Media Inc .. as a visionary entrepreneur and engineer, Asif undertakes to take advantage of the potential of artificial intelligence for the social good. Its most recent effort is the launch of an artificial intelligence media platform, Marktechpost, which stands out for its deep coverage of automatic learning and deep learning news that is technically solid and easily understandable by a broad audience. The platform has more than 2 million monthly views, illustrating its popularity among the public.