OpenAI – connection in Python
Firstly some essentials about OpenAI and their Chat GPT. Chat GPT is an advanced language model based on AI. It answers in English and many other languages. Chat GPT knows many things about programming, technology, car brands, and countless other things. What I will present in this article is mainly:
- How to easily connect to OpenAI API from Python level
- How to ask multiple questions from Python to OpenAI API
What you will need is:
- Python (I’m using version 3.9)
- Some Python writing program (I’m using the free PyCharm Community)
Connecting to OpenAI from the API – API key
First, you need is to log in to openai.com account. If you don’t have an account, you can create one. By creating account you will get $18 free bonus. After use it, you have to pay for each query. Honestly, to generate several thousand texts, $18 should be enough , so you don’t pay anything for using it while testing.
In the upper right corner of the screen, click ” View API Keys “. Click ” Create new secret Key ” and copy it immediately. After closing the window, the API Key cannot be viewed a second time. However, if you were unable to save it, you can delete the previously generated API Key and create a new one .
Now it’s time for the Python code step by step:
Step 1: Install Python and Pycharm
If you haven’t used Python and PyCharm before, just install them:
- Python installation
- Installing PyCharm (Community Version)
Step 2: Create a virtual environment in Python (not necessary, but it’s better) or Install the openai package directly
We launch pyCharm and create a new project . In the upper right corner you should see a message about no interpreter. Click it, and then select the Python interpreter from the list (system or virtual environment).
Install the openai package. We open CMD and enter:
pip install openai
Step 3: The proper code of communication with the GPT chat from the input level
Below is an example of a program that will return a response after entering the so-called input in Python . We launch our program with the shortcut CTRL+SHIFT+F10 . The text “Insert text:” will appear in the command line. You need to enter your query + Enter.
# -*- coding: utf-8 -*-
# import packages:
import json
import openai
openai.api_key = '>>>YOUR_KEY<<<'
def get_content(in_prompt):
response = openai.Completion.create(model="text-davinci-003",
prompt=in_prompt,
temperature=0,
max_tokens=3900)
return response
if __name__ == "__main__":
in_prompt = input("Insert text: ")
content_json = get_content(in_prompt)
# result as JSON
print(content_json)
content_text = content_json["choices"][0]["text"]
# Result as text
print(content_text)
The response will be visible in the console in the form of a JSON message and in the form of text (marked in the screenshot below). Variable content_text = content_json[“choices”][0][“text”] – this is a fragment of our code that extracts the response itself from JSON.
Short description of the code:
- # -*- coding: utf-8 -*- – encoding setting. Especially important when you want to get an answer from OpenAI in an alphabet other than English (e.g. an answer containing Polish characters).
- openai.api_key = ‘>>>YOUR_KEY<<<‘ – enter your API key here. If you develop the program professionally, the key should of course not be in the code!
- get_content() – function returning response from OpenAI. in_prompt argument where we insert the question into OpenAI. The function returns response .
- content_text = content_json[“choices”][0][“text”] – this is a fragment of our code that extracts the text response from JSON.
- max_tokens =3900 – defines the maximum number of tokens that can be used for a single API request. I entered the number 3900 which is close to the maximum (4095). If you’ll be querying openAI in a language other than English, I suggest choosing a smaller number like 3900.
What else may catch your attention is the selected language model . The most popular model at the moment is text-davinci-003 . This model is also used as the standard model in the GPT chat . You can also choose from (cheaper to use and charging less tokens per query): text-babbage-001 , text-ada-001 .
Step 4: Proper code for retrieving answers to questions provided in the form of a list
Below is the modified code that works as follows:
- There is a list containing many OpenIA questions – The questions_list variable
- The loop asks the elements of the list one by one
- Answers are saved to another list – answers_list
- Finally , a list with the answers is displayed
Code:
# -*- coding: utf-8 -*-
# import packages:
import json
import openai
openai.api_key = '>>>YOUR_KEY<<<'
def get_content(in_prompt):
response = openai.Completion.create(model="text-davinci-003",
prompt=in_prompt,
temperature=0,
max_tokens=3900)
return response
if __name__ == "__main__":
# in_prompt = input("Insert text: ")
questions_list = ["How to learn Python?",
"What is your favorite film?",
"Can you write a program?"]
answers_list = []
for i in questions_list:
content_json = get_content(i)
content_text = content_json["choices"][0]["text"]
answers_list.append(content_text)
# Result as text
for j in answers_list:
print(j)
What else should we know about Chat GPT support from the API level?
- The chat often crashes (at least at this moment) – it’s worth writing some exception handling in communication with the API, or a simple try…except .
- The copyright of content generated from OpenAI Chat is not clear. Chat replies that he does not own the copyright to the lyrics. The question is whether we have them, since we pay after the test period for the chat service.