Readme

Ask gpt to update a val on your behalf

Usage

import { askAI } from "https://esm.town/v/pomdtr/ask_ai"; await askAI(`Add jsdoc comments for each exported function of the val @pomdtr/askAi`);
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 { email } from "https://esm.town/v/std/email?v=12";
import { OpenAI } from "https://esm.town/v/std/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,
}),
});
}
async function sendEmail({ subject, text }: { subject: string; text: string }) {
await email({
subject,
text,
});
}
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.
`;
type Tool = {
type: "function";
function: {
function: (...args: any[]) => any;
description: string;
parse: (...args: any[]) => any;
required: string[];
parameters: {
type: "object";
properties: Record<string, {
type: "string";
}>;
};
};
};
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,
Val Town is a social website to write and deploy JavaScript.
Build APIs and schedule functions from your browser.
Comments
Nobody has commented on this val yet: be the first!
v73
May 4, 2024