-: Now we are going to talk about custom functions.
As we saw before, OpenAI has preset functions such
as retrieval or cold interpreter, but we can create or own.
So let's see a little bit how to do it.
So here we have our code.
I have already imported OpenAI and my API key.
So now I'm going to import requests and json,
just for this custom function exercise.
We are going to create a get_website_content function
using the voxscript plugin.
You can get your API
and check the documentation on this website.
So this is the function that the assistant is going to run
to scrape the website content.
We are going to input the websiteURL, our apiKey,
and these preset parameters, okay?
So let's run this to have it ready for later.
There we go.
So now we have to prepare a dictionary
that is going to be the list of tools
that the assistant is going to be able to call.
So here we have the get_website_content function named
and all its parameters.
So here we are setting our API key.
So let's run this to have it ready for the assistant.
There we go.
So now we are going to instantiate the OpenAI client,
and the next step is to create the assistant.
This is going to be the website content assistant.
And its instructions are "You execute a custom function
to get content from the website the user gives you
and provide the summary."
You see, I'm not going to run this cell block in particular,
because I have already created the assistant.
So here's where the actual fun begins,
because we are going to run the assistant.
So I have already here my assistant ID.
We are going to locate the assistant.
Then we are going to create a thread just as before.
And we are going to input this message,
"get the content from this website,"
an article about Stable Diffusion 3.
Then we are going to run the conversation just like before.
And here is what's different.
We are going to retrieve the run,
and, when the assistant calls a function
or when the assistant gets from the user
that it needs to run the function,
the run, it's going to be set
on hold waiting for the function response.
So once the run is completed, it will, sorry,
the run is completed and we'll get the output message.
But if it says, requires_action,
we will call the tools.
We are going to output which tool it's going to be called.
And if the tool call it's get_website_content,
we are going to run it here, get_website_content.
Then we are going to submit to the run the function results.
And once we've done that,
we should get the message, "Custom Function Executed
and Output Submitted," and the run should finish there.
So let's run this to see what we get. There we go.
Assistant Located, Thread Created, Thread Ready,
Assistant Loaded, Run Started.
And we'll have to wait a little bit.
So after 25 seconds, we have the result, Function arguments,
Custom Function Executed Output Submitted, Run is Completed,
and, "The article discusses Stability AI's early preview
of its new text-to-image model, Stable Diffusion 3."
And here we have the summary of the article.
So we got the website content correctly.
If you got lost in the middle of the code, don't worry.
Feel free to pause the video
to review the code line by line,
to ask ChatGPT to help you, because it's normal.
I mean, I also struggled a little bit
to create this, to make this work.
So it's normal, but it ends up being really fun.
So that's it on custom functions.
I really hope you find it useful to create your own.
So now we are going
to see another use case using OpenAI's Playground.