Skip to content

Commit

Permalink
Add a new command to call openai using a user note or selection as th…
Browse files Browse the repository at this point in the history
…e prompt, ignoring built-in prompts
  • Loading branch information
justyns committed Feb 19, 2024
1 parent 845d037 commit d2461c3
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 10 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ The response is inserted at the cursor position.
Generated tags are added to the note's frontmatter.
- **AI: Generate and insert image using DallE**: Prompts the user for a custom prompt to send to DALL·E, then sends the prompt to DALL·E to generate an image.
The resulting image is then uploaded to the space and inserted into the note with a caption.
- **AI: Call OpenAI with selected text as prompt**: Uses either the selected text or the entire note as the prompt for the LLM.
No pre-defined prompt will be sent with the request.
The response is inserted at the cursor position if the whole note is used. Otherwise
it will replace the selected text.

<!-- end-commands-and-functions -->

Expand Down
30 changes: 29 additions & 1 deletion sbai.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ async function initializeOpenAI() {
openAIBaseUrl: "https://api.openai.com/v1",
dallEBaseUrl: "https://api.openai.com/v1",
};
aiSettings = await readSetting("ai", defaultSettings);
aiSettings = await readSetting("ai", {});
aiSettings = { ...defaultSettings, ...aiSettings };
console.log("aiSettings", aiSettings);
}

Expand Down Expand Up @@ -141,6 +142,33 @@ export async function callOpenAIwithNote() {
}
}

/**
* Uses either the selected text or the entire note as the prompt for the LLM.
* No pre-defined prompt will be sent with the request.
* The response is inserted at the cursor position if the whole note is used. Otherwise
* it will replace the selected text.
*/
export async function callOpenAIWithSelectionAsPrompt() {
const selectedTextInfo = await getSelectedTextOrNote();
const response = await chatWithOpenAI(
"You are an AI note assistant in a markdown-based note tool.",
[{
role: "user",
content:
`${selectedTextInfo.text}`,
}],
);
if (selectedTextInfo.isWholeNote) {
await editor.insertAtCursor(response.choices[0].message.content);
} else {
await editor.replaceRange(
selectedTextInfo.from,
selectedTextInfo.to,
response.choices[0].message.content,
);
}
}

/**
* Uses a built-in prompt to ask the LLM for a summary of either the entire note, or the selected
* text. Opens the resulting summary in a temporary right pane.
Expand Down
Loading

0 comments on commit d2461c3

Please sign in to comment.