DEV Community

Cover image for My experience using Open AI completion API in a Node.js application
Pilar Fernandez
Pilar Fernandez

Posted on

My experience using Open AI completion API in a Node.js application

Knowing how to extract the best usage out of Chat GPT is increasingly being considered a superpower, especially for developers. You can pretty much ask any question or any task, and you will get it done in seconds.

So, I was curious about how would it be to have that potential being applied in my application, and I thought I'd git it a try.

What I implemented?

An application to use Artificial Intelligence to create cool new recipes. For that, I created a frontend Vue.js application, where the user can select several edibles, in a intuitive way and ask for a recipe containing all those ingredients. Within a click of a button, the backend Node.js application is called, which calls Open AI completion API, asking for a recipe. Then, the user sees the final recipe generated by AI.

What is completion AI?

Of course, we should ask this question to the completion AI itself! And here is the answer: "Completion AI generally refers to artificial intelligence system that are designed to assist or complete tasks bases on natural language input or prompts."

Step by step process

As most of the APIs out there, is necessary to create an API key to authenticate and be able to send requests. To do so, you must have an account in Open AI and, after being logged-in, go to the menu on the left and select "API Keys".

Image description

Then, click in the "+ Create new secret key" button.

Image description

Add an identification name to the key, I suggest referring to the application in which it will be used.

Image description

Make sure to copy the key and paste somewhere, because after this pop-up closes, it will not be possible to see the complete key again, for safety purposes.

After having generated a key for my project, I added it on the .env of my backend application, to make sure it won't be commited to GitHub.

OPEN_AI_API_KEY="Enter key here"

Then, to effectively use the key and the API it is necessary to install the Open AI Node.js library by running

npm install --save openai

I followed this tutorial to call the API.

API Performance

I did 10 requests with the following query: "Give me a recipe with only Banana, Sugar, Oat, cinnamon and provide duration and difficulty".
In average, the requests took 11 seconds to complete.

Costs

Even though Chat GPT is free, the completion API is not. The costs related are $0.0010 per 1,000 tokens for the input and $0.0020 per 1,000 tokens for the output, on the GPT-3.5 Turbo model that I used on my application. The total tokens are calculated by adding up the number of input and output tokens. For example, if the input text has 10 tokens, and the model generates 20 tokens in response, you would be billed for total 30 tokens.

Final consideration and thoughts

I found it easy to create API key and call the API. Every step of the process was very intuitive, and the documentation was helpful on guiding me through it.

I didn't find the costs description very clear at first. The concept of a token is not given, and it could be confusing to some people. However, I consider to be adequate that the total cost value is corresponding to the computational usage of the request.

With all that, I enjoyed my experience using Open AI. I was able to build a nice application with minimal effort on the backend side.

For more reference on the source code, you can check my GitHub.

Top comments (2)

Collapse
 
jsspen profile image
Jordan Spencer

Just watch out with those recipes, I've seen it spit out (pun intended) some pretty gross combinations before! 🀣

Collapse
 
erikgiovani profile image
Erik Giovani

Nice post Pilar and welcome to Dev πŸ‘