42 · Using pretrained models
The practitioner's view: load a model from the Hugging Face Hub and use it. pipeline for one-liners, AutoTokenizer/AutoModel for control, zero/few-shot prompting, and sentence embeddings for retrieval.
You rarely build LLMs from scratch — you LOAD a pretrained one from the Hugging Face Hub: pipeline for instant tasks, AutoTokenizer/AutoModel for control, prompting (zero/few-shot) to steer it, and sentence embeddings to power semantic search.
Without this:
Everything before this lesson explains how models work internally. This is the everyday skill: actually calling a pretrained model to solve a real task in a few lines of code.
You now understand how these models are built. The everyday job is much simpler: load a pretrained model and use it. The Hugging Face Hub hosts hundreds of thousands of ready-to-use models, and the transformers library makes calling them a few lines of code.
There are two levels of access:
-
pipeline— the one-liner.pipeline("sentiment-analysis")downloads a sensible default model, wires up its tokenizer, and hands you a function:pipe("I love this!")returns a label and score. There are pipelines for classification, translation, summarization, question-answering, and more. Perfect for prototyping. -
AutoTokenizer+AutoModel— full control. When you need to customize, load the tokenizer and model explicitly, tokenize your text into IDs yourself, run the model, and decode the output. This exposes generation knobs (temperature, top-p, max tokens) and lets you batch, stream, or feed embeddings into your own pipeline.
Two ways to steer a generative model WITHOUT any training:
- Zero-shot prompting — just describe the task in plain language ("Classify the sentiment of this review: ...").
- Few-shot prompting — include a handful of worked examples in the prompt before the real query, so the model infers the pattern from the demonstrations (this is "in-context learning").
And a workhorse use of encoder models: sentence embeddings. Encode each sentence into a single vector (lesson 39's encoder family), then compare vectors by cosine similarity to find semantically related text — the engine behind semantic search and retrieval-augmented generation (RAG). The runnable cell below builds exactly that ranking from toy embeddings.
HuggingFace usage: pipeline('sentiment-analysis') one-liner; AutoTokenizer + AutoModelForCausalLM with model.generate(temperature, top_p, do_sample); and a few-shot prompt with labeled examples before the query.
Python (in browser)
Python runs entirely in your browser via Pyodide (~6 MB on first Run, cached after).
What does cosine similarity over sentence embeddings enable?
- Load pretrained models from the Hugging Face Hub: pipeline('task') for instant one-liners, or AutoTokenizer + AutoModel for full control over tokenization and generation (temperature, top_p, max_new_tokens).
- Steer a generative model with NO training via zero-shot prompting (describe the task) or few-shot prompting (include worked examples — in-context learning).
- Sentence embeddings + cosine similarity rank texts by meaning regardless of exact word overlap — the core mechanism of semantic search and retrieval-augmented generation (RAG).
This IS day-to-day applied NLP: a few lines of transformers / sentence-transformers to classify, generate, or embed text — and cosine-ranked embeddings power virtually every search and RAG system in production.
If you remove it: You'd understand transformers in theory but not know how to actually call one, treating the Hub and the prompting/embedding workflow as a mystery instead of a few lines of code.