| 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 |
Learn how to extract the AI response from the API data.
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:
console.log(response) → see the whole objectconsole.log(response.choices) → see the array of choicesconsole.log(response.choices[0]) → drill into the first elementconsole.log(response.choices[0].message) → see the object holding the replyconsole.log(response.choices[0].message.content) → see the actual reply stringFinal step: Assign the value into the variable:
botReply = response.choices[0].message.content;console.log("Bot reply:", botReply);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.
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.
sendToModel() and press EnterNext: 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 |