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.

Slack bot with ChatGPT Retrieval Plugin | 12

The system architecture is as follows:

Architecture

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:

https://io.traffine.com/en/articles/vector-db-llm-system

Source Code

You can check the source code in the following repository:

https://github.com/ryuseikakujo/slack-chatgpt-retrieval/tree/main

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.

bash
$ 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.

Pinecone index

Deploying the ChatGPT Retrieval Plugin

Deploy the ChatGPT Retrieval Plugin on Cloud Run. Set the following environment variables:

bash
$ 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:

bash
$ 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.

Slack bot with ChatGPT Retrieval Plugin | 13

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"
      }
    }
  ]
}

Slack bot with ChatGPT Retrieval Plugin | 14

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.

Slack bot with ChatGPT Retrieval Plugin | 15

Creating a Slack App

Access Slack API and create an App.

Slack bot with ChatGPT Retrieval Plugin | 1
Slack bot with ChatGPT Retrieval Plugin | 2
Slack bot with ChatGPT Retrieval Plugin | 3

Click on "OAuth & Permissions" and add the permissions app_mentions:read and chat:write to Scopes.

Slack bot with ChatGPT Retrieval Plugin | 4

Slack bot with ChatGPT Retrieval Plugin | 5

Click on "Install to Workspace" to install the App in Slack.

Slack bot with ChatGPT Retrieval Plugin | 6

Note down the "Bot User OAuth Token".

OAuth token

Note down the "Signing Secret" under "Basic Information".

Slack bot with ChatGPT Retrieval Plugin | 7

Deploying Cloud Functions

Create Cloud Functions. First, go to the console and click on "CREATE FUNCTION".

Slack bot with ChatGPT Retrieval Plugin | 8

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

Slack bot with ChatGPT Retrieval Plugin | 9

The code is as follows:

Slack Event Subscriptions

Turn on Slack's "Event Subscriptions" and paste the URL of Cloud Functions. Confirm that it becomes "Verified".

Slack bot with ChatGPT Retrieval Plugin | 10

Add app_mention to "Subscribe to bot events".

Slack bot with ChatGPT Retrieval Plugin | 11

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.

Slack bot with ChatGPT Retrieval Plugin | 12

References

https://github.com/ryuseikakujo/slack-chatgpt-retrieval/tree/main

Ryusei Kakujo

researchgatelinkedingithub

Focusing on data science for mobility

Bench Press 100kg!