๐Ÿš€ Setting Up Ollama & Running DeepSeek R1 Locally for a Powerful RAG System

๐Ÿค– Ollama

Ollama is a framework for running large language models (LLMs) locally on your machine. It lets you download, run, and interact with AI models without needing cloud-based APIs.
๐Ÿ”น Example: ollama run deepseek-r1:1.5b โ€“ Runs DeepSeek …


This content originally appeared on DEV Community and was authored by Ajmal Hasan

๐Ÿค– Ollama

Ollama is a framework for running large language models (LLMs) locally on your machine. It lets you download, run, and interact with AI models without needing cloud-based APIs.

๐Ÿ”น Example: ollama run deepseek-r1:1.5b โ€“ Runs DeepSeek R1 locally.

๐Ÿ”น Why use it? Free, private, fast, and works offline.

๐Ÿ”— LangChain

LangChain is a Python/JS framework for building AI-powered applications by integrating LLMs with data sources, APIs, and memory.

๐Ÿ”น Why use it? It helps connect LLMs to real-world applications like chatbots, document processing, and RAG.

๐Ÿ“„ RAG (Retrieval-Augmented Generation)

RAG is an AI technique that retrieves external data (e.g., PDFs, databases) and augments the LLMโ€™s response.

๐Ÿ”น Why use it? Improves accuracy and reduces hallucinations by referencing actual documents.

๐Ÿ”น Example: AI-powered PDF Q&A system that fetches relevant document content before generating answers.

โšก DeepSeek R1

DeepSeek R1 is an open-source AI model optimized for reasoning, problem-solving, and factual retrieval.

๐Ÿ”น Why use it? Strong logical capabilities, great for RAG applications, and can be run locally with Ollama.

๐Ÿš€ How They Work Together?

  • Ollama runs DeepSeek R1 locally.
  • LangChain connects the AI model to external data.
  • RAG enhances responses by retrieving relevant information.
  • DeepSeek R1 generates high-quality answers.

๐Ÿ’ก Example Use Case: A Q&A system that allows users to upload a PDF and ask questions about it, powered by DeepSeek R1 + RAG + LangChain on Ollama! ๐Ÿš€

๐ŸŽฏ Why Run DeepSeek R1 Locally?

Benefit Cloud-Based Models Local DeepSeek R1
Privacy โŒ Data sent to external servers โœ… 100% Local & Secure
Speed โณ API latency & network delays โšก Instant inference
Cost ๐Ÿ’ฐ Pay per API request ๐Ÿ†“ Free after setup
Customization โŒ Limited fine-tuning โœ… Full model control
Deployment ๐ŸŒ Cloud-dependent ๐Ÿ”ฅ Works offline & on-premises

๐Ÿ›  Step 1: Installing Ollama

๐Ÿ”น Download Ollama

Ollama is available for macOS, Linux, and Windows. Follow these steps to install it:

1๏ธโƒฃ Go to the official Ollama download page

๐Ÿ”— Download Ollama

2๏ธโƒฃ Select your operating system (macOS, Linux, Windows)

3๏ธโƒฃ Click on the Download button

4๏ธโƒฃ Install it following the system-specific instructions

๐Ÿ“ธ Screenshot:

Image description

Image description

๐Ÿ›  Step 2: Running DeepSeek R1 on Ollama

Once Ollama is installed, you can run DeepSeek R1 models.

๐Ÿ”น Pull the DeepSeek R1 Model

To pull the DeepSeek R1 (1.5B parameter model), run:

ollama pull deepseek-r1:1.5b

This will download and set up the DeepSeek R1 model.

๐Ÿ”น Running DeepSeek R1

Once the model is downloaded, you can interact with it by running:

ollama run deepseek-r1:1.5b

It will initialize the model and allow you to send queries.

๐Ÿ“ธ Screenshot:

Image description

๐Ÿ›  Step 3: Setting Up a RAG System Using Streamlit

Now that you have DeepSeek R1 running, let's integrate it into a retrieval-augmented generation (RAG) system using Streamlit.

๐Ÿ”น Prerequisites

Before running the RAG system, make sure you have:

  • Python installed
  • Conda environment (Recommended for package management)
  • Required Python packages
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

For detailed setup, follow this guide:

๐Ÿ”— Setting Up a Conda Environment for Python Projects

๐Ÿ›  Step 4: Running the RAG System

๐Ÿ”น Clone or Create the Project

1๏ธโƒฃ Create a new project directory

mkdir rag-system && cd rag-system

2๏ธโƒฃ Create a Python script (app.py)
Paste the following Streamlit-based script:

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)

๐Ÿ›  Step 5: Running the App

Once the script is ready, start your Streamlit app:

streamlit run app.py

๐Ÿ“ธ Screenshot:

CHECK GITHUB REPO FOR COMPLETE CODE
LEARN BASICS HERE

๐ŸŽฏ Final Thoughts

โœ… You have successfully set up Ollama and DeepSeek R1!

โœ… You can now build AI-powered RAG applications with local LLMs!

โœ… Try uploading PDFs and asking questions dynamically.

๐Ÿ’ก Want to learn more? Follow my Dev.to blog for more development tutorials! ๐Ÿš€


This content originally appeared on DEV Community and was authored by Ajmal Hasan


Print Share Comment Cite Upload Translate Updates
APA

Ajmal Hasan | Sciencx (2025-01-28T20:30:37+00:00) ๐Ÿš€ Setting Up Ollama & Running DeepSeek R1 Locally for a Powerful RAG System. Retrieved from https://www.scien.cx/2025/01/28/%f0%9f%9a%80-setting-up-ollama-running-deepseek-r1-locally-for-a-powerful-rag-system/

MLA
" » ๐Ÿš€ Setting Up Ollama & Running DeepSeek R1 Locally for a Powerful RAG System." Ajmal Hasan | Sciencx - Tuesday January 28, 2025, https://www.scien.cx/2025/01/28/%f0%9f%9a%80-setting-up-ollama-running-deepseek-r1-locally-for-a-powerful-rag-system/
HARVARD
Ajmal Hasan | Sciencx Tuesday January 28, 2025 » ๐Ÿš€ Setting Up Ollama & Running DeepSeek R1 Locally for a Powerful RAG System., viewed ,<https://www.scien.cx/2025/01/28/%f0%9f%9a%80-setting-up-ollama-running-deepseek-r1-locally-for-a-powerful-rag-system/>
VANCOUVER
Ajmal Hasan | Sciencx - » ๐Ÿš€ Setting Up Ollama & Running DeepSeek R1 Locally for a Powerful RAG System. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2025/01/28/%f0%9f%9a%80-setting-up-ollama-running-deepseek-r1-locally-for-a-powerful-rag-system/
CHICAGO
" » ๐Ÿš€ Setting Up Ollama & Running DeepSeek R1 Locally for a Powerful RAG System." Ajmal Hasan | Sciencx - Accessed . https://www.scien.cx/2025/01/28/%f0%9f%9a%80-setting-up-ollama-running-deepseek-r1-locally-for-a-powerful-rag-system/
IEEE
" » ๐Ÿš€ Setting Up Ollama & Running DeepSeek R1 Locally for a Powerful RAG System." Ajmal Hasan | Sciencx [Online]. Available: https://www.scien.cx/2025/01/28/%f0%9f%9a%80-setting-up-ollama-running-deepseek-r1-locally-for-a-powerful-rag-system/. [Accessed: ]
rf:citation
» ๐Ÿš€ Setting Up Ollama & Running DeepSeek R1 Locally for a Powerful RAG System | Ajmal Hasan | Sciencx | https://www.scien.cx/2025/01/28/%f0%9f%9a%80-setting-up-ollama-running-deepseek-r1-locally-for-a-powerful-rag-system/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.