ChatGPT Python Hook

Using Python to hook to ChatGPT's API

  • 04 April 2023

Introduction

After all the thrill in the developer community and media - Chat-GPT has been unveiled to the world πŸ€–.

I'm sure you've heard enough, and I simply want it to be easily accessible to people. So feel free to use the tool below! However, I want to be clear that Chat-GPT is simply AI that has been trained on a immense data set, that in addition, has been supervised! The modelling is under the influence of a reward system, on which relationships are determined.

Given the 165 billion estimated parameters (which have little to no meaning), predictions are made. However, that's all they are - predictions. Simply, given a prompt or input - Chat-GPT is predicting the best string of words given the aforementioned training. Chat-GPT is not a superior search engine that aggregates the correct result for you. As such, treat it with care and enjoy!



OpenAI API and Python

Here, I demonstrate how to use Python to hook to OpenAI's API, so you may easily use Chat-GPT (that's what I've done to yield the above).

1. Install

Install the required packages openai and python-dotenv.


	pip install openai python-dotenv

openai allows us to use Python to interact with Chat-GPT. python-dotenv is used for retrieving stored values, and will be explained in depth later

2. Get API Key

OpenAI provides access to it's API through an 'API Key'. To get it go to https://platform.openai.com/account/api-keys, make an account, and 'Create a new secret key'. Record it!

3. Setup workspace - with .env file

Create the following folder structure:


	└── chat_gpt_api/
		β”œβ”€β”€ .env
		└── main.py

The .env file contains the environment variables, which will contain the secret key found in step 2. It is good practice to hold all secret variables in such a file. Don't let anyone tell you otherwise. The .env file should like


	OPENAI_API_KEY=YOUR_SECRET_KEY

3. Time to Python - main.py

With everything setup - we are ready to crunch some code.


	# Imports
	import openai
	import os
	from dotenv import load_dotenv # This is where python_dotenv comes in
	
	# Load environment variables from .env
	load_dotenv()
	
	# Variables required to setup the API
	MAX_TOKENS = 1024
	CHATGPT_MODEL = 'text-davinci-003'
	OPEN_AI_SECRET_KEY = os.getenv('OPENAI_API_KEY')
	OPEN_AI_ORG_KEY = 'org-r95rDmiPDWBEwR7J2VM9AEnd'
	
	# Variable for the prompt
	USER_PROMPT = 'What is the meaning of life?'
	
	# Ask and get a response from OpenAI
	response = openai.Completion.create(
		model=CHATGPT_MODEL,
		prompt=USER_PROMPT,
		max_tokens=MAX_TOKENS,
		temperature=0
	)
	
	# Print out response, and tokens
	print(f'{response.choices[0].text}\nTotal Tokens: {response.usage.total_tokens}')

load_dotenv() will load all 'environment variables' from .env into your session (it's not a permanent save, which is exactly what we want!). Tokens (MAX_TOKENS) may be viewed as the cost of the response. The lower it is, the lower the quality of your prediction. I've set it to 1024 arbitrarily. There are a number of models Chat-GPT was trained on. The most popular is in 'text-davinci-003'. The two keys represent one that is your OpenAi secret key, and another related to OpenAi, that I won't get into. The response will be a class, and I've printed both the text response, and the token cost (out of curiosity!).