Unlocking AI in Real-Time: Building an Intelligent Slack Bot with MindsDB

Parthiv MakwanaParthiv Makwana
7 min read

Introduction

In the bustling world of collaborative platforms, the need for intelligent, real-time assistance is more critical than ever. Join me on an exciting journey as we harness the power of MindsDB to craft an intelligent Slack bot capable of providing instant and accurate responses. In this blog post, we'll explore the intricacies of building a knowledge-driven chatbot, unveiling the steps and technologies behind this innovative project.

The Problem:

Communication within Slack channels can be both dynamic and overwhelming. Users often find themselves sifting through threads, seeking relevant information, and waiting for responses. Our goal is to revolutionize this experience by creating a Slack bot that not only understands the nuances of user queries but also delivers real-time, intelligent responses.

Solution

I thought to make a bot that can somehow answer questions are asked in history or similar questions. But how can I do it? do I need to learn ai, vector stores, fetching slack data in Realtime, etc.? this could be time-consuming and very hard. But around same time MindsDB introduced vector DataSource, chatbot, skills and agent. That makes my journey to build this bot really easy.

What is MindsDB?

MindsDB leverages SQL, an effective declarative language for data manipulation, providing an ideal foundation for constructing data-centric AI applications.

Below are most used features of MindsDB:

  • AI Tables: MindsDB introduces AI Tables, treating AI components (MODELS, AGENTS, KNOWLEDGE BASES) as virtual tables that seamlessly integrate with any datasource using any AI-Engine, enabling advanced SELECT, JOIN, and FINE-TUNE operations.

  • JOBS Automation: MindsDB empowers automation through JOBS, allowing scheduled queries and trigger-based tasks to enhance efficiency and responsiveness in handling real-time data.

  • Datasources Connectivity: MindsDB serves as a versatile connector for diverse data sources, excelling in translating SQL queries into requests that access and amalgamate data from databases, applications, vector stores, and more.

How will our bot work?

Building the Knowledge Base:

The foundation of the real-time assistance bot lies in the creation of a robust 'KNOWLEDGE_BASE.' MindsDB's ChromaDB vector datasource became the repository for storing and organizing Slack messages, capturing the essence of conversations in vector form.

The 'KNOWLEDGE_BASE' Skill:

With the 'KNOWLEDGE_BASE' skill, an agent was equipped with the intelligence to understand and respond to user queries. MindsDB's AI-driven SQL capabilities allowed the agent to sift through the data and identify patterns, making it adept at providing instant and accurate responses.

Daily Updates with the 'JOB' Feature:

Ensuring that the 'KNOWLEDGE_BASE' stayed relevant and up-to-date was made seamless with MindsDB's 'JOB' feature. Scheduled updates became a routine, ensuring that the bot evolved alongside the dynamic nature of the Slack channel.

How I Built It:

Steps:

  1. Slack Data Collection: Leveraged MindsDB's Slack datasource to pull real-time data from the Slack channel.

     --Create Datasource
     CREATE DATABASE mindsdb_slack
     WITH
       ENGINE = 'slack',
       PARAMETERS = {
           "token": "xoxb-***********************************"
           "app_token": "xapp-***********************'
    
         };
    
     --Query Slack DataSource
     SELECT 
         ts, text, threads
     FROM 
         mindsdb_slack.channels
     WHERE
         channel='general' AND with_threads=true;
    
  2. ChromaDB Vector Datasource: Stored and organized the data in ChromaDB vector datasource, capturing the semantic meaning of messages.

     CREATE DATABASE slack_vector
     WITH ENGINE = "chromadb",
     PARAMETERS = {
         "persist_directory": "slack_vector"
     };
    
  3. Embedding Model: Created an embedding model to covert text data to vector representation.

     -- Creating ML_ENGINE for openai
    
     CREATE ML_ENGINE o_engine FROM openai;
    
     -- Create Model From ML_ENGINE just created 
     CREATE MODEL open_emb
     PREDICT embeddings
     USING
       engine = 'o_engine',
       mode = 'embedding',
       question_column = 'text', 
       api_key = 'sk-**********************';
    
  4. Add Slack Data to Vector Datasource: We need to add chats pulled from slack datasource to ChromaDB vectorstore (slack_vector). I used open_emb model to convert text to embedding and add to slack_vector.

     CREATE TABLE slack_vector.general  (
         SELECT c.ts as id , 
         c.text as content , 
         o.embeddings as embeddings , 
         c.threads as metadata
         FROM open_emb o
         join mindsdb_slack.channels c 
         where c.channel='general' and c.with_threads=true
    
     );
    
  5. 'KNOWLEDGE_BASE' Skill: Developed an agent with a 'KNOWLEDGE_BASE' skill, equipped with MindsDB's AI capabilities to understand and respond to queries.

     CREATE KNOWLEDGE_BASE slack_kb
     USING
         model = open_emb,
         storage = slack_vector.general;
    
  6. Create LLM Model: Created a model that will be used to answer our question.

     --CREATE ENGINE
     CREATE ML_ENGINE langchain FROM langchain;
     -- CREATE COVERSATIONAL MODEL
     CREATE MODEL chat_model
     PREDICT answer
     USING
         engine = 'langchain',
         input_column = 'question',
         api_key = 'sk-*********', 
         model_name='gpt-3.5-turbo', 
         mode = 'conversational',
         user_column = 'question' ,
         assistant_column = 'answer',
         max_tokens=100,
         temperature=0,
         verbose=True,
         prompt_template='You have to answer question asked in helpful way.Use tools to get more nice answer';
    
  7. Agents having Skills: Created a SKILL out of this KNOWLEDGE_BASE, which further needs to assign to AGENT, this AGENTS can have one or more than one SKILLS which CHATBOT can use to get final answer.

     -- Created Skill
     CREATE SKILL slack_skill
     USING
         type = 'knowledge_base',
         source = 'slack_kb', 
         description = 'Use this whenever question is asked about error, MindsDB or technical quwstion';
    
     --  CREATED Agent from skill AND LLM Mode'
     CREATE AGENT slack_agent
     USING
        model = 'chat_model',
        skills = ['slack_skill'];
    
  8. Chatbot Integration: Integrated the agent into a Slack bot, transforming insights from MindsDB into real-time assistance for users.

     CREATE CHATBOT vector
     USING
         database = 'mindsdb_slack', 
         agent = 'slack_agent',
         included_channels = ['general'], 
         excluded_channels = [], 
         enable_dms = true,
         is_running = true;
    
  9. Daily Updates with 'JOB': Implemented MindsDB's 'JOB' feature to schedule daily updates, ensuring the 'KNOWLEDGE_BASE' stayed relevant.

     -- Insert Slack Chat data into Knowledge Base every Day, if there is new data
     CREATE JOB keep_knowledge_base_up_to_date AS (
         INSERT INTO slack_kb (
             SELECT 
                 c.ts AS id,
                 c.text AS content,
                 o.embeddings AS embeddings,
                 CONCAT('{"answers":"', REPLACE(c.threads, '"', "'"), '"}') AS metadata
             FROM 
                 open_emb o
             JOIN 
                 mindsdb_slack.channels c ON o.text = c.text -- Replace with the actual join condition
             WHERE 
                 c.channel = 'general' AND 
                 c.with_threads = true AND
                 AND id > LAST
          );
      ) every DAY;
    

Challenges I faced while Implementation:

  • Lack of Threads Table in Slack Datasource:

    • Challenge: The absence of a dedicated threads (replies) table in the Slack datasource posed a hurdle to capturing comprehensive conversation data.

    • Solution: Leveraging the open-source nature of the Slack datasource, I delved into the code, made modifications, and successfully added both a threads table and a threads column. This enhancement proved instrumental in capturing and analyzing threaded conversations within the Slack channel. (PR Submitted)

Insufficient Data for Testing:

  • Challenge: Adequate testing requires a substantial amount of real-world data, which was initially lacking.

  • Solution: To overcome this challenge, I took matters into my own hands. Creating a dedicated Slack channel, I curated and added synthetic chat data, simulating the dynamics of a genuine Slack community. This ensured a more robust testing environment for the chatbot.

Initial Lack of API Key:

  • Challenge: Access to the OpenAI API key was initially a roadblock in querying the AI model.

  • Solution: Grateful for MindsDB's support, an OpenAI API key was provided, enabling me to seamlessly integrate and test my project with the MindsDB AI model. This collaboration facilitated the refinement of the real-time assistance bot.

Future Work:

  • The journey is just started; future plans include expanding the bot's reach by integrating with other platforms like Discord.

  • Upvote functionality will be introduced, allowing users to indicate the relevancy of responses for improved curation.

  • Documentation knowledge will be enriched to provide users with comprehensive and detailed insights.

  • Integration with Stack Overflow is on the horizon to enhance the knowledge base and deliver even more refined and accurate answers.

  • And much more, as the road ahead promises continuous innovation and evolution.

Conclusion:

In the ever-evolving landscape of digital communication, MindsDB stands out as a catalyst for innovation. The journey of building a real-time assistance bot for Slack channels has not only addressed immediate challenges but has opened doors to a future where AI-driven assistance is seamlessly woven into the fabric of online communities. MindsDB's real-time data, AI through SQL, and extensive features have empowered this endeavor, promising a future where instant and accurate assistance is just a query away. As the bot continues to evolve, MindsDB remains at the forefront of shaping the future of intelligent, real-time interactions.

๐Ÿšจ Note: Currently, the live bot operates in my local environment. Once the PR merges and it's deployed to the cloud, I'll eagerly share the link for you to experience the real-time AI-powered magic! Stay tuned for the big reveal. ๐ŸŒ๐Ÿค–

0
Subscribe to my newsletter

Read articles from Parthiv Makwana directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Parthiv Makwana
Parthiv Makwana