1# SQLite QueryWriter
2
3The 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.
4
5This val is inspired by [prisma-gpt](https://github.com/aliyeysides/prisma-gpt). PRs welcome! See **Todos** below for some ideas I have.
44- `table`: The name of the database table to operate on.
45- `model` (optional): The model to use for generating SQL queries. Defaults to "gpt-3.5-turbo".
46- `apiKey` (optional): An OpenAI API key. Defaults to `Deno.env.get("OPENAI_API_KEY")`.
47
48
4import Instructor from "npm:@instructor-ai/instructor";
5import { Client } from "npm:@notionhq/client";
6import OpenAI from "npm:openai";
7import { render } from "npm:preact-render-to-string";
8import { z } from "npm:zod";
30};
31
32const oai = new OpenAI({
33 apiKey: process.env.OPENAI_API_KEY ?? undefined,
34});
35
3import Instructor from "npm:@instructor-ai/instructor";
4import { Client } from "npm:@notionhq/client";
5import OpenAI from "npm:openai";
6import { z } from "npm:zod";
7
26};
27
28const oai = new OpenAI({
29 apiKey: process.env.OPENAI_API_KEY ?? undefined,
30});
31
14Supports: checkbox, date, multi_select, number, rich_text, select, status, title, url, email
15
16- Uses `NOTION_API_KEY`, `OPENAI_API_KEY` stored in env variables and uses [Valtown blob storage](https://esm.town/v/std/blob) to store information about the database.
17- Use `get_notion_db_info` to use the stored blob if exists or create one, use `get_and_save_notion_db_info` to create a new blob (and replace an existing one if exists).
18
2
3const gpt3Example = async () => {
4 const response = await fetch("https://api.openai.com/v1/completions", {
5 method: "POST",
6 headers: {
7 "Content-Type": "application/json",
8 "Authorization": "Bearer " + process.env.OPENAI_API_KEY, // Replace with your OpenAI API Key
9 },
10 body: JSON.stringify({
25
26 const getSearch = async (data) => {
27 const response = await fetch("https://api.openai.com/v1/completions", {
28 method: "GET",
29 body: JSON.stringify(data),
8 method: "POST",
9 body: JSON.stringify({
10 "url": "https://en.wikipedia.org/wiki/OpenAI",
11 "elements": [{
12 // The second <p> element on the page
1// set at Sat Dec 09 2023 01:45:57 GMT+0000 (Coordinated Universal Time)
2export let openAiFreeUsage = {"used_quota":12709400,"used_quota_usd":1.27094,"exceeded":false};
1import { email } from "https://esm.town/v/std/email?v=11";
2import { fetch } from "https://esm.town/v/std/fetch";
3import { OpenAI } from "npm:openai";
4
5let location = "Halifax UK";
9).then(r => r.json());
10
11const openai = new OpenAI();
12let chatCompletion = await openai.chat.completions.create({
13 messages: [{
14 role: "user",
1If you fork this, you'll need to set `OPENAI_API_KEY` in your [Val Town Secrets](https://www.val.town/settings/secrets).
2
3