On November 2022 openai lauches its new product chatgpt3. Which is the most advanced chatbot now. So in this blog article i will help you with step by step guide to Build Your own Chatgpt3 using python with the help of openai API. So lets begins with getting the api key from openai site.
For that vist chatgpt3 api create an account. After that click on your profile and then click on “View Api Key”
After that select the create new secret key.
After that now copy your API key and now lets move to te code part.
Installation
now we need to install openai package to access the openai api with python for that open command prompt or terminal whichever you are using and type:
pip install openai
Code
import openai
# Replace YOUR_API_KEY with your OpenAI API key
openai.api_key = "YOUR_API_KEY"
Here we have firstly imported the openai package which we installed earlier and then initialized it with openai API key. Enter the openai key which we generated earlier.
# Set the model and prompt
model_engine = "text-davinci-003"
prompt = "Write a blog on ChatGPT"
# Set the maximum number of tokens to generate in the response
max_tokens = 1024
# Generate a response
completion = openai.Completion.create(
engine=model_engine,
prompt=prompt,
max_tokens=max_tokens,
temperature=0.5,
top_p=1,
frequency_penalty=0,
presence_penalty=0
)
# Print the response
print(completion.choices[0].text)
Now in this part of the code, I have selected the gpt3 model. here we have used Davinci-03 model which is one of the most powerful models and also created a variable called “prompt” in this variable put you query or prompt which you wanted to ask chatgpt3.
Complete Code
import openai
# Replace YOUR_API_KEY with your OpenAI API key
openai.api_key = "YOUR_API_KEY"
# Set the model and prompt
model_engine = "text-davinci-003"
prompt = "Write a blog on ChatGPT"
# Set the maximum number of tokens to generate in the response
max_tokens = 1024
# Generate a response
completion = openai.Completion.create(
engine=model_engine,
prompt=prompt,
max_tokens=max_tokens,
temperature=0.5,
top_p=1,
frequency_penalty=0,
presence_penalty=0
)
# Print the response
print(completion.choices[0].text)