Introduction
Welcome to the guide on how to query and get ChatGPT answers using python. This tutorial aims to provide you with all the necessary information and steps required to use python for querying ChatGPT.
Creating a ChatGPT account
Before we begin, it is important to have a ChatGPT account. If you do not have one already, head over to the ChatGPT website and sign up for an account.
Step 1: Click on the "Sign Up" button.
Step 2: Enter your email address and create a secure password.
Step 3: Check your email inbox and click on the link sent by the OpenAI website to verify your email address.
You can now begin exploring the capabilities of ChatGPT. For example, I plan to utilize it to automatically gather information on significant fire events for a research project
Creating a ChatGPT API key
To get started with the ChatGPT API, the first step is generating your API key. Follow these steps:
Step 1: Visit the OpenAI subdomain platform.
Step 2: Locate the small padlock symbol on the left navigation bar (refer to the image below).
Step 3: Click on the symbol to access a new page.
Step 4: Choose the option to create a new API key. Please note that phone validation is required for this step.
Step 5: Once you've created the key, make sure to copy it and store it in a secure location that only you can access easily. This key will be necessary every time you want to use the ChatGPT API. Please be aware that the key is only provided once, so if you misplace it, you'll need to create a new one.
Installing openai library
To use python for querying ChatGPT, we will need to install the openai python library. This can be easily done through conda:
conda install -c conda-forge openai
or pip
pip install openai
Using ChatGPT API
To begin, we start by importing the "openai" library:
import openai
Once the library is imported, we need to authenticate our python script with our ChatGPT account. This allows us to access all the features and capabilities of ChatGPT.
openai.api_key = 'INSERT_YOUR_KEY_HERE'
Please replace "INSERT_YOUR_KEY_HERE" with the key you obtained in the previous section.
Now that your python script is linked to your ChatGPT account, you can start querying and receiving answers from ChatGPT. The openai.Completion
function is used to query ChatGPT with a prompt and receive a response.
output = openai.ChatCompletion.create(model = 'gpt-3.5-turbo', messages = [{"role":"user", "content":"what the name of big fire event in august 2019 in Washington State"}], stream = True)
The above code will query ChatGPT with the prompt "What the name of big fire event in august 2019 in Washington State" and provide a response that ends at a new line character.
for chunk in output:
print(chunk.choices[0].delta.get("content",""), end = "")
Example of output
The big fire event in Washington State in August 2019 was called the "Williams Flats Fire."
Another example:
output = openai.ChatCompletion.create(model = 'gpt-3.5-turbo', messages = [{"role":"user", "content":"big fire events latittudes and longitudes in august 2019"}], stream = True)
for chunk in output:
print(chunk.choices[0].delta.get("content",""), end = "")
gave me:
There were several significant fire events around the world in August 2019. Here are some of the major ones along with their latitudes and longitudes:
1. Amazon rainforest fires:
- Latitude: Varies across the Amazon rainforest, between approximately 10°N and 25°S.
- Longitude: Varies across the Amazon rainforest, between approximately 45°W and 75°W.
2. Siberian wildfires:
- Latitude: Varies across Siberia, between approximately 50°N and 70°N.
- Longitude: Varies across Siberia, between approximately 60°E and 150°E.
3. Canary Islands wildfires (Gran Canaria):
- Latitude: Approximately 28°N.
- Longitude: Approximately 15°W.
4. Alaska wildfires:
- Latitude: Varies across Alaska, between approximately 54°N and 71°N.
- Longitude: Varies across Alaska, between approximately 130°W and 180°W.
5. Greece wildfires (on the island of Evia):
- Latitude: Approximately 38°N.
- Longitude: Approximately 23°E.
Please note that the latitudes and longitudes mentioned above are appr
References
Links | Site |
---|---|
openai | anaconda.org |
How to Print ChatGPT API Response Output as a Stream: An Example in Python | youtube.com |