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

חזרה למפגשBack
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.

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

  • מונחיםTerms
  • הקריאה האמיתיתThe real call
  • המודל מאחורי interfaceThe model behind an interface
  • פרויקט מדףShelf project
נושאTopic 1

מונחיםTerms

המונחים של המפגש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:
נושא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.
נושא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.
נושאTopic 4

פרויקט מדףShelf project

הצעד של היום בפרויקט מדףToday's step in the Shelf project

  • מדף, צעד 41: המלצה לפי מצב רוחShelf, step 41: a recommendation by mood

מודל שפה: תוכנה שמקבלת טקסט (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:

בקשת 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.

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.

מדף, צעד 41: המלצה לפי מצב רוחShelf, step 41: a recommendation by mood

איפה שומרים מפתח API?Where do you keep an API key?

  • במשתנה סביבהIn an environment variable
  • בקוד, בקבועIn the code, as a constant
  • בקובץ READMEIn the README
  • ב-URLIn the URL

במשתנה סביבהIn an environment variable

המודל המליץ על ספר שלא קיים במדף. מה עושים?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

בודקים, ומחזירים ברירת מחדל או מבקשים שובCheck, and return a fallback or ask again

למה לבדוק עם תשובות מוקלטות ולא מול המודל?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

הבדיקה מהירה, חינמית, ונותנת אותה תוצאה בכל פעםThe test is fast, free, and gives the same result every time

לבנות promptBuilding a prompt

  • prompt(shelf, mood) בונה טקסט:prompt(shelf, mood) builds text:

תשובה מוקלטת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:

לפרק תשובהParsing a reply

  • המודל ענה בטקסט שבתוכו JSON.The model answered with text containing JSON.

לא לסמוך בעיניים עצומותDon't trust blindly

  • safeRecommend(model, shelf) מחזירה את התשובה רק אם היא באמת במדף.safeRecommend(model, shelf) returns the reply only if it really is on the shelf.

מדף, צעד 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).

מטמון לתשובותCaching replies

  • CachedModel עוטף Model ושומר תשובות לפי prompt (computeIfAbsent).CachedModel wraps a Model and stores replies by prompt (computeIfAbsent).

ניסיון חוזרRetrying

  • שירותים עונים לפעמים 429 (יותר מדי בקשות).Services sometimes answer 429 (too many requests).

StringBuilder ו-append.StringBuilder and append.

ה-RecordedModel מחפש מילת מפתח בתוך ה-prompt.The RecordedModel looks for a keyword inside the prompt.

מודלים מוסיפים לפעמים טקסט מסביב: מחפשים את התבנית, לא מניחים שכל התשובה JSON.Models sometimes add text around it: look for the pattern, don't assume the whole reply is JSON.

shelf.contains(reply)shelf.contains(reply)

עם מודל אמיתי משנים רק שורה אחת: איזה Model מעבירים לבנאי.With a real model you change only one line: which Model you pass to the constructor.

עוד מימוש של אותו interface: Decorator.Another implementation of the same interface: a Decorator.

במציאות מחכים בין ניסיונות, וכל פעם יותר (exponential backoff).In real life you wait between tries, longer each time (exponential backoff).

מה למדנו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

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

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

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