DEVSAIO

Build an AI chatbot web app with Python and Flask

A complete, minimal chatbot: Flask backend that keeps your key safe, streaming answers, and a tiny HTML front end.

DEVSAIO team · 2 min read
Share on X

In about 80 lines you'll have a working chatbot website with streaming answers.

1. Install

bash
pip install flask openai
export DEVSAIO_API_KEY=YOUR_KEY

2. The backend (app.py)

python
import os
from flask import Flask, request, Response, send_file
from openai import OpenAI

app = Flask(__name__)
client = OpenAI(base_url="https://devsaio.lol/v1", api_key=os.environ["DEVSAIO_API_KEY"])
SYSTEM = {"role": "system", "content": "You are a friendly assistant. Keep answers short unless asked for detail."}

@app.get("/")
def index():
    return send_file("index.html")

@app.post("/chat")
def chat():
    history = request.json.get("messages", [])[-16:]  # keep the last 16 messages
    def gen():
        stream = client.chat.completions.create(
            model="claude-sonnet-4-6", messages=[SYSTEM] + history, stream=True, max_tokens=800)
        for chunk in stream:
            if chunk.choices and chunk.choices[0].delta.content:
                yield chunk.choices[0].delta.content
    return Response(gen(), mimetype="text/plain")

if __name__ == "__main__":
    app.run(debug=True)

3. The front end (index.html)

html
<!doctype html>
<meta charset="utf-8">
<title>My chatbot</title>
<div id="log"></div>
<form id="f"><input id="q" autocomplete="off" placeholder="Ask anything"><button>Send</button></form>
<script>
const msgs = [], log = document.getElementById("log");
document.getElementById("f").onsubmit = async (e) => {
  e.preventDefault();
  const q = document.getElementById("q"); const text = q.value.trim(); if (!text) return; q.value = "";
  msgs.push({ role: "user", content: text });
  log.insertAdjacentHTML("beforeend", "<p><b>You:</b> </p><p><b>Bot:</b> <span></span></p>");
  log.querySelectorAll("p")[log.querySelectorAll("p").length - 2].append(text);
  const out = log.querySelector("p:last-child span");
  const res = await fetch("/chat", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ messages: msgs }) });
  const reader = res.body.getReader(), dec = new TextDecoder(); let answer = "";
  while (true) { const { value, done } = await reader.read(); if (done) break; answer += dec.decode(value, { stream: true }); out.textContent = answer; }
  msgs.push({ role: "assistant", content: answer });
};
</script>

Note the use of append and textContent: model output is inserted as text, never as HTML, which prevents script injection.

4. Run it

bash
python app.py

Open http://127.0.0.1:5000.

Before going live

  • Add per-user rate limiting and a monthly limit on the key.
  • Put it behind a real web server (gunicorn + nginx), and turn off proxy buffering for /chat so streaming works.
  • Swap the model by changing one string.
Try it with one API key

Claude, GPT, Gemini, DeepSeek, image and video models through one OpenAI- and Anthropic-compatible endpoint. Prepaid, pay only for what you use.

Keep reading