Setup and Run DeepSeek R1 in Local Environment
Published
Install Ollama
curl -fsSL https://ollama.com/install.sh | sh
For macOS and Windows, simply run the installer and follow the on-screen instructions to complete the installation.
Run DeepSeek R1 using Ollama
ollama pull deepseek-r1:8b
This command fetches the DeepSeek R1 model and prepares it for local use.
ollama run deepseek-r1:8b
This will launch the model, allowing you to interact with it directly in your terminal. Once the model is running, you can start typing prompts and receive responses in real-time.
Integrate Chainlit UI
pip install chainlit
app.py
, and use it to connect Chainlit with DeepSeek R1 via Ollama. For sample example, refer to the Chainlit Cookbook on GitHub. The cookbook provides sample code and additional guidance to help you integrate Chainlit with DeepSeek R1 via Ollama. To run below code, you also need to install the OpenAI package, using the same way you did for Chainlit.app.py
import time
from openai import AsyncOpenAI
import chainlit as cl
client = AsyncOpenAI(
api_key="ollama",
base_url="http://localhost:11434/v1/"
)
async def on_message(msg: cl.Message):
start = time.time()
stream = await client.chat.completions.create(
model="deepseek-r1:8b",
messages=[
{"role": "system", "content": "You are an helpful assistant"},
*cl.chat_context.to_openai()
],
stream=True
)
thinking = False
# Streaming the thinking
async with cl.Step(name="Thinking") as thinking_step:
final_answer = cl.Message(content="")
async for chunk in stream:
delta = chunk.choices[0].delta
if delta.content == "<think>":
thinking = True
continue
if delta.content == "</think>":
thinking = False
thought_for = round(time.time() - start)
thinking_step.name = f"Thought for {thought_for}s"
await thinking_step.update()
continue
if thinking:
await thinking_step.stream_token(delta.content)
else:
await final_answer.stream_token(delta.content)
await final_answer.send()
Once the script is ready, you can run the Chainlit application using the following command:
chainlit run app.py -w
This will launch the Chainlit interface in your browser at http://localhost:8000.
Integrate with Custom Applications
- Start the Ollama Server:
ollama serve
- Execute the API: Send HTTP requests to the Ollama server (default port: 11434) to interact with DeepSeek R1 programmatically. For example, using curl:
curl http://localhost:11434/api/generate -d '{ "model": "deepseek-r1", "prompt": "Explain quantum computing in simple terms." }'
By following these steps, you can easily install Ollama and run DeepSeek R1 locally and integrate it with custom applications.