nerdymomocat-add_to_notion_w_ai_webpage.web.val.run
January 18, 2024
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
/** @jsxImportSource https://esm.sh/preact */
import { blob } from "https://esm.town/v/std/blob";
import process from "node:process";
import Instructor from "npm:@instructor-ai/instructor";
import { Client } from "npm:@notionhq/client";
import OpenAI from "npm:openai";
import { render } from "npm:preact-render-to-string";
import { z } from "npm:zod";
const dbid = "DB_ID_GOES_HERE";
const NOTION_API_KEY = process.env.NOTION_API_KEY;
const notion = new Client({
auth: NOTION_API_KEY,
});
const MAGIC_AI_FILL_SYMB = "✨";
const supported_notion_props = {
"checkbox": "boolean",
"date": "date",
"multi_select": "array_enum",
"number": "number",
"rich_text": "string",
"select": "enum",
"status": "enum",
"title": "string",
"url": "string_url",
"email": "string_email",
};
const oai = new OpenAI({
apiKey: process.env.OPENAI_API_KEY ?? undefined,
});
const client = Instructor({
client: oai,
mode: "TOOLS",
});
function createPrompt(title, description, properties) {
let prompt =
"You are processing content into a database. Based on the title of the database, its properties, their types and options, and any existing descriptions, infer appropriate values for the fields:\n";
prompt += `Database Title: ${title}\n`;
if (description) {
prompt += `Database Description: ${description}\n\n`;
} else {
prompt += "\n";
}
prompt += "Properties (with types and options where applicable.):\n";
Object.keys(properties).forEach(key => {
const prop = properties[key];
prompt += `Name: ${prop.name}, Type: ${prop.type}`;
if (prop.description) {
prompt += `, Description: ${prop.description}`;
}
prompt += "\n";
if (prop.options) {
if (prop.type === "select" || prop.type === "status") {
prompt += "Options (choose one or none of these options):\n";
} else if (prop.type === "multi_select") {
prompt += "Options (choose one, multiple or none of these options):\n";
}
prop.options.forEach(option => {
prompt += ` - ${option.name}`;
if (option.description) {
prompt += `: ${option.description}`;
}
prompt += "\n";
});
}
});
prompt +=
"\nInfer and assign values to these properties based on the provided content and the aforementioned cotext.";
return prompt;
}
function processProperties(jsonObject) {
const properties = jsonObject.properties;
const filteredProps = {};
Object.keys(properties).forEach(key => {
const prop = properties[key];
const supportedType = supported_notion_props[prop.type];
if (supportedType && (prop.description?.startsWith(MAGIC_AI_FILL_SYMB) || prop.type === "title")) {
filteredProps[key] = {
name: prop.name,
type: prop.type,
description: prop.description ? prop.description.replace(MAGIC_AI_FILL_SYMB, "") : "",
};
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!
January 18, 2024