How to build an API with Edge Functions and ChatGPT
Learn about the required process to create an API with Edge Functions and ChatGPT and have the response shown on the Azion Preview Deployment.
To do so, you need to:
- Be registered on OpenAI.
- Have your OpenAI API key.
- Have your OpenAI organization ID.
To start creating your API, proceed as follows:
- On Real-Time Manager (RTM), on the upper-left corner, select Edge Functions in the Edge Libraries section.
- Click on Add Function.
- Choose a name for your function.
- Delete the placeholder function that is inside the code editor.
- Add your OpenAI API key and organization ID to the code as a comment:
- Copy and paste the following
const
:
- Write the following prompt inside the function, right below the OpenAI credentials that were pasted on step 5:
- Select the prompt > click on it with the right button > choose ChatGPT: Generate. Wait a few seconds for it to complete the request.
The expected output should be similar to:
- Add the
handleRequest
function, containing aswitch case
, with aGET
and aPOST
method:
- The
POST
method returns aJSON
object, that was generated by theChatGPT: Generate
option. - The
GET
method returns the HTMLconst
that was declared on step 6.
- Copy and paste the
PreviewProvider
function, responsible for simulating requests:
- Add the
addEventListener
function, which triggers the whole processing of the edge function:
- Click with the right button anywhere inside the code editor and select Format Document.
At this point, your edge function should look like this:
If you want, you can switch the method inside the PreviewProvider
function and get a live preview of the responses on the Azion Preview Deployment.
- Click the Save button.
Now, the function is ready to be instantiated in an edge application. Check how to instantiate and execute the functions in your edge application and try it out.
- Access Azion Console > Edge Functions.
- Click on + Edge Function.
- Choose a name for your function.
- In the Code tab, delete the placeholder function that is inside the code editor.
- Add your OpenAI API key and organization ID to the code as a comment:
- Copy and paste the following
const
:
- Write the following prompt inside the function, right below the OpenAI credentials that were pasted on step 5:
- Select the prompt > click on it with the right button > choose ChatGPT: Generate. Wait a few seconds for it to complete the request.
The expected output should be similar to:
- Add the
handleRequest
function, containing aswitch case
, with aGET
and aPOST
method:
- The
POST
method returns aJSON
object, that was generated by theChatGPT: Generate
option. - The
GET
method returns the HTMLconst
that was declared on step 6.
- Copy and paste the
PreviewProvider
function, responsible for simulating requests:
- Add the
addEventListener
function, which triggers the whole processing of the edge function:
- Click with the right button anywhere inside the code editor and select Format Document.
At this point, your edge function should look like this:
If you want, you can switch the method inside the PreviewProvider
function and get a live preview of the responses on the Azion Preview Deployment.
- Click the Save button.
Now, the function is ready to be instantiated in an edge application. Check how to instantiate and execute the functions in your edge application and try it out.