Building a Support Bot with MindsDb and Llamaindex

Building a Support Bot with MindsDb and Llamaindex

Data

·

3 min read

Artificial Intelligence has come a long way in recent years, and the use of machine learning models to create chatbots is becoming increasingly popular.

However, building a chatbot that can truly understand and respond to human language can be a challenging task. That's where AI technologies like MindsDB and GPT-3 come in.

MindsDB is an open-source tool that uses machine learning to make predictions and automate decision-making tasks. It allows users to create predictive models with minimal coding, making it accessible to both developers and non-technical users. Meanwhile, GPT-3 is a language model developed by OpenAI, capable of generating human-like text and answering questions based on natural language input.

By combining these two technologies, we can create a chatbot that responds to user queries about MindsDB. Here's how you can create a chatbot using MindsDB and GPT-3.

Step 1:

Sign up to MindsDb here to use the cloud editor or deploy your instance using this guide. If you're using the cloud editor, you will need to upgrade to pro if you are planning to use it for Production.

Step 2:

After signing, Upload a File. MindsDb uses a File-based system for tables. Json,xlsx, and CSV are converted to database tables.

After uploading your file, your tables should look like this. I indexed the entire MindsDb documentation and stored it as a JSON file.

Next, we need to be able to query the Database with Natural language. Currently, running natural language queries on this kind of table is a bit tasking. Run a query to output the values.


CREATE MODEL que
PREDICT answer
USING
    engine = 'openai',
    prompt_template = 'answer the question of text:{{question}} about text:{{text}}';

Then run this query

SELECT *
FROM que
WHERE question = 'how do i use cockroachdb with mindsdb'
AND text = 'mindsdb';

You should see a table with the response to the question asked like the image below.

But we want to be able to access this response outside of the editor, so we will use llama-index to connect to the MindsDb. That way we can query the DB with SQL or natural language. Llamaindex also gives us access to a lot of data sources not available on MindsDb. Connecting to Langchain to run agents is also easy with llama-index.

import os
import json
from dotenv import load_dotenv
from llama_index.indices.struct_store import SQLContextContainerBuilder
from dbreader import DatabaseReader
from sqlalchemy import create_engine
from llama_index import GPTSimpleVectorIndex
from langchain.chains.question_answering import load_qa_chain
load_dotenv()

OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
user = os.getenv("user")
password = os.getenv("password")
host = os.getenv("host")
port = os.getenv("port")
database = os.getenv("database") #database can be anything
engine = create_engine(f"mysql+pymysql://{user}:{password}@{host}:{port}/{database}")
reader = DatabaseReader(engine=engine)

query = f"""
SELECT CONCAT('[', GROUP_CONCAT(JSON_OBJECT('id', id, 'title', title, 'page_link', page_link, 'text', text)), ']')
FROM files.mdb;
"""
documents = reader.load_data(query=query)
# index = GPTSimpleVectorIndex.from_documents(documents)
# index.save_to_disk('mdb.json')
#  load from disk
index = GPTSimpleVectorIndex.load_from_disk('mdb.json')
response=index.query('what is mindsdb? provide references and sources')
print(response)

In the code above, I converted everything to a JSON structure using this query command SELECT CONCAT('[', GROUP_CONCAT(JSON_OBJECT('id', id, 'title', title, 'page_link', page_link, 'text', text)), ']') FROM files.mdb;

The mdb table is not exactly a standard table, it's file-based and file-based databases need a table name, which normally should have been mdb . But that doesn't work with our current setup(dbreader.py). This is pretty bleeding-edge technology, so it's understandable.

If for any reason you must run a PREDICTOR command and other similar commands, ensure you create a standard table and import your files into it.

Slackbot setup

The Slack bot was built using DJANGO and Slack's Bolt package. Use this repo for setting it up.

Conclusion

In this guide, I have shown how to connect to MindsDb externally using llama-index and run also how to run SQL queries in the web editor.

Here's a Demo of the Slackbot currently running on MindsDb's community Slack.

Source code for the Project: https://github.com/Smyja/mdbindex

#MindsDB and #MindsDBHackathon