A Practical Introduction to Large Language Models

Vancouver Independent School for Science and Technology

Daniel Chen, MPH, PhD

Hello

Daniel Chen

What I do

  • Lecturer, UBC Statistics, Master’s of Data Science
  • Developer Advocate, Posit, PBC
    • Talks, Workshops, Documentation
    • Shiny for Python
    • Get things working for the MDS team

Daniel Chen

What I’ve done

  • Macaulay Honors College CUNY Hunter College
    • Psychology + Neuroscience: Learning and memory
  • Columbia University Mailman School of Public Health
    • Epidemiology: Spread of ideas in social networks
  • Virginia Tech
    • Data science education in biomedical sciences
  • The Carpentries, 2014

Daniel Chen

What I enjoy

Daniel Chen Daniel Chen

A Practical Introduction to LLMs

Poll: How skeptical are you about LLMs?

You?

  • Python (or some other programming language)
    • variables, functions, lists, loops, conditionals
    • objects, classes, methods, and attributes
  • Familiar with Large Language Models (LLMs)
    • ChatGPT
    • Claude?
    • Any others?

Maybe you?

  • Application programming interfaces (APIs)
  • HTTP requests
  • GitHub account
  • Python virtual environments

Today

  • Know how LLM interactions work
  • Set up a simple chat with an LLM using Python
  • Give you a framework to build on

Packages

Let’s chat

Your first chat: Setup

import os

import chatlas as clt
from dotenv import load_dotenv

load_dotenv()
True

Your first chat: Provider

chat = clt.ChatAnthropic(api_key=os.getenv("ANTHROPIC_API_KEY"))

Your first chat: Chat

chat.chat("what is the capital of the moon?")


The Moon doesn’t have a capital city. The Moon is Earth’s natural satellite - a rocky celestial body in space with no permanent human settlements, cities, or government that would require a capital.

However, if you’re asking this as part of a creative writing project, science fiction scenario, or hypothetical future lunar colonization discussion, I’d be happy to help explore that idea! In science fiction, lunar capitals are often imagined to be located in places like the Sea of Tranquility or inside protective domes in lunar craters.

<chatlas._chat.ChatResponse at 0x115d949d0>

Your first chat: Turn

chat.chat("are you sure?")


Yes, I’m sure. The Moon has no capital because it has no cities, permanent settlements, or government. It’s an uninhabited celestial body.

The only humans who have ever been to the Moon were the Apollo astronauts between 1969-1972, and they were only there for brief visits. Currently, there are no people living on the Moon at all.

Is there perhaps a specific context you’re thinking of - like a joke, riddle, fictional story, or hypothetical scenario? I’d be happy to engage with that if you let me know what you have in mind!

<chatlas._chat.ChatResponse at 0x115d95e00>

Your turn!

Your turn: Python environment

python -m venv .venv
source .venv/bin/activate
pip install chatlas python-dotenv openai anthropic
uv init
source .venv/bin/activate
uv add chatlas python-dotenv openai anthropic

Your turn: API key

  • You should have an API key
  • Do not share this, especially online

Your turn: Chat!

Create 2 files

  1. .env: stores the python environment variables
ANTHROPIC_API_KEY="your_api_key_here"
OPENAI_API_KEY="your_api_key_here"
  1. chat.py: python script to run the chat
import os
import chatlas as clt
from dotenv import load_dotenv

load_dotenv()  # this loads the variables from .env

chat = clt.ChatAnthropic(api_key=os.getenv("ANTHROPIC_API_KEY"))
chat.chat("what is the capital of the moon?")

Modifying behaviors

The system prompt

  • Instructions that you give the model to change its behavior.
  • It’s really one of the only ways you can change how a model will respond.
  • If you see a result you want to tweak or specify more, modify the system prompt!
  • System prompts will grow as you use your model
  • Prompts may change as you use different models (or update models)

System prompt example

import os
import chatlas as clt
from dotenv import load_dotenv

load_dotenv()

chat = clt.ChatAnthropic(
    api_key=os.getenv("ANTHROPIC_API_KEY"),
    system_prompt="You only speak in rhyme.",
)
chat.chat("what is the capital of the moon?")


The Moon has no capital, I must say, No cities or governments hold sway. It’s a barren sphere of rock and dust, Where no earthly rules or laws are thrust.

Just craters, plains, and ancient stone, This celestial body stands alone. No capitals crown its silver face, Just silent beauty floating through space.

<chatlas._chat.ChatResponse at 0x1167d2140>

Prompts are usually long

  • Usually you save your prompt into a separate file (usually in markdown format)
  • LLMs actually like it when you use a bit of formatting
  • You can even use a bit of {templating} to customize prompts

Example: File w/ templates

Prompt file:

I want you to act like {{ role }}.
But be brief and concise in your responses.

Code:

import os
import chatlas as clt
from dotenv import load_dotenv

load_dotenv()

chat = clt.ChatAnthropic(
    api_key=os.getenv("ANTHROPIC_API_KEY"),
    system_prompt=clt.interpolate_file(
        "code/prompt.md",
        variables={"role": "Yoda"},
    ),
)
chat.chat("what is the capital of the moon?")
'Capital of the Moon, there is not. No cities or governments, the Moon has. Only rock and dust, it contains. Hmm.'

Your turn!

Prompt file:

I want you to act like {{ role }}.
But be brief and concise in your responses.

Code:

import os
import chatlas as clt
from dotenv import load_dotenv

load_dotenv()

chat = clt.ChatAnthropic(
    api_key=os.getenv("ANTHROPIC_API_KEY"),
    system_prompt=clt.interpolate_file(
        "code/prompt.md",
        variables={"role": "Yoda"},
    ),
)
chat.chat("what is the capital of the moon?")

Anatomy of a conversation

Example Conversation

“What’s the capital of the moon?”

"There isn't one."

“Are you sure?”

"Yes, I am sure."

Example Request

curl https://api.openai.com/v1/chat/completions \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer $OPENAI_API_KEY" \
  -d '{
    "model": "gpt-4.1",
    "messages": [
        {"role": "system", "content": "You are a terse assistant."},
        {"role": "user", "content": "What is the capital of the moon?"}
    ]
}'
  • Model: model used
  • System prompt: behind-the-scenes instructions and information for the model
  • User prompt: a question or statement for the model to respond to

Example Response

Abridged response:

{
  "choices": [{
    "message": {
      "role": "assistant",
      "content": "The moon does not have a capital. It is not inhabited or governed.",
    },
    "finish_reason": "stop"
  }],
  "usage": {
    "prompt_tokens": 9,
    "completion_tokens": 12,
    "total_tokens": 21,
    "completion_tokens_details": {
      "reasoning_tokens": 0
    }
  }
}
  • Assistant: Response from model
  • Why did the model stop responding
  • Tokens: “words” used in the input and output

Example Followup Request

curl https://api.openai.com/v1/chat/completions \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer $OPENAI_API_KEY" \
  -d '{
    "model": "gpt-4.1",
    "messages": [
      {"role": "system", "content": "You are a terse assistant."},
      {"role": "user", "content": "What is the capital of the moon?"},
      {"role": "assistant", "content": "The moon does not have a capital. It is not inhabited or governed."},
      {"role": "user", "content": "Are you sure?"}
    ]
}'
  • The entire history is re-passed into the request

Example Followup Response

Abridged Response:

{
  "choices": [{
    "message": {
      "role": "assistant",
      "content": "Yes, I am sure. The moon has no capital or formal governance."
    },
    "finish_reason": "stop"
  }],
  "usage": {
    "prompt_tokens": 52,
    "completion_tokens": 15,
    "total_tokens": 67,
    "completion_tokens_details": {
      "reasoning_tokens": 0
    }
  }
}

Previous usage:

  "usage": {
    "prompt_tokens": 9,
    "completion_tokens": 12,
    "total_tokens": 21,

Tokens

Try it yourself:

Token example

Common words represented with a single number:

  • What is the capital of the moon?
  • 4827, 382, 290, 9029, 328, 290, 28479, 30
  • 8 tokens total (including punctuation)

Other words may require multiple numbers

  • counterrevolutionary
  • counter, re, volution, ary
  • 32128, 264, 9477, 815
  • 4 tokens total
  • 2-3 Tokens ﷺ (Arabic)

Token pricing (Anthropic)

https://www.anthropic.com/pricing -> API tab

Claude Sonnet 4

  • Input: $3 / million tokens
  • Output: $15 / million tokens
  • Context window: 200k

Context window

  • Determines how much input can be incorporated into each output
  • How much of the current history the agent has in the response

For Claude Sonnet:

  • 200k token context window
  • 150,000 words / 300 - 600 pages / 1.5 - 2 novels
  • “Gödel, Escher, Bach” ~ 67,755 words

Context window - chat history

200k tokens seems like a lot of context…

… but the entire chat is passed along each chat iteration

{"role": "system", "content": "You are a terse assistant."},
{"role": "user", "content": "What is the capital of the moon?"},
{"role": "assistant", "content": "The moon does not have a capital. It is not inhabited or governed."},
{"role": "user", "content": "Are you sure?"},
{"role": "assistant", "content": "Yes, I am sure. The moon has no capital or formal governance."}

Free?

Github Models

  • Runs models on Microsoft servers

  • Has access to OpenAI models (and many others)

  • Free tier available on a rate limit

  • You will need to create a GitHub Personal Access Token (PAT).

    • It does not need any context (e.g., repo, workflow, etc).

Example: Github model

Save it into an environment variable, GITHUB_TOKEN

https://github.com/marketplace?type=models

import os

import chatlas as clt
from dotenv import load_dotenv

load_dotenv()

# GITHUB_TOKEN
# GITHUB_API_KEY
chat = clt.ChatGithub(api_key=os.getenv("GITHUB_TOKEN"))
chat.chat("what is the capital of the moon?")


The Moon does not have a capital city because it is not a country or a region governed by people. It is a natural satellite of Earth and does not have permanent human habitation or any formal government or administrative divisions. Therefore, there is no capital of the Moon.

However, in science fiction or hypothetical scenarios, you might encounter imaginary lunar cities or capitals—such as “Luna City”—but these are fictional and not real.

<chatlas._chat.ChatResponse at 0x116e3fc50>

Github default model

chat.get_turns()
[<Turn role='user'>
   <ContentText text='what is the capital of the moon?'>,
 <Turn role='assistant' tokens=(15, 87, 0) finish_reason='stop' completion=ChatCompletion(id='chatcmpl-Cl1xSuXqiOTIUONwXHDM9cZdESYzf', choices=[Choice(finish_reason='stop', index=0, logprobs=None, message=ChatCompletionMessage(content='The Moon does not have a capital city because it is not a country or a region governed by people. It is a natural satellite of Earth and does not have permanent human habitation or any formal government or administrative divisions. Therefore, there is no capital of the Moon.\n\nHowever, in science fiction or hypothetical scenarios, you might encounter imaginary lunar cities or capitals—such as “Luna City”—but these are fictional and not real.', refusal=None, role='assistant', annotations=None, audio=None, function_call=None, tool_calls=None), content_filter_results={'hate': {'filtered': False, 'severity': 'safe'}, 'self_harm': {'filtered': False, 'severity': 'safe'}, 'sexual': {'filtered': False, 'severity': 'safe'}, 'violence': {'filtered': False, 'severity': 'safe'}})], created=1765324978, model='gpt-4.1-2025-04-14', object='chat.completion.chunk', service_tier=None, system_fingerprint='fp_f99638a8d7', usage=CompletionUsage(completion_tokens=87, prompt_tokens=15, total_tokens=102, completion_tokens_details=CompletionTokensDetails(accepted_prediction_tokens=0, audio_tokens=0, reasoning_tokens=0, rejected_prediction_tokens=0), prompt_tokens_details=PromptTokensDetails(audio_tokens=0, cached_tokens=0)), prompt_filter_results=[{'prompt_index': 0, 'content_filter_results': {'hate': {'filtered': False, 'severity': 'safe'}, 'jailbreak': {'filtered': False, 'detected': False}, 'self_harm': {'filtered': False, 'severity': 'safe'}, 'sexual': {'filtered': False, 'severity': 'safe'}, 'violence': {'filtered': False, 'severity': 'safe'}}}], obfuscation='gSAh8cKwFou3yYTJv87aaQI50y6uACGI4fQ3Yu6yBwtjAYa2XiLQkng2dQUu6b2gmWIHFdLwmS7ndG4PKadT5tDUzG1RYEL3jnsGw6ywWqsa516KhMpr43L6F8bPGyrxibRNNUAXU1bMTXWNH48v0qIKrWJmroYYCLyZi2uLq12W9AQGBaHnfG0NWjyLQtsHnmqGDcAsNHW5rfaDu3iLBN3Nnbvg1EEX1263uNtXCnCHlxM7qzmPQaNbunyGKryQZfsJM2We73dUFJt1mGUBOBF4WuqXz3sUj25SkRnvr7PG8g1iwu2L4UoFlPSQ8KFNd9nw3BWN1jrl8yHXc6oObhePRlfHFhIWrdu5xADfjZuIwMLixkzHz9vFQV4XrQhsMojnzZ39xUv5NhplWpux7A0FbYZmqGSqfQJZJ6HG2X8CX8bZJiNEsm7Rt9YEanTST865yoqgguke9WuGTohR4YGpTiAVJ3c4')>
   <ContentText text='The Moon does not have a capital city because it i...'>]

Ollama

  • Completely offline
  • Only real option for guaranteed privacy
  • Models aren’t as good as frontier models
  • Some models take up a lot of space
ollama run qwen3:0.6b

Example: Ollama

import chatlas as clt

chat = clt.ChatOllama(model="qwen3:0.6b")  # 523mb
chat.chat("what is the capital of the moon?")


Okay, so the user is asking about the capital of the Moon. Let me start by confirming the facts. The Moon is indeed a celestial body, and its official capital is usually referred to as the lunar capital. But wait, how does this work if there’s no official government here? I remember that in some cases, especially in countries using a system of heads of state, the capital might be shared. For example, in the United States, the capital is Washington, D.C. So in the context of a country, the term “lunar capital” might be used in a similar way.

But the user is asking about the Moon in general. Maybe they’re looking for a different concept. Alternatively, perhaps they confused the Moon with a different entity. But since the Moon is a natural celestial body, I should stick to that. Another thing to consider is the political aspect. If there’s no actual ruling body on the Moon, how is the capital determined? In some cases, people might refer to it as the capital if it’s associated with important locations.

I should double-check if there is any official definition or use. For example, in some countries, the capital is known as “the Moon capital” or similar. But in the absence of a government, it’s likely that the answer is “the Moon capital” as per the established practice. Also, the user might be interested in the historical or cultural significance, but the question seems to be straightforward.

The Moon is a celestial body, and while it doesn’t have an official capital or government, the term “lunar capital” is commonly used to describe the administrative or governing body associated with it. However, due to the lack of a direct governing institution, the term is often used metaphorically to indicate the importance of a location in the context of celestial or political significance.

In many cases, when considering a location like the Moon, the terms “lunar capital” may refer to its historical or strategic role, rather than a literal capital. If the question is specifically about the Moon’s political entity, it might be understood as part of the broader context of celestial or strategic importance.

For clarity and adherence to standard practices, the answer is usually interpreted as “the Moon capital” in the context of the celestial or administrative domain.

<chatlas._chat.ChatResponse at 0x116ef1e50>

Tools and functions

What are tools?

  • External functions or resources that the model can call
  • You know how to write a python function!

Register tools (Agents)

import chatlas as clt


# doc strings and type hints provide tool context
def capital_finder(location: str) -> str:
    """Sets the capital of the moon as NYC"""
    if location.lower() == "moon":
        return "NYC"


chat = clt.ChatAnthropic()
chat.register_tool(capital_finder)
chat.chat("what is the capital of the moon?")

Register tools (Agents)


I’ll help you find the capital of the moon using the available tool.

# 🔧 tool request (toolu_01L5evZkHnihx2vYRFWmSS1w)
capital_finder(location=moon)
# ✅ tool result (toolu_01L5evZkHnihx2vYRFWmSS1w)
NYC


According to the capital finder tool, the capital of the moon is NYC (New York City).

Of course, this is a fictional or humorous result, as the moon doesn’t actually have any cities or capitals - it’s a celestial body with no permanent human settlements or political divisions.

<chatlas._chat.ChatResponse at 0x116f90d70>

Summary

Review some vocabulary

  • system prompt: instructions to the model to modify its behavior
  • user prompt: a question or statement for the model to respond to
  • assistant: the model’s response
  • tokens: fundamental units of information for LLMs
  • context window: how much input can be incorporated into each output
  • tool: external function or resource that the model can call

More vocabulary

  • agent: an LLM that can use tools to accomplish tasks
    • Like a chatbot that can also use a calculator, search the web, or access your files
  • mcp: Model Context Protocol: standarized way for you to talk to other services (usually tools)
    • It’s like an standarized API for LLMs to talk to tools created by different people
  • RAG: Retrieval-Augmented Generation: technique where the model retrieves relevant information from a database or document collection to enhance its responses
    • Think of doing natural language search on your own documents and then using results into your context.

Next time?

Thanks!

https://github.com/chendaniely/2025-12-09-visst-llm