aiml

AI Fundamentals

Build the parts of a docs assistant: model requests, context, structured output, document ranking, validated tool calls and evaluations.

What is an AI application?

An AI application uses a model to process inputs and produce outputs. These lessons focus on applications built around language models. A language model generates text from the request it receives and the weights learned during training. Application code chooses the instructions, documents, history and tools supplied with that request. These lessons separate those responsibilities before combining them.

Follow the fictional docuctl documentation assistant: ask a rollback question, supply the relevant page, then inspect tokens, structured replies, document ranking and one proposed tool call. The final evaluation checks answers against fixed cases.

What you'll learn

  • Trace tokenization, next-token generation, context limits and sampling in recorded model runs.
  • Call local and hosted models, validate structured responses, and distinguish runtime errors from incorrect answers.
  • Rank docs with embeddings and check a model-proposed tool call before executing it.
  • Test prompt injection controls and compare answers against fixed evaluation cases.

Lessons

Module 1 · 4 lessons · about 37 minHow LLMs work

Run inference, inspect tokens and next-token scores, then assemble messages for one model request.

  1. Run inference with a local LLMRead

    Run a language model with and without supplied documentation, and explain how request context differs from trained model weights.

    9 min readLabQuiz

  2. Count tokens with the model's tokenizerRead

    Tokenize docuctl docs, commands and IDs with Gemma's own tokenizer, then compare your counts with what Ollama actually processed, chat template included.

    9 min readLabQuiz

  3. Trace next-token generationRead

    Trace generation one token at a time, inspect the candidate scores, and compare how supplied documentation changes the model’s next-token predictions.

    9 min readLabQuiz

  4. Send chat messages within the context windowRead

    Build a multi-turn chat request for the docuctl docs assistant, resend the history Ollama needs, and detect when the configured context window loses input.

    10 min readLabQuiz

Module 2 · 4 lessons · about 40 minCalling models from code

Control generation, call local and hosted models, validate structured replies and compare document vectors.

  1. Control sampling, output limits and streamingRead

    Control response variation and length, stream a reply as it arrives, and distinguish a finished answer from a cutoff or timeout.

    10 min readLabQuiz

  2. Call a local model and a hosted APIRead

    Send one docuctl docs question to Gemma through Ollama and to Gemini through its API, compare latency and token usage, and handle each failure.

    10 min readLabQuiz

  3. Get structured output and validate it with PydanticRead

    Make the docuctl docs assistant return a typed answer: send a Pydantic JSON schema as the output format, validate every reply, and see its limits.

    10 min readLabQuiz

  4. Rank documents with embeddingsRead

    Embed the docuctl docs pages and questions with a local model, rank pages by cosine similarity, and see the two ways similarity search misleads.

    10 min readLabQuiz

Module 3 · 3 lessons · about 33 minTools, safety and evals

Validate proposed tool calls, keep people in control of consequential actions and check answers against cases.

  1. Validate a model-proposed tool callRead

    Send a read tool schema to a local model, inspect one proposed call, and reject unknown names or invalid page arguments before the tool function runs.

    11 min readLabQuiz

  2. Defend a tool-using assistant against prompt injectionRead

    Test whether document text can trigger an unauthorized tool call, and compare prompt warnings with application permission checks.

    11 min readLabQuiz

  3. Write your first eval for an LLM appRead

    Write fixed cases for the docuctl docs assistant, grade every answer in code, run three trials per case, and use the results to judge two attempted fixes.

    11 min readLabQuiz