Tutorials · Chapter D (4/4) · ~10 min
Talk to an LLM from code
Play → read → next
Shape a real LLM API call — system + user messages you can run and show.
Code Lab
Talk to an LLM from code
Practice message lists (system + user) like real APIs — offline fake client.
Recap
What you just did
You composed an API-shaped prompt: roles (system/user), content strings, maybe temperature. You saw that the product is not “open a website” — it’s a request your program can repeat, log, and wrap in features.
Teach
How it works
Python-ish sketch of the shape:
messages = [
{"role": "system", "content": "Reply in one short sentence."},
{"role": "user", "content": "Name one use of embeddings."},
]
# client.chat(model="...", messages=messages) → text
- System steers job + style
- User is the ask
- Response text is what your app displays or pipelines next
Mental model: the LLM is a callable function — strings in, string out — not a person in a browser.
Use it
When you'd use this
- Drafting helper bots inside your own app
- Generating labels or summaries for a batch of rows
- Prototyping before you add retrieval or tools
Watch out
Watch out
Never bake API keys into shared notebooks or git. Responses can be wrong with full confidence — keep a human check for money, health, or legal. Cap length so a runaway reply doesn’t flood your UI.
Try next
Try this next
Change only the system message (same user ask). Save both replies. That’s prompt control you can show in two screenshots.