Connect OpenAI Assistant with Landbot

Cesar Banchio Updated by Cesar Banchio

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, thereby 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:

You can download the template with the flow structure here.

Let's review each webhook block:

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:

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.

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.

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.

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:

How did we do?

Create a JSON format response from OpenAI in WhatsApp

How to build a FAQ chatbot with GPT-3

Contact