# OpenAI: GPT-3, ChatGPT and GPT-4

<figure><img src="https://2786867680-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LLTwFwbOqJj4dDhg8Ju%2Fuploads%2FxyWMOgG4Wxl1Nc6kdqJz%2FScreenshot%202023-05-26%20at%2014.16.00.png?alt=media&#x26;token=e9196fa5-194b-4661-b805-0e842366d383" alt=""><figcaption><p>How your fallback flow should look like if you use GPT as a Not Understood message</p></figcaption></figure>

## What is GPT

**GPT** is a series of advanced Natural Language Processing systems developed by OpenAI that allow you to execute several text-based tasks.&#x20;

The **OpenAI integration** allows you to generate responses in your chatbot. As a bot builder or conversation designer, all you need to do is select the **GPT-3, ChatGPT or GPT-4** model and tell it the right query that defines the kind of answer that you would like, and it will make it up for you!

To be able to use GPT models on Chatlayer, you will need to [set up the OpenAI integration](#set-up-the-gpt-3-integration), then [use this integration inside](#use-gpt-3-inside-a-bot) blocks.

{% hint style="warning" %}
Using the OpenAI integration means that your data is sent to OpenAI services which are based in the United States. Make sure you understand the [OpenAI terms of service](https://openai.com/terms/) and implications. Use OpenAI in production bots with caution, Chatlayer is not held responsible for generated content.
{% endhint %}

## \[Video] GPT in customer-facing conversations

Here is a video we made about how and why you could bring value to your bot if you use GPT.

{% embed url="<https://vimeo.com/911546543?share=copy>" %}

## Set up your OpenAI integration

Before being able to use GPT inside blocks, you need to connect an OpenAI account to the OpenAI integration on Chatlayer. For this, you might require an OpenAI API key.

{% hint style="success" %}
Chatlayer provides customers with its own **OpenAI API key** to use GPT with a limit of 100 messages per bot. This means that from the 101st message, GPT will stop generating and send errors. If you wish to be able to use an unlimited amount of GPT-generated messages, you will need to create [your own API key on the OpenAI's website](https://platform.openai.com/account/api-keys).
{% endhint %}

To connect your chatbot with GPT:

{% stepper %}
{% step %}
On your Chatlayer home page, go to **Integrations.**&#x20;
{% endstep %}

{% step %}
Search for "OpenAI" in the search bar.

<figure><img src="https://2786867680-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LLTwFwbOqJj4dDhg8Ju%2Fuploads%2FnP4WnbfZZ5GCkCQ3kZ7b%2FScreenshot%202023-05-26%20at%2013.30.11.png?alt=media&#x26;token=fcb7a22c-78bc-49f7-ba79-78abe7fefb89" alt=""><figcaption><p>Search for OpenAI and you will find the OpenAI integration ready to be configured</p></figcaption></figure>

{% endstep %}

{% step %}
Click on **Open AI**.

<figure><img src="https://2786867680-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LLTwFwbOqJj4dDhg8Ju%2Fuploads%2FrElCd4cq8wugAoRbZdo1%2FScreenshot%202023-05-26%20at%2013.32.36.png?alt=media&#x26;token=48383c55-5fd4-4837-b029-dd99a95a45b0" alt=""><figcaption><p>View of the OpenAI integration when open.</p></figcaption></figure>
{% endstep %}

{% step %}
Click on **+ Add Account** to start connecting your OpenAI account with the OpenAI integration.

<figure><img src="https://2786867680-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LLTwFwbOqJj4dDhg8Ju%2Fuploads%2FX5g65BQ6JYys2Tjsmlp6%2FScreenshot%202023-05-26%20at%2013.33.29.png?alt=media&#x26;token=b8ee9c44-2b29-4574-a4af-cfa1d63ae52c" alt="" width="278"><figcaption><p>Pop-up to provide the necessary credentials for connecting OpenAI to Chatlayer</p></figcaption></figure>
{% endstep %}

{% step %}
Paste your access token in the **Access token** field

{% hint style="success" %}
If you do not have a Chatlayer access token, you can generate one [here](https://app.chatlayer.ai/settings/api-access/tokens).
{% endhint %}

{% endstep %}

{% step %}
Under **Display name**, write an identifier that can be later used to select this specific access token when using the OpenAI integration inside a bot. In this example, we have used the identifier *Chatlayer Token OpenAI Example*.
{% endstep %}

{% step %}
Optionally, you can provide an **OpenAI API key**.
{% endstep %}

{% step %}
Click on **Submit** when you're done.
{% endstep %}
{% endstepper %}

The OpenAI account should now be visible under the `Connected Accounts` tab. Note that the word "Auth" was automatically added in front of the "Display Name".

<figure><img src="https://2786867680-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LLTwFwbOqJj4dDhg8Ju%2Fuploads%2FSJ2LDhfCOgTiC0118RoD%2FScreenshot%202023-05-26%20at%2013.44.10.png?alt=media&#x26;token=c9025c4b-e02b-4d6d-9d1e-c370ec89f3c5" alt=""><figcaption><p>You can see your OpenAI account under the Connected Accounts tab.</p></figcaption></figure>

{% hint style="success" %}
To try the OpenAI integration, Chatlayer offers 100 messages per bot. If this suits you, you can leave the OpenAI key field empty. Yet if you would like to go unlimited in the your number of GPT-generated messages, you will need to [get your own API key on the OpenAI's website](https://platform.openai.com/account/api-keys).
{% endhint %}

{% hint style="info" %}
You've set up your GPT account and would like to jump straight to bot building? We've got you covered with our [GPT-3 template](https://docs.chatlayer.ai/start-quickly/bot-templates/gpt) bot. Give it a go!
{% endhint %}

Your OpenAI integration is set up! Now it's time to use it inside the conversation.

## How to use OpenAI GPT models inside a bot

You will need to do two things so that your customers can interact with GPT-3, ChatGPT or GPT-4: generate a response, and display it.

### Generate a GPT response

To generate a GPT-3, ChatGPT or GPT-4 response:

{% stepper %}
{% step %}
Create an [Action ](https://docs.chatlayer.ai/bot-answers/dialog-state/action-bot-dialog)block and open it.
{% endstep %}

{% step %}
Search for the OpenAI integration in the search bar and select it

<figure><img src="https://2786867680-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LLTwFwbOqJj4dDhg8Ju%2Fuploads%2FYRqngDLjobhSm7LRuemz%2FScreenshot%202023-05-26%20at%2013.57.39.png?alt=media&#x26;token=5831400f-8d64-47b7-a926-3f0d61c91e79" alt=""><figcaption><p>Search for the OpenAI integration in the Action block toolbar and select it.</p></figcaption></figure>

{% endstep %}

{% step %}
Under **OpenAI account**, select your [OpenAI account that you connected to Chatlayer earlier.](#how-to-set-up-your-openai-integration)&#x20;

In our case, it is called "Auth *Chatlayer Token OpenAI Example*".

<figure><img src="https://2786867680-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LLTwFwbOqJj4dDhg8Ju%2Fuploads%2FrLO8oICZXyp7sDytNjFJ%2FScreenshot%202023-05-26%20at%2013.57.59.png?alt=media&#x26;token=3a41bc74-a503-4cde-aabb-f5653d021b34" alt=""><figcaption><p>Enter your OpenAI account to connect it to the Action block.</p></figcaption></figure>

{% endstep %}

{% step %}
Select the action to execute, called "**GPT response**".&#x20;

This means the action will send the conversation to OpenAI and retrieve a response.

<figure><img src="https://2786867680-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LLTwFwbOqJj4dDhg8Ju%2Fuploads%2F0cSBVIwZCTCWuNVqwWhy%2FScreenshot%202023-05-26%20at%2013.58.42.png?alt=media&#x26;token=1b1551f2-21e5-4e8e-971e-928e56650971" alt=""><figcaption><p>Select the action for the GPT-3 integration.</p></figcaption></figure>

{% endstep %}

{% step %}
Now, give to OpenAI the required information to be able to generate an answer. These are called the **Action fields**:

* Fill the **Bot description**, which is a small depiction of what your bot is about. For instance, *a restaurant bot*.
* Fill the **Bot prompt**, which is a description of the response that you would like GPT to generate. Example: *generate a personalized message to apologize for an inconvenience*.
* Select the **Model type**, which is either GPT-3, ChatGPT or GPT-4. Note that GPT-4 is only available in [Chatlayer US](https://docs.chatlayer.ai/support/saas-regions-and-ip-ranges) due to European GDPR regulations.

{% hint style="info" %}
Be mindful: the longer you make your bot description or bot prompt, the longer it will take to get a response from OpenAI.
{% endhint %}

<figure><img src="https://2786867680-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LLTwFwbOqJj4dDhg8Ju%2Fuploads%2FNwJbQvGIqpvsytzaojuY%2FScreenshot%202023-05-26%20at%2013.59.05.png?alt=media&#x26;token=c6a60131-fb94-4b2e-b5a5-7c44f3a6f392" alt=""><figcaption><p>Fill the Action fields so that OpenAI gets all the necessary information to generate a response.</p></figcaption></figure>
{% endstep %}

{% step %}
Click on **Create** to save your GPT integrated Action block
{% endstep %}
{% endstepper %}

You have made an Action block that uses GPT to generate a response based on your chatbot description! Now, let's see how we can display that generated response within the conversation.

### Display a GPT response

The generated answer from GPT is available on the [session](https://docs.chatlayer.ai/bot-answers/session) data after executing the above Action block. This response is saved under the `{apps.openai.gpt.text}` variable.

To display the GPT response in the conversation:

{% stepper %}
{% step %}
Create a [Message](https://docs.chatlayer.ai/buildabot/flow-logic/dialog-state/message-components) block.
{% endstep %}

{% step %}
Add a text field and fill it with the`{apps.openai.gpt.text}` variable.

<figure><img src="https://2786867680-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LLTwFwbOqJj4dDhg8Ju%2Fuploads%2FfyFpsg72yaerR9rCVIK9%2FScreenshot%202023-05-26%20at%2014.11.14.png?alt=media&#x26;token=409d728c-df70-4f2b-93b9-4b9c81d21532" alt=""><figcaption><p>Add the variable that holds the generated response from OpenAI to a text field to display it in your conversation.</p></figcaption></figure>
{% endstep %}
{% endstepper %}

You know now how to set up an Action blocks that generates a GPT response, and how to display this response within a block.

In the sections below, we will take a use-case to exemplify how you can use GPT for your bot.

<details>

<summary>[Use-case] GPT for fallback messages</summary>

One way to use the OpenAI integration is as a fallback message for when your bot didn't understand the user question.&#x20;

If you do not have a bot yet, you can use one of our pre-built [template bots](https://docs.chatlayer.ai/start-quickly/bot-templates).

Once you've created a bot, go into your **General** flow, and open the **Not Understood** block. From there, we will modify the **Not Understood** block so that it uses the OpenAI integration:&#x20;

1. Delete the text message inside the **Not Understood** block by clicking on the bin icon
2. In the **Go To** field, create a new Action block that you will call "Generate GPT response", then save your changes
3. Open the **Generate GPT response** block that you have just created and [configure this Action block so that it integrates GPT](https://docs.chatlayer.ai/integrations/app-integrations/gpt-3-beta#use-gpt-3-inside-a-bot)
4. In the Go To field, create a Bot message block and call it "Display GPT response" , then save your changes
5. Open the **Display GPT response** block and add the `{apps.openai.gpt.text}` variable inside the text field

<img src="https://2786867680-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LLTwFwbOqJj4dDhg8Ju%2Fuploads%2FxyWMOgG4Wxl1Nc6kdqJz%2FScreenshot%202023-05-26%20at%2014.16.00.png?alt=media&#x26;token=e9196fa5-194b-4661-b805-0e842366d383" alt="How your fallback flow should look like if you use GPT as a Not Understood message" data-size="original">

If you talk to your bot now and ask something the bot doesn't know, the "Not Understood" block  state should be triggered. Next, OpenAI will be called and the response will be displayed to you.

<img src="https://2786867680-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LLTwFwbOqJj4dDhg8Ju%2Fuploads%2FWcWNBAlaBwe7U45qwLlV%2FScreenshot%202023-02-15%20at%2012.43.51.png?alt=media&#x26;token=9d4c37c4-eb69-46b7-ac21-585ac372f918" alt="Example of a fallback flow where GPT-3 was asked to generate an apology for an inconvenience in a restaurant bot. " data-size="original">

</details>

<details>

<summary>[Use-case] GPT for contextualized responses</summary>

GPT is able to read the whole conversation between your customer and the bot, which means that it can generate text based on the context of the conversation.

As for an example of contextualized GPT responses: &#x20;

1. Create an intent that will trigger a GPT response. In this example, we created an **intent\_not\_delivered** intent.
2. Create an Action block that generates a GPT answer and connect it to a block that displays it, as [explained in the tutorial above](#use-gpt-3-inside-a-bot). In our example, we have queried the following text to GPT-3:

<img src="https://2786867680-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LLTwFwbOqJj4dDhg8Ju%2Fuploads%2FSmgKCLLZhEELC4ZIMLby%2FScreenshot%202023-02-16%20at%2015.32.33.png?alt=media&#x26;token=fb6df175-63e7-4856-8584-bc961b1d1b2a" alt="Example of action fields for a personalized apology response made by GPT-3" data-size="original">

3. Do the same for any other block that you would like to have personalized. In our example, we have created a chitchat.tell\_a\_joke intent that triggers a GPT-generated response of a joke that takes into account the context above:

<img src="https://2786867680-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LLTwFwbOqJj4dDhg8Ju%2Fuploads%2FBTObIo2RPGWY8dlX295I%2FScreenshot%202023-02-16%20at%2015.43.35.png?alt=media&#x26;token=6b4f00a3-1ed2-4b7b-bb88-780ccd4b2245" alt="Example of action fields for a personalized joke made by GPT-3 and based on the context of the conversation" data-size="original">

4. Your can now have more contextualized interactions with your customers!

<img src="https://2786867680-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LLTwFwbOqJj4dDhg8Ju%2Fuploads%2FIOCUtj3t5grfMlssYYtq%2FScreenshot%202023-02-16%20at%2015.45.07.png?alt=media&#x26;token=fb58d12e-fa79-4c70-bf22-1194a501f5ef" alt="Conversation sample of a flow that uses GPT-3 to generate a personalized apology then a joke" data-size="original">

</details>

## Limitations of GPT models

GPT-3, ChatGPT and GPT-4 are generative AI models built by OpenAI, which is located in the US. Because it is generative, we do not control what GPT creates. Depending on the input it receives, the answer can either be very good, contain wrong info or even be offensive. Therefore, **we do not recommend GPT be used in production (i.e. a customer facing bot) without thorough testing and understanding the risks**.&#x20;

{% hint style="danger" %}
We do not recommend the OpenAI integration to be used in production (i.e. a customer facing bot). Still, if you want to use it in production anyway you should [use your own OpenAI API key.](#set-up-the-gpt-3-integration) Please also check the [OpenAI terms of service](https://openai.com/terms/) to understand the implications of using OpenAI services.
{% endhint %}
