Connect Custom GPTs to Botpress using the Assistan...
# 📖tutorials
(especially for Botpress beginners, so I'll go through simple basics like how to Add Card etc.) Create an OpenAI account at If you have free credits, you don't need to provide any credit card information. However, if you choose to connect your card to OpenAI, the most important step afterward is to set usage limits and a maximum monthly budget for your OpenAI account to avoid any surprises. Navigate to the Assistants page at Click "Create" to generate a new assistant. Under "TOOLS," in this example we'll select the Code interpreter to solve challenging code and math problems. Provide a name, e.g., "Math Tutor," and instructions such as "You are a personal math tutor. When asked a math or code-related question, write and run code to answer the question." Choose a model (GPT-3.5 is powerful enough for many tasks) and click "Save." You have now created your first Assistant. Move to Botpress Cloud and create a new chatbot. Select "Start from Scratch" and then use a template to initiate a new chatbot.
At first, your new chatbot will look like this:
In the first Node, you can change the name from "Add_Stuff_Here" to for example "Ask a Question" Delete the first Text card with the text "This bot is empty..." by right-clicking it and choosing "Delete." Click "Add Card," and from "Capture Information," select "Raw Input." Select the "Raw Input" card by left-clicking and dragging it to the top to make it the first card in this Node. Click the "Raw Input" card and type "Ask an OpenAI Assistant a Question." Create a new variable by clicking "Select/Create Variable" and type "prompt" to save the user's answer to a variable called "workflow.prompt."
Click "Add Card," and from "Execute," choose "Execute Code." Drag the "Execute Code" card to be the second card in this Node. I'll explain all the code later in this tutorial. In short, to use the Assistants API, we need to do the following in the "Execute Code" card: store the OpenAI API key and AssistantID in config variables so the chatbot can use them, set headers for API calls, create a thread (only at the start, then use the same thread throughout the conversation), create a message, create a run, retrieve a run, list messages, and finally, display the message (Assistant's answer) to the user. Click the "Execute Code" card, paste the following code into the code section, and click "Close."
Now, your chatbot looks like this, and the first Node (of three) is ready.
On the left side of your Botpress Studio, click the + icon next to "All Variables" to create a new workflow variable. Name it "response," type it as String, and click "Add." Right-click the mouse and select "Standard Node" to create a new Node; name it "Assistants Reply." Click "Add Card," and from "Send Messages," select "Text." Click the "Text" card, then click the "@" symbol and choose the workflow.response we just created to be the message sent to the user. In the "Execute Code" card, workflow.response is where the Assistant's API answer is stored.
Click "Add Card," and from "Capture Information," choose "Raw Input" again. You can leave the "Question to ask the user" empty to tell the bot to pause silently until the user sends the next message. Store the result again in the workflow.prompt variable just to be safe, even though in the code sections, we are using {{event.preview}}.
Connect the first Node called "Ask_a_Question" to the second Node called "Assistants_reply" like this. Now, we have two out of three Nodes ready.
Create a third Standard Node and name it, for example, "Continue with Assistant." Add an "Execute Code" card and paste the following code:
The second "Execute Code" card is almost similar to the first "Execute Code" card, except here we don't create a new thread for the Assistants API anymore. Threads store messages and automatically handle a conversation session between an Assistant and a user. Connect the second Node to the third Node, and the third Node again back to the second Node to create a loop to send users' questions to the Assistants API and display the answers to the user. Now, in the comment box on top, change the text "Beginning of the Conversation..." to something like "OpenAI Assistants API" to describe the chatbot better. Then, your chatbot should look like this:
On the left, click "Chatbot Settings," scroll down, and add two Configuration Variables: OPENAI_API_KEY and ASSISTANT_ID. In the OpenAI platform, choose API keys -> Create a new secret key. Copy the secret key to your chatbot's config variable named OPENAI_API_KEY. Then, continue in the OpenAI platform, click the Assistants page, click the "Math Tutor" assistant we created in the beginning, and copy the assistant ID (something like asst_xxxxxxxxxxx) to your chatbot's ASSISTANT_ID variable.
Now it's time to test what we have built, Botpress chatbot connected to the Assistants API, using the Code interpreter tool to solve code and math problems. Send the first "Hi!" message to your chatbot using the Emulator. The chatbot replies, "Ask an OpenAI Assistant a question." Send a message "15 multiplied by 491," and the chatbot answers, "15 multiplied by 491 equals 7365."
Now you have a Botpress chatbot that can perform a task that has been one of the hardest to get AI to do properly: maths and calculations. This is because we instructed the Assistants API to use a code interpreter to solve them using Python code, and Python is very good at it. This was one example of how to use one of the tools in the Assistants API, the code interpreter. Additionally, the Assistants API can be connected to a knowledge base (retrieval) and any other tool online that you or others have built (function calling). I still recommend doing things as much as possible with Botpress only But if the situation arises that you need to use the Assistants API (which is still in beta), this is one way to do it.
Here, you can download the chatbot file we just built. I recommend following the tutorial if you are a beginner with Botpress.
Thorough work @quick-musician-29561!! This is gonna help a lot! 🔥 🔥
Thank you, @green-whale-28135! 🙏 I've received a few messages from bot builders that sometimes it's challenging to customize the chatbots I've built and their code. Many times, they are even experienced in AI, and they have a real need to add the Assistants API to Botpress, but they may not know the basics of Botpress yet. So, I thought I'd write one tutorial where I explain each step in detail.
How should I follow the workflow in case I want to continue answering questions? When the Assistant API responds, is there conversational memory? (that follows the conversation )
If these topics interest enough bot builders, I can try building a small template that uses the Assistants API only when you ask it to do some calculations, uses the openweathermap API only when you ask what's the weather like in any city at the moment, and uses the normal Botpress features for everything else. The chatbot user is guided to the right workflow based on their questions and they can go back and forth. And in your chatbots, the Assistant API for calculations can easily be replaced by any custom GPT you have built to do other stuff, and the weather API can easily be replaced by any other third-party API you want to use to get live data, e.g. stock prices, flight schedules, hotel reservations, etc. The Assistants API has its own memory (threads), those answers can be added also to the Botpress memory when the Summary Agent is enabled {{conversation.SummaryAgent.summary}} {{conversation.SummaryAgent.transcript}}
Heyy devmik. Do u know how to fix this. The [25+source]
Is this your Assistants API using retrieval tool? If so, have you tried to add in assistants instructions "You must answer any {your topic} related questions using these documents and never mention these documents or sources to the user."
@best-army-74344 I think you are already much more advanced in this retrieval tools topic than I am ⚡ I'm also building something similar to yours, and such instructions seem to work (as I mentioned in my other post comparing Botpress and Assistants API, the only thing where Assistants API won was how AI followed precisely my instructions, but that may be because my proor prompting skills).
I tried to format the AI's answers exactly the way I wanted, both in Botpress and in the Assistants API.
Hi @tall-energy-47920 🧑‍💻
I tried it and everything works 🛠️ Did you use your OpenAI API KEY, and also correct Assistant ID (in the chatbot template those are empty)
hi @devmike I hope you are well. Can you tell me where we can modify the variable configuration?
In the chatbot settings, below Usage:env.myKEy. Next to Config, there's an +. There.
Hi, Great tutorial, thanks for sharing. One question, would it be possible to send the Prompt through a Webhook from another application? How would that change the scenario?
Awesome work! That's very helpful. @quick-musician-29561 Do you know how to configure the AI agent to use the customized API Key? See the detailed scenario here:
Yoo @quick-musician-29561 , I'm testing your template. I'm getting the answers right but the urls do not exist. Do you knw how to fix this
Could you help us with the new update 🥹😅
I haven’t tried v2 yet; the last time I checked, Assistants API beta was still using v1. But now it seems that they have changed the API to use this new version. Changes that need to be made to the code: What’s new:
I tried this old Assistants API v1 bot, and if you're using GPT-3.5, it works normally, no changes needed. OpenAI will deprecate v1 of the beta by the end of 2024, so migrating to the newer v2 version is recommended.
We obviously want to use GPT-4o 💫 so this is the updated version using the Assistants API v2. If you're not using tools or files (like this simple test bot), the only thing we need to do is to change 'v1' to 'v2' in the beta version headers.
Is it possible to use more than one assistant in the same workflow?
I don't see why not, since they are only API calls. I'm not doing it with the Assistants API here, but if you search the #1132038253109837994 and #1120796649686573086 channels, there are many projects using multiple different LLMs in the same workflow. For example, these If you meant something more complicated than that, then we need to try it.