OpenAI

The OpenAI Python library provides convenient access to the OpenAI API from applications written in the Python language. It includes a pre-defined set of classes for API resources that initialize themselves dynamically from API responses which makes it compatible with a wide range of versions of the OpenAI API [1].

To use the openAI's API, you first need to have your openAI's API key. To do so, you could register your account in https://platform.openai.com/, then go to your account API-key settings, click "+ Create new secret key" to generate an API key, then you copy the key and use it in the sample code below.

For a step-by-step tutorial on how to obtain the API key, please click here.

Warning

When including private API keys in your code, make sure you DO NOT make your project "Open Source" when you share it.

Examples

Built AI to answer question

import openai

api_key = input("🤖 What's your openAI's API Key?\n")
client = openai.OpenAI(api_key=api_key)

question = input("🤖 What's your question?\n")
print("🤖 Please wait a second ...")

completion = client.chat.completions.create(
  model="gpt-3.5-turbo",
  messages=[
        {"role": "system", "content": "You are ChatGPT"},
        {"role": "user", "content": question},
    ]
)

answer = completion.choices[0].message.content
print("🤖", answer)

For more information on how to use the openAI library, see the official openAI documentation [2].

Reference

  1. https://pypi.org/project/openai/
  2. openAI's official documentation