Skip to content

Commit

Permalink
examples: rag over your life in obsidian.md
Browse files Browse the repository at this point in the history
  • Loading branch information
louis030195 committed Jul 4, 2024
1 parent adc7e37 commit 74a53da
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 0 deletions.
Empty file.
7 changes: 7 additions & 0 deletions examples/ts/rag-over-your-life-in-obsidian/conf.js
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-..."}
64 changes: 64 additions & 0 deletions examples/ts/rag-over-your-life-in-obsidian/screenpipe.js
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;
1 change: 1 addition & 0 deletions examples/ts/rag-over-your-life-in-obsidian/screenpipe.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<% tp.user.screenpipe(tp.user.conf()) %>

0 comments on commit 74a53da

Please sign in to comment.