Renaming to Dialogue Command0:00
All right, so in the last episode, we learned how to send out a prompt and receive a response. But then the conversation was over, and there was no way to continue the conversation. So let's figure that out in this video. So we're going to continue with this command idea. So here's what I'll do. I'll say chat commands, and I'm going to change this one with a duplicate to dialogue commands.
Introducing Conversation History0:24
dialogue commands. Now, we're not just chatting to you, we're having an actual dialogue here. All right, we'll say converse with OpenAI. Okay, so now this is all the work from the previous episode, but how would we continue the conversation? Well, if you think about it, we need a way to track the history, right? So as it turns out, actually, with the responses API, there are ways that it can help us and
So as it turns out, actually, with the responses API, there are ways that it can help us and remember the conversation, but for the purposes of learning, I want to be in charge of maintaining the history. So keep that in mind. So let's do this. At the very top, let's track our, if I can type correctly, let's track our history. All right, so this is going to be sort of the source of truth for our
history. All right, so this is going to be sort of the source of truth for our conversation. If I came up to you and said, hello, and then you responded with hello, how are you? Well, now the history would be two items, right? One from me, where I say hello, and one from you, where you say hello back. All right, so we're going to track that dialogue within this array. So now if you think about it, after I know what I'm going to say, I'm going to immediately
Sending History as Input1:30
So now if you think about it, after I know what I'm going to say, I'm going to immediately write to the history like this, this history, and let's push a new item to it, and it's going to be exactly what we did before. Role is user, and the content is the prompt. Okay, so now when I scroll down, we run the model, and this time, we're not receiving a single prompt, we're going to send through all of the input. So I'm going to pass the history array to input.
a single prompt, we're going to send through all of the input. So I'm going to pass the history array to input. All right, so now if I switch back, we no longer need to pass the prompt. All right, so we still don't have a dialogue here, but let's just make sure we haven't broken anything. PHP artisan dialogue, what is up on your end? Give it a second. It's thinking not much just running here and ready to help. Cool.
Appending AI Output to History2:22
It's thinking not much just running here and ready to help. Cool. We haven't broken anything. Okay, but now, how do I continue the conversation, right? We're tracking what I wrote, but do we do anything with the response? No, not really. We just immediately echo it. So let's fix that, and I'm going to go to full screen. Just here where we fetch the response, you will remember in the last episode that we
Just here where we fetch the response, you will remember in the last episode that we have response output that will be an array of arrays that represent part of the history, really, if you think about it. So if I were to dump and run this again, hi there, we're going to see, yeah, so we have an array of arrays and notice that in this case, the role is not me because I'm not speaking, this is the AI or the assistance response.
not speaking, this is the AI or the assistance response. So if you really think about it, I could take all of this and record it in the history. So let's do that now. What if I said, all right, let's override the history. The history is now whatever it currently is, and this looks right to me, actually, as well as what we fetched from the output response. All right, so by the way, this is also just the same as doing array merge, if
as what we fetched from the output response. All right, so by the way, this is also just the same as doing array merge, if you like that one, this is just sort of a modern option. All right, so now, think about it, if I dump the history, the history will now contain my initial prompt, as well as whatever output the AI returns, hi there. And here is our history, our initial prompt, and then we have the response, cool. But here's the problem, we now are tracking all of the history correctly, but
cool. But here's the problem, we now are tracking all of the history correctly, but then once the AI responds, we dump it to the console and that's it, the command completes . So it's almost like we need a loop that allows me to continue the dialogue so that I can then provide a new prompt. All right, so how long could this dialogue go? Well, potentially, never ending, right, or I could set a maximum, but generally
Looping for Ongoing Dialogue4:17
All right, so how long could this dialogue go? Well, potentially, never ending, right, or I could set a maximum, but generally , a dialogue can go on as long as you want. So with that in mind, why don't we wrap this within a while true. And this is sort of the general secret sauce for an agent, right? It's going to be a while loop. All right, so now I'm going to take this whole thing here. I'm going to move it in to our loop. So think about how things have changed now.
I'm going to move it in to our loop. So think about how things have changed now. With this single addition, now I'm going to prompt the user. I will send that prompt to AI, I will store the history, I will log or record the output, and then it repeats again, right? Now we prompt again, but now think about it. When I provide my follow-up prompt, that will be appended to the history. And now that collection, which would be my initial prompt, the response from AI , and
And now that collection, which would be my initial prompt, the response from AI , and now my response to its response will be a collection that we send to the model. So now the input is three items, me, then AI, then me again, and we'll get a new response. And then we will update the history again. So it's very simple when you think about it. So let's do this again. Let's jump the history, because I want to make this crystal clear, it's really important
Let's jump the history, because I want to make this crystal clear, it's really important that you understand this. By log. Hi there. All right, we get our response. And now here is our history, my initial prompt and the AI's response. Let's continue. Hi, how can I help you today? Call me Jeffrey.
Hi, how can I help you today? Call me Jeffrey. All right, sure thing, Jeffrey, nice to meet you. And let's look at the history now, so we can see my initial high there. Now can I help you, call me Jeffrey, sure thing. So now it more basically filling up the context, right? So if I say what is my name, well, it knows my name because it's included with our history. Your name is Jeffrey, it remembers it. So if we didn't take this approach, every new message we send to AI would be
Your name is Jeffrey, it remembers it. So if we didn't take this approach, every new message we send to AI would be like a clean slate. It wouldn't even know my name because it wasn't included as part of the input. So that's why we store and pass the history along with the input. All right, so yes, step by step, right? In episode one, we learned how to create a simple call and response command. In episode two, this one, we now learned how to continue the dialogue using a while loop.
Previewing Tools for Agents6:40
In episode two, this one, we now learned how to continue the dialogue using a while loop. So we're not quite into agent territory yet, I wouldn't say, because as it turns out, the key to an agent isn't just simply the loop. But also the ability to run tools. So we'll talk about tools in the next episode.
