Skip to content

Commit 69d7373

Browse files
committed
alfred-search-and-context
1 parent ea5a42e commit 69d7373

4 files changed

+104
-5
lines changed

.gitignore

+3-1
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,6 @@ records*.json
66
books-notion-table.csv
77
kindle-highlights
88
kindle-export-supabase.js
9-
ocr
9+
ocr
10+
note_exports
11+
alfred-searches

alfred-search-and-context.js

+87
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
import { createClient } from "@supabase/supabase-js";
2+
import dotenv from "dotenv";
3+
import { Configuration, OpenAIApi } from "openai";
4+
import clipboard from "clipboardy";
5+
import fs from "fs";
6+
import { similaritySearch } from "./similarity-search.js";
7+
import { exec } from "child_process";
8+
9+
dotenv.config();
10+
11+
const configuration = new Configuration({
12+
apiKey: process.env.OPENAI_API_KEY,
13+
organization: process.env.OPENAI_ORG,
14+
});
15+
16+
const openai = new OpenAIApi(configuration);
17+
18+
const supabaseUrl = process.env.SUPABASE_URL;
19+
const supabaseKey = process.env.SUPABASE_KEY;
20+
const supabase = createClient(supabaseUrl, supabaseKey, {
21+
auth: {
22+
persistSession: false,
23+
},
24+
});
25+
26+
const readFromClipboard = async () => {
27+
return clipboard.readSync();
28+
};
29+
30+
const writeToFile = async (data) => {
31+
// write to alfred-searches/{date}.md
32+
const date = new Date();
33+
const filename = `alfred-searches/${date.toISOString()}.md`;
34+
fs.writeFileSync(filename, data);
35+
return filename;
36+
};
37+
38+
const openTextEditor = async (filename) => {
39+
// open -a typora filename.md
40+
const command = `open -a typora ${filename}`;
41+
return new Promise((resolve, reject) => {
42+
const child = exec(command, (error, stdout, stderr) => {
43+
if (error) {
44+
console.error(error);
45+
reject(error);
46+
}
47+
resolve();
48+
});
49+
});
50+
};
51+
52+
const main = async () => {
53+
// Get the text from the clipboard
54+
const queryPromise = await readFromClipboard();
55+
const query = queryPromise.toString().trim();
56+
console.log(`Query: ${query}`);
57+
// Search for similar quotes
58+
const similar = await similaritySearch(query);
59+
60+
let results = `Query = ${query}:\n\n---\n\n`;
61+
62+
for (const quote of similar) {
63+
const contextPrompt = `I only have the following quote, recreate outside context using concepts from the world. Do a breadth first search of external data. This means that you are **not rephrasing the content in the quote**. You are pulling in examples and information from the world **outside the set** of the quote. Do not bother repeating or rehashing the quote.\n\nQuote:\n${quote.text}\n-- ${quote.title} by ${quote.author}\n\nContext:\n`;
64+
65+
const completion = await openai.createChatCompletion({
66+
model: "gpt-4-turbo-preview",
67+
messages: [
68+
{
69+
role: "system",
70+
content: contextPrompt,
71+
},
72+
],
73+
});
74+
75+
const context = completion.data.choices[0].message.content;
76+
77+
results += `Title: ${quote.title}\n\nAuthor: ${quote.author}\n\nQuote:\n\n> ${quote.text}\n\nContext:\n\n${context}\n\n---\n\n`;
78+
}
79+
80+
console.log(results);
81+
const filename = await writeToFile(results);
82+
await openTextEditor(filename);
83+
};
84+
85+
if (process.env.DEV) {
86+
main();
87+
}

compile-quotes.js

+10-3
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ const openai = new OpenAIApi(configuration);
2222

2323
dotenv.config();
2424

25-
const BOOK_ID = "38318378";
25+
const BOOK_ID = "1474870";
2626

2727
const supabaseUrl = process.env.SUPABASE_URL;
2828
const supabaseKey = process.env.SUPABASE_KEY;
@@ -235,7 +235,8 @@ const main = async () => {
235235

236236
console.log(quotes.length + " quotes found.");
237237

238-
const k = 5;
238+
// w want roughly 5-10 quotes per cluster
239+
const k = Math.ceil(quotes.length / 5);
239240
const assignments = kmeans(
240241
quotes
241242
.map((quote) => JSON.parse(quote.embedding))
@@ -267,8 +268,14 @@ const main = async () => {
267268
markdown += `## ${topic}\n\n`;
268269
markdown += `### Summary\n\n${summary}\n\n`;
269270
markdown += `### Quotes\n\n`;
271+
// // add each quote to the markdown with highlighting
272+
// for (const quote of cluster) {
273+
// markdown += `- ${await highlightQuote(quote.text, topic)}\n`;
274+
// }
275+
// markdown += `\n\n`;
276+
// add each quote to the markdown without highlighting
270277
for (const quote of cluster) {
271-
markdown += `- ${await highlightQuote(quote.text, topic)}\n`;
278+
markdown += `- ${quote.text}\n`;
272279
}
273280
markdown += `\n\n`;
274281
// markdown += `### Follow Up Questions\n\n${followUp}\n\n`;

embed-into-supabase.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -211,4 +211,7 @@ const justBooks = async () => {
211211
// console.log(highlights.length)
212212
}
213213

214-
// justBooks()
214+
// justBooks() only runs if flag is set and also in termina process arg is set
215+
if (process.env.DEV && process.argv[2] === 'justBooks') {
216+
justBooks()
217+
}

0 commit comments

Comments
 (0)