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.
- Import the QueryWriter class into your script:
import {
QueryWriter }
from "https://esm.town/v/nbbaier/sqliteWriter";
- 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" });
- 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);
- 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);
- Handle the generated query or query result according to your application's needs.
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.