In this tutorial, you will learn how to prepare for using the OpenAI API. I will guide you step by step through the process to ensure that you set up all necessary components - from creating an API key to installing the required OpenAI NPM package. After that, I will show you how to use the API in your application. Let's get started!
Main Takeaways
- An API key is required to access the OpenAI API, which you can generate on the OpenAI platform.
- You need the openai-npm package to make API calls.
- This is done with Node.js and JavaScript, and I will show you how to configure it.
Step-by-Step Guide
To successfully use the OpenAI API, follow these steps:
1. Create an Account on the OpenAI Platform
First, you need to create an account on the OpenAI platform. Open your browser and go to platform.openai.com. Click on "Sign up" to register.
Enter your email address and choose a password. Alternatively, you can also sign up using your Google, Microsoft, or Apple account.
2. Generate Your API Key
After creating your account, navigate to the settings. There you will find the "API Keys" section. Here you can generate your API key.
Click the corresponding button to create a new API key. You can give it a name. After creation, the API key will be displayed. Copy this key and securely store it - either in your application source code or in a separate file.
Make sure not to lose this key. If you close the dialog box, you will not be able to view the key again and will need to create a new one.
3. Understand the Token System
To use the API, you need tokens or credits. Each API call consumes a certain number of tokens. It is important to know that tokens often represent more than the actual number of words, as they can also include word components.
When you sign up, you usually receive a certain number of credits that you can use for your API calls. Make sure to securely store your keys to have them accessible at all times.
4. Install the openai-npm Package
The next step is to install the openai-npm package. Go to your terminal and navigate to the directory where you created your application.
Run the command npm install openai to install the package.
Once the installation is complete, you can check the package.json to ensure that the package was successfully added.
5. Import the Required Modules
Now that the package is installed, you need to import the required modules in your JavaScript code. Import the Configuration and OpenAI modules from the openai package.
6. Configure the OpenAI API
Now you can configure the API. Create a new configuration object that contains your API key.
To do this, use the call new Configuration() and pass your API key object.
After that, you can initialize the OpenAI API with the command call new OpenAI({configuration}).
7. Prepare the API request
Next, you need to prepare the API request to make the API call. Create an asynchronous function where you need the API query.
The function completeChat will receive a message passed in a variable. Call the method createChatCompletion of OpenAI and pass the necessary parameters such as the model (e.g. gpt-3.5-turbo) and all messages you have sent.
8. Process the API response
Once you have successfully called the API, you will receive a response that you need to process. In the response, you will find the data you need, such as choices containing the generated API response.
Save the content of the response and return it so you can use it in your application.
9. Test your function
Now it's time to test the function. Start your local server and check if you receive a response from the AI.
Screenshot_567
You should be able to get different responses when querying multiple times, demonstrating that the communication with the model works.
Summary
In this guide, you learned how to prepare for accessing the OpenAI API. From generating an API key and installing the required npm package to creating an API request and processing responses – you have gone through all the basic steps.
Frequently Asked Questions
What is an API Key?An API Key is a unique key that grants you access to the OpenAI API.
How many credits do I get when signing up?Typically, you receive a credit of several dollars (e.g. 5 dollars) that you can use for API requests.
What should I do if I lose my API Key?You need to generate a new API Key through the settings on the OpenAI platform.
Are tokens the same as words?No, tokens can be part of a word, which is why the number of tokens is usually higher than the number of words in a text.
Can I use the API for commercial purposes?Yes, however, it is advisable to check OpenAI's current terms of use.