נבנה על ידיBuilt by Ariel Yohimovich
JavaJava
Java בעידן ה-AIJava in the AI era
מפגש 41: עבודה נכונה עם עוזר AI, קריאה ל-API של מודל שפה מ-Java (עם תשובות מוקלטות מראש), ובדיקת מה שהמודל כתב.Session 41: Working well with an AI assistant, calling a language model API from Java (with recorded replies), and checking what the model wrote.
עבודה נכונה עם עוזר AI, קריאה ל-API של מודל שפה מ-Java (עם תשובות מוקלטות מראש), ובדיקת מה שהמודל כתב. בסוף המפגש מוסיפים צעד לפרויקט המלווה.Working well with an AI assistant, calling a language model API from Java (with recorded replies), and checking what the model wrote. At the end we add a step to the companion project.מה נעשה היוםToday's plan
- מונחיםTerms
- הקריאה האמיתיתThe real call
- המודל מאחורי interfaceThe model behind an interface
- פרויקט מדףShelf project
עוברים על סדר היום ומזכירים את הפרויקט המלווה.Walk through the plan and mention the companion project.המונחים של המפגשThis session's terms
- מודל שפה: תוכנה שמקבלת טקסט (prompt) ומחזירה טקסט.language model: Software that takes text (a prompt) and returns text.
- prompt: הטקסט ששולחים למודל:prompt: The text sent to the model:
- מפתח API: סיסמה שמזהה אתכם מול השירות.API key: A password identifying you to the service.
- תשובה מוקלטת: מימוש של ה-interface שמחזיר תשובות קבועות מראש.recorded reply: An implementation of the interface returning answers fixed in advance.
- בדיקת הפלט: לפני שמשתמשים בתשובה:validating output: Before using a reply:
לפני הקוד, המילים. כל מונח כאן מקושר גם במילון של הקורס.Before the code, the words. Every term here is also linked in the course glossary.נושאTopic 2
הקריאה האמיתיתThe real call
הקריאה האמיתיתThe real call
- בקשת POST עם מפתח מתוך משתנה סביבה, גוף JSON עם המודל וההודעות, ותשובה ב-JSON.A POST request with an API key from an environment variable, a JSON body with the model and the messages, and a JSON reply.
- ה-text block (
""") שומר על ה-JSON קריא.The text block (""") keeps the JSON readable.
בקשת POST עם מפתח מתוך משתנה סביבה, גוף JSON עם המודל וההודעות, ותשובה ב-JSON. ה-text block (""") שומר על ה-JSON קריא.A POST request with an API key from an environment variable, a JSON body with the model and the messages, and a JSON reply. The text block (""") keeps the JSON readable.נושאTopic 3
המודל מאחורי interfaceThe model behind an interface
המודל מאחורי interfaceThe model behind an interface
Model הוא interface עם מתודה אחת.Model is an interface with one method.RecordedModel עונה מתשובות מוקלטות, ולכן כל הקוד שסביבו רץ ונבדק כאן, באותה צורה שבה ירוץ מול מודל אמיתי.RecordedModel answers from recorded replies, so all the code around it runs and is checked here, just as it will run against a real model.
Model הוא interface עם מתודה אחת. RecordedModel עונה מתשובות מוקלטות, ולכן כל הקוד שסביבו רץ ונבדק כאן, באותה צורה שבה ירוץ מול מודל אמיתי.Model is an interface with one method. RecordedModel answers from recorded replies, so all the code around it runs and is checked here, just as it will run against a real model.נושאTopic 4
פרויקט מדףShelf project
הצעד של היום בפרויקט מדףToday's step in the Shelf project
- מדף, צעד 41: המלצה לפי מצב רוחShelf, step 41: a recommendation by mood
Recommender מקבל Model ומדף, בונה prompt עם המדף ומצב הרוח, ומחזיר רק כותר שבמדף (אחרת no match on your shelf). הדפיסו adventure -> Dune, romance -> Emma, something light -> no match on your shelf.Recommender takes a Model and a shelf, builds a prompt with the shelf and the mood, and returns only a title on the shelf (otherwise no match on your shelf). Print adventure -> Dune, romance -> Emma, something light -> no match on your shelf.מודל שפה: תוכנה שמקבלת טקסט (prompt) ומחזירה טקסט.language model: Software that takes text (a prompt) and returns text.
המונחים של המפגשThis session's termsprompt: הטקסט ששולחים למודל:prompt: The text sent to the model:
המונחים של המפגשThis session's termsמפתח API: סיסמה שמזהה אתכם מול השירות.API key: A password identifying you to the service.
המונחים של המפגשThis session's termsתשובה מוקלטת: מימוש של ה-interface שמחזיר תשובות קבועות מראש.recorded reply: An implementation of the interface returning answers fixed in advance.
המונחים של המפגשThis session's termsבדיקת הפלט: לפני שמשתמשים בתשובה:validating output: Before using a reply:
המונחים של המפגשThis session's termsבקשת POST עם מפתח מתוך משתנה סביבה, גוף JSON עם המודל וההודעות, ותשובה ב-JSON.A POST request with an API key from an environment variable, a JSON body with the model and the messages, and a JSON reply.
הקריאה האמיתיתThe real callה-text block (""") שומר על ה-JSON קריא.The text block (""") keeps the JSON readable.
הקריאה האמיתיתThe real callModel הוא interface עם מתודה אחת.Model is an interface with one method.
המודל מאחורי interfaceThe model behind an interfaceRecordedModel עונה מתשובות מוקלטות, ולכן כל הקוד שסביבו רץ ונבדק כאן, באותה צורה שבה ירוץ מול מודל אמיתי.RecordedModel answers from recorded replies, so all the code around it runs and is checked here, just as it will run against a real model.
המודל מאחורי interfaceThe model behind an interfaceמדף, צעד 41: המלצה לפי מצב רוחShelf, step 41: a recommendation by mood
הצעד של היום בפרויקט מדףToday's step in the Shelf projectאיפה שומרים מפתח API?Where do you keep an API key?
- במשתנה סביבהIn an environment variable
- בקוד, בקבועIn the code, as a constant
- בקובץ READMEIn the README
- ב-URLIn the URL
שואלים את הכיתה ומצביעים. התשובה בשקופית הבאה.Ask the class and take a vote. The answer is on the next slide.במשתנה סביבהIn an environment variable
נכון.Right.המודל המליץ על ספר שלא קיים במדף. מה עושים?The model recommended a book that isn't on the shelf. What do you do?
- בודקים, ומחזירים ברירת מחדל או מבקשים שובCheck, and return a fallback or ask again
- מציגים כמו שהואShow it as is
- קורסיםCrash
- מוחקים את המדףDelete the shelf
שואלים את הכיתה ומצביעים. התשובה בשקופית הבאה.Ask the class and take a vote. The answer is on the next slide.בודקים, ומחזירים ברירת מחדל או מבקשים שובCheck, and return a fallback or ask again
נכון: לא מציגים בלי לבדוק.Right: don't show it unchecked.למה לבדוק עם תשובות מוקלטות ולא מול המודל?Why test with recorded replies and not against the model?
- הבדיקה מהירה, חינמית, ונותנת אותה תוצאה בכל פעםThe test is fast, free, and gives the same result every time
- כי המודל לא עובדBecause the model doesn't work
- כי Java לא יודעת HTTPBecause Java can't do HTTP
- אין סיבהNo reason
שואלים את הכיתה ומצביעים. התשובה בשקופית הבאה.Ask the class and take a vote. The answer is on the next slide.הבדיקה מהירה, חינמית, ונותנת אותה תוצאה בכל פעםThe test is fast, free, and gives the same result every time
נכון.Right.לבנות promptBuilding a prompt
prompt(shelf, mood) בונה טקסט:prompt(shelf, mood) builds text:
תרגיל מהעמוד הבא: שואלים איך הייתם מתחילים.An exercise from the practice page: ask how you would start.תשובה מוקלטתA recorded reply
- עם
RecordedModel בקשו סיכום של Dune, הדפיסו אותו, בדקו שהוא עד 80 תווים (short enough), והדפיסו `calls:With RecordedModel ask for a summary of Dune, print it, check it's at most 80 characters (short enough), and print `calls:
תרגיל מהעמוד הבא: שואלים איך הייתם מתחילים.An exercise from the practice page: ask how you would start.לפרק תשובהParsing a reply
- המודל ענה בטקסט שבתוכו JSON.The model answered with text containing JSON.
תרגיל מהעמוד הבא: שואלים איך הייתם מתחילים.An exercise from the practice page: ask how you would start.לא לסמוך בעיניים עצומותDon't trust blindly
safeRecommend(model, shelf) מחזירה את התשובה רק אם היא באמת במדף.safeRecommend(model, shelf) returns the reply only if it really is on the shelf.
תרגיל מהעמוד הבא: שואלים איך הייתם מתחילים.An exercise from the practice page: ask how you would start.מדף, צעד 41: המלצה לפי מצב רוחShelf, step 41: a recommendation by mood
Recommender מקבל Model ומדף, בונה prompt עם המדף ומצב הרוח, ומחזיר רק כותר שבמדף (אחרת no match on your shelf).Recommender takes a Model and a shelf, builds a prompt with the shelf and the mood, and returns only a title on the shelf (otherwise no match on your shelf).
תרגיל מהעמוד הבא: שואלים איך הייתם מתחילים.An exercise from the practice page: ask how you would start.מטמון לתשובותCaching replies
CachedModel עוטף Model ושומר תשובות לפי prompt (computeIfAbsent).CachedModel wraps a Model and stores replies by prompt (computeIfAbsent).
תרגיל מהעמוד הבא: שואלים איך הייתם מתחילים.An exercise from the practice page: ask how you would start.ניסיון חוזרRetrying
- שירותים עונים לפעמים 429 (יותר מדי בקשות).Services sometimes answer 429 (too many requests).
תרגיל מהעמוד הבא: שואלים איך הייתם מתחילים.An exercise from the practice page: ask how you would start.StringBuilder ו-append.StringBuilder and append.
לבנות promptBuilding a promptה-RecordedModel מחפש מילת מפתח בתוך ה-prompt.The RecordedModel looks for a keyword inside the prompt.
תשובה מוקלטתA recorded replyמודלים מוסיפים לפעמים טקסט מסביב: מחפשים את התבנית, לא מניחים שכל התשובה JSON.Models sometimes add text around it: look for the pattern, don't assume the whole reply is JSON.
לפרק תשובהParsing a replyshelf.contains(reply)shelf.contains(reply)
לא לסמוך בעיניים עצומותDon't trust blindlyעם מודל אמיתי משנים רק שורה אחת: איזה Model מעבירים לבנאי.With a real model you change only one line: which Model you pass to the constructor.
מדף, צעד 41: המלצה לפי מצב רוחShelf, step 41: a recommendation by moodעוד מימוש של אותו interface: Decorator.Another implementation of the same interface: a Decorator.
מטמון לתשובותCaching repliesבמציאות מחכים בין ניסיונות, וכל פעם יותר (exponential backoff).In real life you wait between tries, longer each time (exponential backoff).
ניסיון חוזרRetryingמה למדנוWhat we learned
- מודל שפה: תוכנה שמקבלת טקסט (prompt) ומחזירה טקסט.language model: Software that takes text (a prompt) and returns text.
- בקשת POST עם מפתח מתוך משתנה סביבה, גוף JSON עם המודל וההודעות, ותשובה ב-JSON.A POST request with an API key from an environment variable, a JSON body with the model and the messages, and a JSON reply.
Model הוא interface עם מתודה אחת.Model is an interface with one method.- מדף, צעד 41: המלצה לפי מצב רוחShelf, step 41: a recommendation by mood
כל אחד אומר דבר אחד שלמד היום.Everyone says one thing they learned today.עכשיו לתרגולNow, practice
5 תרגילים ו-2 אתגרים, עם בדיקה אוטומטית5 exercises and 2 challenges, auto-checked
מפנים לעמוד התרגול.Point to the practice page.