Note: As the ChatGPT API has not been made public yet, this implementation uses the text-davinci-003 model, not the ChatGPT model
Introduction
So first, to import ChatGPT into python, you will need to install some libraries. Let's get started.
Writing the Code
Step 1
Before you can even write the code, run this command into your command prompt:
pip install openai
Step 2
Create a new file, then name it main.py. Then, paste the following code:
import openai
openai.api_key = "YOUR_API_KEY"
def generate_response(prompt):
model_engine = "text-davinci-003"
prompt = (f"{prompt}")
completions = openai.Completion.create(
engine=model_engine,
prompt=prompt,
max_tokens=1024,
n=1,
stop=None,
temperature=0.5,
)
message = completions.choices[0].text
return message.strip()
prompt = input("Enter your question: ")
response = generate_response(prompt)
print(response)
Wrap Up
Put in your API key, then you're done! You (un)offically have ChatGPT (actually text-davinci-003) in your file explorer! Thanks for reading my article, have fun and happy coding!