Using OpenAI Library with Python
OpenAI, GenerativeAI AKA GenAI and ChatGPT are probably the mostly used buzzwords in technology world since last couple of years. Bigshots like Google, Microsoft, Amazon already are in the race for creating a Library or models like ChatGPT by OpenAI. According to the pundits, OpenAI is just the beginning of a revolution in technology world. To stay relevant in this race, Developers should start making their hands dirty with different OpenAI libraries. In this article I am going to show a basic example of connecting and consuming OpenAI library using python.
Acquire ChatGPT (OpenAI) API Key
To consume a OpenAI library first you need to create an API-key. Register & Login to platform.openai.com website.
Login to https://platform.openai.com/account/api-keys. Follow below steps and generate a key, which can be used later.
Keep the key safe somewhere as you won’t be able to open that in future.
Steps to use OpenAI Library
The OpenAI Python library provides convenient access to the OpenAI API from applications written in the Python language. It includes a pre-defined set of classes for API resources that initialize themselves dynamically from API responses which makes it compatible with a wide range of versions of the OpenAI API.
Step 1: Install OpenAI Python Library.
$ pip install openai
-OR- if you already have OpenAI installed below command will help you to upgrade the OpenAI library.
$ pip install --upgrade openai
Step 2: Use OpenAI library and write a small script to pass a query to OpenAI library.
In below code snippet, notice there are 3 main segments. Importing the OpenAI library, passing a key to get your service call authenticated an at last passing the Query as a parameter.
.api_key() method takes token as a parameter. In this example to hide the actual key from viewer, key has been kept in a plain text file “key.txt”.
Create() method has mandatory parameters model and message.
Step 3: Handling the response. Response will be in JSON format which makes it very easy to handle.
Few important segments in the response would be:
1. Content: This is the actual response from OpenAI
2. Finish_reason: tells you the status of the of the service call.
3. Usage: To know the token balance used for this transaction.
Recommended by LinkedIn
Use below snippet to show only the API response or may be use that response to use further in your App. Below example can be the only response from OpenAI if manipulated well and can be used further.
Tokenization in OpenAI Library
Now you have understood that tokens are the currency in OpenAI world to buy the rights to question the AI.
You can think of tokens as pieces of words used for natural language processing. For English text, 1 token is approximately 4 characters or 0.75 words. As a point of reference. Rule of thumb: 100 tokens ~ 75 words
Let’s experiment with the Tokenizer tool.
Notice the sentence we have written, is going to consume 12 tokens. The unknown part of a complete transaction will be the response received from OpenAI. A complete transaction consists of Question and Answer both. For Questions you have a control on token usage but for the answer you can’t have control. So it’s always better to ask questions with very specific keywords which will help OpenAI to come up with very crisp response.
Tokens can be bought from the OpenAI portal. Tokens can be bought with pay-as-you-go plan. OpenAI portal has a nice dashboard to manage your usage and expense.
As of 28th June 2023 below are the rates for tokens.
Here are few useful links to understand the pricing for Token in details:
Best Practices for GPT
Looking at the pricing you have already guessed there should be proper planning on writing the queries to manage the billing and budget. Some of the common practices for better billing are:
For more knowledge on OpenAI refer below reference blogs:
!!! Happy Learning !!!