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

Introducing Assistants API0:00

All right, let's move on to a new chapter and discuss the Assistance API. So I've logged into my account at platform.openai.com, and switch over here to the Assistance tab, and let's create a new one. Okay, but yeah, of course, first, what is an Assistant? Well, think of it like this. The Assistance API allows us to create an Assistant that is unique and tailor-made for your own project, or organization, or package, or use case, fill in the blank, right? So here's an example that might help. Imagine that you've created a brand new Composer package. It's amazing.

Imagine that you've created a brand new Composer package. It's amazing. And you want to allow newcomers to ask questions about how to use that package. Well, there's only one problem. Right now, OpenAI doesn't know anything about your package, right? It may a few years down the line, if it becomes popular, but right now, it has no clue what your package is. So it's almost like you need a way to create your own Assistant who is familiar with the inner workings of your package. And yeah, that's precisely what the Assistance API allows for.

Preparing Documentation Files1:11

inner workings of your package. And yeah, that's precisely what the Assistance API allows for. So let me show you an example. If I switch to my editor within the Storage directory, I've added this new Docs folder. And yeah, just to get us going, I've created one dummy markdown file. But yeah, in real life, this would represent all of the documentation for your framework or your package. And yeah, it's just some kind of dummy layer of parse package. I guess you feed it the path to a markdown file, and it splits it up. Maybe you can access all of the paragraphs, all of the snippets.

I guess you feed it the path to a markdown file, and it splits it up. Maybe you can access all of the paragraphs, all of the snippets. Maybe you can manipulate it. Whatever you want to do here. It's all made up anyways. Okay. So now I want to feed this to a new Assistant so that I can then ask OpenAI questions, such as how do I access the first paragraph of a markdown file? All right, let's switch back. We will call this LaraParseTutor.

Creating Assistant in UI2:04

All right, let's switch back. We will call this Lara Parse Tutor. And as for instructions, this is sort of like the system message. You are a helpful programming teacher. Okay. So now for the model, which one do we want to use? We could use GPT-4 preview, or yeah, let's stick with that. Okay. So now in terms of tools, yeah, this is one of the cool things about the Assistance API. It can run and execute any number of tools, such as triggering certain functions, interpreting

So now in terms of tools, yeah, this is one of the cool things about the Assistance API. It can run and execute any number of tools, such as triggering certain functions, interpreting code or writing code or generating graphs and responding with diagrams based on files that you can upload. That's really cool. There's also one called Retrieval. And yeah, as you see here, it enables the Assistant with knowledge from files that you upload, such as your documentation. So I'm definitely going to turn this one on at the very least. Okay.

So I'm definitely going to turn this one on at the very least. Okay. Next, what files should go along with this? Well, right now it's just that one documentation file. So let's see. It's in the storage directory, docs, and yeah, again, in real life, you'd have a number of these files. All right. Let's save it. And that's all there is to it, at least in terms of creating the Assistance through the

Testing in Playground3:13

Let's save it. And that's all there is to it, at least in terms of creating the Assistance through the UI. Of course, very shortly, we're going to do this from our code editor instead. So now let's test it out or go into the Playgrounds and you can see we already have our first Assistant ready to go. Okay. So now I'm going to ask a question based on this file and something that OpenAI could only answer if it had access to this file. So for example, how about this one right here?

only answer if it had access to this file. So for example, how about this one right here? How do I fetch the first paragraph from a Markdown file? All right. Let's give it a run. And notice I use that word run. All right. Give it a second. So notice it takes a moment, right? And that's because when we execute this, it actually gets thrown onto a queue.

So notice it takes a moment, right? And that's because when we execute this, it actually gets thrown onto a queue. So it's not going to be processed instantly like you might be used to. I can't tell you that because we're going to need to know that once we start writing some code. But yeah, nonetheless, we see to fetch the first paragraph using the laravel parse package, you would use the following code. And notice it grabbed that right here. Very cool. The code first parses the file, which returns an array of paragraphs.

Very cool. The code first parses the file, which returns an array of paragraphs. This code parses the file, which returns an array of paragraphs. Right here, returns an array of paragraphs. Super cool, isn't it? Okay. We understand the basic idea here. We can create an assistant. We give it a name. We provide an instruction.

We give it a name. We provide an instruction. We set a model. We can enable certain tools. In this case, we only enabled retrieval, which allows me to add files that it can interact with. We can then create a thread. And yeah, a thread is exactly what you think. It's an interaction between one User and our assistant. Each thread consists of various messages, which we're used to.

Building Assistant in Code5:00

It's an interaction between one User and our assistant. Each thread consists of various messages, which we're used to. And then when we're ready to run this thread, yeah, notice I don't hit submit or send. We're specifically using this word run. And we'll talk about that more shortly. Okay. So now I want to take effectively everything we've done here and translate it to code. So let's do that now. Back in my editor, as usual, let's visit our routes file. And yeah, we can play around here.

Back in my editor, as usual, let's visit our routes file. And yeah, we can play around here. All right. So the first thing I want to do is, as always, pull in the OpenAI facade. And then I can say, OpenAI, and let's create our first assistant. create. Okay. So as usual, if I command click on this create method, you'll find a shortcut link to the associated documentation. All right.

associated documentation. All right. Here we go. So to create a new assistant as part of the request body or the parameters, yeah, we need to provide a model, a name, instructions, tools, an array of file IDs. Yeah, it corresponds exactly to what we had here. name, instructions, model, tools, file IDs. All right. So let's just migrate it over. If I switch back, model, and that's going to be GTP4.

So let's just migrate it over. If I switch back, model, and that's going to be GTP4. And what is it? At the time of this recording, I'm on 11.06-preview. All right. Next, a name. Well, we called ours LaraParseTutor. Next, instructions, sort of like the system message, is fairly simple here. Next, the tools that we want to use. Let's switch back.

Next, the tools that we want to use. Let's switch back. Let's see. I only enabled retrieval. So if you want to switch over to the documentation, you can see these are the keys that represent that retrieval. All right. So we have tools. That's an array where each item has a key of type and then a value of one of these. All right.

That's an array where each item has a key of type and then a value of one of these. All right. So type, retrieval. So when we run and execute the thread, yeah, it's going to reference and use these specific tools. All right. Next, if we want, we can even include an array of file IDs as part of this assistant. Or of course, we could do it as part of a second API call. Okay. So what does it mean by file IDs?

Okay. So what does it mean by file IDs? Well, it wants the file object itself. So here's what we can do. I'm going to go up here and create a new call using OpenAI, and we will use the files endpoint. But real quick, I think this is new to us. So let's have a quick look. Back to the documentation. Here's the files endpoint.

Back to the documentation. Here's the files endpoint. This is used to upload documents that can be used with features like assistance and fine tuning. And yes, very simple. Notice we make a request to this files endpoint, and we provide a corresponding file. Very, very simple. Okay. So let's do that now. files.

So let's do that now. Files. And a quick note, you would think it would be create, but actually the method name is upload. Okay. So as we saw in that documentation section, we need to give it a purpose. What is the purpose and the use case for this file? And we're going to use assistance. And again, by the way, just to be crystal clear, I'm grabbing that right here. So notice assistance is the one we want.

And again, by the way, just to be crystal clear, I'm grabbing that right here. So notice assistance is the one we want. Next, the file object to be uploaded. So I can do that here. And yeah, what I can do is just say fopen, and then provide a path to the storage directory, then docs, then parsing, and D. Finally, we could open this to read in binary mode, and that should be fine. Okay. So that should return to us a file object, an OpenAI file object. And as you can expect, we can fetch the id of that file by doing fileId.

So that should return to us a file object, an OpenAI file object. And as you can expect, we can fetch the ID of that file by doing fileID. All right. Good. All right. Are we all on the same page? To be crystal clear, we start by uploading any number of files. So yeah, you could do that as part of a loop if you need to. We upload those files, we declare their purpose, and then when we generate or create our new assistant, we can reference those file IDs.

We upload those files, we declare their purpose, and then when we generate or create our new assistant, we can reference those file IDs. And that way, the assistant knows, all right, I'm going to refer to them. It's almost like the encyclopedia for your assistant. I'm going to refer to them whenever I need to answer a question that you have. All right. So now, that will give us our new assistant. The next step, of course, is to create a new thread. All we've done so far is uploaded some files and created the assistant itself. And keep in mind, this is not something you would want to do every single time the user.

All we've done so far is uploaded some files and create the assistant itself. And keep in mind, this is not something you would want to do every single time the user hits the homepage. You will do this separately, and then you can reference the assistant ID when you need to. But yeah, right now, we're still figuring things out, so I will do it all in line. I think that helps. All right. So let's create a new thread and run it all in one go. So I can do this.

Running Thread and Polling9:44

So let's create a new thread and run it all in one go. So I can do this. Open AI, threads, and I'm going to say create and run. And this is a helpful one. It allows us to merge multiple API requests into one. So, for example, otherwise, we would have to create a thread, add messages to it, and then run it. But this method does it all in one go, which is really helpful. All right. Let's have a look at the parameter list.

All right. Let's have a look at the parameter list. All right. Just an array of parameters. Let's go to the documentation. All right. And it wants to know a couple things. What is the assistantId? So let's do that now. The assistantId is the ID of the assistantResponse object.

So let's do that now. The assistantId is the ID of the assistant response object. All right. Next, it needs a thread. And of course, any thread will consist of messages. All right. So I can do this right here. thread is an array that consists of messages. And this messages array is just like the ones we've done in the last several episodes. So it'll take the shape of something like this.

And this messages array is just like the ones we've done in the last several episodes. So it'll take the shape of something like this. role is user. content is your question. So, for example, once again, how do I grab the first paragraph? All right. So that should then return a queued run object. And, yeah, once again, just to be crystal clear, you're generally going to have one thread per user. So if five people visit your site, well, you're going to have five different threads,

thread per user. So if five people visit your site, well, you're going to have five different threads, but only one assistant. Okay? So now, yeah, like I said, if we die and dump that $run object, we're not going to see the response yet. Instead, it gets queued. So what we have to do at this point, of course, is manually check in to see, do we have a response yet? Do we have a response?

response yet? Do we have a response? So, yeah, clearly, we need some kind of loop. Just to show you now, I'm going to dump that run response object. And whoops, we have some kind of error. Tools value is not a valid list. Did I not do that? Tools is an array. Yep. I'm sorry.

Yep. I'm sorry. Of course, because you could have multiple tools, each set needs to be wrapped within its own array. So, for example, if I want the code interpreter, then I would do another one right here. Okay. Sorry about that. Give it another go. And, yeah, now we get a threadRunResponse object. And I want to point your attention right here.

And, yeah, now we get a threadRunResponse object. And I want to point your attention right here. You're not going to find any messages array yet. It hasn't responded because it's still queued. Okay. A couple things I do want you to notice, though. On this run object, I can grab its ID. I can grab the corresponding thread ID and the corresponding assistant ID. Okay. So it sounds like we just need to have a loop where we occasionally check in to see, has

Okay. So it sounds like we just need to have a loop where we occasionally check in to see, has the status changed to completed? Let's do that now. So let's do this. This is actually probably a good use case for a do while statement. So do check on the status of the run. So I can do that by saying openAIThreads runs, and we will retrieve the run that has that corresponding ID. And actually, real quick, let me remind myself.

that corresponding ID. And actually, real quick, let me remind myself. We need the thread ID and the run ID. But this is not an array. So if I want to use named parameters or properties, I can do that. Thread ID will be run. And I told you we had thread ID on that response object, and then also the run ID. All right. So that will once again give us the run object. And what we could say is keep doing that while run status does not equal completed.

So that will once again give us the run object. And what we could say is keep doing that while run status does not equal completed. All right. And yeah, we probably need a bit of a wait. So why don't we say sleep? And I don't know. Why don't we just start with one second? But you could probably shorten that as well. All right. So once again, let's go over this.

All right. So once again, let's go over this. We upload a file. We create a new assistant. But yeah, once again, don't forget, this kind of code, you don't need to create a new Assistant every single time the user visits the home page. And in fact, if you go into your dashboard, you would see multiple Assistants, which makes no sense. We're only including it here to see the full flow. But yeah, otherwise, you would do it separately.

We're only including it here to see the full flow. But yeah, otherwise, you would do it separately. In some kind of dashboard page on your own website. Okay. Next, we create a new thread, and we run it all in the same request. And right now, there's only a single message that says, how do I grab the first paragraph? Okay. So that will queue it up. We're going to sleep for a second and then check on the status. And while that status is still set to queued, we'll do it all over again.

We're going to sleep for a second and then check on the status. And while that status is still set to queued, we'll do it all over again. Sleep another second and check for an update. All right. And now, if we reach this point, we can be sure that the job has completed. All right. So the only remaining step is to fetch the messages. And we can do it like this. Open AI, here's the full API call, threads, messages, and we're going to call the list method.

Open AI, here's the full API call, threads, messages, and we're going to call the list method. And yeah, as you can imagine, if we want the messages for a thread, we need to give it the ID of the thread. Just like that. All right. So there's our messages. All right. Let's dd and dump and have a look. Come back.

Let's die and dump and have a look. Come back. Give it a refresh. One, two, three. Yeah, it'll take a few seconds. All right. Here we go. We have our Thread, Message, List, Response object. And sure enough, we have a data array of two messages. Of course, the second one will represent my original question.

And sure enough, we have a data array of two messages. Of course, the second one will represent my original question. Let's go in. Here's the value. How do I grab the first paragraph? The follow-up will be the response. And yeah, the way we interact with this is just like you should. We're going to call it a message. And we're going to call it a thread. And we're going to call it a message.

And we're going to call it a thread. And we're going to call it a message. And we're going to call it a message. And we're going to call it a thread. Okay. Why don't we stop right there? Of course, there's still more to cover. We have to render it in the view and things like that. But I think this is a good time to wrap up. I hope you found this helpful.

But I think this is a good time to wrap up. I hope you found this helpful. And I'll see you next time. Bye. Bye. Bye. Bye. Bye. Bye. Bye.

Bye. Bye. Bye. Bye. Bye. Bye. Bye. Bye. Bye. Bye.

Bye. Bye. Bye. Bye. Bye. Bye. Bye. Bye. Bye. Bye.

Bye. Bye. Bye. Bye. Bye. Bye. Bye. Bye. Bye. Bye.

Bye. Bye. Bye. Bye. Bye. Bye. Bye. Bye. Bye. Bye.

Bye. Bye. Bye. Bye. Bye. Bye. Bye. Bye. Bye. Bye.

Bye. Bye. Bye. Bye. Bye. Bye. Bye. Bye. Bye. Bye.

Bye. Bye. Bye. Bye. Bye. Bye.

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