Building with AI
Write programs that use AI models: prompts, conversations, structured output, tools, retrieval and testing.
1. How an AI API call works
Send a request to a model and read the reply. Keep your key safe.
2. Messages, roles and conversations
System, user and assistant messages; keeping history; a chat loop.
3. Prompt templates
Build prompts with f-strings, functions, clear instructions and examples.
4. Structured output: JSON
Ask for JSON, parse it safely, check it, and retry when it is wrong.
5. Tokens, limits and cost
Estimate tokens, work out prices, and stay inside limits.
6. Tool calling
Let the model ask your code to run functions, then use the results.
7. Retrieval: answering from your documents
Find the right documents and put them in the prompt (the idea behind RAG).
8. Testing and safety
Measure quality, protect personal data, check before acting, and handle limits.
9. Final project: a study-helper bot
Combine tools, a tool loop, privacy and tests into one small app.