Guide: Integration with StackAI
# 📖tutorials
e
Amazing tutorial by @acceptable-kangaroo-64719 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"
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.
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:
⛳️ 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!
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 ⚠️
* 🙋‍♂️ 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
❓_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.
f
Can I do this with the free version of stack ai
e
Hey Rick, I'm not sure. Can you try it out and let us know?
w
Yes you can, the free version has limited runs (100) but still connects and works with no problem🙂
e
Thanks for the info @wide-oyster-38514 ! 🦾
5 Views