This content originally appeared on DEV Community and was authored by Dione Castro Alves
Introduction
Chatbots powered by Large Language Models (LLMs) are becoming increasingly popular across industries, from customer service to productivity tools. However, many implementations can be complex or require heavy infrastructure.
Flask, a lightweight Python framework, allows developers to quickly prototype and deploy chatbots powered by LLMs. In this guide, youβll learn how to build a simple yet functional chatbot using Flask and an external LLM API.
The goal: simplicity + practicality.
Requirements and Tools
Before you start, make sure you have the following:
Python 3.9+
Flask
Requests or HTTPX (for API calls)
Any LLM API endpoint (GPT, Claude, Perplexity, etc.)
Basic HTML/CSS for the frontend
(Note: The approach here is API-agnostic. You can connect to OpenAI, Anthropic, or any LLM provider.)
Project Structure
Hereβs the structure of the project:
chatbot-flask/
βββ app.py
βββ templates/
β βββ index.html
βββ static/
β βββ style.css
βββ requirements.txt
Backend with Flask (app.py)
from flask import Flask, render_template, request, jsonify
import requests
app = Flask(name)
API_URL = “https://api.example-llm.com/chat” # Replace with your LLM endpoint
@app.route(“/”)
def index():
return render_template(“index.html”)
@app.route(“/chat”, methods=[“POST”])
def chat():
user_input = request.json[“message”]
response = requests.post(API_URL, json={“prompt”: user_input})
return jsonify(response.json())
if name == “main“:
app.run(debug=True)
Simple Frontend (index.html)
<!DOCTYPE html>
Flask Chatbot
Chatbot with Flask
Send
<script>
async function sendMessage() {
let message = document.getElementById("userInput").value;
let response = await fetch("/chat", {
method: "POST",
headers: {"Content-Type": "application/json"},
body: JSON.stringify({message})
});
let data = await response.json();
document.getElementById("chatbox").innerHTML += "<p><b>You:</b> " + message + "</p>";
document.getElementById("chatbox").innerHTML += "<p><b>Bot:</b> " + data.reply + "</p>";
}
</script>
Next Steps
This is just the starting point. You can expand this chatbot in many ways:
Add authentication to secure the API.
Implement conversation memory (chat history).
Switch between different LLM providers (OpenAI, Claude, Perplexity).
Deploy to cloud platforms like Heroku, Render, or Vercel.
Conclusion
By combining Flask with modern LLMs, developers can build functional chatbots quickly and easily. This setup is perfect for prototyping customer service bots, productivity assistants, or internal AI helpers.
Now itβs your turn: try the code, customize it, and share what you build with the community!
This content originally appeared on DEV Community and was authored by Dione Castro Alves