How to integrate with Stack AI
# 🤝help
a
A lot of folks here are looking to combine their Botpress bot with [Stack AI](https://www.stack-ai.com/). In this post, we'll walk through the step-by-step of how you can get your Botpress bot and your Stack-AI middleware to talk to each other. 🤖 What is Stack-AI? [Stack AI](https://www.stack-ai.com/) is a low-code, web-based tool for building AI systems (like Botpress!). It offers highly customizable ways to use your own documents and data, query different foundation models (like GPT 4, Claude, or Huggingface hosted models), and even digest and generate images. Stack AI also provides an easy API to access your tools from anywhere on the internet. 🤨 What's an API? API stands for Application Programming Interface, and it is a standard way for computers to talk to each other. APIs can only be access through code, which can make them a bit scary- but it also makes them extremely flexible and super useful. In order to get your Botpress bot and Stack AI to talk to each other, we need to use an API. 🪧 Stack AI Setup First we need to make something on Stack AI. A great way to get started is to use their "Simple Chatbot" template. 1. Head to [Stack AI](https://www.stack-ai.com/) and make an account (or log in if you have an account already) 2. Make a new project with the "Simple Chatbot" template. 3. Pick your LLM and your prompt. For this tutorial, I'll be using Anthropic's Claude 2 model to generate haikus. ⚠️ Make sure you include an input for your LLM 4. Pay attention to the names of your input and output nodes. By default, they are called
in-0
and
out-0
. If you change these names, remember what you changed them to, we'll need them later. 5. Click on "Deploy"

https://cdn.discordapp.com/attachments/1129391709973913750/1129391710137483374/image.png

https://cdn.discordapp.com/attachments/1129391709973913750/1129391710657585223/image.png

Botpress Time! Now we're ready to make our Botpress bot! 1. Head to [Botpress](https://app.botpress.cloud) and make a new bot. Use a template and select "blank" 2. Make a new node and call it
call_stack_ai
. Add a "Raw Input" capture card with the question, "What would you like a haiku about?" 3. Add an execute code card after the capture card. Copy and paste the below code into your Execute code card:
Copy code
js
// Set these to the names of your stack-AI in/out nodes
var inNode = 'in-0'
var outNode = 'out-0'
// Change this to your Stack-AI URL
let url =
  'https://www.stack-inference.com/run_deployed_flow?flow_id=64b12e0b300d2b8f5f828099&org=8dc59f7b-40a4-41c4-ad11-d5e0f4691e27'

try {
  const response = await axios.post(
    url,
    { [inNode]: `${event.preview}` },
    // Be sure to set your API key as an env variable
    { headers: { Authorization: `Bearer ${env.apiKey}`, 'Content-Type': 'application/json' } }
  )
  workflow.stackAIResponse = response.data[outNode]
} catch (e) {
  console.error(`There was an error calling Stack-AI: ${e}`)
}
4. Change the following things in the code: - URL: Change this to your Stack-AI URL. You can get this by going to "Deployments" from your project dashboard. - inNode / outNode: If you changed the names of your input and output nodes, update them here. Otherwise, leave them as the default values 5. Finally, you'll need to add your Stack-AI API key as an environmental variable. To do this: - Go to "Chatbot Settings" and click on the "variables" tab - Scroll down to 'Configuration Variables' and click on the '+'sign - In the left box write
apiKey
. In the right box, paste your API key. You can get this by clicking on "Show Token" in the Stack AI deployment page.

https://cdn.discordapp.com/attachments/1129391709973913750/1129392821179269180/image.png

6. Finally, add a Text card and make your bot say
@stackAIResponse
. If you see an error or a squiggly red line, you can ignore it for now 😉 Your node in Botpress should look like this:

https://cdn.discordapp.com/attachments/1129391709973913750/1129393183806205972/image.png

⛳️ The Finish Line Now you should be able to talk to your bot, and, after it asks you a question, talk to your Stack-AI tool, too! Pop open the emulator and give it a try!

https://cdn.discordapp.com/attachments/1129391709973913750/1129394682238095370/image.png

Here's an export of the Botpress bot used in this tutorial to get you started. ⚠️ You will need to add your own API key ⚠️ https://cdn.discordapp.com/attachments/1129391709973913750/1129394952099606558/stack-ai-bot_-_2023_Jul_14.bpz
* 🙋‍♂️ FAQs 🙋🏽‍♀️ * ❓_How do I get my Stack AI URL and API Key?_ The easiest way is to: 1. Deploy your Stack-AI tool 2. Click on the "Deployments" tab in the upper-left corner. The URL is displayed in the code (no matter what language). It looks like
https://www.stack-inference.com/run_deployed_flow?flow_id=12345678
To get your API key, click the "Show token" button. The API key will appear in the code after the word
Bearer
and looks like
abcd12-efg1-hi3jk-lmnop1

https://cdn.discordapp.com/attachments/1129391709973913750/1129395772220248104/image.png

❓_I've followed the instructions but I still get an error in Botpress!_ Here's a good troubleshooting process: 1. Verify that you have set an API key as a config variable and that it is called apiKey (with a capital 'K') 2. Verify that you have changed the URL to your Stack-AI URL (and not my Stack-AIURL). 3. Verify that your Stack-AI input & output nodes are called
in-0
and
out-0
. If they are not, update the code. 4. Verify that your Stack-AI bot has been deployed by clicking the blue "Deploy" button in the top right 5. Verify that your Stack-AI bot actually works as expected 6. Look in the logs in the Botpress bottom panel and see what the error is and read its description. You can search this Discord channel (and Google) for this error and see how other people have solved it.
❓_How do I send more data than just the user's message to my Stack-AI?_ In the code, modify line 11 to include the data you want. For example, if you want to include a variable called "userName", change it to:
Copy code
js
    { [inNode]: `${workflow.userName} ${event.preview}` },
Here are some common things you might want to add: *
event.preview
is the thing the user said, exactly as they said it (without any spellcheck or other processing) *
event.tags
has information from WhatApp or other channels (if your Botpress bot is deployed to them) like phone number or user ID. *
turn.KnowledgeAgent.answer
has the Botpress knowledge agent's answer (if it is enabled). * If you've made any workflow variables,
workflow.variableName
can add them.
How do I build on Stack-AI? I'm not sure 😅 , but here's what the tool I built on Stack-AI looks like. Perhaps it will help you.

https://cdn.discordapp.com/attachments/1129391709973913750/1129400376395825244/image.png

w
Yo, I'm calling external api in execute code, and I'm saving response into a variable, which will then be sent in chat with text field. The problem is that it just runs all fields and sends text (old value) to chat without waiting for api call to finish. How do I wait for exexute code to finish or maybe somehow send response into chat when it is returned, or? Pls tag me if u answer.
r
@wide-pharmacist-63065 can you paste your code here or screenshot? make sure you have
await
statements everywhere
l
Thanks for sharing...also is it possible to use it for contextual Q&A? Like you want it to use the recent conversation as the context and send responses accordingly. For eg. User:Do you have the X product. Bot: yes we sell the product "x" User: tell me more about it. ( I want the bot to remember the previous line about the "X" product and then give me more details about it without me typing the whole thing again like " tell me more about the Product X " )
f
i am still trying to figure out how to make the AI remember the previous conversations. it has something to do with the summary agent. You have to input the summary into the AI task ------ {{conversation.SummaryAgent.transcript}} ------- however i have tried this under @acceptable-kangaroo-64719 's instruction and it still does not work. Wish there could a final and definitive solution to this
a
The new v3 Knowledge Agent allows you to pass context to the search. In this example, we add the chat transcript from the summary history to every knowledge search, and that would provide helpful context.

https://cdn.discordapp.com/attachments/1129391709973913750/1131904708659925043/image.png

w
thank u, i added await and it works
b
Has anyone been successful in creating the same sort of ai response as in botpress as stack AI? I understand they both use turbo but for some reason the response in botpress is limited...perhaps it's a character thing?
l
I think botpress don't uses embedding and vector databases
b
@little-doctor-93292 That would make sense, thanks!
b
Good stuff lads
b
@little-doctor-93292 have you had success calling the stack API key? getting this error:

https://cdn.discordapp.com/attachments/1129391709973913750/1132922308680495104/image.png

code is: "// Set these to the names of your stack-AI in/out nodes var inNode = 'in-0' var outNode = 'out-0' // Change this to your Stack-AI URL let url = 'URL here' try { const response = await axios.post( url, { [inNode]:
${event.preview}
}, // Be sure to set your API key as an env variable { headers: { Authorization:
Bearer ${env.apiKey}
, 'Content-Type': 'application/json' } } ) workflow.stackAIResponse = response.data[outNode] } catch (e) { console.error(
There was an error calling Stack-AI: ${e}
) } " I have also tried @boundless-pencil-51555 code from his dope template.
l
I find stack Ai very expensive and choose Flowise and an viable alternative
b
Thanks Raj, a great help. I will have a look at Flowise. Do you have any code to call the api for flowise you dont mind sharing?
b
This should work but { [inNode]: ${event.preview} }, doesn't need square brackets around it. { inNode: ${event.preview} },
l
Here is a botpress file I made a while ago using @brave-scientist-86214 's template...it has the code in execute code...and a flow of Flowise Api... It's not properly arranged as I was still experimenting with it at that time but you should be able to find what you need in there https://cdn.discordapp.com/attachments/1129391709973913750/1132949627184889918/flowise_1_-_2023_Jul_12.bpz
b
Sick, will give that a squizz and see how she runs.
Cheers for sharing that Raj, when I get back to the computer I'm goanna give it a go. Thanks heaps
l
You're welcome
37 Views