OpenAI Python API Example for NLP Tasks

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(
    model='text-davinci-003',
    prompt=prompt,
    max_tokens=90,
    temperature=0
)

print(output)

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.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’])

Conclusion

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

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.

Recent Posts

Agentic Reasoning Design Patterns in AI: Examples

In recent years, artificial intelligence (AI) has evolved to include more sophisticated and capable agents,…

1 month ago

LLMs for Adaptive Learning & Personalized Education

Adaptive learning helps in tailoring learning experiences to fit the unique needs of each student.…

1 month ago

Sparse Mixture of Experts (MoE) Models: Examples

With the increasing demand for more powerful machine learning (ML) systems that can handle diverse…

2 months ago

Anxiety Disorder Detection & Machine Learning Techniques

Anxiety is a common mental health condition that affects millions of people around the world.…

2 months ago

Confounder Features & Machine Learning Models: Examples

In machine learning, confounder features or variables can significantly affect the accuracy and validity of…

2 months ago

Credit Card Fraud Detection & Machine Learning

Last updated: 26 Sept, 2024 Credit card fraud detection is a major concern for credit…

2 months ago