codex-lv2-may-2025

Level Navigation: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 Current Level: 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32


đź§Ş Level 17: Inspect Response

What You’ll Do

Learn how to extract the AI response from the API data.

Instructions

General principle: Responses from APIs are just POJOs (plain old JavaScript objects). Sometimes they contain arrays of objects. Use console.log at each step to drill deeper until you find the reply text.

Walkthrough with Hugging Face format:

  1. console.log(response) → see the whole object
  2. console.log(response.choices) → see the array of choices
  3. console.log(response.choices[0]) → drill into the first element
  4. console.log(response.choices[0].message) → see the object holding the reply
  5. console.log(response.choices[0].message.content) → see the actual reply string

Final step: Assign the value into the variable:

đź’ˇ Code Hints

Extracting data from API responses? You’ll need to navigate the response object to get the actual text. Check out the Console Logging section in SNIPPETS.md for object exploration examples and the Variables section for storing the result.

📚 Connect to Previous Lessons

Working with JavaScript objects and arrays? Check out Week 2, Lesson 4: POJOs for a complete guide to navigating JavaScript objects and accessing nested properties.

âś… Check

  1. Open your webpage in a browser
  2. Open Chrome DevTools (F12) and go to the Console tab
  3. Type sendToModel() and press Enter
  4. You should see the step-by-step exploration of the response object
  5. Look for the final “Bot reply:” log - it should show the actual AI response text
  6. If you see “undefined” for the bot reply, check that you’re accessing the correct path in the response object
  7. The bot reply should be a readable string, not an object or array

Next: Level 18 - Connect to Button



Level Navigation: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 Current Level: 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32