THE CHATCOMPLETION CLASS

The openai.Completion class used in the previous post was designed to generate text in response to a prompt. However, such a class does not take into account the context of the conversation. This is where the ChatCompletion class comes in. designed to maintain the history of the conversation. The ChatCompletion API allows for the simulation of a conversation between a human user and a virtual assistant that remembers the dialogue as it takes place in order to create a context of the conversation. With your favorite editor, create a script named test_api_chat1.py. I bring you the code to insert in the script.

test_api_chat_1

The create method of ChatCompletion returns an object having a set of attributes. I explain the parameters passed to the create method.

  • model unlike Completion the ChatCompletion class allows us to use the latest GPT models.
  • messages is an array that contains objects representing messages exchanged between a user and a virtual assistant. Each of the objects in the array has two attributes, the role. There are three types of roles allowed in a message:
    • system is provided to the virtual assistant to define the general context of a conversation.
    • user the message is user-generated, so it is a prompt.
    • assistant is the virtual assistant’s response message.

The second attribute is the content representing the text of the user or virtual assistant. We will see in the next example how a conversation can be created between the user and the virtual assistant by exchanging a series of objects.

With the last instruction we are going to read the content generated by the assistant in response to the one user prompt provided.

CONVERSATION WITH OPENAI CHATCOMPLETION

You will learn how to create a real conversation between a user and a virtual assistant equipped with a context. Create a script and call it test_api_chat2.py.

test_api_chat2

EXPLANATION OF THE EXAMPLE CODE

We define a chat_with_openai() function . Let’s comment on the code: The first thing to do is to create an array that will progressively contain the history of our conversation(chat_history=[]). What the function briefly does is to take the user’s terminal input add it to the array and pass the prompt to the remote API. In this example, the virtual assistant’s responses are also added to chat_history with the append method so that the conversation history is maintained, and the cycle (infinite loop) repeats until the user types the word “fine” on the terminal. Run the script and ask questions to your heart’s content. I bring you in the following image my conversation.

response

The interesting thing to note is that the question “how many inhabitants does it have” would not make sense unless put in a specific context. ChatGPT’s API is able to maintain the history of the conversation and respond correctly.

THE ROLE SYSTEM

We modify the previously created script by adding the system role. Duplicate the previous script and make the following change(there is only the line chat_history.append({“role”: “system”, “content”: “Usa un tono da teenager.”}). to be added).

role system

These are the responses I got with the role system and the content “Use a teenager tone.”

response

LINK TO CODE ON GITHUB

GITHUB

LINKS TO PROGRAMMING IN PYTHON

THE PYTHON LANGUAGE