OpenAI Python API Example for NLP Tasks

OpenAI Python API Example

Ever wondered how you can leverage the power of OpenAI’s GPT-3 and GPT-3.5 (from Jan 2024 onwards) directly in your Python application? Are you curious about generating human-like text with just a few lines of code? This blog post will walk you through an example Python code snippet that utilizes OpenAI’s Python API for different NLP tasks such as text generation. Check out my other post on how to use Langchain framework for text generation using OpenAI GPT models.

OpenAI Python APIs

The OpenAI Python API is an interface that allows you to interact with OpenAI’s language models, including their GPT-3 model. The following are different popular models that you can integrate using the APIs to accomplish different NLP tasks such as text classification, text completion, conversation, text generation, etc:

  • text-davinci-003
  • text-curie-001
  • text-babbage-001
  • text-ada-001

You can find details on the capabilities of these models in my another post: OpenAI GPT-3 Models List: Explained with Examples. You can get the details about APIs in this page, OpenAI API Reference.

OpenAI API Python Code Example

The following code represents how to generate text using the GPT-3 model namely text-davinci-003.

secret_key='sk-PXWRJppSdiQcVDwo9EcWT3BRCVeMOxLR0XHyDe9jYyI' # Use your own key
prompt="Define machine learning"

import openai
openai.api_key = secret_key

output = openai.Completion.create(


Executing above code would print the following JSON data. The JSON data consists of output and other details.

Pay attention to some of the following related to above code and the output:

  • You will need to login into OpenAI account and get your secret key. Here is the picture representing how to get the key:

    OpenAI API Keys
  • OpenAI.Completion.create API takes the input for parameters namely, model, prompt, max_tokens, and temperature. max_tokens is the sum of prompt_tokens + completion_tokens. Completion_tokens represent the output text. The parameter, temperature, is used to represent how creative GPT model can be, while generating response. A value close to 1 represents high creativity while a value close to 0 represents high accuracy / correctness. The parameter, prompt, takes the input request which is used to generate the response. In the above example, the value of prompt is “define machine learning”.
  • print command prints out the generated text to the console. You could print the output using the following command: print(output[‘choices’][0][‘text’])


The OpenAI Python APIs are a gateway to a world of possibilities in the realm of text generation and other NLP tasks such as text classification, sentiment analysis, text completion, etc. Whether you’re a data scientist, a machine learning engineer, or a developer looking to incorporate intelligent text generation into your application, this API offers a robust, scalable, and incredibly straightforward way to accomplish that.

Ajitesh Kumar

Ajitesh Kumar

I have been recently working in the area of Data analytics including Data Science and Machine Learning / Deep Learning. I am also passionate about different technologies including programming languages such as Java/JEE, Javascript, Python, R, Julia, etc, and technologies such as Blockchain, mobile computing, cloud-native technologies, application security, cloud computing platforms, big data, etc. I would love to connect with you on Linkedin. Check out my latest book titled as First Principles Thinking: Building winning products using first principles thinking.
Posted in Generative AI, Machine Learning, NLP, OpenAI, Python. Tagged with , , , .

Leave a Reply

Your email address will not be published. Required fields are marked *