Locked learning resources

Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Giving the Model a Short-Term Memory

00:00 So I’m an agent here, right? Why is this thing not remembering what we did before? Can you spot why? And the answer is that at the moment, we are only ever sending that message, whatever user_input.

00:13 So every time, the next time that I wrote, can you run that, to the harness, that is the only message that I sent. Only one message went to the model. Let’s fix this.

00:24 So I’m going to do a few changes here. First of all, outside of the while loop, I’m going to create a list called messages, which is for the time being empty.

00:34 And instead of overwriting it here, I’m going to append to it. Why? Because now messages is going to be a list that is going to grow. And let’s make sure.

00:45 That’s the problem with changing things, right? Parentheses and brackets get in the way. So now I’m adding that dictionary, which has the role and the content, to messages.

00:55 Good. Nothing changes here. And you may already know this about LLMs. The reply that the LLM gives us is part of the conversation. So next time I’m going to send anything to the LLM again, I actually want to append the reply.

01:12 So now there is my original message and the reply, which will come from the agent. And then every time I’m going to keep adding because I’m in a while loop, right?

01:21 So now every time the loop runs, we’re going to add my message and the response from the LLM. This is why, by the way, when your conversations get long, they get more expensive because you’re always, even if you just write “thank you” at the end, you’re actually sending the whole conversation, all the messages you sent and all the replies the LLM gave you.

01:41 There’s actually one more change I want to do. Here it says “sending a message.” Well, this is now not technically correct. In fact, you may see that I had preempted this by making this an f-string.

01:52 Here I want to say sending how many messages. Yeah, it could be one, it could be two, it could be three, it could be a chapter. Let me start this again. What’s in the habits folder?

02:08 So now you can see it is sending one message and the model says, as we now know, it’s always a bit different, but I can’t access your file system from here.

02:16 Run ls -la habits. Let me now see. Can you run that for me? I made a typo there, but it doesn’t matter. Agents, LLMs are quite good at that. Notice how now it said sending three messages.

02:30 Why? My original message, the reply from the LLM, and then my follow-up message. So there are three messages going through. And notice how this time it still doesn’t do it for us.

02:41 That’s going to be next, but at least it remembers what we talked about earlier. I can’t run shell commands or access habits from this chat. And it tells me again, right?

02:50 But earlier you may remember that when I said something as plain as, can you run that for me? It said, what do you want me to run? Whereas now it remembers that.

02:58 We don’t have an agent yet, right? Here we just have a chat with an LLM. You’ve moved the messages list outside the loop, so it keeps growing. Each turn adds your message and the model’s reply.

03:11 So the model sees the whole conversation, but the model still can’t do anything for you. In the next lesson, you’ll describe your first tool to the model so it can ask your code to run it.

Become a Member to join the conversation.