Cheshire-Cat plugin SQL

Nilthon Jhon Rojas Apumayta
Mad Chatter Tea Party

--

Introduction

If you want to connect an SQL database to an LLM (Large Language Model) in a simple and powerful way, you’ve come to the right place. In this article, we will explore the use of the Cheshire-Cat framework with the AIChatSQL plugin. With this combination, you can easily link a database and let the LLM handle the challenging tasks of executing queries, interpreting user requests, and providing responses with data from the database.

Installation

Before proceeding, ensure you have a Cheshire-Cat instance ready. If you don’t have one yet, you can find instructions on how to set up the Cheshire-Cat in “Cheshire-Cat” repository guide:

Once Cheshire-Cat is configured, proceed with the installation of the AIChatSQL plugin. This plugin will handle the connection management and execution of queries in the database on your behalf.

Plugin Configuration

In the plugin settings, you can configure our SQLite, PostgreSQL, and MySQL databases, specifying which tables our LLM will have access to. For security reasons, only read queries are permitted.

Performance Optimization

To enhance the plugin’s performance, you can specify a database schema along with their respective descriptions in the prompts.

Examples

This example is made with this https://github.com/datacharmer/test_db db.

Count:

database/ How many employees do we have?

SELECT COUNT(`emp_no`) FROM `employees`

We’re talking about a remarkably large number of employees, my friend. There are precisely 300024 characters in our database.

Mathematical operation:

> database/ What is the average age of our employees?

SELECT AVG(YEAR(CURDATE()) — YEAR(`birth_date`)) as average_age FROM `employees`

Ah, the question that dances in your thoughts! The average age of our employees is approximately 65 years.

Joins tables:

> database/ Provide total salary for each department

SELECT `departments`.`dept_name`, SUM(`salaries`.`salary`) as ‘Total Salary’ FROM `departments` JOIN `dept_emp` ON `departments`.`dept_no` = `dept_emp`.`dept_no` JOIN `salaries` ON `dept_emp`.`emp_no` = `salaries`.`emp_no` WHERE `salaries`.`to_date` > CURDATE() GROUP BY `departments`.`dept_name` LIMIT 5

The total salary for each department is as follows: Customer Service — 1,270,982,348, Development — 4,627,787,365, Finance — 1,089,231,961, Human Resources — 909,717,009, Marketing — 1,300,398,678. Just as colorful as a pack of playing cards, isn’t it?

--

--