Skip to content

theswerd/ai-sql

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

33 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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()),
};

About

AI Tool Use Utility for letting your AI query SQL

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 3

  •  
  •  
  •