gittech. site

for different kinds of informations and explorations.

AI-SQL, Tools for your AI to talk to your database

Published at
Jan 3, 2025

AI SQL

Tools for Vercel AI SDK that lets your AI query PostgreSQL, MySQL or SQLite databases in one line.

[!CAUTION] AI-generated SQL queries are not recommended in production and could risk injection attacks. Please proceed with caution.

Installation

npm install ai-sql

Usage

import * as ai from "ai";
import { createOpenAI } from "@ai-sdk/openai";
import { postgres } from "ai-sql"; // or mysql, sqlite

const openai = createOpenAI({
  compatibility: "strict",
});

const { text } = await ai.generateText({
  model: openai("gpt-4-turbo"),
  prompt: "",
  tools: {
    database: await postgres(process.env.POSTGRES_URL!),
  },
  maxSteps: 3,
});

For more examples, see the example directory.

Creating a Custom Database Provider

import { Schema, Database, sqlTool } from "ai-sql";

export class MyDbTool implements Database {
  async initialize() {
    // do setup here
  }

  async describe(): Promise<Schema> {
    // describe the schema

    return {
      // database type
      database: "my database",
      // stringified schema representation
      description: `
        create table messages (
          id integer primary key,
          text string not null,
        );
      `,
    };
  }

  async query(query: string) {
    // return result rows here
    return [];
  }
}

const tools = {
  database: await sqlTool(new MyDbTool()),
};