Use Open AI API for Text Generation in Python

Checkout video for Tutorial : https://www.youtube.com/watch?v=VOeVKoaRkqI
Here are the steps to use the OpenAI API for ChatGPT :
- 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.
- 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: