Links
Comment on page

🐍 OpenAI with Python

Learn how to start using OpenAI with Python
Would you like to connect your software to OpenAI but you don't know how? Don't worry, here I provide you a snippet to start using OpenAI API. For this example first you will need to install the requests module:
$ pip install requests

Generate Text

In this example we will use the API for generate text, using model text-davinci-002.
import requests
def generate_text(prompt):
api_key = "your_api_key_here"
model = "text-davinci-002"
completions_endpoint = "https://api.openai.com/v1/engines/{}/completions".format(model)
response = requests.post(completions_endpoint,
headers={"Content-Type": "application/json",
"Authorization": "Bearer {}".format(api_key)},
json={"prompt": prompt, "max_tokens": 50})
if response.status_code == 200:
completions = response.json()['choices'][0]['text']
return completions
else:
return "Request failed with status code {}".format(response.status_code)
generated_text = generate_text("What is the meaning of life?")
print(generated_text)

Analyze Sentiment

import requests
def analyze_sentiment(text):
api_key = "your_api_key_here"
model = "text-davinci-002"
analysis_endpoint = "https://api.openai.com/v1/engines/{}/analysis".format(model)
response = requests.post(analysis_endpoint,
headers={"Content-Type": "application/json",
"Authorization": "Bearer {}".format(api_key)},
json={"text": text, "features": ["sentiment"]})
if response.status_code == 200:
analysis = response.json()
sentiment = analysis['sentiment']['score']
return sentiment
else:
return "Request failed with status code {}".format(response.status_code)
text = "I love the OpenAI API! It's so easy to use."
sentiment = analyze_sentiment(text)
print("Sentiment score:", sentiment)

Generating Images

import requests
def generate_image(prompt):
api_key = "your_api_key_here"
model = "image-alpha-001"
completions_endpoint = "https://api.openai.com/v1/images/generations".format(model)
response = requests.post(completions_endpoint,
headers={"Content-Type": "application/json",
"Authorization": "Bearer {}".format(api_key)},
json={"model": model, "prompt": prompt})
if response.status_code == 200:
image_data = response.json()['data'][0]
return image_data
else:
return "Request failed with status code {}".format(response.status_code)
generated_image = generate_image("A cat playing with a ball of yarn")
print(generated_image)

Question Answering

import requests
def answer_question(question, context):
api_key = "your_api_key_here"
model = "text-davinci-002"
completions_endpoint = "https://api.openai.com/v1/engines/{}/questions".format(model)
response = requests.post(completions_endpoint,
headers={"Content-Type": "application/json",
"Authorization": "Bearer {}".format(api_key)},
json={"question": question, "context": context})
if response.status_code == 200:
answer = response.json()['answers'][0]['text']
return answer
else:
return "Request failed with status code {}".format(response.status_code)
question = "What is the capital of Italy?"
context = "The capital of Italy is Rome."
answer = answer_question(question, context)
print(answer)

Translation

import requests
def translate_text(text, target_language):
api_key = "your_api_key_here"
model = "text-davinci-002"
completions_endpoint = "https://api.openai.com/v1/engines/{}/translations".format(model)
response = requests.post(completions_endpoint,
headers={"Content-Type": "application/json",
"Authorization": "Bearer {}".format(api_key)},
json={"text": text, "target_language": target_language})
if response.status_code == 200:
translation = response.json()['translated_text']
return translation
else:
return "Request failed with status code {}".format(response.status_code)
text = "The OpenAI API is great!"
translation = translate_text(text, "fr")
print(translation)
Last modified 9mo ago