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:
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"
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.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’][‘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.