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

Plan and Setup0:00

All right. Here's the game plan. I'm going to take this step by step, but with the understanding that each step is going to move pretty quick. So watch me for the changes and try to keep up, right? Okay. So here I have a fresh install of Laravel. I haven't done anything else. So before we even talk about agents, let's first figure out, well, how do we prompt AI and receive a response, right? Let's make sure we're all on the same page and then we'll continue.

Create Artisan Command0:20

how do we prompt AI and receive a response, right? Let's make sure we're all on the same page and then we'll continue. All right. Why don't we do that via an Artisan command like this? PHP Artisan make me a command. I'm going to call it how about chat command or something like that. All right. Let's open this up. Why don't we give it a signature of chats, receive an AI response. All right. So how do we interact with something like OpenAI or Cloud? Well, you just do it via an HTTP request and then you

Configure OpenAI API Key0:48

All right. So how do we interact with something like OpenAI or Cloud? Well, you just do it via an HTTP request and then you include the API token that you would generate on their respective sites. So I already have an AI key or an OpenAI key. Here's what I can do. I'll go to my environment file at the bottom. I will say OpenAI API key and behind the scenes, I'm going to paste in my token so you don't steal it. Next in the sidebar, let's go into config services is a fine place to put that. At the very bottom,

Send OpenAI HTTP Request1:19

let's go into config services is a fine place to put that. At the very bottom, we could do something like AI or OpenAI and let's add our key here. Looks great. So now anywhere we can access it via services.openai.key. In fact, I'm just going to copy that because I'm going to use it in just a couple of seconds. Cool. So let's return to our commands and we'll use where those HTTP facade to include our token. I'll paste that in here and then we're going to make a post request. So a post request to where? Well, I'll just check the documentation, right?

I'll paste that in here and then we're going to make a post request. So a post request to where? Well, I'll just check the documentation, right? Or have AI do it for you, which is what you're going to do anyways. Anyways, here is the URL. We're going to use the OpenAI responses API, which is relatively new. It might be a little different from how you did it a couple of years ago. Anyways, as part of the parameters, of course, we need to say, well, what is the input? What is the prompt, right? What model are we going to use, right? Stuff like that. So this is 2026 as I record this for the model.

What model are we going to use, right? Stuff like that. So this is 2026 as I record this for the model. Why don't we use GPT 5.4 nano? That's fairly advanced, but also very cheap to use. All right. Next, the system instructions. Yeah, you've seen this a hundred times, right? You are a helpful assistant is the most generic one. But yeah, you can imagine if you're building an agent for like helping first graders or something, the system instructions could specify that all of its responses

graders or something, the system instructions could specify that all of its responses should be optimized so that a first grader can clearly understand it. And examples should be provided that would benefit a first grader, stuff like that. All right, finally, our input. What is the prompt, right? Now, our input could be an array of arrays, right? It's not just a single how are you today. It could have multiple messages. It could have responses. It could have tool calls and stuff like that.

It could have multiple messages. It could have responses. It could have tool calls and stuff like that. For now, though, I'm going to say we have a single message. And the role is user. Role is what role are we playing here? Is it the user? Is it the assistant providing a response? Is it a function call? We're going to be explicit about that. And then finally, the content is going to be something hard coded for now. Like, how are you today? All right. Finally, if there's any exceptions or something I just wanted to throw, I don't want to swallow those. And then I want to get the JSON response,

Finally, if there's any exceptions or something I just wanted to throw, I don't want to swallow those. And then I want to get the JSON response, save it here. And then at the bottom, we will dump the response to the console. All right. So are we on the same page? We created a new artisan command. When we run the command, we will use the HTTP facade to make a request to the open AI responses API. We're going to send through a simple prompt. How are you today? That will receive a big, weighty JSON response that we've dumped to the console .

Parse Response Output3:59

That will receive a big, weighty JSON response that we've dumped to the console . Let's go. PHP, Artisan, chat. And there we go. Here's our response. Okay. A bunch of stuff here don't be overwhelmed. The main thing for now is output. Now, output is not a simple string, and it's not even an array. It's an array of arrays. So in this case, we can see, all right, the first item in the output is our message. And then the content itself is an array of arrays that includes the text here. Now, the reason for this,

is our message. And then the content itself is an array of arrays that includes the text here. Now, the reason for this, and we're going to talk more about this in the future, is because the output, again, could be multiple things. The output could be, hey, I want to call this tool, and then I want to call it that tool, and then I'm going to provide a response, right? So it could be a collection of outputs. For now, we're going to be a little bit naive and just assume the first item is always the text response when it actually won't be, but it's fine for now. Okay. So if I wanted to grab, I'm doing well, thanks.

the first item is always the text response when it actually won't be, but it's fine for now. Okay. So if I wanted to grab, I'm doing well, thanks. I would go output, first item, content, first item, text. Let's do that now. Response, output, first item, content, first item, and then text. Yeah, just remember you can't depend on this because sometimes the first item might be a tool call or a function call. Let's give it another shot. Each be artisan chat, and now we got it. Cool. So we could pass this to info, and we have our initial interaction with AI. And what I want you to notice is it's just

Make Prompt Interactive5:30

info, and we have our initial interaction with AI. And what I want you to notice is it's just pretty simple if you think about it. It's a simple HTTP request. Include your token, make a post request to the proper endpoint, include the parameters that you'll get straight from the documentation, and you're all set to go here. So of course, the next step would be, let's make it dynamic, right? So why don't we introduce a variable, and we'll call it prompt. And here's what we're going to do. I'm a big fan of a first party Laravel package called

And here's what we're going to do. I'm a big fan of a first party Laravel package called Laravel prompts. It's for working on the CLI, and it includes lots of helpers and elegant little tools and such. So let's pull that in. Composer require Laravel prompts. And now we can do things like this. I could say text, give me a text prompt, and I want you to notice how we import that at the top. So you'll see we use the function Laravel prompts text. So I can say, what is on your mind? And if I click

the function Laravel prompts text. So I can say, what is on your mind? And if I click through here, you'll see a bunch of things we can provide. The label, the placeholder, the default, whether it's required or not. In this case, it is required. I need a prompt in order to make this request. So I will use named parameters to be explicit that required is true in this case. Okay, so now we're making this dynamic, right? We get a prompt, and then we

true in this case. Okay, so now we're making this dynamic, right? We get a prompt, and then we include that prompt with our request. Let's go. B2B artisan, chat. What is on your mind? In one sentence, how long until AI completely captures my programming job? Be honest. Give it a second. No one can give an exact timeline, but in a best case scenario, routine parts of programming will be largely automated within the next five to 10 years. I think maybe is sooner than that,

be largely automated within the next five to 10 years. I think maybe is sooner than that, but we'll see. Nonetheless, this is pretty cool, right? And it just doesn't require much effort at all. Now, two things I want to show you, and then we're done with episode one. First of all, of course, maybe we extract this into a method, something like run model. All right. So now this encapsulates the HTTP request. We receive the response. What's the issue here?

right. So now this encapsulates the HTTP request. We receive the response. What's the issue here? Unhandled. Yeah, that's fine. We're going to ignore that. Next, because we're using layer felt prompts, we can provide a little more feedback when the AI is or when the HTTP request is waiting for the response. We can use a spin function for that, which is cool. This is one of my favorites. So we give it a closure, and then we give it a placeholder message effectively.

of my favorites. So we give it a closure, and then we give it a placeholder message effectively. So I could say right here, this is what we're running. The placeholder will be thinking about that. And then that will return the result of this function call here. All right, exact same thing. So we prompt the user, we display a spinner while we pass that prompts to our model call. Once it's done, we have response, and then we echo the text content. So one more time.

to our model call. Once it's done, we have response, and then we echo the text content. So one more time. I'm sorry, ppr to send chat. What is two plus two? Thinking about that, we get four, and we're done with episode one. Let's keep going.

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