נבנה על ידי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.

או Ctrl+Por Ctrl+P

הנקודות החשובותKey points

  • מודל שפה: תוכנה שמקבלת טקסט (prompt) ומחזירה טקסט.language model: Software that takes text (a prompt) and returns text.
  • prompt: הטקסט ששולחים למודל:prompt: The text sent to the model:
  • בקשת 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

מונחיםTerms

  • מודל שפהlanguage model: תוכנה שמקבלת טקסט (prompt) ומחזירה טקסט. היא יכולה לטעות או להמציא, ולכן הקוד שסביבה בודק את מה שחזר.Software that takes text (a prompt) and returns text. It can be wrong or make things up, so the code around it checks what came back.
  • promptprompt: הטקסט ששולחים למודל: מה התפקיד, אילו נתונים יש, ובאיזו צורה לענות. prompt מדויק נותן תשובה שקל לבדוק.The text sent to the model: the role, the data available, and the shape of the answer. A precise prompt gives an answer that's easy to check.
  • מפתח APIAPI key: סיסמה שמזהה אתכם מול השירות. שומרים אותה במשתנה סביבה (System.getenv), אף פעם לא בקוד או ב-Git.A password identifying you to the service. Keep it in an environment variable (System.getenv), never in code or in Git.
  • תשובה מוקלטתrecorded reply: מימוש של ה-interface שמחזיר תשובות קבועות מראש. כך בודקים את הקוד בלי רשת, בלי עלות ובלי תוצאות משתנות.An implementation of the interface returning answers fixed in advance. That way you test the code with no network, no cost and no changing results.
  • בדיקת הפלטvalidating output: לפני שמשתמשים בתשובה: בודקים שהיא בפורמט הנכון ושהיא הגיונית (למשל שהספר באמת במדף). אם לא, ברירת מחדל.Before using a reply: check it's in the right format and makes sense (say, the book really is on the shelf). If not, a fallback.
  • מודל שפהlanguage model: תוכנה שמקבלת טקסט (prompt) ומחזירה טקסט. היא יכולה לטעות או להמציא, ולכן הקוד שסביבה בודק את מה שחזר.Software that takes text (a prompt) and returns text. It can be wrong or make things up, so the code around it checks what came back.
  • promptprompt: הטקסט ששולחים למודל: מה התפקיד, אילו נתונים יש, ובאיזו צורה לענות. prompt מדויק נותן תשובה שקל לבדוק.The text sent to the model: the role, the data available, and the shape of the answer. A precise prompt gives an answer that's easy to check.
  • מפתח APIAPI key: סיסמה שמזהה אתכם מול השירות. שומרים אותה במשתנה סביבה (System.getenv), אף פעם לא בקוד או ב-Git.A password identifying you to the service. Keep it in an environment variable (System.getenv), never in code or in Git.
  • תשובה מוקלטתrecorded reply: מימוש של ה-interface שמחזיר תשובות קבועות מראש. כך בודקים את הקוד בלי רשת, בלי עלות ובלי תוצאות משתנות.An implementation of the interface returning answers fixed in advance. That way you test the code with no network, no cost and no changing results.
  • בדיקת הפלטvalidating output: לפני שמשתמשים בתשובה: בודקים שהיא בפורמט הנכון ושהיא הגיונית (למשל שהספר באמת במדף). אם לא, ברירת מחדל.Before using a reply: check it's in the right format and makes sense (say, the book really is on the shelf). If not, a fallback.

Cheatsheet

הקריאה האמיתיתThe real call
String key = System.getenv("ANTHROPIC_API_KEY");
String body = """
        {"model": "claude-sonnet-5-5", "max_tokens": 200,
         "messages": [{"role": "user", "content": "Suggest one classic novel. Reply with the title only."}]}
        """;
HttpRequest req = HttpRequest.newBuilder(URI.create("https://api.anthropic.com/v1/messages"))
        .header("x-api-key", key)
        .header("anthropic-version", "2023-06-01")
        .header("content-type", "application/json")
        .POST(HttpRequest.BodyPublishers.ofString(body))
המודל מאחורי interfaceThe model behind an interface
Model model = new RecordedModel(Map.of(
        "classic novel", "Emma",
        "author of Dune", "Frank Herbert"));
System.out.println(model.complete("Suggest one classic novel. Title only."));
System.out.println(model.complete("Who is the author of Dune? Name only."));
System.out.println(model.complete("What is the weather?"));

שגיאות שפגשנוErrors we met

שגיאהErrorמה זה אומרWhat it meansמה עושיםWhat to do

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