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( model="text-davinci-003", prompt="A three-column spreadsheet of top contemporary fiction books, the year of release, and their Goodreads rating:\n\nTitle | Year of release | Rating", temperature=0.5, max_tokens=60, frequency_penalty=0.0, presence_penalty=0.0 )
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.
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_pthat you can use to vary the confidence of the response, but it is recommended to vary either
top_pbut 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 print(response.choices.text) # turn text into dataframe for use data = StringIO(response.choices.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
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 www.einblick.ai and follow us on LinkedIn and Twitter.