nbbaier avatar
nbbaier/sqliteWriter

Create a new fork

SQLite QueryWriter

The QueryWriter class is a utility for generating and executing SQL queries using natural language and OpenAI. It provides a simplified interface for interacting with your Val Town SQLite database and generating SQL queries based on user inputs.

This val is inspired by prisma-gpt. PRs welcome! See Todos below for some ideas I have.

Usage

  1. Import the QueryWriter class into your script:
import { QueryWriter } from "https://esm.town/v/nbbaier/sqliteWriter";
  1. Create an instance of QueryWriter, providing the desired table and an optional model:
const writer = new QueryWriter({ table: "my_table", model: "gpt-4-1106-preview" });
  1. Call the writeQuery() method to generate an SQL query based on a user input string:
const userInput = "Show me all the customers with more than $1000 in purchases.";
const query = await writer.writeQuery(userInput);
  1. Alternatively, use the gptQuery() method to both generate and execute the SQL query:
const userInput = "Show me all the customers with more than $1000 in purchases.";
const result = await writer.gptQuery(userInput);
  1. Handle the generated query or query result according to your application's needs.

API

new QueryWriter(args: { table: string; model?: string }): QueryWriter

Creates a new instance of the QueryWriter class.

  • table: The name of the database table to operate on.
  • model (optional): The model to use for generating SQL queries. Defaults to "gpt-3.5-turbo".
  • apiKey (optional): An OpenAI API key. Defaults to Deno.env.get("OPENAI_API_KEY").

writeQuery(str: string): Promise<string>

Generates an SQL query based on the provided user input string.

  • str: The user input string describing the desired query.

Returns a Promise that resolves to the generated SQL query.

gptQuery(str: string): Promise<any>

Generates and executes an SQL query based on the provided user input string.

  • str: The user input string describing the desired query.

Returns a Promise that resolves to the result of executing the generated SQL query.

Todos

  • Handle multiple tables for more complex use cases
  • Edit prompt to allow for more than just SELECT queries
  • Allow a user to add to the system prompt maybe?
  • Expand usage beyond just Turso SQLite to integrate with other databases
Readme
Your profile