Release of HiveSQL Discord Bot with AI Capabilities

2025-03-12T14:22:48
GM HiveDevs!
In my last post, I introduced the HiveSQL AI Bot, a tool that combines Discord, HiveSQL database querying, and AI capabilities to help users interact with the database through natural language.
This post is to officially release the source code for the community.
I want to focus this post on the code review, to help you navigate in the code and contribute to the project. Fell free to fork it, update and create a PR.

Core Features

  • Natural Language to HiveSQL: Users can ask questions, and the bot converts them to SQL queries
  • Direct SQL Queries: Execute SQL queries directly through Discord commands
  • Database Schema Exploration: View available tables and their structure
  • AI-Powered Help: Get assistance and explanations about queries and errors
  • Rate Limiting: Prevents abuse through cool downs and daily query limits

Architecture Overview

The bot is written in Python, forked from @mengao HiveSQL Discord bot, updated and structured into new components:
Let's build!

Configuration (config.py)

Well, this is where the code start. After configure the .env file.
Copy the env.sample to .env and input your API Keys and Hive SQL credentials.
This file manages all configuration settings:
  • Discord bot settings
  • Database connection details
  • AI model configuration
  • Query limitations

Discord Bot (HiveSQLBot.py)

The main bot class handles Discord interactions, user inputs, command processing and error handling.
It includes the features:
  • Command aliases for user convenience
  • Cool down system for rate limiting
  • Admin privileges with no cool down period
  • Error handling and user feedback

Database Management (database.py)

Yes, that's right, this file Handles the database interactions.
To avoid consulting the database, we created a cache for the available tables and tables scheme.
The Table scheme has 2 versions, one version with only fields names, and another with full fields types. Made that to do not send to many tokens to LLM, but then figured out another solution to send only the suggestion tables for the query.
  • Connection management
  • Query execution
  • Schema information retrieval
  • Table metadata caching

Command Handler (commands.py)

Here is where all bot commands are processed. Also, the AI prompts are here.
Maybe move the prompts to a new file is a good practice to do, but I'll leave it for later.
  • Natural language query processing. Tested in Portuguese and English so far
  • SQL query execution
  • Table information retrieval
  • Help system integration
  • Result formatting

AI Integration

The bot uses AI models, ready to use OpenAI or Groq. Check the .env file and config.py for tweeks.
  • Converting natural language to SQL queries
  • Explaining errors and providing suggestions
  • Helping users understand the database schema
  • Evaluating query complexity and safety

User Interface

The bot uses Discord as interface. Also you can check console for logs and if developing will be useful turn DEBUG_MODE flag on.
Use this commands to interact with the bot.
!aiquery - Generate SQL from natural language
!hivesql - Execute SQL queries directly
!tablelist - List available tables
!tableinfo - Show table schema
!help - Get assistance

Security

Always good to care about security, any suggestions for improvements are welcome. So far we implemented some safety features:
  • Rate limiting for non admin users
  • Daily query limits
  • Error handling
  • Ask AI to not create DELETE, INSERT, UPDATE queries
  • Credential management through environment variables

Future Improvements

Potential enhancements could include:
  • Query result caching
  • More AI provides options
  • Advanced query validation
  • Interactive query building
  • Query history and analytics
  • Plot graphics
Hope this project can be an example in how to combine AI capabilities with database operations in a user friendly interface, making database querying more accessible to users of all skill levels.
Know the HiveSQL database and how hive works help to create the questions for the model.

Final considerations

I noticed that sometimes the AI get confused about tables names, like transfers and transactions or use escrow transfers instead TxTransfer table. Maybe that can be solved with prompt engineering enhancements.
To not use all database scheme that has more than 15k tokens, first we ask model to suggest tables for the query.
Then it try to execute the query 3 times, asking model to fix according to the errors returning from HiveSQL.
Any doubt in about the code or how to install, or even during the setup, feel free to contact me. Open an issue on github can be a good call or find us Discord #hivebr channel.
The Source code available at GitHub: https://github.com/rferrari/hivesql_aibot
Want to test it out in our test server? Let us know.
See you soon.
Let's build!

135
20
5.20
20 Replies