forked from mckaywrigley/paul-graham-gpt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathembed.ts
59 lines (46 loc) · 1.61 KB
/
embed.ts
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
import { PGEssay, PGJSON } from "@/types";
import { loadEnvConfig } from "@next/env";
import { createClient } from "@supabase/supabase-js";
import fs from "fs";
import { Configuration, OpenAIApi } from "openai";
loadEnvConfig("");
const generateEmbeddings = async (essays: PGEssay[]) => {
const configuration = new Configuration({ apiKey: process.env.OPENAI_API_KEY });
const openai = new OpenAIApi(configuration);
const supabase = createClient(process.env.NEXT_PUBLIC_SUPABASE_URL!, process.env.SUPABASE_SERVICE_ROLE_KEY!);
for (let i = 0; i < essays.length; i++) {
const section = essays[i];
for (let j = 0; j < section.chunks.length; j++) {
const chunk = section.chunks[j];
const { essay_title, essay_url, essay_date, essay_thanks, content, content_length, content_tokens } = chunk;
const embeddingResponse = await openai.createEmbedding({
model: "text-embedding-ada-002",
input: content
});
const [{ embedding }] = embeddingResponse.data.data;
const { data, error } = await supabase
.from("pg")
.insert({
essay_title,
essay_url,
essay_date,
essay_thanks,
content,
content_length,
content_tokens,
embedding
})
.select("*");
if (error) {
console.log("error", error);
} else {
console.log("saved", i, j);
}
await new Promise((resolve) => setTimeout(resolve, 200));
}
}
};
(async () => {
const book: PGJSON = JSON.parse(fs.readFileSync("scripts/pg.json", "utf8"));
await generateEmbeddings(book.essays);
})();