Open AI Python
Open AI Python
Cheat sheet:
Alter the randomness and novelty of the output text by tuning it. # Create Subrip subtitles with openai.Audio.transcribe(response_format="srt")
response_format="text"
)
# Limit output length with max_tokens
We recommend using a platform like DataCamp Workspace that allows secure storage of your API secret key.
You'll need to load the os package to access your secret key, the openai package to access the API, pandas to make some Basic Flow for Embeddings from io import BytesIO
JSON output easier to work with, and some functions from IPython.display to render markdown output.
# Embed a line of text
# Generate images with openai.Image.create()
response = openai.Embedding.create(
response = openai.Image.create(
model="text-embedding-ada-002",
prompt="Oil painting of data scientist rejoicing
import os
import openai
import pandas as pd
openai.api_key = os.environ["OPENAI"]
img_bytes = get(response["data"][0]["url"]).content
img = Image.open(BytesIO(img_bytes))
> Generate Text with GPT Review # Return generated image directly with response_format="b64_json"
response = openai.Image.create(
The best pizza I've ever eaten. The sauce was so tangy! prompt="Digital illustration of data scientist
Basic flow for Chat The pizza was disgusting. I think the pepperoni was made from rats. and a robot high-fiving.",
response_format="b64_json"
I hate pineapple on pizza. It is a disgrace. Somehow, it worked well on this pizza though.
models for chat are
I ate 11 slices and threw up. The pizza was tasty in both directions. # Decompress image & display
"gpt-4": GPT-4 (recommended for high-performance use # Helper function to get embeddings
"gpt-4-32k": GPT-4 with 32k context (recommended for high performance, long chats txt = txt.replace("\n", " ")
img = Image.open(BytesIO(img_bytes))
input=[txt]
user: Specifies what you want the AI assistant to say prompt="A data scientist winning a medal in the data Olympics.",
assistant: Contains previous output from the AI assistant or specifies examples of desired AI output. # Get embedding for each row of a text column of a DataFrame
n=3
pizza["embedding"] = pizza["review"].apply(get_embedding) )
response = openai.ChatCompletion.create(
response["data"][i]["url"]
model="gpt-3.5-turbo",
messages=[ {
> Convert Speech to Text with Whisper response["data"][i]["b64_json"]
:
"role" "system",
# Reduce the image size with the size argument
"content" :'
You are a stand-up comic performing to an audience of data Audio files can be converted to text. Supported file formats are mp3 , mp4, mpeg, mpga, m4a, wav, and webm. The output # Choices are 256x256, 512x512, 1024x1024 (default)
response = openai.Image.create(
}, {
prompt="A data scientist saving the world from alien attack.",
:
"role" "user",
Supported models
whisper-1: Whisper (recommended)
size="256x256"
"content" :'
Tell a joke about statistics. '
)
}, {
:
"role" "assistant",
# Note that model is the second arg here, not the first
}
with open("audio.mp3", "rb") as audio_file:
]
transcript = openai.Audio.transcribe(
Learn AI Online at
www.DataCamp.com
)
file = audio_file,
model = "whisper-1",
response["choices"][0]["finish_reason"]
language="en"
)
# Extract the AI output content
ai_output = response["choices"][0]["message"]["content"]