تماشای این درس نیاز به اشتراک حرفه‌ای دارد.

Install OpenAI Client0:00

Welcome back, everybody. All right, let's get back to work. Now, before we move on to our next example, I will return to the Chat class that we've been working on. And, you know, right down here, I want to swap out this API request with a dedicated package. Have a look. It turns out that there's a really great OpenAI client for php that we can pull in. And on top of that, there is a Laravel wrapper.

for php that we can pull in. And on top of that, there is a Laravel wrapper. So this is the one that we will pull in. Okay, so I'm going to scroll down, and let's go ahead and require it. And then I will run composer install, as you see here. All right, paste that in. And while that's doing its thing, I'll grab this one as well. All right, paste that in. All right, so it looks like that created a new OpenAI configuration file. And, hmm, look right here.

Configure API Credentials0:48

All right, so it looks like that created a new OpenAI configuration file. And, hmm, look right here. In the .env file, it's going to expect those two keys. So you'll see they added those to environment.example right down here. So let's grab that. And actually, we're going to migrate over to their naming. So I will go to .env. And yeah, I'm going to grab this right here. And the only difference is we were calling it secret, but they just call it API key.

And the only difference is we were calling it secret, but they just call it API key. All right. Now, we also need an organization ID, and I will show you how you can grab that. All right, so if you're working along, return to platform.openai.com, and then log in, and then come down to settings, organization, and you can see that key right here. So I will grab that and paste it in. Cool. Now, the only other thing I want to do is return to config/services,

Cool. Now, the only other thing I want to do is return to config/services.php, and I need to update the name of this key, like so. Okay, so now we have pulled in the OpenAI PHP package, and then we have updated our environment keys appropriately. So I think we should be all set to go. So check this out. If I return to the documentation here and scroll down, yeah, you can see that now we can use OpenAI as a facade. We are going to work with the chat completions API,

Refactor to OpenAI Facade2:02

yeah, you can see that now we can use OpenAI as a facade. We are going to work with the chat completions API, so we can call this helpful chat method, and then we call create, and then behind the scenes, it will take care of creating the payload and firing off the API request. Okay, and then look in response, we can interact with it as if it were an object. Okay, so let's do that now. I'm going to return to chat, and let's go ahead and remove HTTP and replace it with OpenAI, and make sure you pull in the facade. Okay, so now right down here, I can replace all of this, I think,

and replace it with OpenAI, and make sure you pull in the facade. Okay, so now right down here, I can replace all of this, I think, with OpenAI, chat, create, and then I will reference our options like so. All right, and then response will be an object, so I don't have to call JSON on it. Instead, I can just replace this with choices[0]['message']['content']. And yeah, I think everything should still just work. So why don't we load this in the browser and see if we screwed anything up. All right, we lucked out, and it seems like everything is still working. Okay, so now we can move on to the example for this episode.

Build Artisan Chat Command3:07

All right, we lucked out, and it seems like everything is still working. Okay, so now we can move on to the example for this episode. And here's what I was thinking. It might be fun if we created a little command line utility for, once again, chatting or interacting with OpenAI. But now we can do it directly from the command line. So we'd have the ability to ask a question, follow up, ask for clarification, and I can just keep that conversation going, again, directly from the command line. Okay, and also on that note, it would be cool if we can also pull in the laravel-prompts package just to make the whole interface.

Okay, and also on that note, it would be cool if we can also pull in the layerfl prompts package just to make the whole interface a little bit more attractive. All right, let's see what we can come up with in 10 minutes or so. So if I come to the command line, yeah, I'm thinking something like this. php artisan chat. So let's make that command. All right, open that up. And we will interact with it like this, chat. And hmm, do we want to include our question at the start?

And we will interact with it like this, chat. And hmm, do we want to include our question at the start? Possibly, but I will add that later. Okay, start a chat with OpenAI. And yeah, let's begin with this. This, ask a question. What is your question for AI? All right, so this will be the question. And yeah, very quickly, if I die and dump that, you can see how this will work. php artisan chat.

And yeah, very quickly, if I die and dump that, you can see how this will work. php artisan chat. What is your name? All right, and we're going to take that message and fire it off to OpenAI's API. Okay, now what's nice, though, is we've already created this Chat class. So it should be relatively easy to get up and running. So if I come back to our routes file, yeah, we're just mostly reproducing what we had in the first or second episode. All right, let's switch back. All right, and paste all of that in here.

All right, let's switch back. All right, and paste all of that in here. So create a new Chat class. We're not going to have a system message yet, but we will send the question, and that will then give us our response. Okay, so now I could say this info response. And yeah, this is just our first draft of it. Let's see if it's working. php artisan chat. How are you today?

php artisan chat. How are you today? All right, it makes the API request. And here we go. As an AI, I don't have feelings, so I don't have good or bad days. But I'm here to help you with any questions you have. Okay, so this is working. But now I need the ability to follow up, to keep the conversation going. So what I could do is something like this. This ask, do you want to respond?

So what I could do is something like this. This ask, do you want to respond? And you'll see right here the default for the ask is null. So maybe I could say something like, if this ask, do you want to respond? Then what is your response? And that will once again be saved to this. And yeah, we're just sort of reproducing what we had here. All right. All right. So does this all make sense?

All right. So does this all make sense? We create a new chat, and we send off our initial question. And that will return a response, which we dump to the CLI. And then we ask, do you want to keep going? Do you want to respond? If that is true, then we ask another question. All right, what is your response? Or what is your reply? And once again, that's saved to a new question that we fire off to OpenAI.

Or what is your reply? And once again, that's saved to a new question that we fire off to OpenAI. Now, keep in mind, when we call send the second time, it remembers that we had messages right up here. And that's what we worked on in the last episode, right? So whenever we send a new message, it records that within this messages array. So when we fire off our response, it's going to remember that. And remember, that's how we continue a conversation, is we just take that whole dump of the chat, and we include that with our reply. And actually, just to make sure that we're all on the same page here, let's do this.

is we just take that whole dump of the chat, and we include that with our reply. And actually, just to make sure that we're all on the same page here, let's do this. Before we fire it off, or actually right here, let's dd and dump the chat messages so you can see what's going on here. What is your name? All right, do you want to respond? Yes. What is your reply? My name is Jeffrey. All right, and that should dump.

My name is Jeffrey. All right, and that should dump. Yeah, here we go. So here are all the messages that were included with that reply. So we had our initial question, and then the response from our assistant, and then our reply, and then the next reply from the assistant, which was nice to meet you, Jeffrey. How can I assist you today? That's the way that works. Okay, so notice, though, if I were to remove this,

That's the way that works. Okay, so notice, though, if I were to remove this, well, this will work, but only for one reply, right? Let's try it together. What is your name? My name is Jeff. Oh, I'm sorry, I screwed up. My name is Jeff. I'll do it again. All right, nice to meet you, Jeffrey.

Loop Conversation Replies8:19

I'll do it again. All right, nice to meet you, Jeffrey. And then conversation over. So it's almost like we need a loop. While we have more things to say, then ask us what we need to say, right? So why don't we swap this out with a while statement? So while this question is truthy, then run this code. All right, so think about it. The first time we get it, do you want to respond? Yes.

The first time we get it, do you want to respond? Yes. So we ask for your response, and we send it to the chat, and then we dump it to the console, and then it repeats. Do it again. Do you want to respond? And then we can do it indefinitely. Okay, so let's try it this time. All right, what is two plus two? Okay, I want to respond.

This is just a simple explanation with no code.

So sometimes it will tell you seven plus six is 14. I ran into that the other day. And then you will let it know, like, wait a minute, seven plus six is 13. And of course, it will then do that thing where it goes. So I'm very sorry about that. Thank you for catching that. I endeavor to improve or something like that. But yeah, it's really funny when it can do incredibly complicated things, but then miss first grade math questions. Very interesting.

but then miss first grade math questions. Very interesting. Okay, cool. So if I don't want to respond, this would then be falsy, and it will kill the conversation. The conversation there. Okay, so now at the very bottom, I could do something like this info conversation over. Okay, so now I just want to clean this up a little bit. And then why don't we also pull in Laravel prompts, right?

Enhance CLI with Prompts10:04

Okay, so now I just want to clean this up a little bit. And then why don't we also pull in Laravel Prompts, right? So maybe that will be your first introduction to prompts, which is cool. composer require laravel/prompts. All right, cool. So now what I can do at the top is pull in a text function from Laravel Prompts. And that can be right here. Let's swap out all of our ask calls with text. Next info if we want.

Let's swap out all of our ask calls with text. Next info if we want. If we want to be consistent, we could do text info. And of course, visit the Laravel prompts documentation for all of the functions that you can call. But yeah, now I can replace this and one more with our response. Okay, so let's give this a run. And actually on that note, if you're not familiar with Laravel prompts, it is an interface for creating beautiful forms on the command line, just like you see here.

it is an interface for creating beautiful forms on the command line, just like you see here. So why don't we write something like, what are your thoughts about Laravel? Now, notice it's firing off this request, but it could potentially take a number of seconds to respond. So we should probably provide a little bit of feedback, if only like a little loading bubble, just some indication that, hey, we're sending that request right now. Okay, Laravel, you can read that if you want to pause.

just some indication that, hey, we're sending that request right now. Okay, Laravel, you can read that if you want to pause. Okay, do you want to respond? Yes. Thanks so much. And there we go. Cool. Okay, so let's work on that little loading indicator. And we can do that after each chat. So at the very top here, we're going to pull in a new one called spin. It's kind of a cool name. So spin will accept a closure here.

It's kind of a cool name. So spin will accept a closure here. So it looks like chatSendQuestion is what we want to do here. And even though we're providing a closure, the result here will be returned from the spin function itself. So we could do something like this. And then notice, if we go into spin, we give it our closure. And then we can give it a message, which is like our loading message. So sending request. Something like that.

So sending request. Something like that. All right, let's see how that one looks. What is 2 plus 2? Sending request. And then once the request comes back, of course, the loading indicator goes away. Cool. So now we can do the exact same thing right down here. Okay, so what else could we do here? I'm thinking maybe it's a little redundant.

Okay, so what else could we do here? I'm thinking maybe it's a little redundant. Do we need two questions here? Do you want to respond? Yes. What is your reply? Can't we just merge that into one? For example, what if I said, while $question equals, do you want to respond? Then just skip that next part. Because remember, if this is falsy, then we won't run any of this code to begin with. So yeah, it just felt a little redundant to have two questions there.

Because remember, if this is falsy, then we won't run any of this code to begin with. So yeah, it just felt a little redundant to have two questions there. All right, let's keep going. Chat, what is your question for AI? What do you think about Laracast? Sending request. It'll take a number of seconds to respond. All right, blah, blah, blah. Once again, you can read that on your own. Should folks sign up for Laracast?

Once again, you can read that on your own. Should folks sign up for Laracast? Sending request. Okay, oh, and we got a lot here. Signing up for Laracast can be a valuable investment for individuals interested in blah, blah, blah, blah, comprehensive learning, high quality instruction, constantly updated, active community, cost effective. Whether you should sign up depends on your specific learning. Wrong, they were so close. But of course, the answer is yes.

This is just a simple explanation with no code.

Maybe skipping it will end the conversation. Maybe I could even write no, and then it would detect that I wrote no, and then kill the conversation. You can do whatever you want there. But yeah, this will give you a general idea of the workflow. Okay, so is there anything else before we wrap up? How about we allow for a system message as part of the command? Maybe something like this. Let's offer an option for the system message that will expect a value. And then let's see, let's put our chat at the top.

Let's offer an option for the system message that will expect a value. And then let's see, let's put our chat at the top. And that can maybe be the first thing. So I could say, if this option system, then we should do that thing where we call chatSystemMessage. And then I could pass that in here. This option system. All right, cool. Next, right here, something to keep in mind. What is your question for AI?

Next, right here, something to keep in mind. What is your question for AI? It's possible that that could be falsy. So for example, if I die and dump that, give it another shot, and I click through it, notice, yeah, it's a falsy value. And that doesn't make sense. That's really a required field. So what I can do here is pass through label. We're just using named arguments here. And required true.

We're just using named arguments here. And required true. Yes, so that would fix that particular issue. Try again, try to skip it. And it lets me know, hey, this field is required. You got to give us something, which is cool. Okay, but anyways, that's mostly it. Last little tip related to Laravel prompts is keep in mind, there are a bunch of these little helpers you can use. info, display an intro, display an outro message,

there are a bunch of these little helpers you can use. info, displayIntro, displayOutro message, tables, warnings, notes, errors, anything you can think of. So for example, if we wanted to try out displayOutro, I could import that at the top as you see here. And let's see what that looks like. How are you today? I am doing well. All right, we're done. And yeah, notice the displayOutro just has some unique styling.

All right, we're done. And yeah, notice the outro just has some unique styling. That makes it a little more clear. All right, this is the closing outro line. Okay, so cool. That was sort of an introduction to a couple of things. Creating artisan commands, working with layerful prompts, and then interacting or conversing with open AI. All right, I'll see you in the next episode for another example. Bye.

All right, I'll see you in the next episode for another example. Bye.

دوست دارید گاهی خبرهای Laracasts را ایمیل کنیم؟