|
| 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 | +} |
0 commit comments