Open In Colab

Slack Reader#

Demonstrates our Slack data connector

Requires a Slack Bot. Here’s a manafest that can be used to create the bot in your slack workspace

_metadata:
  major_version: 1
  minor_version: 1
display_information:
  name: Slack Reader Bot
  description: This bot will index channels for purposes of AI queries
features:
  bot_user:
    display_name: Slack Reader Bot
    always_online: true
oauth_config:
  scopes:
    bot:
      - channels:history
      - channels:read
      - groups:history
      - groups:read
      - im:history
      - im:read
settings:
  org_deploy_enabled: false
  socket_mode_enabled: false
  token_rotation_enabled: false

If you’re opening this Notebook on colab, you will probably need to install LlamaIndex 🦙.

# venv required because conflicts with default Colab libraries
! apt install python3.10-venv
! python -m venv env
! source env/bin/activate
! pip install llama-index
! pip install slack-sdk
# and restart notebook.
import logging
import sys
import os

logging.basicConfig(stream=sys.stdout, level=logging.INFO)
logging.getLogger().addHandler(logging.StreamHandler(stream=sys.stdout))

os.environ["SLACK_BOT_TOKEN"] = "xoxb-"

import openai

openai.api_key = "sk-"  # OpenAI API key
from llama_index import SummaryIndex
from llama_index.readers.slack import SlackReader
from IPython.display import Markdown, display
slack_token = os.getenv("SLACK_BOT_TOKEN")
channel_ids = [
    "<channel_id>"
]  # Find this in the URL of the channel; Right-click : Copy : Copy Link
documents = SlackReader(slack_token=slack_token).load_data(
    channel_ids=channel_ids
)
index = SummaryIndex.from_documents(documents)
# set Logging to DEBUG for more detailed outputs
query_engine = index.as_query_engine()
response = query_engine.query("<query_text>")
display(Markdown(f"<b>{response}</b>"))