در حال بارگذاری ...

Getting API Key0:00

Hey there, let's play around with OpenAI inside of a PHP project. And I promise, from the very beginning, I will show you lots and lots of examples so that you can get up and running in no time at all. All right, so why don't you work along, platform.openai.com, and we will start with the quick start. And yeah, of course, if we want to work with OpenAI, we first have to create an account and then generate an API key. So I already have an account, so I can log in, like so, and now it takes me to my API keys page. All right, so let's generate a demo token, and yeah, don't try to steal this, I'm going to delete it at the end of the video.

Reviewing Quickstart cURL0:38

All right, so let's generate a demo token, and yeah, don't try to steal this, I'm going to delete it at the end of the video. But I will go ahead and copy that, and yeah, now I can switch back to the quick start, and if we scroll down, yeah, you can see they have examples using cURL, Python, and Node. So let's stick with cURL, and then we can easily translate it to php. Okay, so right down here, this is going to be our Hello World example. So notice it's making a POST request to this endpoint. I will go ahead and copy that, and let's see. We see a bunch of stuff here. It's sending through the token, it's referencing a model, and this refers to your desired language.

We see a bunch of stuff here. It's sending through the token, it's referencing a model, and this refers to your desired language model. We'll talk about that in a minute. And then we have some initial messages. So again, we'll talk about this in just a couple moments, but for now, the system role sort of creates the world that our conversation can take place in, and then the user role would represent what we want. It's us starting the conversation, and in this case, that conversation is compose a poem that explains the concept of recursion in programming.

Creating Laravel Project1:39

It's us starting the conversation, and in this case, that conversation is compose a poem that explains the concept of recursion in programming. So we are sending that message to the Completions API. So ChatGPT, or I'm sorry, OpenAI, will read that message and try to complete it by responding with some kind of poem about recursion. That's likely not that great. Okay, so let's get going. I'm now going to switch to the terminal and create a new Laravel project. We'll call it AI. All right, let's cd and open this in my editor.

Building Route API Call2:11

We'll call it AI. All right, let's CDN and open this in my editor. All right, very good. So while it's indexing, yeah, for our Hello World example, we're going to do everything from our routes file. So for example, when the user visits the homepage, we then want to make this request to the Completions API. All right, let's do that now. So we can use Laravel's HTTP facade or clients, and then we can make a POST request to that endpoint that I just copied.

So we can use Laravel's HTTP facade or clients, and then we can make a POST request to that endpoint that I just copied. I'll paste it in here. All right, next, we have to send through our model and the messages. So yeah, let's see. Let's grab this, and then I will convert it for php. So for example, change all of the colons to arrows, and then this, the braces should be brackets. All right, is that good? Reformat.

All right, is that good? Reformat. Yeah. All right, so let's go ahead and import the HTTP facade, like so. And this will give us a response as JSON. All right, and then I will dump this response. Now, just keep in mind, though, this is not going to work, at least not quite yet. But as always, I want you to see the error so that if you run into it, you know exactly what to do. All right, switch to the browser.

Adding Token Configuration3:28

what to do. All right, switch to the browser. And yeah, sure enough, if we load the homepage, we do get an error. And we can see right here, of course, you did not provide an API key as part of the request. All right, so that's our next step. Let's go into our .env file. And at the very bottom, I will add our OpenAI secret. And then I will paste in that API key that we generated earlier. All right, next, I want to reference this key.

And then I will paste in that API key that we generated earlier. All right, next, I want to reference this key. So within our configuration directory, I usually put this in the services.php file. Yeah, right down here, we'll have a service called OpenAI. And then we will add our secret here. And that will look for OpenAI_SECRET. Okay, so now we have an easy way to grab that value. So if I switch back to our routes file, yeah, right here, before we submit the request, let's add our bearer token by using the withToken method. And I can say config('services.OpenAI.secret').

let's add our bearer token by using the withToken method. And I can say config services, whoops, services.OpenAI.secret. All right, reformat, and I'll let you take a look at that. All right, so are we all on the same page? We are using Laravel's HTTP facade to make an API request to this endpoint. We are passing through our secret OpenAI key. And then we're referencing a model and a list of messages. Finally, we get the result as JSON, and then we dump it to the screen. All right, let's take a look. All right, and here we have our response object.

Parsing and Displaying Output4:55

All right, let's take a look. All right, and here we have our response object. All right, so this one right here is the one you really care about, the choices array. And notice we have the first item, the message, the content, and sure enough, there is a poem about recursion generated by AI. Super, super cool. All right, so now, of course, most of the time, this is the value you want. So I'll show you a couple ways to do that. First, you could just go through it. So go into the choices array.

First, you could just go through it. So go into the choices array. And then it was the first item within choices. And that itself is an array that has a message key. And then in the message key, we want content. All right, so that would be one way to grab the value. However, we can also use sort of a key string syntax as part of the JSON method. So notice right here, what key do we want? And we can use .notation for that. So I could say choices.0.message.content.

And we can use .notation for that. So I could say choices.0.message.content. And at least I think that should work the way we want. That'll give us our poem. And by the way, if it can't find anything, it will return null. All right, so once again, let's just return poem as a string and give it a refresh. All right, and there we go. So of course, we've lost some of our formatting, but we can bring that back in just a minute. But yeah, we do have our poem.

but we can bring that back in just a minute. But yeah, we do have our poem. Okay, so check this out. We're going to return a view called welcome. I will send through our poem. All right, click through there. And replace that splash welcome page with our poem. AI poems. For some quick formatting, I will pull in Tailwind. And then we'll have a div here.

For some quick formatting, I will pull in Tailwind. And then we'll have a div here. And what I'm going to do is use that php, what is it, nl2br function so that it will maintain any line breaks or formatting like that. Okay, so finally, let's do something like make the text extra-small, maybe font-sans serif. And then I want it centered on the page. So a quick little hack is on the HTML class, set height to full. Do the same thing for body. And then you can do a display of grid on the body tag,

Do the same thing for body. And then you can do a display of grid on the body tag, and then use place-items to center. All right, finally, a little padding all around. And I think we're ready to check this out. So we come back, give it one more refresh. And there we go. We have our poem about recursion entirely generated by AI. Okay, so yeah, just keep in mind in real life, you would, of course, need to implement some caching.

Okay, so yeah, just keep in mind in real life, you would, of course, need to implement some caching to ensure that we're not hammering their API every single time somebody wants to view your poem. Of course, you get the idea. All right, so now let's wrap up our Hello World example by talking about already everything that you've learned. You've learned how to interact with OpenAI's API. You learned how to generate a token. You learned about messages, at least a little bit.

You learned how to generate a token. You learned about messages, at least a little bit. You had your first introduction to language models, but I promise we're going to talk about that more a good bit in the next episode. You learned how to fetch a response message from OpenAI, and then we displayed it on the page. Okay, so in the next episode, we're going to talk about some ways to clean this up, at least ways to organize it in terms of architecture.

we're going to talk about some ways to clean this up, at least ways to organize it in terms of architecture. And then I also want to talk about how we could potentially continue the conversation if we wanted to. And that brings us back to this messages array. All right, stay tuned. I hope you're excited. I'll see you in the next episode.

I'll see you in the next episode.

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