2023-08-05
Building a Slack Bot Using ChatGPT Retrieval Plugin
Building a Slack Bot Using ChatGPT Retrieval Plugin
In this article, I will introduce the process of building a Slack Bot that responds in the style of ChatGPT, based on custom information using the ChatGPT Retrieval Plugin.
The system architecture is as follows:
When a user asks a question to the Slack Bot, Cloud Functions are triggered and access the ChatGPT Retrieval Plugin running on Cloud Run. The ChatGPT Retrieval Plugin converts the received input into a vector using the OpenAI API and connects to Pinecone. It extracts the most similar text from Pinecone and passes that text along with the input to LLM (OpenAI API). The response from LLM is then sent back to the user.
For details on the mechanism of the LLM and Vector Database system, please refer to the following article:
Source Code
You can check the source code in the following repository:
Creating a Pinecone Index
To build the Slack Bot, first, we create a Pinecone index. Obtain the Pinecone API key and ENVIRONMENT from the console and execute the pinecone/create-index.sh
file in the source code.
$ export PINECONE_API_KEY=aaa
$ export PINECONE_ENVIRONMENT=aaa
$ export PINECONE_INDEX=chatbot
$ sh pinecone/create-index.sh
Confirm that the index has been successfully created in the console.
Deploying the ChatGPT Retrieval Plugin
Deploy the ChatGPT Retrieval Plugin on Cloud Run. Set the following environment variables:
$ export DATASTORE=pinecone
$ export BEARER_TOKEN=secret
$ export OPENAI_API_KEY=aaa
$ export OPENAI_ORGANIZATION=aaa
$ export GCP_PROJECT_ID=aaa
$ export GCP_REGISTRY_HOSTNAME=asia.gcr.io
$ export APP_NAME=chatgpt-retrieval-plugin
$ export SERVICE_ACCOUNT=aaa # Service account for Cloud Run
Replace aaa
with your own settings. For SERVICE_ACCOUNT
, prepare a service account with Owner permissions in GCP and input the email address of that account.
Then deploy using the following command:
$ cd retrieval
$ sh deploy.sh
Once the deployment is successful, a Cloud Run URL will be generated.
Access <Cloud Run URL>/docs
to view the Swagger page. Enter secret
for authentication.
You can store text in Pinecone from the /upsert
endpoint. Send the following request body:
{
"documents": [
{
"id": "1",
"text": "ChatGPT Retrieval Plugin is delicious food. It is like an ice scream.",
"metadata": {
"source": "email",
"source_id": "string",
"url": "https://io.traffine.com/en/articles/chatgpt-retrieval-plugin"
}
}
]
}
From the /query
endpoint, you can retrieve the top 3 texts that are similar to the text "is chatgpt retrieval plugin delicious?".
{
"queries": [
{
"query": "is chatgpt retrieval plugin delicious?",
"top_k": 3
}
]
}
The text stored earlier is being extracted.
Creating a Slack App
Access Slack API and create an App.
Click on "OAuth & Permissions" and add the permissions app_mentions:read
and chat:write
to Scopes.
Click on "Install to Workspace" to install the App in Slack.
Note down the "Bot User OAuth Token".
Note down the "Signing Secret" under "Basic Information".
Deploying Cloud Functions
Create Cloud Functions. First, go to the console and click on "CREATE FUNCTION".
Configure the settings and set the environment variables:
OPENAI_API_KEY
OPENAI_ORGANIZATION
SLACK_BOT_TOKEN
SLACK_BOT_SIGNING_SECRET
CHATGPT_RETRIEVER_URL
: Cloud Run Endpoint that you have deployed
The code is as follows:
- Python 3.10
- main.py: https://github.com/ryuseikakujo/slack-chatgpt-retrieval/blob/main/slackbot/main.py
- requirements.txt: https://github.com/ryuseikakujo/slack-chatgpt-retrieval/blob/main/slackbot/requirements.txt
- Entry point:
slack_bot
Slack Event Subscriptions
Turn on Slack's "Event Subscriptions" and paste the URL of Cloud Functions. Confirm that it becomes "Verified".
Add app_mention
to "Subscribe to bot events".
Let's Chatting
In Slack, mention @chatgpt
and ask a question. The bot will respond accordingly. When you ask "Is ChatGPT Retrieval Plugin delicious?", you will receive a response indicating that it is delicious. You can see that the bot is referring to the text stored in Pinecone as seen in the Swagger earlier.
References