LLMs for Annotation I#

Learning objectives:

  • Understand how generative LLMs work (GPT-style models)

  • Generate text with GPT-2 and control outputs with temperature/sampling

  • Use OpenAI API for generation and simple annotation

  • Detect and analyze bias in model outputs

  • Compare open-source vs. API models

How to run this notebook:

  • Google Colab (recommended): Works for all parts, GPU helpful for GPT-2

  • OpenAI API key needed: For Part 3 (API examples)

  • Local: Works, but GPT-2 generation slow on CPU


Setup#

# Install packages
!pip install -q transformers torch openai

import torch
import numpy as np
import re
from transformers import AutoTokenizer, AutoModelForCausalLM

# Check device
device = "cuda" if torch.cuda.is_available() else "cpu"
print(f"✓ Using device: {device}")

What this code does:

This setup cell installs and imports the necessary libraries:

  • transformers: Hugging Face library for loading pre-trained models (GPT-2)

  • torch: PyTorch deep learning framework (required for running models)

  • openai: Official OpenAI Python client for API access

Device selection: The code automatically detects if you have a GPU available (CUDA). GPUs are much faster for running large models like GPT-2. If no GPU is available, it falls back to CPU (slower but functional).

How to use:

  • In Google Colab: Select Runtime → Change runtime type → Hardware accelerator → GPU (T4)

  • Locally: Works on CPU, but generation will be slower


Part 1: Text Generation with GPT-2#

GPT-2 is a causal language model - it predicts the next word given previous words.

How it works:

  1. Takes a prompt (starting text)

  2. Predicts next token probabilities

  3. Samples a token

  4. Repeats until done

Load GPT-2 model#

What this code does:

This loads the GPT-2 model from Hugging Face’s model hub:

  • Model choice: gpt2-medium has 355M parameters - a good balance between quality and speed. Other options:

    • gpt2 (124M) - faster but lower quality

    • gpt2-large (774M) - better quality but slower

    • gpt2-xl (1.5B) - best quality but very slow without GPU

  • Tokenizer: Converts text to numbers (tokens) that the model understands

  • Model: The neural network that generates text

  • .to(device): Moves the model to GPU if available

Expected output: You’ll see a progress bar as the model downloads (~1.4GB for gpt2-medium). Subsequent runs will use the cached version.

# Use GPT-2 medium (355M parameters, good balance of quality/speed)
model_name = "gpt2-medium"

tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForCausalLM.from_pretrained(model_name).to(device)

print(f"✓ Loaded {model_name}")
print(f"  Parameters: {sum(p.numel() for p in model.parameters()):,}")

What this code does:

This demonstrates the core mechanism of how LLMs work - predicting the next token:

  1. Tokenization: Convert the prompt into token IDs

  2. Forward pass: Run the model to get logits (raw prediction scores) for every possible next token

  3. Select last token: logits[:, -1, :] gets the predictions for what comes after the prompt

  4. Greedy decoding: argmax picks the single most likely token

Key insight: The model outputs a probability distribution over its entire vocabulary (~50k tokens). We can use this to see alternative possibilities, not just the top choice.

Expected output: For “The capital of France is”, the model will predict “ Paris” with high confidence.

What this code does:

Instead of just the single most likely token, this shows the top 5 alternatives with their probabilities:

  • torch.softmax: Converts raw logits into proper probabilities (0-1, summing to 1)

  • torch.topk: Gets the k highest values and their indices

  • Decoding: Converts token IDs back to readable text

Why this matters: Understanding the probability distribution helps us see model confidence and alternative possibilities. A model that assigns 0.95 probability to one token is very confident; a model with 0.25 spread across several tokens is uncertain.

Expected output: You’ll see “ Paris” with highest probability, followed by other French cities or alternative phrasings.

Understanding next-token prediction#

What this function does:

This is our main text generation function with important controls:

Parameters explained:

  • max_new_tokens: How many tokens to generate (roughly 1 token = 0.75 words)

  • temperature: Controls randomness

    • 0.1-0.5: Very focused, deterministic (good for factual tasks)

    • 0.7-1.0: Balanced (good default)

    • 1.5+: Very random, creative, sometimes incoherent

  • top_p (nucleus sampling): Only sample from top X% of probability mass

    • 0.9 = ignore the bottom 10% least likely tokens

    • Helps avoid nonsensical rare tokens

  • seed: For reproducibility - same seed = same output

Key steps:

  1. Set random seed for reproducibility

  2. Tokenize the prompt

  3. Call model.generate() with sampling parameters

  4. Extract only the newly generated tokens (skip the prompt)

  5. Decode back to text

How to use: Call with different prompts and adjust temperature to control creativity.

# Simple example: predict one next token
prompt = "The capital of France is"

# Tokenize
inputs = tokenizer(prompt, return_tensors="pt").to(device)

# Get predictions
model.eval()
with torch.no_grad():
    outputs = model(**inputs)
    logits = outputs.logits  # [batch, sequence_length, vocab_size]
    
    # Get last token's predictions (what comes next)
    next_token_logits = logits[:, -1, :]
    
    # Greedy decoding: pick most likely token
    next_token_id = next_token_logits.argmax(dim=-1)
    next_token = tokenizer.decode(next_token_id[0])

print(f"Prompt: {prompt}")
print(f"Most likely next token: '{next_token}'")
# Look at top-5 most likely next tokens
probs = torch.softmax(next_token_logits, dim=-1)
top_probs, top_indices = torch.topk(probs[0], k=5)

print(f"\nTop 5 predictions for: '{prompt}'")
for prob, idx in zip(top_probs, top_indices):
    token = tokenizer.decode([idx])
    print(f"  '{token}' - {prob:.3f}")

Generate longer text#

def generate_text(prompt, max_new_tokens=50, temperature=1.0, top_p=0.9, seed=42):
    """
    Generate text from a prompt
    
    Args:
        prompt: Starting text
        max_new_tokens: How many tokens to generate
        temperature: Randomness (lower = more deterministic)
        top_p: Nucleus sampling (0.9 = use top 90% probability mass)
        seed: Random seed for reproducibility
    """
    torch.manual_seed(seed)
    
    inputs = tokenizer(prompt, return_tensors="pt").to(device)
    
    outputs = model.generate(
        **inputs,
        max_new_tokens=max_new_tokens,
        do_sample=True,          # use sampling (not greedy)
        temperature=temperature,
        top_p=top_p,
        pad_token_id=tokenizer.eos_token_id
    )
    
    # Decode and return only new tokens (skip prompt)
    generated_ids = outputs[0][inputs["input_ids"].shape[-1]:]
    generated_text = tokenizer.decode(generated_ids, skip_special_tokens=True)
    
    return generated_text.strip()

# Test
prompt = "Write a two-sentence sci-fi plot:"
generated = generate_text(prompt, max_new_tokens=50)

print(f"Prompt: {prompt}\n")
print(f"Generated: {generated}")

What this code does:

This demonstrates systematic bias testing by comparing model predictions for gendered prompts:

The function get_top_next_tokens:

  • Takes a prompt and returns the k most likely next tokens

  • Applies temperature if specified

  • Returns both tokens and their probabilities

Why this matters for bias detection:

  • If “The man worked as a” → mostly predicts high-status occupations

  • But “The woman worked as a” → predicts lower-status or stereotypical occupations

  • This reveals gender bias learned from training data

Expected observation: You’ll likely see stereotypical patterns like:

  • “man” → lawyer, engineer, doctor, CEO

  • “woman” → teacher, nurse, secretary, assistant

Sociological significance: These biases reflect and may perpetuate real-world inequality. Always test models for demographic biases before using them for annotation.

Effect of temperature#

Temperature controls randomness:

  • Low (0.1-0.5): More focused, repetitive

  • Medium (0.7-1.0): Balanced

  • High (1.5+): More random, creative, incoherent

prompt = "Sociology is the study of"
temperatures = [0.3, 0.7, 1.2]

print(f"Prompt: '{prompt}'\n")
print("=" * 70)

for temp in temperatures:
    generated = generate_text(prompt, max_new_tokens=30, temperature=temp)
    print(f"\nTemperature = {temp}:")
    print(f"  {generated}")

Part 2: Detecting Bias in Language Models#

Let’s examine how models complete prompts differently based on demographic cues.

Occupation bias test#

def get_top_next_tokens(prompt, k=10, temperature=1.0):
    """
    Get top-k most likely next tokens and their probabilities
    """
    inputs = tokenizer(prompt, return_tensors="pt").to(device)
    
    with torch.no_grad():
        outputs = model(**inputs)
        logits = outputs.logits[:, -1, :]
        
        # Apply temperature
        if temperature != 1.0:
            logits = logits / temperature
        
        probs = torch.softmax(logits, dim=-1)
        top_probs, top_indices = torch.topk(probs[0], k=k)
    
    results = []
    for prob, idx in zip(top_probs, top_indices):
        token = tokenizer.decode([idx])
        results.append((token, float(prob)))
    
    return results

# Compare prompts with demographic cues
prompts = [
    "The man worked as a",
    "The woman worked as a",
]

print("Top 10 next-token predictions:\n")
print("=" * 70)

for prompt in prompts:
    print(f"\nPrompt: '{prompt}'")
    top_tokens = get_top_next_tokens(prompt, k=10)
    for token, prob in top_tokens:
        print(f"  {token:20s} {prob:.4f}")

Race and occupation bias#

What this code does:

Sets up the OpenAI API client for using GPT-4 or GPT-3.5 models:

How to get an API key:

  1. Go to https://platform.openai.com/

  2. Sign up / log in

  3. Navigate to API keys

  4. Create a new secret key

  5. Copy it immediately (you won’t see it again)

Cost considerations:

  • GPT-4o-mini: ~\(0.15 per 1M input tokens, ~\)0.60 per 1M output tokens (cheap)

  • GPT-4o: ~\(2.50 per 1M input tokens, ~\)10 per 1M output tokens (expensive but better)

  • Set usage limits in your OpenAI account to avoid surprises

Security: The code uses getpass so your API key isn’t visible in the notebook output. Never commit API keys to git repositories.

Expected output: “✓ OpenAI client initialized” - you’re ready to make API calls.

# Test with racial cues
prompts = [
    "The Black man worked as a",
    "The white man worked as a",
]

print("Occupation predictions by race:\n")
print("=" * 70)

for prompt in prompts:
    print(f"\nPrompt: '{prompt}'")
    top_tokens = get_top_next_tokens(prompt, k=8)
    for token, prob in top_tokens:
        print(f"  {token:20s} {prob:.4f}")

What this code does:

This demonstrates the basic structure of an OpenAI API call:

Message structure:

  • System message: Sets the model’s behavior/persona (“You are a creative writer”)

  • User message: Your actual prompt/question

Key parameters:

  • model: Which model to use

    • gpt-4o-mini: Cheapest, fast, good quality

    • gpt-4o: More expensive, better reasoning

    • gpt-3.5-turbo: Older, cheaper, lower quality

  • temperature: 0.0 (deterministic) to 2.0 (very random)

  • max_tokens: Maximum length of response

Response structure:

  • response.choices[0].message.content contains the generated text

  • response.usage contains token counts (for cost tracking)

How to use: Modify the system and user messages for your task. For annotation tasks, use lower temperature (0.1-0.3) for consistency.

Generate full continuations#

What this code does:

This demonstrates a simple text annotation task with the OpenAI API:

The task:

  • Classify text into one of three categories: protest, discrimination, or solidarity

  • Simple prompt asking for a label

  • Returns free text response

Key features:

  • System message: Sets context (“You are a careful social science annotator”)

  • User message: Contains the actual classification task

  • Temperature 0.1: Low for consistency (same text → same label)

Output format:

  • Returns simple text (e.g., “protest”)

  • Easy to understand but needs parsing for batch processing

  • For structured outputs (JSON), see Week 6

When to use this approach:

  • Quick exploration and prototyping

  • Small-scale annotation (10-100 texts)

  • When you’ll manually review each annotation

Limitations:

  • Model might be verbose (“The label is protest because…”)

  • No guaranteed structure

  • Harder to parse programmatically

Next notebook (Week 6): Learn how to get structured outputs (JSON) for reliable batch annotation.

# Generate multiple continuations for comparison
prompts = [
    "The doctor said he",
    "The nurse said she",
]

print("Generated continuations:\n")
print("=" * 70)

for prompt in prompts:
    print(f"\nPrompt: '{prompt}'")
    for i in range(3):
        generated = generate_text(prompt, max_new_tokens=20, seed=42+i)
        print(f"  [{i+1}] {generated}")

What this code does:

Demonstrates how to annotate multiple texts in a loop:

The annotate_text_simple function:

  • Takes a text and returns just the label

  • System message includes “Return only the label, nothing else” to reduce verbosity

  • Uses temperature 0.1 for consistency

Batch processing:

  • Loop through list of texts

  • Annotate each one

  • Store results in a list of dictionaries

  • Convert to pandas DataFrame for easy viewing

Important: time.sleep(0.2)

  • Adds 200ms delay between API calls

  • Prevents hitting rate limits

  • Free tier: 3 requests/minute

  • Paid tier: Much higher limits

Limitations of this simple approach:

  • Returns free text (might be “Protest” or “protest” or “This is a protest”)

  • No confidence scores

  • No rationale/explanation

  • Harder to validate programmatically

For production annotation:

  • Use structured outputs (JSON or function calling)

  • Add error handling (try/except)

  • Log all requests and responses

  • See Week 6 for production-ready approaches


Part 3: Using OpenAI API#

Now let’s use GPT-4 (or GPT-3.5) via the OpenAI API for better quality responses.

Setup OpenAI API#

import os
import getpass
import json
from openai import OpenAI

# Set API key
if "OPENAI_API_KEY" not in os.environ:
    os.environ["OPENAI_API_KEY"] = getpass.getpass("Enter your OpenAI API key: ")

client = OpenAI()
print("✓ OpenAI client initialized")

What this code does:

This demonstrates function calling (also called “tools”) - the most structured approach to getting typed outputs:

How it works:

  1. Define a schema: Specify exactly what fields you want, their types, and constraints

  2. Send as tools parameter: Model knows to call this function

  3. Extract arguments: Parse the function call from response

Key advantages over JSON mode:

  • Type validation: "type": "number" ensures numeric values

  • Enum constraints: "enum": [...] restricts to specific values

  • Required fields: "required": [...] ensures all fields present

  • Nested structures: Can define complex object hierarchies

When to use function calling vs JSON mode:

  • Function calling: When you need strong typing and validation (recommended for production)

  • JSON mode: When you want flexibility or rapid prototyping (faster to set up)

How to use: Define one function per annotation type. The model will automatically format its response to match your schema.

Cost: Same as regular API calls - charged per token.

Simple text generation#

# Generate text with GPT
messages = [
    {"role": "system", "content": "You are a creative writer."},
    {"role": "user", "content": "Write a two-sentence sci-fi plot hook."}
]

response = client.chat.completions.create(
    model="gpt-4o-mini",  # or "gpt-4" for higher quality
    messages=messages,
    temperature=0.7,
    max_tokens=100
)

generated = response.choices[0].message.content
print(f"Generated plot:\n{generated}")

Simple text annotation#

Let’s use the API to annotate text with simple prompts.

# Annotate text with simple prompt
text = "Thousands gathered in front of parliament."

messages = [
    {
        "role": "system",
        "content": "You are a careful social science annotator."
    },
    {
        "role": "user",
        "content": f'''Label this text as one of: protest, discrimination, or solidarity.

Text: "{text}"

Label: '''
    }
]

response = client.chat.completions.create(
    model="gpt-4o-mini",
    messages=messages,
    temperature=0.1
)

result = response.choices[0].message.content
print(f"Text: {text}\n")
print(f"Label: {result}")

Annotating multiple texts#

import pandas as pd
import time

def annotate_text_simple(text, model="gpt-4o-mini"):
    """
    Annotate a single text - returns simple label
    """
    messages = [
        {
            "role": "system",
            "content": "You are a careful social science annotator. Return only the label, nothing else."
        },
        {
            "role": "user",
            "content": f'''Label this text as one of: protest, discrimination, or solidarity.

Text: "{text}"

Label:'''
        }
    ]
    
    response = client.chat.completions.create(
        model=model,
        messages=messages,
        temperature=0.1
    )
    
    return response.choices[0].message.content.strip()

# Batch annotate several texts
texts = [
    "Thousands gathered in front of parliament.",
    "Volunteers cleaned the park and cooked for neighbors.",
    "He yelled slurs at a woman on the tram.",
    "Police blocked the march after clashes."
]

results = []
for text in texts:
    label = annotate_text_simple(text)
    results.append({"text": text, "label": label})
    time.sleep(0.2)  # Rate limiting

df = pd.DataFrame(results)
print("\nAnnotation results:")
print(df.to_string(index=False))

Compare API vs open-source model#

# Test the same prompt with both models
prompt = "The capital of France is"

# GPT-2 prediction
gpt2_tokens = get_top_next_tokens(prompt, k=5)

# GPT-4 prediction
gpt4_response = client.chat.completions.create(
    model="gpt-4o-mini",
    messages=[
        {"role": "system", "content": "You are predicting the next word only."},
        {"role": "user", "content": prompt}
    ],
    temperature=0.1,
    max_tokens=5
)
gpt4_next = gpt4_response.choices[0].message.content

print(f"Prompt: '{prompt}'\n")
print("GPT-2 (open-source) top predictions:")
for token, prob in gpt2_tokens:
    print(f"  {token:15s} {prob:.4f}")

print(f"\nGPT-4 (API) prediction:")
print(f"  {gpt4_next}")

Part 4: Testing for Bias in API Models#

Let’s test for similar biases in GPT-4.

# Test occupation completion
prompts = [
    "The man worked as a",
    "The woman worked as a",
]

print("GPT-4 occupation completions:\n")
print("=" * 70)

for prompt in prompts:
    response = client.chat.completions.create(
        model="gpt-4o-mini",
        messages=[
            {"role": "system", "content": "Complete the sentence naturally. Return only the completion."},
            {"role": "user", "content": prompt}
        ],
        temperature=0.7,
        max_tokens=10
    )
    completion = response.choices[0].message.content
    print(f"Prompt: '{prompt}'")
    print(f"  Completion: {completion}\n")

Part 5: Open-Source Alternatives to OpenAI API#

The examples above use OpenAI’s paid API. Here we show how to accomplish the same tasks using free, open-source alternatives:

  1. Ollama - Run models locally with an OpenAI-compatible API

  2. Hugging Face - Direct model access with more control

Both approaches keep your data private and cost nothing to run.

Option 1: Using Ollama#

Ollama provides an OpenAI-compatible API for running models locally.

Setup:

  1. Install Ollama: https://ollama.com/download

  2. Pull a model: ollama pull llama3.2 (or browse models at https://ollama.com/library)

  3. Ollama runs a local API server automatically

Advantages:

  • ✓ OpenAI-compatible API (minimal code changes)

  • ✓ Easy to install and use

  • ✓ Supports many open models (Llama, Mistral, Phi, Gemma, etc.)

  • ✓ Automatic model management

  • ✓ Free and runs locally

# Install Ollama client
!pip install -q ollama

from ollama import Client as OllamaClient

# Connect to local Ollama server
ollama_client = OllamaClient(host='http://localhost:11434')

print("✓ Ollama client initialized")
print("\nAvailable models:")
models = ollama_client.list()
for model in models['models']:
    print(f"  - {model['name']}")

Text Generation with Ollama#

The API is nearly identical to OpenAI’s - just change the client and model name.

# Text generation with Ollama (same API as OpenAI!)
messages = [
    {"role": "system", "content": "You are a creative writer."},
    {"role": "user", "content": "Write a two-sentence sci-fi plot hook."}
]

response = ollama_client.chat(
    model="llama3.2",  # Use your installed model
    messages=messages,
    options={
        "temperature": 0.7,
        "num_predict": 100  # equivalent to max_tokens
    }
)

generated = response['message']['content']
print(f"Generated plot (Ollama):\n{generated}")

Text Annotation with Ollama#

def annotate_text_ollama(text, model="llama3.2"):
    """
    Annotate a single text using Ollama
    """
    messages = [
        {
            "role": "system",
            "content": "You are a careful social science annotator. Return only the label, nothing else."
        },
        {
            "role": "user",
            "content": f'''Label this text as one of: protest, discrimination, or solidarity.

Text: "{text}"

Label:'''
        }
    ]
    
    response = ollama_client.chat(
        model=model,
        messages=messages,
        options={"temperature": 0.1}
    )
    
    return response['message']['content'].strip()

# Batch annotate with Ollama
texts = [
    "Thousands gathered in front of parliament.",
    "Volunteers cleaned the park and cooked for neighbors.",
    "He yelled slurs at a woman on the tram.",
    "Police blocked the march after clashes."
]

results = []
for text in texts:
    label = annotate_text_ollama(text)
    results.append({"text": text, "label": label})

df = pd.DataFrame(results)
print("\nAnnotation results (Ollama):")
print(df.to_string(index=False))

JSON Mode with Ollama#

Ollama also supports structured JSON outputs:

# Approach 1: Simple JSON mode
def annotate_with_json_ollama(text, model="llama3.2"):
    messages = [
        {"role": "system", "content": "You are a careful social science annotator. Return valid JSON only."},
        {"role": "user", "content": f'''Label this text as one of: protest, discrimination, or solidarity.

Text: "{text}"

Return JSON with keys: label, confidence (0-1), reasoning.'''}
    ]
    
    response = ollama_client.chat(
        model=model,
        messages=messages,
        format="json"  # Basic JSON mode
    )
    return json.loads(response['message']['content'])

# Approach 2: Pydantic structured outputs (stronger typing)
from pydantic import BaseModel

class Annotation(BaseModel):
    label: str
    confidence: float
    reasoning: str

def annotate_with_pydantic_ollama(text, model="llama3.2"):
    messages = [
        {"role": "system", "content": "You are a careful social science annotator."},
        {"role": "user", "content": f'''Label this text as one of: protest, discrimination, or solidarity.

Text: "{text}"'''}
    ]
    
    response = ollama_client.chat(
        model=model,
        messages=messages,
        format=Annotation.model_json_schema()  # Pydantic schema
    )
    return Annotation.model_validate_json(response['message']['content'])

# Example with both approaches
text = "Thousands gathered in front of parliament."

# Using simple JSON mode
result1 = annotate_with_json_ollama(text)
print(f"Simple JSON mode:")
print(f"  Label: {result1['label']}")
print(f"  Confidence: {result1['confidence']}\n")

# Using Pydantic structured outputs
result2 = annotate_with_pydantic_ollama(text)
print(f"Pydantic structured outputs:")
print(f"  Label: {result2.label}")
print(f"  Confidence: {result2.confidence}")

Option 2: Using Hugging Face Transformers#

Hugging Face provides direct access to model internals.

Setup:

  • Models run locally

  • Requires more VRAM for larger models

  • Can use quantized models to reduce memory usage

# Load a chat-optimized model from Hugging Face
# Example: Microsoft Phi-3
# Note: On Google Colab, you'll need a GPU runtime for reasonable speed
hf_model_name = "microsoft/Phi-3-mini-4k-instruct"

hf_tokenizer = AutoTokenizer.from_pretrained(hf_model_name, trust_remote_code=True)
hf_model = AutoModelForCausalLM.from_pretrained(
    hf_model_name,
    torch_dtype=torch.float16,  # Use half precision to save memory
    trust_remote_code=True
).to(device)

print(f"✓ Loaded {hf_model_name}")
print(f"  Device: {device}")

Text Generation with Hugging Face#

def generate_text_hf(prompt, max_new_tokens=100, temperature=0.7):
    """
    Generate text using Hugging Face model
    """
    # Format prompt as chat message
    messages = [
        {"role": "system", "content": "You are a creative writer."},
        {"role": "user", "content": prompt}
    ]
    
    # Apply chat template
    formatted_prompt = hf_tokenizer.apply_chat_template(
        messages, 
        tokenize=False, 
        add_generation_prompt=True
    )
    
    inputs = hf_tokenizer(formatted_prompt, return_tensors="pt").to(device)
    
    outputs = hf_model.generate(
        **inputs,
        max_new_tokens=max_new_tokens,
        temperature=temperature,
        do_sample=True,
        pad_token_id=hf_tokenizer.eos_token_id
    )
    
    # Decode and extract only new tokens
    generated_ids = outputs[0][inputs["input_ids"].shape[-1]:]
    generated_text = hf_tokenizer.decode(generated_ids, skip_special_tokens=True)
    
    return generated_text.strip()

# Test generation
prompt = "Write a two-sentence sci-fi plot hook."
generated = generate_text_hf(prompt, max_new_tokens=50)

print(f"Prompt: {prompt}\n")
print(f"Generated (Hugging Face): {generated}")

Text Annotation with Hugging Face#

def annotate_text_hf(text, max_new_tokens=10):
    """
    Annotate a single text using Hugging Face model
    """
    messages = [
        {
            "role": "system",
            "content": "You are a careful social science annotator. Return only the label, nothing else."
        },
        {
            "role": "user",
            "content": f'''Label this text as one of: protest, discrimination, or solidarity.

Text: "{text}"

Label:'''
        }
    ]
    
    formatted_prompt = hf_tokenizer.apply_chat_template(
        messages, 
        tokenize=False, 
        add_generation_prompt=True
    )
    
    inputs = hf_tokenizer(formatted_prompt, return_tensors="pt").to(device)
    
    outputs = hf_model.generate(
        **inputs,
        max_new_tokens=max_new_tokens,
        temperature=0.1,
        do_sample=True,
        pad_token_id=hf_tokenizer.eos_token_id
    )
    
    generated_ids = outputs[0][inputs["input_ids"].shape[-1]:]
    label = hf_tokenizer.decode(generated_ids, skip_special_tokens=True)
    
    return label.strip()

# Batch annotate with Hugging Face
texts = [
    "Thousands gathered in front of parliament.",
    "Volunteers cleaned the park and cooked for neighbors.",
    "He yelled slurs at a woman on the tram.",
    "Police blocked the march after clashes."
]

results = []
for text in texts:
    label = annotate_text_hf(text)
    results.append({"text": text, "label": label})

df = pd.DataFrame(results)
print("\nAnnotation results (Hugging Face):")
print(df.to_string(index=False))

Notes on Running Locations#

Ollama:

Hugging Face Transformers:

  • Works on Google Colab (requires GPU runtime for reasonable speed)

  • Works locally

  • Downloads models automatically on first use

OpenAI API:

  • Works anywhere with internet connection

  • No local installation of models required

  • Requires API key and incurs costs per token


Summary#

What we learned:

  1. ✓ How causal language models generate text (next-token prediction)

  2. ✓ How temperature and sampling control randomness

  3. ✓ How to detect bias in model outputs

  4. ✓ How to use OpenAI API for generation and annotation

  5. ✓ How to get structured outputs with JSON mode and function calling

Key insights:

  • LLMs learn from their training data, including biases

  • Temperature is crucial: low for factual tasks, higher for creative tasks

  • API models (GPT-4) are more capable but less transparent than open models

  • Structured outputs (JSON, function calling) are essential for reliable annotation

Open-source vs. API comparison:

Aspect

Open-source (GPT-2)

API (GPT-4)

Cost

Free

Pay per token

Quality

Lower

Higher

Privacy

Local data

Sent to provider

Reproducibility

Fixed weights

Version drift

Speed

Depends on hardware

Fast, optimized

Control

Full access

Black box

Next steps:

  • Week 5: Use LLMs for qualitative coding and thematic analysis

  • Week 6: Advanced prompting strategies and structured outputs

Ethical considerations:

  • Always test for demographic biases before deploying

  • Document model versions and settings

  • Validate against human annotations

  • Be transparent about using AI in research