Add User input to Conversation Array

Tom Chant
InstructorTom Chant
Share this video with your friends

Social Share Links

Send Tweet
Published a year ago
Updated a year ago

Learn how to capture user input and add it to a conversation array in JavaScript. The process involves listening for a submit event, extracting the user's input, and creating an object with role and content properties to represent the user's message.

The object is then pushed to the conversation array. By logging the conversation array, you can verify that the user's input has been successfully added. This step is crucial as it prepares the data to be sent to the OpenAI API for further processing.

[00:00] Okay, so we have the instructions object in the array, and now we need to add the user's input. So in index.js, we've got this event listener listening out for a submit,

[00:12] and when it detects the submit, the anonymous function takes the user's input and saves it to this const user input. So what we need to do is take that incoming text and put it into an object and push it to conversation array.

[00:28] Now, we've already seen how the chat GPT model wants its objects, and we've got an example of it right here. We've got an object with two key value pairs, role and content, and what we're going to do next is actually very, very similar.

[00:42] The object is going to have two key value pairs. The first key will be role, and this should correspond to the value user. The second key will be content, and this should correspond to a string holding whatever the user has inputted.

[00:58] So right here inside the event listeners anonymous function, I have written you a challenge. I want you to push an object holding the user's input to conversation array and then just log out conversation array to check it's worked. Now, I'm going to leave this slide right here so you know exactly what that object should hold.

[01:18] OK, pause now, get that sorted, and I'll see you back here in just a moment. OK, so hopefully you managed to do that just fine, so I'm going to come in here and I'm going to say conversation array.

[01:37] And I'll use dot push, and we're going to push an object, and we know the first key will be role, and that will have the value of the string user. The second key will be content, and that will hold whatever the user has inputted.

[01:54] OK, now let's log out conversation array and see if it's worked. So I'll open up the console and I'll ask a question. And there we are. We are logging out conversation array, and you can see that we've got two objects. The first object has got the role of system and the content with our instruction.

[02:13] The second object has got the role of user and the content. What is your name? Which is the question that I just asked. OK, so that is perfect. We have got the conversation array growing. We've got the instructions object. We've got the user's inputted text. And the next step of the process is to send it all off to the OpenAI API.

[02:33] So in the next scrim, let's look at how we actually use the create chat completions endpoint, which is just a little bit different to the previous endpoints we've used.

egghead
egghead
~ just now

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

  • This was great!
  • This was horrible!
  • I didn't like this because it didn't match my skill level.
  • +1 It will likely be deleted as spam.

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!

Markdown supported.
Become a member to join the discussionEnroll Today