Start learning free

LESSON 14 OF 129 · AI ENGINEERING: CALLING A MODEL

An AI Endpoint in Flask in Python

Practice

So far your model calls lived in scripts that only you can run. The shop's website can't run a script, and it must never hold the API key. It can send a request to your server, though. So the model call goes inside a route: the browser asks your server, your server asks the model, and the key stays on the server.

Check before you work: a route like this first reads the JSON the client sent, and checks it before doing anything else. Here is the idea on another route, with no model in it:

@app.post("/shout")
def shout():
    data = request.get_json()
    word = data.get("word")
    if word is None:
        return jsonify({"error": "A word is required"}), 400
    return jsonify({"shout": word.upper()})

data.get("word") gives None when the key is missing, instead of crashing. A missing value is the client's mistake, so the answer is a 400 that says what to fix. The function ends at that return, before the real work.

In this lesson the real work is a model call, the slow and paid part of the route. Your code must answer these requests like this:

POST /ask  {"question": "When are you open?"}  →  200  {"answer": "We are open from 8:00 to 20:00, every day."}
POST /ask  {}  →  400  {"error": "A question is required"}
POST /ask  {"question": ""}  →  400  {"error": "A question is required"}
POST /ask  {"question": "   "}  →  400  {"error": "A question is required"}
POST /ask  {"question": "Do you sell teapots?"}  →  200  {"answer": "Yes, we do. The Teapot costs $22."}
GET /check/model-calls  →  200  {"model_calls": 2}

The last line counts the model calls: five requests came in, but the model was called only twice, once for each real question.

Your task: the app, the client, MODEL and the SYSTEM prompt are already there. Write the route POST /ask: read the question from the JSON body, send it to the model as one user message, and answer {"answer": ...} with the reply's text. A missing, empty or all-spaces question gets a 400 with {"error": "A question is required"}, and the model is not called for it.


Assignment
Follow the Coding Tutorial to build the POST /ask route: first the model call that answers a question, then the check that stops bad requests with a 400.


Hint
The check goes right after the line that reads question and before the model call. A question must be a string, and it must not be empty once .strip() removes the spaces around it. Remember the , 400 after the error body.


STEP 1 OF 9
    Ctrl + Enter to check

    Ai Flask Endpoint in Python

    Keep going with Python1,600+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free