Python OpenAI Text Generator Example

Paul Yang - December 2nd, 2022

Try out the OpenAI Python API, which backs ChatGPT (Updated 2023)

You can sign up for an account at OpenAI, the API behind ChatGPT, to get an API Key from them - they give you $18 of credits to start out, so plenty of free opportunities to play around with their various models and API.

Rather than being forced to give and receive responses in a chat window though, you can directly operationalize many of the same features through simple Python calls.

Below is a simple example based on a prompt to OpenAI to generate a dataset, and how we can convert that response into a data frame in order to keep working in Python pandas. Read on to learn more about the OpenAI API.

You can start by importing and installing the necessary libraries, as well as setting your API key. Remember, don't share your API keys with anyone! You can store an API key, and any other secrets, such as account numbers, usernames, or passwords in the environment settings of any Einblick canvas. These secrets are only available to canvas editors.

Then you can set the openai.api_key equal to your API key value.

%pip install openai
import os
import openai

# api_key is a variable saved in the secrets menu
openai.api_key = api_key

Now that you've set your API key, you've been authenticated as a user. Now you can create your prompt, and get a response from OpenAI. There are a few arguments to consider, which we'll review in brief below, when creating a response via the openai.Completion.create() function. Check out the OpenAI documentation for a more comprehensive review.

OpenAI Completion Arguments

response = openai.Completion.create(
  prompt="A three-column spreadsheet of top contemporary fiction books, the year of release, and their Goodreads rating:\n\nTitle |  Year of release | Rating",
  • model: GPT-3 has 4 model options that you can choose between when submitting your request. Davinci is the most advanced, and the most expensive to run, and Ada is the least advanced, and least expensive to run.
    • text-davinci-003
    • text-curie-001
    • text-babbage-001
    • text-ada-001
  • prompt: this is a string that you're giving OpenAI to respond to. The only limitation here is the number of tokens, which we'll explain further below. Each model has different token limitations, which you can learn more about in OpenAI's documentation.
  • temperature: any value from 0 to 1 that controls how "confident" the model should be when generating its response. A temperature of 0 requires the model to have high confidence, resulting in similar or identical responses every time you submit the same prompt. A temperature of 1 gives the model freedom to have low confidence in its response, so you'll see more variation in its responses to the same prompt. There is another variable top_p that you can use to vary the confidence of the response, but it is recommended to vary either temperature or top_p but not both.
  • max_tokens: this is the number of tokens that you are allowing the individual request to use. This variable is a sum of the tokens (similar to word count) in the prompt and the response from OpenAI. Each model has different token limitations, with Davinci allowing the most tokens at 4,000.
  • frequency_penalty: a value between -2.0 and 2.0. The more positive the value, the less likely that lines will be repeated verbatim.
  • presence_penalty: a value between -2.0 and 2.0. The more positive the value, the more likely new topics will be introduced.
from io import StringIO


# turn text into dataframe for use
data = StringIO(response.choices[0].text)
df = pd.read_csv(data, sep="|", skiprows=1, header = None)

Lastly, we can use the StringIO function to extract the responses, and in this case since we've asked for a spreadsheet, we can then read the data into a dataframe using the pd.read_csv() function.


Einblick is an AI-native data science platform that provides data teams with an agile workflow to swiftly explore data, build predictive models, and deploy data apps. Founded in 2020, Einblick was developed based on six years of research at MIT and Brown University. Einblick is funded by Amplify Partners, Flybridge, Samsung Next, Dell Technologies Capital, and Intel Capital. For more information, please visit and follow us on LinkedIn and Twitter.

Start using Einblick

Pull all your data sources together, and build actionable insights on a single unified platform.

  • All connectors
  • Unlimited teammates
  • All operators