Model APIs
Call a language model from code, and handle its errors, its cost, and its answers.
A language model service takes text and gives text back. In this course you call a model from Python. You write messages, read the answer, count tokens and cost, ask for JSON, check the answer, and try again after an error. The lessons run in your browser, so they use a simulated model that follows fixed rules. Its calls, answers, errors, and costs have the same shape as those of a real service. Do the courses Python and Python for data first.
- Intermediate
- 16 lessons
- 4 h 11 min
- Free
Not started
Lessons
- 1Your first call to a modelSend one message to a language model and read the parts of its answer.10 min
- 2Roles and the system messageGive the model standing instructions in a system message, and put the data in the user message.12 min
- 3Tokens and limitsCount the tokens of a text, check that messages fit a model, and find out when an answer was cut.14 min
- 4The cost of a callCalculate the price of a call from its tokens, compare two models, and estimate the cost of a month.12 min
- 5ConversationsSend the earlier messages again so that the model can use them, and keep a long conversation inside the limit.16 min
- 6Temperature and steady answersSee how the temperature changes the answers of a model, and make the answers of a program steady.12 min
- 7Answers as JSONAsk the model for JSON with fixed keys, parse the answer, and find the JSON when the model adds other text.16 min
- 8Check the answer, and try againCheck the keys and the values of an answer, ask again after a bad answer, and use a default when no try works.16 min
- 9When a call failsCatch the errors of a model service, and separate the errors that a new try can fix from the errors that it cannot fix.14 min
- 10Try again with a waitWrite a function that tries a failed call again, waits longer after each failure, and stops after a fixed number of attempts.16 min
- 11Keys and settingsRead the key and the name of the model from environment variables, stop when the key is missing, and never print a key.12 min
- 12Many itemsProcess a file of comments, continue after a failed call, write a result file, and add up the tokens and the cost.18 min
- 13Choose a modelRun two models on comments that have a known label, and compare how often each model is right and what it costs.16 min
- 14From the simulation to a real serviceQuizRead what is the same and what is different when you call a real model service, and answer six questions.12 min
- 15Quiz, Model APIsQuizFourteen questions on messages, tokens, cost, conversations, JSON answers, errors, keys, and the choice of a model.10 min
- 16Project: a report on rider commentsProjectAnalyze the comments of one week with one call for each, handle bad answers and errors, and write a report with the cost.45 min