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

JavaJava
41
חלקPart 7: Java בעולם האמיתיJava in the real world כ-3 שעות (כולל הפסקה)About 3 hours (with a break)

Java בעידן ה-AIJava in the AI era

עבודה נכונה עם עוזר 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.

כותבים בעורך שבכל תרגיל, לוחצים הרצה כדי לראות פלט, ובדיקה כדי שהבודק האוטומטי יבדוק. אפשר גם לעבוד ב-IntelliJ ולסמן פתרתי. ההתקדמות נשמרת.Write in each exercise's editor, press Run to see output and Check for the automatic checker. You can also work in IntelliJ and mark I solved it. Progress is saved.

חימום: 3 שאלותWarm-up: 3 questions

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

2. המודל המליץ על ספר שלא קיים במדף. מה עושים?The model recommended a book that isn't on the shelf. What do you do?

3. למה לבדוק עם תשובות מוקלטות ולא מול המודל?Why test with recorded replies and not against the model?

תרגיליםExercises

תרגילExercise 1

לבנות promptBuilding a prompt

prompt(shelf, mood) בונה טקסט: תפקיד, רשימת הספרים (כל אחד בשורה עם - ), מצב רוח, והוראה לענות בכותר אחד מהמדף. הדפיסו את ה-prompt עבור Dune ו-Emma ו-adventure.

prompt(shelf, mood) builds text: a role, the list of books (each on a line with - ), a mood, and an instruction to answer with one title from the shelf. Print the prompt for Dune and Emma and adventure.

איך הפלט אמור להיראות?What should the output look like?
You recommend books from a personal shelf.
Shelf:
- Dune
- Emma
Mood: adventure
Answer with exactly one title from the shelf.
Main.java
ההשוואה נעשית בדפדפן מול הפלט האמיתי של הפתרון שלנו. אם הקוד נפל, הדביקו את הודעת השגיאה ותקבלו הסבר.The comparison happens in your browser, against our solution's real output. If the code crashed, paste the error message and get an explanation.
רמזHint 1: StringBuilder ו-append.StringBuilder and append.
לפתרוןSolution
תרגילExercise 2

תשובה מוקלטתA recorded reply

עם RecordedModel בקשו סיכום של Dune, הדפיסו אותו, בדקו שהוא עד 80 תווים (short enough), והדפיסו calls: 1.

With RecordedModel ask for a summary of Dune, print it, check it's at most 80 characters (short enough), and print calls: 1.

איך הפלט אמור להיראות?What should the output look like?
A desert planet, a young duke, and a fight over spice.
short enough
calls: 1
Main.java
ההשוואה נעשית בדפדפן מול הפלט האמיתי של הפתרון שלנו. אם הקוד נפל, הדביקו את הודעת השגיאה ותקבלו הסבר.The comparison happens in your browser, against our solution's real output. If the code crashed, paste the error message and get an explanation.
רמזHint 1: ה-RecordedModel מחפש מילת מפתח בתוך ה-prompt.The RecordedModel looks for a keyword inside the prompt.
לפתרוןSolution
תרגילExercise 3

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

המודל ענה בטקסט שבתוכו JSON. parse(reply) מוציאה רשימת Suggestion(title, score) בעזרת regex, ומחזירה רשימה ריקה כשאין JSON. הדפיסו [Suggestion[title=Dune, score=9], Suggestion[title=Emma, score=7]] ואז [].

The model answered with text containing JSON. parse(reply) pulls out a list of Suggestion(title, score) with a regex, and returns an empty list when there's no JSON. Print [Suggestion[title=Dune, score=9], Suggestion[title=Emma, score=7]] then [].

איך הפלט אמור להיראות?What should the output look like?
[Suggestion[title=Dune, score=9], Suggestion[title=Emma, score=7]]
[]
Main.java
ההשוואה נעשית בדפדפן מול הפלט האמיתי של הפתרון שלנו. אם הקוד נפל, הדביקו את הודעת השגיאה ותקבלו הסבר.The comparison happens in your browser, against our solution's real output. If the code crashed, paste the error message and get an explanation.
רמזHint 1: מודלים מוסיפים לפעמים טקסט מסביב: מחפשים את התבנית, לא מניחים שכל התשובה JSON.Models sometimes add text around it: look for the pattern, don't assume the whole reply is JSON.
לפתרוןSolution
תרגילExercise 4

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

safeRecommend(model, shelf) מחזירה את התשובה רק אם היא באמת במדף. אחרת: fallback: Dune (model said 'Moby Dick'). הדפיסו עבור שני מודלים מוקלטים: Emma ואז ה-fallback.

safeRecommend(model, shelf) returns the reply only if it really is on the shelf. Otherwise: fallback: Dune (model said 'Moby Dick'). Print for two recorded models: Emma then the fallback.

איך הפלט אמור להיראות?What should the output look like?
Emma
fallback: Dune (model said 'Moby Dick')
Main.java
ההשוואה נעשית בדפדפן מול הפלט האמיתי של הפתרון שלנו. אם הקוד נפל, הדביקו את הודעת השגיאה ותקבלו הסבר.The comparison happens in your browser, against our solution's real output. If the code crashed, paste the error message and get an explanation.
רמזHint 1: shelf.contains(reply)shelf.contains(reply)
לפתרוןSolution
תרגילExercise 5

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

פרויקט מלווהCompanion project

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.

איך הפלט אמור להיראות?What should the output look like?
adventure -> Dune
romance -> Emma
something light -> no match on your shelf
Main.java
ההשוואה נעשית בדפדפן מול הפלט האמיתי של הפתרון שלנו. אם הקוד נפל, הדביקו את הודעת השגיאה ותקבלו הסבר.The comparison happens in your browser, against our solution's real output. If the code crashed, paste the error message and get an explanation.
רמזHint 1: עם מודל אמיתי משנים רק שורה אחת: איזה Model מעבירים לבנאי.With a real model you change only one line: which Model you pass to the constructor.
לפתרוןSolution

אתגרים לחזקיםChallenges for the brave

אתגרChallenge 1

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

CachedModel עוטף Model ושומר תשובות לפי prompt (computeIfAbsent). ארבע שאלות זהות עולות קריאה אמיתית אחת: Frank Herbert | real calls: 1.

CachedModel wraps a Model and stores replies by prompt (computeIfAbsent). Four identical questions cost one real call: Frank Herbert | real calls: 1.

איך הפלט אמור להיראות?What should the output look like?
Frank Herbert | real calls: 1
Main.java
ההשוואה נעשית בדפדפן מול הפלט האמיתי של הפתרון שלנו. אם הקוד נפל, הדביקו את הודעת השגיאה ותקבלו הסבר.The comparison happens in your browser, against our solution's real output. If the code crashed, paste the error message and get an explanation.
רמזHint 1: עוד מימוש של אותו interface: Decorator.Another implementation of the same interface: a Decorator.
לפתרוןSolution
אתגרChallenge 2

ניסיון חוזרRetrying

שירותים עונים לפעמים 429 (יותר מדי בקשות). withRetry(model, prompt, tries) מנסה שוב אחרי כישלון, עד מספר ניסיונות. עם מודל שנכשל פעמיים: try 1 failed: ..., try 2 failed: ..., Emma (after 3 tries).

Services sometimes answer 429 (too many requests). withRetry(model, prompt, tries) tries again after a failure, up to a number of tries. With a model that fails twice: try 1 failed: ..., try 2 failed: ..., Emma (after 3 tries).

איך הפלט אמור להיראות?What should the output look like?
try 1 failed: 429 too many requests
try 2 failed: 429 too many requests
Emma (after 3 tries)
Main.java
ההשוואה נעשית בדפדפן מול הפלט האמיתי של הפתרון שלנו. אם הקוד נפל, הדביקו את הודעת השגיאה ותקבלו הסבר.The comparison happens in your browser, against our solution's real output. If the code crashed, paste the error message and get an explanation.
רמזHint 1: במציאות מחכים בין ניסיונות, וכל פעם יותר (exponential backoff).In real life you wait between tries, longer each time (exponential backoff).
לפתרוןSolution

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