In about 80 lines you'll have a working chatbot website with streaming answers.
1. Install
pip install flask openai
export DEVSAIO_API_KEY=YOUR_KEY2. The backend (app.py)
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)
<!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
python app.pyOpen 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
/chatso streaming works. - Swap the model by changing one string.
Claude, GPT, Gemini, DeepSeek, image and video models through one OpenAI- and Anthropic-compatible endpoint. Prepaid, pay only for what you use.