נבנה על ידיBuilt by Ariel Yohimovich

חזרה למפגשBack
PythonPython

קריאה למודל שפה מ-PythonCalling a language model from Python

מפגש 33: API של מודל שפה, מפתחות, פרומפטים, עלויות ובטיחות.Session 33: A language-model API, keys, prompts, cost and safety.

מה נעשה היוםToday's plan

  • איךHow
  • מפתחKey
  • שיחהConversation
  • JSONJSON
  • עלותCost
  • SDKSDK
נושאTopic 1

איךHow

מה קורה כשקוראים למודלWhat happens when you call a model

  • בקשת POST עם JSON, כמו במפגש 22A POST request with JSON, like session 22
  • שולחים הודעות, מקבלים הודעהSend messages, get a message back
  • טוקנים = עלותTokens = cost

אנלוגיהAnalogy

לקרוא למודל זה כמו לשלוח מכתב לחבר חכם מאוד: מה שתכתבו במכתב הוא כל מה שהוא יודע על המצב, והתשובה חוזרת במעטפה.Calling a model is like posting a letter to a very clever friend: what you write is all they know about the situation, and the answer comes back in an envelope.
נושאTopic 2

מפתחKey

מפתח API ומשתני סביבהAPI keys and environment variables

אנלוגיהAnalogy

מפתח API הוא כמו כרטיס אשראי: לא מצלמים אותו ולא שולחים בקבוצה. אם דלף, מבטלים ומוציאים חדש.An API key is like a credit card: you don't photograph it or post it in a group chat. If it leaks, cancel it and get a new one.

טעות נפוצהCommon mistake

keys.py
import os
os.environ.pop("ANTHROPIC_API_KEY", None)
key = os.environ["ANTHROPIC_API_KEY"]
מה Python עונהWhat Python says
KeyError: 'ANTHROPIC_API_KEY'
KeyError: 'ANTHROPIC_API_KEY': המשתנה לא מוגדר בטרמינל הזה. מגדירים אותו (ובוינדוס פותחים טרמינל חדש אחרי setx), או משתמשים ב-.get עם הודעה ברורה.KeyError: 'ANTHROPIC_API_KEY': the variable isn't set in this terminal. Set it (and on Windows open a new terminal after setx), or use .get with a clear message.
נושאTopic 3

שיחהConversation

הנחיות ושיחהInstructions and conversation

  • `system`: תפקיד וסגנון`system`: role and style
  • messages: כל ההיסטוריה, בכל בקשהmessages: the full history, every request
  • המודל לא זוכר בין בקשותThe model doesn't remember between requests

אנלוגיהAnalogy

המודל הוא כמו מישהו בלי זיכרון לטווח קצר: בכל שיחת טלפון מקריאים לו את כל מה שנאמר עד עכשיו, ואז הוא עונה.The model is like someone with no short-term memory: on every call you read out everything said so far, then they answer.
נושאTopic 4

JSONJSON

לבקש JSON ולבדוקAsk for JSON and validate

  • מבקשים בפירוש: "רק JSON עם השדות..."Ask explicitly: "only JSON with the fields..."
  • json.loads בתוך tryjson.loads inside try
  • בודקים שדות לפני שמשתמשיםCheck fields before use

אנלוגיהAnalogy

לבדוק JSON ממודל זה כמו לבדוק משלוח לפני שחותמים: לרוב הכול בסדר, אבל פותחים את הקופסה בכל זאת.Validating a model's JSON is like checking a delivery before signing: usually it's fine, but you open the box anyway.
נושאTopic 5

עלותCost

עלות, מגבלות ושגיאותCost, limits and errors

  • usage: טוקנים נכנסים ויוצאיםusage: input and output tokens
  • max_tokens מגביל את אורך התשובהmax_tokens caps the answer length
  • 429 / 529: מנסים שוב אחרי המתנה429 / 529: retry after a wait

אנלוגיהAnalogy

טוקנים הם כמו דקות שיחה בחבילה של פעם: כל מילה עולה משהו, ולכן כדאי לשאול בקיצור ולא לחזור על אותה שאלה.Tokens are like minutes on an old phone plan: every word costs something, so ask concisely and don't repeat the same question.
נושאTopic 6

SDKSDK

ה-SDK הרשמיThe official SDK

  • pip install anthropicpip install anthropic
  • client.messages.create(...)client.messages.create(...)
  • ניסיונות חוזרים ושגיאות מסודרות מובניםRetries and tidy errors built in

אנלוגיהAnalogy

SDK הוא כמו שלט אוניברסלי מקורי: אותם ערוצים שהיה אפשר להחליף ביד בטלוויזיה, רק נוח ובטוח יותר.An SDK is like the original remote control: the same channels you could change by hand on the TV, just more convenient and safer.

בקשת POST עם JSON, כמו במפגש 22A POST request with JSON, like session 22

שולחים הודעות, מקבלים הודעהSend messages, get a message back

טוקנים = עלותTokens = cost

מפתח = כסף. לא בקוד, לא ב-GitA key = money. Not in code, not in Git

משתנה סביבה: export / setxEnvironment variable: export / setx

קובץ .env שמופיע ב-.gitignoreA .env file listed in .gitignore

`system`: תפקיד וסגנון`system`: role and style

messages: כל ההיסטוריה, בכל בקשהmessages: the full history, every request

המודל לא זוכר בין בקשותThe model doesn't remember between requests

מבקשים בפירוש: "רק JSON עם השדות..."Ask explicitly: "only JSON with the fields..."

json.loads בתוך tryjson.loads inside try

בודקים שדות לפני שמשתמשיםCheck fields before use

usage: טוקנים נכנסים ויוצאיםusage: input and output tokens

max_tokens מגביל את אורך התשובהmax_tokens caps the answer length

429 / 529: מנסים שוב אחרי המתנה429 / 529: retry after a wait

pip install anthropicpip install anthropic

client.messages.create(...)client.messages.create(...)

מה למדנוWhat we learned

  • בקשת POST עם JSON, כמו במפגש 22A POST request with JSON, like session 22
  • מפתח = כסף. לא בקוד, לא ב-GitA key = money. Not in code, not in Git
  • `system`: תפקיד וסגנון`system`: role and style
  • מבקשים בפירוש: "רק JSON עם השדות..."Ask explicitly: "only JSON with the fields..."
  • usage: טוקנים נכנסים ויוצאיםusage: input and output tokens
  • pip install anthropicpip install anthropic

עכשיו לתרגולNow, practice

5 תרגילים ו-2 אתגרים, עם בדיקה אוטומטית5 exercises and 2 challenges, auto-checked
הערות מרצהSpeaker notes

נבנה על ידיBuilt by Ariel Yohimovich