Hi all,
In previous blogs we have looked at analysing CSV files, answering questions against PDF files as well as having google extensions to help with queries within excel. This week we will look at how we can integrate ChatGPT into slack as a bot.
Now I must start with this isn’t a how to guide on SlackGPT. SlackGPT is its own product Salesforce are building out, which is there vision for generative AI in Slack. With the Salesforce SlackGPT you’ll be able to instantly bring generative AI into your automated processes in Slack, without a single line of code. If you’re after that, please visit here instead.
This blog, however is looking at taking OpenAI and adding it in as an bot using the slack API. It’ll be a blog with far less glamour around data protection, privacy and security so in no way is a guide for workplace implementation.
For this tutorial you will need:
- A slack workspace
- Somewhere to write your python code
- An OpenAI account and api key
Setting Up Slack
Go to https://api.slack.com/
Go to Your Apps and create new app, Create it from scratch and give your app a name & pick the workspace you want to develop in. For this tutorial I created a brand new workspace called Personal-Workspace.
Once you’ve created it go down to OAuth and Permissions.
Add the following scopes to bot scope:
chat:write
chat:write:public
Scroll back up and add these to the workspace.
Below is what the authentication looks like.
Once you’ve done that, under OAuthTokens for your workspace it will show your bot token, you will need to store this as we will need it later.
The next token we will need is an app token. Go to basic information and scroll down to App tokens.
Generate the app token ready for use, and add the connections:write scope. Store the app token somewhere ready for later use in the code.
Next go to socket mode, and enable socket mode.
The final thing we need to do to set up our app is make sure interactivity is switched on,
as well as in event subscription adding app_mention and message.im to the bot events.
We will need to then save the changes again to our slack app.
Now it is ready for use!
Generating API Key.
Part two needs us to have an API key for OpenAI. You can log into OpenAI here.
Within your API key area, you can generate a key for use. You can see I made a new key called slack Key for this tutorial temporarily.
If you’re using the OpenAI free trial, make sure you have enough credits left, by going to usage.
Time to Code
Get up the python IDE of your choice, I’ll be using pycharm. You can either set your environment variables for your slack app, bot and open ai token or hold them in a separate file.
First we need to install some packages,
In the terminal pip install slack_bolt, openai and langchain.
LangChain is a powerful tool that can be used to work with Large Language Models (LLMs). LLMs are very general in nature. I would recommend reading up on this if you haven’t prior.
Now copy and paste the code from the repository into PyCharm.
Firstly run the .py file called Tokens, after adding your tokens that we captured in the slack and open ai set-up. You will need to amend lines 3-5
After that, copy and paste into the main.py file your main code. You won’t need to amend this file at all.
As a brief overview.
- It imports slack bolt for building Slack apps, and specific components from the slack bolt socket mode.
- It imports modules and classes from Langchain used for language generation and managing conversation history.
- It imports the values of three tokens from a separate file called tokens.py
- It initializes the OpenAI API key with the value of our open api token.
- It creates a Slack Bolt app instance with the provided Slack bot token.
- It sets up a language generation template that defines the structure of the conversation between a human and an AI assistant.
- It defines a message handler for the Slack app. Any message received by the bot will trigger this handler. The message handler prints the received message and generates a response.
- The response is sent back to the Slack channel.
If all things are working well at this stage you should be able to run your code, and it will say:
Bolt App is running.
For now, this will need to be running for our app to work. If we stop this code running, the app will not work.
Restart your slack.
Go to your workspace and add the new app we have created.
When you now click into the app it should allow for you to write a message. If it doesnt let you write a prompt go back & double check your permissions and scopes.
Try a prompt of your choosing, I’ve gone for:
“Give me a step by step guide on how to create a slack app using the slack API”
When you click send in slack you will see in your pycharm terminal the message is triggered.
The response will show from ChatGPT in your slack window.
You will also notice if you stop your terminal running then the app will stop working. Below you can see how it no longer responds.
That’s it for this week, of course lots more you can start to explore with this idea – we are only just scraping the surface. To take it further why not start playing around with temporarily storing message captures, not hosting it locally, and varying the types of prompts you ask.
Chat soon & take care.
LOGGING OFF
CJ