ChatGPT is an Artificial intelligence chatbot developed by OpenAI and launched in 2022 November under the proprietary license. It is a Generative pre-trained transformer chatbot. Initial release version of ChatGPT was based on GPT-3.5 and the recent version is GPT-4 which is the latest model, released on 2023 march and only available to a paid subscribers on limited basis.
To integrate a ChatGPT into any custom software or application, OpenAI has released a ChatGPT and whisper models APIs through which developers can implement ChatGPT into their software to enable Artificial intelligence features such as, Information retrieval, Speech-to-text features into their custom application.
ChatGPT model family APIs that is currently available is gpt-3.5-turbo
,which price is $0.002 per 1k tokens a which is 10x cheaper than other existing GPT-3.5 models.
Another is Whisper, the speech-to-text model that OpenAI released in September 2022 which is priced at $0.006 / minute to use, as whisper APIs accepts a variety of audio formats such as m4a, mp3, mp4, mpeg, mpga, wav, webm that can be converted to text.
Integrating ChatGPT APIs in nodejs application
Step 1: Generating an API key
The first step to integrate ChatGPT API in nodejs based application is to obtain an API key from https://openai.com/ as shown below.
Visit https://platform.openai.com/account/api-keys and create a new Key.
Step 2: Install OpenAI Node.js library
Use NPM to install openai nodejs library into your project as shown below.
$ npm install openai
Step 3: Import openai library and Integrate into your nodejs application
Once you install nodejs openai library import the openai library, configure the key and integrate it into your existing application as shown below by creating a file gptnode.js
const { Configuration, OpenAIApi } = require("openai");
const configuration = new Configuration({
apiKey: process.env.OPENAI_API_KEY,
});
const openai = new OpenAIApi(configuration);
const response = await openai.createCompletion({
model: "text-davinci-003",
prompt: "Explain nodejs in a single sentence",
temperature: 0,
max_tokens: 7,
});
console.log(completion.data.choices[0].text);
Once we write the above code run the code by invoking a node interpreter.
$ node gptnode.js
The output of the above application is below.
Node.js is an open-source server-side JavaScript runtime that allows developers to build fast, scalable, and event-driven applications.