Skip to content

AI Features

antarr edited this page Apr 10, 2026 · 2 revisions

AI Features

MySQLGenius supports AI-powered query tools via any OpenAI-compatible API. When configured, an AI Tools tab appears in the dashboard.

Supported Providers

Provider ai_auth_style ai_model Notes
OpenAI :bearer Required (e.g. gpt-4o)
Azure OpenAI :api_key (default) Optional (uses deployment default)
Ollama Cloud :bearer Required (e.g. gemma3:27b) Follows redirects automatically
Local Ollama :bearer Required No API key validation
Custom client N/A N/A You handle everything

Configuration Examples

OpenAI

MysqlGenius.configure do |config|
  config.ai_endpoint = "https://api.openai.com/v1/chat/completions"
  config.ai_api_key = ENV["OPENAI_API_KEY"]
  config.ai_model = "gpt-4o"
  config.ai_auth_style = :bearer
end

Azure OpenAI

MysqlGenius.configure do |config|
  config.ai_endpoint = ENV["AZURE_OPENAI_ENDPOINT"]
  config.ai_api_key = ENV["AZURE_OPENAI_API_KEY"]
  config.ai_auth_style = :api_key
end

Ollama Cloud

MysqlGenius.configure do |config|
  config.ai_endpoint = "https://api.ollama.com/v1/chat/completions"
  config.ai_api_key = ENV["OLLAMA_CLOUD_KEY"]
  config.ai_model = "gemma3:27b"
  config.ai_auth_style = :bearer
end

Local Ollama

MysqlGenius.configure do |config|
  config.ai_endpoint = "http://localhost:11434/v1/chat/completions"
  config.ai_api_key = "ollama"
  config.ai_model = "llama3"
  config.ai_auth_style = :bearer
end

Custom Client

MysqlGenius.configure do |config|
  config.ai_client = ->(messages:, temperature:, **) {
    MyAiService.chat(messages, temperature: temperature)
  }
end

The callable must return an OpenAI-compatible response hash with "choices".

Domain Context

Help the AI understand your schema for better query generation:

config.ai_system_context = <<~CONTEXT
  This is an e-commerce database.
  - `users` stores customer accounts. Primary key is `id`.
  - `orders` tracks purchases. Linked to users via `user_id`.
  - `products` contains the product catalog.
  - Soft-deleted records have `deleted_at IS NOT NULL`.
CONTEXT

AI Tools Available

Query Suggestions

Describe what you want in plain English, get SQL back. The AI sees your actual schema (tables, columns, types) to generate accurate queries.

Query Optimization

Paste a query and its EXPLAIN output. The AI analyzes the execution plan and suggests specific index additions, query rewrites, and structural changes.

Query Description

Paste any SQL query and get a plain-English explanation of what it does, how data flows, and potential pitfalls.

Schema Review

Analyzes your table schemas for anti-patterns: inappropriate column types, missing indexes, missing NOT NULL constraints, redundant indexes, naming inconsistencies.

Query Rewrite

Detects SQL anti-patterns (SELECT *, correlated subqueries, leading wildcards, functions on indexed columns) and suggests rewrites.

Index Advisor

Given a query and its EXPLAIN output, recommends optimal indexes with exact CREATE INDEX statements and rationale for column ordering.

Anomaly Detection

Analyzes query patterns from performance_schema and slow query logs to identify N+1 patterns, degrading performance, and full table scans.

Root Cause Analysis

"Why is the database slow right now?" -- analyzes PROCESSLIST, InnoDB status, key metrics, and recent slow queries to diagnose live issues.

Migration Risk Assessment

Paste a Rails migration or DDL and get a risk assessment: will it lock the table? Is it safe during traffic? Should you use pt-online-schema-change?

Disabling AI

When no AI configuration is set, the AI Tools tab is automatically hidden. No code changes needed.


< Configuration | Dark Theme >