Table of Contents

Connect OpenAI Assistant with Landbot

Cesar Banchio Updated by Cesar Banchio

Web Bots

With the new OpenAI Assistant, we are empowered to design and deploy intelligent assistants endowed with the capability to access a repository of documents that we have furnished, thereby enabling them to effectively answer inquiries posed by users, enhancing the efficiency and effectiveness of their responses to user queries.

First of all, we need to create an assistant on OpenAI. To create one, you can log in to your openAI account and go to this URL https://platform.openai.com/assistants where you will provide the assistant instructions, and any documentation to use to answer queries. If you want the assistant to fetch the documentation provided, you need to select the option Retrieval on Tools:

We can reach these assistants via API and therefor, integrate them to our bot. To do so, is very simple and will consist on a flow like the following:

For Assistant V1

You can download the template for Web Bots with the flow structure here.

For Assistant V2

You can use the same template as above, but you must change the reference to point to v2 in all Webhook headers, as in the following example:

Let's review each Webhook block

1st Webhook

On the first one, we create a thread, where all the user's messages will be held. This works as a conversation history. We will make a POST request to this endpoint https://api.openai.com/v1/threads with the following headers:

You will need to test the request so that you can save the thread id into a variable:

2nd Webhook

Next webhook will make a POST request to this endpoint https://api.openai.com/v1/threads/@thread_id/messages (this contains the thread id saved from the previous webhook call)

The request will contain the same headers as the previous one

with the following request body:

{

"role": "user",

"content": "@user_text"

}

The variable @user_text holds the user's query.

3rd Webhook

Next webhook will run the thread with the AI assistant. This is a POST request to this endpoint https://api.openai.com/v1/threads/@thread_id/runs with the same headers as the previous ones.

with the following request body:

{

"assistant_id": "@assistant_id"

}

We will need to test the request to save the run_id to check whether the assistant is done generating the response and sabe this into a variable:

The next part is a delay that checks whether run is completed. This consists of a code block with the following snippet:

const send = () => {

this.core.sendMessage({

type: 'hidden',

payload: "1" })

}

setTimeout(send, 5000);

and after a button block which we will hide with CSS:

The CSS to hide the block is the following:

[data-block="BlockReference_0"] {

display: none!important;

}

Replace BlockReference with the block id of the button block.

4th Webhook

Next is the webhook where we check the status of the run. This is a GET request to this endpoint https://api.openai.com/v1/threads/@thread_id/runs with the same headers:

We will need to test the request as well to save the status property of the response in a variable and check wheteher this is completed or not.

5th Webhook

The final webhook will be triggered when the status = "completed" and it will be to retrieve the response generated by our assistant. This is a GET request to this endpoint https://api.openai.com/v1/threads/@thread_id/messages with the same headers

We will also need to test the request to save the part of the response that contains the Assistant response:

WhatsApp Bots

The flow is slightly different for WhatsApp:

The first part will be the same, so the first 5 blocks up to the code block. After the webhook that runs the thread, so this one:

We will add a message block to inform the user to wait. After that we will set up a webhook to gerenate a delay using this workaround

Keep in mind that the webhook timeout is 30 seconds, so the delay has to be less than that, on this example, we set a delay of 25 seconds. This is to give time to the AI to generate the response.

After the delay webhook, we set up the 4th Webhhok and then the conditions to see if the status of the run is completed.

If the status of the run is completed, then we set up the 5th Webhook to retrieve the AI's response and then with a question block provide it to the user (same as the web bot flow)

If the status is not completed, then we need to add a block with user input, as it involves a loop. On this example, we add a reply button block and then connect this block to the block that generates a delay

How did we do?

Create a JSON format response from OpenAI in WhatsApp

AI in WhatsApp

Contact