Learn how to send an array of conversations to the OpenAI API. By resurrecting the conversation array and including it in the request, we can ensure that the data is properly sent to the API.
The lesson highlights a challenge where we need to add an instruction object to the front of the conversation array, covering the usage of unshift. It also addresses an error caused by using await inside a non-async function and demonstrates how to fix it by making the callback function async.
[00:00] we need to get the array that we can see in the console off to the OpenAI API. So I'm going to come in here and resurrect the old conversation array. And we'll use it to store the array we get back from the database. And then if I uncomment all of this code,
[00:18] we're now going to send conversation array off to the OpenAI API with each request. Now to check it's working, I'm going to log out the response and I'll just take this opportunity to delete the console.log and also the function call to fetch reply that we were just using as a test. And now we can give this a try,
[00:37] but wait a second, we've actually forgotten about our instruction object. We need that in the conversation that we send to the OpenAI API. So we need to include it in conversation array. And in fact, you can do that as a challenge. So I'm just going to come in here and paste the challenge right there
[00:55] because that is where you'll need to write some code. And I've just said, add instruction object to the front of conversation array. But I've put a warning here. You're going to get an error when you do this. Try to debug it. Okay, we're already logging out the response. So pause now, get this challenge sorted and I'll see you back here in just a minute.
[01:20] Okay, hopefully you got it working. So we can do this with unshift. So we'll say conversation array dot unshift, and then we'll pass in the instruction object. Okay, now let's hit save and we'll give it a try. Oh, but we're getting an error. And I did warn you that you would.
[01:40] Now it's saying unexpected reserved word. So what could be the problem here? Well, we're using await here and we can only use await inside an async function. And that's what we're doing, isn't it? We've got async right here. Well, actually no, because this await is inside the callback function
[01:58] that we've got right here. So that is the function that should be async. So let's delete async from here and add it in right here. Okay, let's save and try again. And there we are, it's working. So the final thing that we need to do with this function is uncomment these two lines of code right here. And in the next scrim,
[02:18] let's see what changes we need to make to them.
Member comments are a way for members to communicate, interact, and ask questions about a lesson.
The instructor or someone from the community might respond to your question Here are a few basic guidelines to commenting on egghead.io
Be on-Topic
Comments are for discussing a lesson. If you're having a general issue with the website functionality, please contact us at support@egghead.io.
Avoid meta-discussion
Code Problems?
Should be accompanied by code! Codesandbox or Stackblitz provide a way to share code and discuss it in context
Details and Context
Vague question? Vague answer. Any details and context you can provide will lure more interesting answers!