Use Open AI API for Text Generation in Python

Brain Glitch
1 min readMar 4, 2023

Checkout video for Tutorial : https://www.youtube.com/watch?v=VOeVKoaRkqI

Here are the steps to use the OpenAI API for ChatGPT :

  1. Sign up for an OpenAI API key: To use the OpenAI API, you will need to sign up for an API key on the OpenAI website. Follow the instructions provided to create an account and obtain an API key.
  2. Install the OpenAI package: Install the OpenAI package using pip. Run the following command in your terminal or command prompt:
pip install openai

3. Import the OpenAI package: Import the OpenAI package in your Python script using the following code:

import openai

4. Set your OpenAI API key: Set your OpenAI API key in your Python script using the following code:

openai.api_key = 'Enter your API_Key'

5. Take Prompt as input from use: Take Prompt as input from the use by using the following code:

text = input("Enter the prompt: ")

6. Call the OpenAI API called Completion with create method, here we need 3 parameter which is Prompt, model, and max_tokens :

output = openai.Completion.create(
prompt=text,
model="text-davinci-003",
max_tokens=1000
)

7 . Print the output

print("Output are as follows:")
print(output["choices"][0]["text"])

Git Repository:

https://github.com/deadlock415/ChatGPT-API.git

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

Brain Glitch
Brain Glitch

Written by Brain Glitch

Artificial Intelligence | Spirituality | Manifestation | Conspiracy | Technology

No responses yet

Write a response