How To Build Your Own ChatGPT API With Express_js

How To Build Your Own ChatGPT API With Express_js


As AI is going so popular, I felt the need to explain how someone could build his own ChatGPT API using one of the prebuilt models provided by the ChatGPT inventor, OpenAI.

I will show you how to create your own chatGPT_API with a step-by-step guide using Express.js, and OpenAI dependencies.

So let us get started…

1| Navigate to OpenAI Platform website

firstly, we will navigate to OpenAI platform website?https://platform.openai.com, then from the OpenAI platform we will get two things: first is the api_key that we will use to get access to OpenAI API, second is the module we want to use, and just below, you’ll find how to get them.

1.Click on the profile and then click on View API keys.

Aucun texte alternatif pour cette image


2.Click on the Create new secret key and copy the API_Key. Then when you go back and go to examples and choose natural language to OpenAI API, then you'll find out what instances we need to build and which pre-built model we will use.

Aucun texte alternatif pour cette image
Aucun texte alternatif pour cette image
Aucun texte alternatif pour cette image
Aucun texte alternatif pour cette image


2| The server

Now we will build the server and for that we need to install some dependencies which are : first is?express?to build the rest API and?nodemon?to run the server each time we save,?CORS?for cross-origin requests and?openai?for the OpenAI instances and?dotenv?to get access to environment variables.


Installing dependencies :

Aucun texte alternatif pour cette image

Build the API :

I need to import the configuration and OpenAIApi from openai module, and then create the configuration and openai instances. Then we create the routes for our restAPI, and for that we will use Get and Post methods to create the completion using the pre_built model?text-divinci-003,?just as I explained above.

Aucun texte alternatif pour cette image
Aucun texte alternatif pour cette image

We need to make sure that the server is listening to our requests.

Aucun texte alternatif pour cette image

We need to add some modification to our package.json file, in scripts we add?start?script, and we also add?type?as module.

Aucun texte alternatif pour cette image

3 | Testing Your ChatGPT API

The last thing to do is to send some requests with get and post methods and see what will be the responses that will come back to us.

Get Method from the browser :

Aucun texte alternatif pour cette image



POST Method from Postman :

As you can see, we send a post request to the API we’ve just built and we get exactly the response we expected.

Aucun texte alternatif pour cette image


Finally, I hope you have enjoyed this post, and you could leave a comment or a question just below.

要查看或添加评论,请登录

Mohamed-Lamin Taleb的更多文章

  • Azure Networking(5) : Network security Groups

    Azure Networking(5) : Network security Groups

    We cannot talk about Azure network security group without mentioning Network security group, so in this article I will…

  • Azure Networking (4) : VPNGateway (Vnet-to-Vnet)

    Azure Networking (4) : VPNGateway (Vnet-to-Vnet)

    In this fourth articles about Azure networking, I would like to share with how to establish connection between two…

  • Azure Networking(3) : Peering

    Azure Networking(3) : Peering

    Peering is the best way to open a connection between two virtual networks, so the virtual machines inside those virtual…

  • Azure Networking (2) : Private Endpoints

    Azure Networking (2) : Private Endpoints

    Private endpoints are somehow extremely Important when it comes to the privacy of the cloud and its security parameter,…

  • Azure Networking (1): Azure Private DNS Zones

    Azure Networking (1): Azure Private DNS Zones

    Azure private DNS zone is one of azure services to keep privacy on virtual networks (Vnet) in Azure networking, in this…

  • Patch Function in Power Apps : SharePoint Number Column

    Patch Function in Power Apps : SharePoint Number Column

    Patch Function in Power Apps : SharePoint Number Column Let me show you, how to update a column of the type Number in…

  • How To Call Power Automate From Canvas App

    How To Call Power Automate From Canvas App

    Here in this article, we’ll see how to call Power Automate From Canvas App using PowerApps(V2) Action in Power Automate…

  • How To Install WordPress: Step-by-Step Guide

    How To Install WordPress: Step-by-Step Guide

    Used by more than 455 million sites, WordPress is a content management system (CMS) that has existed since 2003. It…

  • How To Get Started With NextJS

    How To Get Started With NextJS

    Nextjs is a Framework for creating pre-rendered React websites by server-side rendering (SSR) or static side generation…

  • How to use GPS in PowerApps.

    How to use GPS in PowerApps.

    Maps can be used to show contextual information to your users, reason for witch allowing you to represent location data…

    1 条评论

社区洞察

其他会员也浏览了