Clatter LabsHelp Center

How do I call the Model API from a Flask app?

How-toUpdated
import os
from flask import Flask, request, jsonify
from openai import OpenAI

app = Flask(__name__)
client = OpenAI(
    base_url="https://api.gateway.clatterlabs.xyz/v1",
    api_key=os.environ["CLATTER_API_KEY"],
    default_headers={"X-Clatter-Account": os.environ["CLATTER_ACCOUNT_ID"]},
)

@app.post("/ask")
def ask():
    q = request.get_json()["question"]
    r = client.chat.completions.create(model="supercosmos-flash", messages=[{"role": "user", "content": q}])
    return jsonify(answer=r.choices[0].message.content, usage=r.usage.model_dump())

Create the client once (not per request), and add your own authentication in front of the route.

Good to know#

  • Keep keys on your server, never in a browser or mobile app. Why
  • Each key has its own limit: 10, 30 (default), 60 or 120 requests per minute. Rate limits
  • Every call shows in Console › Logs and Usage as Model API, with its price. See what you spent

Still need help?#

We usually reply within the first hour, and always within 24 hours. A full support team is with you.

Related articles

Still need help?

Tried the steps above and still stuck? Email us. We usually reply within the first hour, and always within 24 hours. A full support team is with you.

Infrastructure questions, or permanent deletion of your data
infrastructure@clatterlabs.xyz
For deleting your whole account or the data of one service.