Ask gpt to update a val on your behalf

Usage

Create valimport { askAI } from "https://esm.town/v/pomdtr/ask_ai"; await askAI(`Add jsdoc comments for each exported function of the val @pomdtr/askAi`);
Readme
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
import { api } from "https://esm.town/v/pomdtr/api";
import { OpenAI } from "https://esm.town/v/pomdtr/OpenAI";
async function getValByAlias({ author, name }: { author: string; name: string }) {
const { id, code, readme } = await api(`/v1/alias/${author}/${name}`, {
authenticated: true,
});
return { id, code };
}
async function updateValCode({ id, code }: { id: string; code: string }) {
console.log({ id, code });
await api(`/v1/vals/${id}/versions`, {
authenticated: true,
method: "POST",
headers: {
"content-type": "application/json",
},
body: JSON.stringify({
code,
}),
});
}
const prompt = `
You are an helpful code assistant that has a deep understanding of javascript.
You help the user maintaining his vals, whose are runnable snippets of javascript.
Vals are often referred by slug.
Ex: @pomdtr/blog or pomdtr/blog or pomdtr.blog all refers to the val named blog created by the user pomdtr.
Here are some instructions on how to write vals:
- You can use in JavaScript, TypeScript, JSX, or TSX
- Utilize Deno-style import syntax for NPM packages, prefixed with "npm:"
- Prefer native fetch for HTTP requests instead of axios
- Prefer double quotes to single quotes
You can import from the standard library for email, blob storage,
sqlite, which have these types:
import { execute, batch } from "https://esm.town/v/std/sqlite";
await execute('SELECT 1 from table');
await batch(['SELECT 1 from table');
import { get, set } from "https://esm.town/v/std/blob";
const blob_contents: Response = await get('key_name');
// second argument has the type BodyInit
await set('key', 'test');
import { email } from "https://esm.town/v/std/email";
await email({
to: 'address@email.com',
text: 'message text',
subject: 'subject line',
});
Do not communicate with the user directly.
Instead, use the provided functions to accomplish your task on the behalf of the user.
For example, if you need to modify the code of a val, use the updateValCode function.
`;
export function askAI(content: string) {
const client = new OpenAI();
const runner = client.beta.chat.completions.runTools({
model: "gpt-3.5-turbo",
messages: [{ role: "system", content: prompt }, {
role: "user",
content,
}],
tools: [{
type: "function",
function: {
function: getValByAlias,
description: "Get val info (id, code) from it's alias",
parse: JSON.parse,
required: ["author", "name"],
parameters: {
type: "object",
properties: {
author: {
type: "string",
},
name: {
type: "string",
},
},
},
},
}, {
type: "function",
function: {
function: updateValCode,
description: "update the javascript code of a val",
parse: JSON.parse,
parameters: {
type: "object",
required: ["id", "code"],
👆 This is a val. Vals are TypeScript snippets of code, written in the browser and run on our servers. Create scheduled functions, email yourself, and persist small pieces of data — all from the browser.