INTRODUCTION TO OPENAI AND CHATGPT

In this article we are going to explore the ChatGPT API. Let’s start by talking about OpenAI an American nonprofit research lab that focuses on developing and promoting artificial intelligence. In 2019 OpenAI created a limited partnership (LP) company. One of the investors is Microsoft, which provided $1 billion for a strategic partnership agreement. OpenAI develops a whole range of applications, the one we will consider is ChatGPT, which simulates conversation between a virtual user and a human user. We will use OpenAI API (application program interface). These APIs are services that allow us to access artificial intelligence features from within our applications.

THE MODEL

Let us go into detail. First, let’s talk about Model which in ChatGPT is an AI (Artificial Intelligence) based language generation model that was trained on an extremely large amount of text so that it could learn natural language structure in an evolved way. The text comes from books, web pages, and so on. The GPT model is named after Generative Pre-Trained Transformer. Simply put, GPT is a model created by OpenAI. The two main models at this time are the GPT-3 and GPT-4. Let’s talk about another important concept, the
Token
. GPT family models process text in tokens i.e., sequences of characters that are recognized within the text. Decomposing text into tokens allows GPT models to learn the relationships between tokens found in the text. Tokens are used both in training and when we provide a sentence. The text that we provide as input to a GPT model is called the Prompt.

OPENAI API SETUP

In this section we are going to set up the OpenAI API and download the Python library that allows us to interact with the API. The first thing we need to do is to go with our browser to https://openai.com/ and register. We will be faced with a screen similar to the following:

OpenAI Home

Click on the login menu item. The following screen opens.

API OpenAI

We are interested in the API so click on the link. The screen for registration will open:

Registrazione

Click on Sign up or a method of your choice and enter the required registration information. Once that is done do the Login the following screen will open:

Home

GENERATE THE UNIQUE KEY

At this point we need to generate a unique key that we will enter into the program either as an environment variable or via an .env file. Keep in mind that this key must remain secret, so I do not recommend that you hardwire it into the code, plus the use of the OpenAI API
is not free
, you have an initial credit which at the time of writing is $5, however you only have this credit available for the initial phase. To generate the key that uniquely represents you, click on Personal and from the menu that opens select View API Keys.

API Keys

Click on the Create new secret key button, name the key and generate it. Keep in mind that you can only display this key once; therefore, I recommend copying and pasting it somewhere and then using it in Python programs. Let us now look at the second step, which is the installation of the Python library. Open your favorite editor, I am using the community version of PyCharm, open a prompt and type the following command:

pip3 install openai

In the next section we will start writing some Python programs with the OpenAI API.

THE OPENAI.COMPLETION CLASS

With your favorite editor create a directory and inside it a script with the name test_api.py . Below is the code.

engine-davinci

WE COMMENT ON THE CODE

The key obtained from the step seen above was not hardwired into the code. In the same directory where you placed the script create a file named .env. I have adopted this solution if you prefer use an environment variable. In the .env file create a key OPENAI_API_KEY having the generated key as its value. Install the decouple module with the following command:

pip3 install decouple

via the second line of code you now have access to the .env configuration file. After setting the key we declare a test_api() function and now invoke the create function of the Completion class. This function allows us to send a prompt to the remote API, which processes the request and returns a response that we record in the response variable. Let’s look at the parameters:

PARAMETERS PASSED TO THE FUNCTION

  • engine indicates the artificial intelligence model that the remote API must use to provide the response to the prompt we provide.
  • prompt is the text we want to pass to the API.
  • max_tokens indicates the maximum number of tokens to be generated for the response. Basically, we can tell the system how long we want the response to be.
  • n specifies the number of responses to be generated by our model.
  • stop specifies an interrupt string in the prompt, in other words, the model stops generating response text when it finds an exact match with the interrupt string set in the prompt. In our example it was not set (None).
  • temperature controls the level of randomness that the model will be able to use when generating the response text. Positive real number between 0 and 1. With values close to zero the model will tend to provide very similar responses to each other, conversely the closer the value is to 1 the more freedom, creativity the model has in generating the response.

This is the response that the remote API generates to our prompt.

response

THE RESPONSE

Obviously the most interesting part of the answer is the text (text) that is in the only choices (array) with only one element thus having index 0. Education:

print(risposta.choices[0].text.strip())

prints only the answer by removing with the strip() method all special characters that are at the beginning and end of the string.

LINK TO CODE ON GITHUB

GITHUB

LINKS TO PROGRAMMING IN PYTHON

THE PYTHON LANGUAGE