DEV Community

Cover image for Quick tip: Using Ollama and DeepSeek-R1 with OpenAI Chat
Akmal Chaudhri for SingleStore

Posted on

1

Quick tip: Using Ollama and DeepSeek-R1 with OpenAI Chat

Abstract

DeepSeek-R1 can run locally using Ollama, which is also compatible with OpenAI. This allows us to configure Ollama to use DeepSeek-R1 as a seamless replacement for an OpenAI model. In this article, we'll build a simple chatbot using Ollama and DeepSeek-R1.

The notebook file used in this article is available on GitHub.

Introduction

In a previous article, we tested a DeepSeek-R1 using Ollama for four different test cases. We'll now build a simple chatbot to test DeepSeek-R1 using OpenAI chat completions.

We'll use the smallest DeepSeek-R1 model, as it does not require any GPUs.

With Jupyter and Ollama running locally, we'll load the notebook downloaded from GitHub.

Run the code

After installing and importing the appropriate libraries, we'll download the smallest DeepSeek-R1 model, as follows:

model = "deepseek-r1:1.5b"

ollama.pull(model)
Enter fullscreen mode Exit fullscreen mode

Next, we'll configure Ollama for OpenAI compatibility:

ollama_client = OpenAI(
    base_url = "http://localhost:11434/v1",
    api_key = "ollama"
)
Enter fullscreen mode Exit fullscreen mode

Finally, we'll ask a question using chat completions:

response = ollama_client.chat.completions.create(
    model = model,
    messages = [
        {"role": "system", "content": "You are a helpful assistant"},
        {"role": "user", "content": "What is the answer to life, the universe and everything?"}
    ]
)

content = response.choices[0].message.content
remove_think_tags = True

if remove_think_tags:
    content = re.sub(r"<think>.*?</think>", "", content, flags = re.DOTALL)

print(content)
Enter fullscreen mode Exit fullscreen mode

We'll disable <think> and </think> using a flag so that we can control the output of its reasoning process.

Example output:

Without knowing what a final theory would look like, we might not be able to distinguish life from non-living matter. Similarly, for the universe itself, without knowing about the ultimate reality, we can't tell. Therefore, both concepts remain indefinitely speculative and undetermined in their completeness.

**Final Answer:**
In our current understanding, neither the universe nor life is fully established with an accepted answer due to the inherent uncertainties in defining ultimate principles and truth.
Enter fullscreen mode Exit fullscreen mode

Summary

We can easily use DeepSeek-R1 with Ollama for chat completions. Since Ollama provides OpenAI compatibility, it is easy to use DeepSeek-R1 as a drop-in replacement for an OpenAI model.

Top comments (0)

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay