Using OpenAI Library with Python

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.

No alt text provided for this image
No alt text provided for this image

Keep the key safe somewhere as you won’t be able to open that in future.

No alt text provided for this image

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.

No alt text provided for this image

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.

No alt text provided for this image

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.

No alt text provided for this image

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.

No alt text provided for this image

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.

No alt text provided for this image

Here are few useful links to understand the pricing for Token in details:

1.   https://openai.com/pricing

2.   https://platform.openai.com/docs/guides/rate-limits/overview



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:

  • Include details in your query to get more relevant answers.
  • Ask the model to adopt a persona.
  • Use delimiters to clearly indicate distinct parts of the input.
  • Specify the steps required to complete a task.
  • Provide examples.
  • Specify the desired length of the output.
  • Instruct the model to answer using a reference text.
  • Instruct the model to answer with citations from a reference text.
  • Use intent classification to identify the most relevant instructions for a user query
  • For dialogue applications that require very long conversations, summarize or filter previous dialogue
  • Summarize long documents piecewise and construct a full summary recursively
  • Instruct the model to work out its own solution before rushing to a conclusion
  • Use inner monologue or a sequence of queries to hide the model's reasoning process
  • Ask the model if it missed anything on previous passes
  • Use embeddings-based search to implement efficient knowledge retrieval
  • Use code execution to perform more accurate calculations or call external APIs
  • Give the model access to specific functions
  • Evaluate model outputs with reference to gold-standard answers


For more knowledge on OpenAI refer below reference blogs:



!!! Happy Learning !!!


To view or add a comment, sign in

More articles by Debayan Mitra

  • 🚀 From JSON to Excel Table - Zero Coding

    Ever found yourself staring at a complex JSON file, wishing it could magically turn into a clean, readable Excel table?…

    3 Comments
  • Create a Chatbot using Microsoft Copilot Studio

    In February 2023 Microsoft launched Copilot, a generative artificial intelligence chatbot. Though it has a tough…

    4 Comments
  • Basic Structure of a Jenkinsfile

    What is Jenkins: Jenkins is an open-source tool, popular among DevOps practitioners. Jenkins helps to create and manage…

    2 Comments

Others also viewed

Explore content categories