Accessing Last Response0:00
Okay, next up, let's review how to extract a field or a property. Okay, so once again, I'm within this AIChat class. And let's start by visiting the send method that we extracted in the last episode. Okay, so once again, this response will be a string that is equal to whatever the response message is from OpenAI. So if I send it hello, maybe its response is hello back to you. Okay, and then we update this messages array, and then we return the current instance. But yeah, there might be situations where we want a helpful getter that gives us the last text response from OpenAI. But, hmm, yeah, right now, it doesn't seem like there's an easy way to do that.
that gives us the last text response from OpenAI. But, hmm, yeah, right now, it doesn't seem like there's an easy way to do that. Instead, I would have to fetch the entire messages array, get the most recently pushed item from it, or the last item, and then read the value for this content key. Yeah, it's just a little bit clumsy. That's something I may frequently need to access. Okay, so let's review some solutions. First up, I'm going to select the variable, and then bring up my refactoring menu with Control-T by default.
Introducing a Field1:06
First up, I'm going to select the variable, and then bring up my refactoring menu with Control-T by default. Let's come down to introduce field, or again, Option-Command-F. All right, let's call it response. And there we go. So now I have a new response property. And by the way, if you want to see it, you can Command-Click on it right here, and it takes us up. Okay, so you'll notice a little squiggly here. That's because it wasn't able to infer what the appropriate type is.
Okay, so you'll notice a little squiggly here. That's because it wasn't able to infer what the appropriate type is. So let's add string. And then just to be consistent, I will once again bring up my QuickFix menu and tweak this to be protected. Okay, so again, if I see a property and I'm curious, well, where is that being used? The exact same process. I can Command-Click on the property, and this will show me everywhere that it's being referenced within this file.
I can Command-Click on the property, and this will show me everywhere that it's being referenced within this file. So once again, it looks like on line 96 as well as line 105. Yeah, pretty cool. That works great. Seamless. Okay, so now we have this property, but it's still not accessible from the outside world, so to speak. So let's return there with Command-Click, and let's generate a getter.
Generating a Getter2:11
So let's return there with Command-Click, and let's generate a getter. So once again, selecting the property name, I will hit Command-N, or I could bring up the QuickFix menu, and this will give me the option to add a getter. And we can see a quick example for what that's going to look like. All right, that looks good to me. Hit Return, and yeah, there we go. So now we have a getter or an accessor that returns the current value of the most recently created response.
So now we have a getter or an accessor that returns the current value of the most recently created Response. Okay, great. And yeah, now if we want, name it whatever you want. Response, lastResponse, lastUpdate, whatever. Whatever makes sense for you. Okay, very cool. But now maybe later we decide, well, it was a little cumbersome to manually go into the messages array and then grab the last item and then grab the content key.
Rolling Back Changes2:51
well, it was a little cumbersome to manually go into the messages array and then grab the last item and then grab the content key. But if we wrap that up within a getter, it's fine if we dynamically fetch that. So in that case, we no longer have a need for the response field. Okay, so let's get rid of it. Let's go back up. And yeah, there's a couple of ways that we can remove it. So I will once again bring up the Quick Fix menu, and yeah, have a look down here at the bottom.
So I will once again bring up the Quick Fix menu, and yeah, have a look down here at the bottom. Rollback changes in current file. So if we run this, yeah, it's just going to revert it to how it was previously. Perfect. So now if I return to send, we could do the exact same thing here to return it to how it originally was. And yeah, that's just one way.
Deriving Response Dynamically3:31
to return it to how it originally was. And yeah, that's just one way. In this case, it's still a little bit cumbersome, but that's one way that you could revert your changes. But now, of course, this is no longer right because it's referencing a property that doesn't exist. All right, let's make our fix. So to grab the last item from the messages array, of course, there's 101 ways to do it. You could use the end function.
of course, there's 101 ways to do it. You could use the end function. That'll work. It will change, of course, the internal pointer, but that may not matter. Alternatively, though, we're using Laravel, so why don't we reach for Illuminate\Support\Arr, and that offers a handy-dandy last method. We'll give it the $messages array, and then we will grab the content key.
We'll give it the messages array, and then we will grab the content key. And yeah, that will do the trick. So now if I have an instance of this Chat class, at any point, if I want to fetch the most recent text response from OpenAI, I now have a getter to do that. Okay, so in this episode, you learned about introducing a field using the Reflector menu. You also learned how to generate a getter.
Bulk Getter Generation4:29
introducing a field using the Reflector menu. You also learned how to generate a getter. And keep in mind, if I go to the top, for any property here, you can select it, hit Command-N or the Quick Fix menu, and then here you can generate a getter or a setter or both at the same time. Alternatively, though, if you're not selecting a property, you can generate it using Command-N, and notice if I create some getters,
you can generate it using Command-N, and notice if I create some getters, I can do them in bulk. So I could select token and messages, and now, as you see here, we have getters for both of those. But yeah, in this case, it doesn't quite make sense, so I will undo that. All right, onward to the next episode. Bye-bye.
