CourseAI Engineering: The Complete Course · Course guide · part 3 of 105
Part 3 · Course guide

Setup: Colab, Your Model Provider, and Running Locally

9 min read·13 Oct 2026

This takes about ten minutes. You do it once.

1. Choose a provider

The course works with three ways of reaching a language model. You only need one. You switch by changing one word in the setup cell.

ProviderWhat it isWhere you run itCost
GroqA cloud service that runs open models very fastGoogle Colab, or your own computerFree plan with daily limits
Google GeminiGoogle's own models, through an APIGoogle Colab, or your own computerFree tier with limits
OllamaA program that runs a model on your own machineYour own computer, in a code editorFree; you supply the computer

If you are using Colab, choose Groq or Gemini. They need a free key and nothing else.

If you want Ollama, work on your own computer in an editor such as VS Code. Ollama runs the model on your machine, so no key is needed and nothing you type leaves your computer. Section 5 shows the local setup.

Free plans limit how many requests you can send per minute and per day. If you hit a limit you get an error with the number 429. Wait a minute and try again, or switch provider.

Model names change. The names below were checked against each provider's documentation in October 2026. If a call fails with "model not found", open the provider's model list (links below) and put a current name into the setup cell. Nothing else in the lesson needs to change.
Groq free key Colab Gemini free key Colab Ollama your computer no key
Groq and Gemini are reached from Colab with a free key; Ollama runs the model on your own computer and needs no key.

2. Get a key (Groq or Gemini)

Groq

  1. Go to console.groq.com and sign in.
  2. Open API Keys and create a key.
  3. Copy it. You will not be shown it again.

Model list: console.groq.com/docs/models

Google Gemini

  1. Go to aistudio.google.com and sign in.
  2. Choose Get API key and create a key.
  3. Copy it.

Model list: ai.google.dev/gemini-api/docs/models

3. Store the key safely in Colab

Never paste a key into a code cell. Notebooks get shared, and a key in a shared notebook is a key someone else can spend.

  1. Open colab.research.google.com and create a new notebook.
  2. In the left sidebar, click the key icon (Secrets).
  3. Add a secret. The name must be exactly GROQ_API_KEY (for Groq) or GOOGLE_API_KEY (for Gemini). The value is your key.
  4. Turn on Notebook access for it.

The secret is remembered across notebooks. For each new notebook you only switch access on.

4. The setup cell

Every lesson that calls a model begins with these two cells. Paste them, set PROVIDER, and run.

The course uses LangChain, a widely used Python library for building applications on language models. One of the things it does is give every provider the same interface, so the rest of your code does not care which one you chose. Later modules also use LangGraph, its companion library for building multi-step workflows and agents.

python
%pip install -q -U langchain langgraph langchain-groq langchain-google-genai langchain-ollama
python
import os
from langchain.chat_models import init_chat_model

PROVIDER = "groq"   # "groq" or "gemini" in Colab; "ollama" on your own computer
MODELS = {"groq": "groq:openai/gpt-oss-20b", "gemini": "google_genai:gemini-3.5-flash-lite", "ollama": "ollama:llama3.2:3b"}
KEY = {"groq": "GROQ_API_KEY", "gemini": "GOOGLE_API_KEY", "ollama": None}[PROVIDER]
if KEY and KEY not in os.environ:
    from google.colab import userdata   # keys live in Colab's Secrets panel
    os.environ[KEY] = userdata.get(KEY)
llm = init_chat_model(MODELS[PROVIDER], temperature=0, max_retries=6)
python
print(llm.invoke("Reply with the single word: ready").text)

If it prints ready (or something close), you are set up.

Code walkthrough

  • %pip install ... installs the libraries once per notebook. langchain and langgraph are the frameworks; the three langchain-... packages are the connectors for each provider.
  • PROVIDER = "groq" is the one word you change to switch provider.
  • MODELS names the model for each provider as provider:model-name. This is the only place model names appear, so when a name changes you edit one line.
  • KEY is the name of the secret the provider needs. Ollama needs none.
  • The if block runs only in Colab when the key is not already set: it copies your key from the Secrets panel into an environment variable, which is where LangChain looks for it.
  • init_chat_model(...) creates the model object, llm. temperature=0 asks for the most consistent output. max_retries=6 makes the client wait and try again when the provider says "too many requests" (a 429); Groq's free plan allows 8,000 tokens a minute, which a long block can pass. It does not retry a reply the provider rejected as malformed: see "When the provider rejects a reply" below.
  • llm.invoke(...) sends one message and returns a reply; .text is the reply as a string.

Why one cell works for three providers

init_chat_model reads the part before the colon (groq, google_genai, ollama) and builds the right connector. All connectors offer the same methods: .invoke() to get a reply, .stream() to get it piece by piece, and so on. Your code talks to llm and never mentions the provider again.

This is also how real teams avoid being locked to one provider: the provider is a setting, not something written all through the code.

When the provider rejects a reply

Sometimes a provider refuses the model's own reply with an error instead of returning it. In test runs on 12 October 2026, Groq did this a few times with openai/gpt-oss-20b: an answer that should have been JSON came back empty, or the model called a tool it was never given. max_retries does not help, because the request itself was fine. The course handles it the same way everywhere: try once more, and if that also fails, say so plainly instead of crashing.

python
def ask(model, messages):
    """Returns (reply, None), or (None, reason) after two refusals."""
    for attempt in range(2):                     # one retry: the next reply is usually fine
        try:
            return model.invoke(messages), None
        except Exception as err:
            last = err
    return None, f"provider_error: {type(last).__name__}"

Code walkthrough

  • ask sends the messages and returns the reply with no reason, or no reply with a reason.
  • On an error it tries once more; after a second error it gives up and names the error, for example provider_error: BadRequestError.

Lessons use this shape: a BrightCart step that gets no answer, such as labelling a ticket or drafting a reply, returns a clear "no answer" result with the reason provider_error, and the next step handles it, so one bad reply never stops a notebook.

Prices and limits (checked 9 October 2026)

This is the course's one table of model prices and limits. Lessons that count cost say "prices from course-guide/02" and use these numbers; when a provider changes them, this table changes and the lessons follow. Prices are US dollars per 1,000,000 tokens on each provider's paid plan.

Model (as written in MODELS)InputOutputContext windowNotes
groq:openai/gpt-oss-20b (course default)$0.075$0.30131,072 tokens, up to 65,536 outputGroq's rate-limits page lists 30 requests a minute, 1,000 a day, 8,000 tokens a minute and 200,000 a day for this model
groq:openai/gpt-oss-120b (stronger)$0.15$0.60131,072 tokens, up to 65,536 output
google_genai:gemini-3.5-flash-lite (course default)$0.30$2.50Output price includes thinking tokens
google_genai:gemini-3.8-flash (stronger)$0.75$3.75Listed price until 31 December 2026; $1.50 and $7.50 from 1 January 2027
ollama:llama3.2:3b and ollama:llama3.2:1b$0$0No fee per token; you pay for the machine

Sources: Groq, "Supported models" (https://console.groq.com/docs/models) and "Rate limits" (https://console.groq.com/docs/rate-limits); Google, "Gemini Developer API pricing" (https://ai.google.dev/gemini-api/docs/pricing). Free plans change often and your own limits are on each provider's console page; these are the most likely numbers in this course to go out of date.

5. Running on your own computer (required for Ollama)

You can run every lesson locally with any provider. For Ollama it is the way to do it.

Install

  1. Python 3.10 or newer from python.org. Check with python --version (on some systems python3 --version).
  2. A code editor. VS Code with the Python and Jupyter extensions is a good choice. Cursor, PyCharm and others work the same way.
  3. Ollama, if you are using it, from ollama.com/download. After installing, download the course's default model once, in a terminal:
bash
ollama pull llama3.2:3b

That is a download of about 2 GB. The model runs acceptably on a recent laptop with 8 GB of memory or more. A small local model makes more mistakes than the cloud models. That is useful for this course: several "break it" steps are easier to see on a small model.

Create a project folder

In a terminal:

bash
mkdir ai-engineering-course
cd ai-engineering-course
python -m venv .venv

Switch the virtual environment on:

bash
# macOS and Linux
source .venv/bin/activate

# Windows (PowerShell)
.venv\Scripts\Activate.ps1

Then install the libraries:

bash
pip install -U langchain langgraph langchain-groq langchain-google-genai langchain-ollama pydantic python-dotenv ipykernel

Keys on your own computer

Ollama needs no key. For Groq or Gemini, create a file named .env in the project folder:

text
GROQ_API_KEY=paste-your-key-here
GOOGLE_API_KEY=paste-your-key-here

and add these two lines at the very top of the setup cell:

python
from dotenv import load_dotenv
load_dotenv()

Never share the .env file or commit it to Git.

Run the lessons

Open the folder in your editor and create a new notebook file, for example module-07-lesson-1.ipynb. Select the .venv Python when asked. A notebook in VS Code works like Colab: paste each code block into a cell and run it.

Set PROVIDER = "ollama" in the setup cell. You can skip the %pip install cell, because you installed the libraries already.

On a Mac: FAISS and PyTorch together

On macOS, loading FAISS and PyTorch in the same Python process can crash with an OpenMP error, because each brings its own copy of the OpenMP library. If you see it, set two environment variables in the terminal before you start Python, Jupyter or your editor:

bash
export KMP_DUPLICATE_LIB_OK=TRUE
export OMP_NUM_THREADS=1

The first tells OpenMP to accept the second copy; it is a workaround, not a fix. The second keeps OpenMP to one thread, which is slower on large indexes but fine for the small ones in this course. The course's own tests of Module 8 Lesson 2 ran on a Mac with both set. You do not need this in Colab.

From notebook cells to a project

In a job you would not keep code in notebook cells. The course grows one repository, a single package called brightcart/. Module 1 Lesson 3 starts it: the setup cell becomes brightcart/llm.py with the llm object created once, the answer function goes in brightcart/answer.py, and a tests/ folder holds the checks as pytest tests. The Module 1 lab adds client.py and log.py. Module 10 adds the web service inside the same package as brightcart/api/. Module 13 puts that same brightcart/ folder in a container and deploys it, and Module 14's capstone collects everything in it. Labs that must run in a temporary folder for testing create brightcart/ there with the same layout and say so. The project page (course-guide/03) lists which module adds which file.

PostgreSQL versions

The course's PostgreSQL code runs on PostgreSQL 16 to 18: Module 8 uses a pgvector Docker image on 17, Module 11 an embedded server on 16, and Module 13 version 18. Nothing in the lessons depends on the difference.

6. When something goes wrong

What you seeWhat it meansWhat to do
SecretNotFoundErrorThe secret is missing or misnamedCheck the name is exactly GROQ_API_KEY or GOOGLE_API_KEY
NotebookAccessErrorThe secret exists but this notebook cannot read itTurn on Notebook access for the secret
ModuleNotFoundError: google.colabRunning locally with no key setCreate the .env file and add the two load_dotenv lines
AuthenticationError (401)The key is wrong or was deletedCreate a new key and update the secret
NotFoundError (404) mentioning the modelThe model name is out of datePut a current model name in MODELS
RateLimitError (429)You hit the free plan's limitWait a minute, or switch PROVIDER
A connection error with OllamaOllama is not runningOpen the Ollama app, or run ollama serve in a terminal
Ollama says the model is not foundThe model has not been downloadedRun ollama pull llama3.2:3b