Intermediate·30 min·0/52 done

Your First Real AI Call

Run a free AI model on your own laptop with Ollama and talk to it from Python. No API key, no credit card.

Why it matters: Every GenAI app is this same loop: build a JSON request, send it, read the reply. Do it once on your laptop and every AI API looks familiar.

Basics

What you will do

You will make an AI model answer you from your own Python code:

  1. Install Ollama, a free app that runs AI models on your computer.
  2. Download one small model.
  3. Send it a question from Python and print the answer.

This website runs Python inside your browser, and the browser cannot reach the AI on your laptop. So you practise the code here (the exercise checks it), then run the real thing on your laptop in the last part of this lesson.

Tiny example

A question is just a string.

model = "llama3.2"
question = "What is MLOps?"
print("Asking", model, "->", question)

Basics

The request is just a dict

You send the model a dict, turned into JSON text:

  • model - which model should answer
  • messages - a list of {"role": ..., "content": ...} dicts. system sets the rules, user is the question
  • stream - False means "send the whole answer at once"

Common mistake: sending a plain string instead of the messages list.

Tiny example

Build the request and print it as JSON.

import json
request = {
    "model": "llama3.2",
    "messages": [{"role": "user", "content": "What is MLOps?"}],
    "stream": False,
}
print(json.dumps(request, indent=2))

Basics

Read the reply

The model sends back JSON text. Turn it into a dict with json.loads, then dig in:

  • data["choices"][0]["message"]["content"] is the answer
  • data["usage"]["total_tokens"] is how many tokens the call used

AI answers often start or end with spaces or new lines, so .strip() the text before you use it.

Tiny example

The answer is inside choices, then message, then content.

import json
raw = '{"choices": [{"message": {"role": "assistant", "content": "Hello!"}}]}'
data = json.loads(raw)
print(data["choices"][0]["message"]["content"])

Basics

Same code, any provider

This request shape (model plus messages, sent to /v1/chat/completions) is the OpenAI-compatible format. OpenAI and many other hosted providers accept it, and Ollama copies it on purpose.

To switch to a hosted model later, change three things: the URL, the model name, and an Authorization: Bearer <key> header.

Common mistake: pasting an API key into your code. Keep keys in environment variables and out of GitHub.

Tiny example

Read the URL and key from environment variables.

import os
url = os.environ.get("LLM_URL", "http://localhost:11434/v1/chat/completions")
key = os.environ.get("LLM_API_KEY", "")
headers = {"Content-Type": "application/json"}
if key:
    headers["Authorization"] = f"Bearer {key}"
print(url, list(headers))

Then AI / ML

AI / ML examples

Same idea, used in real AI work. Press Try in lab to run it.

AI / ML example

AI / ML: keep a chat history

To hold a conversation, send the whole history every time.

In AI / ML: Chatbots remember earlier turns only because the app re-sends them.

example.py

Python

messages = [{"role": "system", "content": "Be brief."}]
messages.append({"role": "user", "content": "What is Docker?"})
messages.append({"role": "assistant", "content": "It packages apps with their dependencies."})
messages.append({"role": "user", "content": "And Kubernetes?"})
for m in messages:
    print(f"{m['role']:>9}: {m['content']}")

AI / ML example 2

AI / ML: never crash on a strange reply

Return a fallback when the reply is not what you expected.

In AI / ML: Production apps log the bad reply and show a friendly message instead of crashing.

example.py

Python

import json

def safe_answer(raw, fallback="Sorry, no answer this time."):
    try:
        text = json.loads(raw)["choices"][0]["message"]["content"].strip()
    except (ValueError, KeyError, IndexError, TypeError):
        return fallback
    return text or fallback

print(safe_answer('{"choices": []}'))
print(safe_answer('{"choices": [{"message": {"content": " Hi! "}}]}'))

Your turn · answer checked automatically

Exercise

Write the two functions you will run on your laptop. build_request(question) returns a dict with model "llama3.2", stream set to False, and two messages: the SYSTEM text as a system message, then the question as a user message. get_answer(raw) takes the raw JSON text the model sends back and returns the answer text, stripped.

Edit the code, then press Run & check. Passing it marks this lesson done.

python console

Exercise

~/lesson.py

Ctrl+Enter

console output

ready

$ waiting for Run...

On your laptop

Run the real thing on your computer

Now the real thing. This takes about 15 minutes, most of it waiting for the model to download. Everything is free and stays on your computer.

  1. 1

    Download Ollama from ollama.com and install it. It works on Windows, Mac and Linux.

  2. 2

    Open a terminal and download a small model (about 2 GB, one time only). On an older laptop with little memory, use llama3.2:1b instead, and change MODEL in the file to match.

    ollama pull llama3.2
  3. 3

    Check that the model answers in the terminal:

    ollama run llama3.2 "Say hi in five words"
  4. 4

    Install Python from python.org if you have not yet (on Windows, tick Add python.exe to PATH). Make a folder, save the file below into it with the Download button, and open a terminal in that folder. On Mac and Linux, type python3 instead of python.

first_ai_call.py

"""Your first real AI call: ask a free local model a question from Python."""
import json
import urllib.error
import urllib.request

URL = "http://localhost:11434/v1/chat/completions"
MODEL = "llama3.2"
SYSTEM = "You are a friendly tutor. Answer in 2 short sentences."


def build_request(question):
    return {
        "model": MODEL,
        "messages": [
            {"role": "system", "content": SYSTEM},
            {"role": "user", "content": question},
        ],
        "stream": False,
    }


def get_answer(raw):
    data = json.loads(raw)
    return data["choices"][0]["message"]["content"].strip()


def ask(question):
    body = json.dumps(build_request(question)).encode("utf-8")
    request = urllib.request.Request(URL, data=body, headers={"Content-Type": "application/json"})
    with urllib.request.urlopen(request, timeout=120) as response:
        return get_answer(response.read().decode("utf-8"))


if __name__ == "__main__":
    try:
        print(ask("What is MLOps? Explain it to a college fresher."))
    except urllib.error.HTTPError as err:
        print(f"Ollama answered with an error ({err.code}). Did you download the model? Run: ollama pull {MODEL}")
    except urllib.error.URLError:
        print("Could not reach Ollama. Open the Ollama app (or run: ollama serve), then try again.")

Run it from the same folder:

python first_ai_call.py

You should see something like:

MLOps is the practice of taking machine learning models from a notebook into
production and keeping them reliable. It covers deployment, monitoring and retraining.

(Your answer will be worded differently. AI models rarely say the same thing twice.)

Takeaways

  • 1An AI call is a JSON request with a model and a list of messages.
  • 2The answer is at choices[0].message.content. Strip it before using it.
  • 3The same OpenAI-compatible shape works with Ollama and many hosted providers.