-
Notifications
You must be signed in to change notification settings - Fork 168
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
examples: rag over your life in obsidian.md
- Loading branch information
1 parent
adc7e37
commit 74a53da
Showing
4 changed files
with
72 additions
and
0 deletions.
There are no files selected for viewing
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
const conf = () => { | ||
const secrets = process.env.HOME + '/secrets.json' | ||
console.log("fetching secrets.json from", secrets) | ||
return require(secrets); | ||
} | ||
module.exports = conf; | ||
// ~/secrets.json: {"OPENAI_API_KEY": "sk-..."} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
const screenpipe = async (conf) => { | ||
const openAIKey = conf.openai; | ||
document.body.style.cursor = "wait"; | ||
const msg = window.getSelection().toString(); | ||
|
||
// Generate parameters for 3 different queries | ||
const paramsResponse = await fetch('https://api.openai.com/v1/chat/completions', { | ||
method: 'POST', | ||
headers: { | ||
'Content-Type': 'application/json', | ||
'Authorization': `Bearer ${openAIKey}`, | ||
}, | ||
body: JSON.stringify({ | ||
'response_format': { type: 'json_object' }, | ||
'messages': [ | ||
{ | ||
'role': 'user', | ||
'content': `Based on this user selection: "${msg}", generate parameters as JSON for 3 different queries to screenpipe. | ||
Each query should have "q", "offset", and "limit" fields. | ||
Rules: | ||
- q should be a single keyword that would properly find in the text found on the user screen some infomation that would help answering the user question. | ||
Return a list of objects with the key "queries"` | ||
}, | ||
], | ||
'model': 'gpt-4o', | ||
}) | ||
}).then(res => res.json()); | ||
|
||
console.log(paramsResponse); | ||
const queries = JSON.parse(paramsResponse.choices[0].message.content).queries; | ||
|
||
// Query screenpipe 3 times with generated parameters | ||
const screenpipeResults = await Promise.all(queries.map(async (query) => { | ||
const response = await fetch(`http://localhost:3030/search?q=${encodeURIComponent(query.q)}&offset=${query.offset}&limit=${query.limit}`); | ||
return response.json(); | ||
})); | ||
|
||
// Ask ChatGPT to write an answer based on screenpipe results | ||
const finalResponse = await fetch('https://api.openai.com/v1/chat/completions', { | ||
method: 'POST', | ||
headers: { | ||
'Content-Type': 'application/json', | ||
'Authorization': `Bearer ${openAIKey}`, | ||
}, | ||
body: JSON.stringify({ | ||
'messages': [ | ||
{ 'role': 'user', 'content': `Based on the user question "${msg}" and these data which corresponds to what has been seen on the user screen or through his mic: ${JSON.stringify(screenpipeResults)}, provide a comprehensive answer to the user intent.` }, | ||
], | ||
'model': 'gpt-4o', | ||
}) | ||
}).then(res => res.json()); | ||
|
||
document.body.style.cursor = "default"; | ||
console.log(finalResponse); | ||
|
||
if (!finalResponse.choices?.[0]?.message?.content) { | ||
new Notice('Error from OpenAI'); | ||
new Notice(JSON.stringify(finalResponse)); | ||
} | ||
|
||
return `${msg}${finalResponse.choices[0].message.content}`; | ||
} | ||
|
||
module.exports = screenpipe; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
<% tp.user.screenpipe(tp.user.conf()) %> |