Introduction
Chatbots are transforming the way we interact with software, offering everything from customer support to personal assistance. While many solutions rely on cloud-based AI, running your chatbot locally has its advantages—enhanced privacy, lower latency, and complete control over your model and data.
In this tutorial, we’ll build a local chatbot powered by the Deepseek model. Deepseek, which you can install locally with Ollama, offers robust performance for natural language tasks. If you haven’t set up Deepseek yet, check out our previous guide on installing Deepseek locally with Ollama before proceeding.
Prerequisites
Before you begin, ensure you have the following installed:
- Python 3.8+
- pip (Python package installer)
- Ollama with Deepseek installed locally
- Basic knowledge of Python and web development
You’ll also need to install some Python packages. Create a virtual environment and install Flask:
python -m venv chatbot-env
source chatbot-env/bin/activate # On Windows: chatbot-env\Scripts\activate
pip install flask
How Deepseek Works with Ollama
The Deepseek model runs locally via Ollama. In our Python code, we’ll leverage the command-line interface (CLI) provided by Ollama
to generate responses. Essentially, we’ll call a command like:
ollama run deepseek --prompt "Your message here"
and capture the output as the chatbot’s reply.
Creating the Backend Chatbot API
Let’s build a simple Flask API that accepts a user’s message, sends it to Deepseek using Ollama’s CLI, and returns the generated response.
Create a new file called app.py
and add the following code:
from flask import Flask, request, jsonify
import subprocess
app = Flask(__name__)
def generate_response(prompt):
"""
Generate a response by calling the Deepseek model through Ollama.
Make sure that Ollama is installed and Deepseek is set up locally.
"""
# Construct the command to call Deepseek via Ollama
command = ["ollama", "run", "deepseek", "--prompt", prompt]
try:
# Run the command and capture the output
result = subprocess.run(command, capture_output=True, text=True, check=True)
# The response is expected in stdout
response = result.stdout.strip()
except subprocess.CalledProcessError as e:
response = f"Error generating response: {e}"
return response
@app.route("/chat", methods=["POST"])
def chat():
"""
API endpoint to receive a user message and return a chatbot response.
"""
data = request.get_json()
user_message = data.get("message", "")
if not user_message:
return jsonify({"error": "No message provided"}), 400
response_text = generate_response(user_message)
return jsonify({"response": response_text})
# Optionally, serve the frontend from the Flask app
@app.route("/")
def home():
return app.send_static_file("index.html")
if __name__ == "__main__":
app.run(debug=True)
A Few Notes:
- Deepseek Integration: We’re using Python’s subprocess module to call ollama run deepseek and pass the user’s message as the prompt.
- Error Handling: If the CLI command fails, an error message is returned.
- Local Environment: This example assumes that Ollama and Deepseek are installed and configured on your machine.
Building the Frontend Interface
Next, let’s create a simple HTML page so you can interact with your chatbot. Create a folder named static in the same directory as app.py
, then add an index.html
file inside static with the following content:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Local AI Chatbot with Deepseek</title>
<style>
body {
font-family: Arial, sans-serif;
margin: 20px;
}
#chatbox {
border: 1px solid #ccc;
padding: 10px;
height: 300px;
width: 80%;
margin-bottom: 10px;
overflow-y: scroll;
}
.message {
margin: 5px 0;
}
.user {
color: blue;
}
.bot {
color: green;
}
</style>
</head>
<body>
<h1>Local AI Chatbot with Deepseek</h1>
<div id="chatbox"></div>
<input type="text" id="message" placeholder="Type your message here..." style="width:70%;">
<button id="send">Send</button>
<script>
document.getElementById("send").addEventListener("click", function() {
const inputField = document.getElementById("message");
const message = inputField.value.trim();
if (!message) return;
// Append user message to chatbox
const chatbox = document.getElementById("chatbox");
chatbox.innerHTML += `<div class="message user"><strong>You:</strong> ${message}</div>`;
// Send the message to the backend
fetch("/chat", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ message: message })
})
.then(response => response.json())
.then(data => {
chatbox.innerHTML += `<div class="message bot"><strong>Bot:</strong> ${data.response}</div>`;
inputField.value = "";
chatbox.scrollTop = chatbox.scrollHeight;
})
.catch(err => console.error("Error:", err));
});
// Allow pressing Enter to send the message
document.getElementById("message").addEventListener("keyup", function(event) {
if (event.key === "Enter") {
document.getElementById("send").click();
}
});
</script>
</body>
</html>
Running Your Chatbot
- Start the Backend Server: In your terminal, run:
python app.py
The Flask server will start at http://127.0.0.1:5000/
.
Access the Chat Interface:
Open your browser and navigate to http://127.0.0.1:5000/
to load the chatbot interface.
Start Chatting:
Type a message in the input field and hit "Send"
(or press Enter). Your message will be sent to the Deepseek model via Ollama, and the chatbot’s response will be displayed.
Future Enhancements
Once you’ve got the basic chatbot up and running, consider the following improvements:
Conversation Context: Extend the backend to maintain conversation history for more context-aware responses.
Model Optimization: Experiment with different prompt designs or tweak the Ollama CLI parameters to fine-tune response behavior.
Dockerize the App: Containerize your application for easier deployment and environment consistency.
UI/UX Improvements: Enhance the frontend with frameworks like React or Vue.js and add features like message timestamps, theming, or even voice input.
Conclusion
Building a local AI-powered chatbot with Deepseek and Ollama is a rewarding project that puts powerful language models at your fingertips—without relying on cloud services. In this guide, we walked through setting up a Flask backend, integrating Deepseek via the Ollama CLI, and creating a user-friendly web interface.
Feel free to share your thoughts or enhancements in the comments. Happy coding, and enjoy exploring the possibilities of local AI!
Happy coding! 🚀
Let connect on LinkedIn and checkout my GitHub repos:
Top comments (0)