Skip to content

Commit

Permalink
Merge pull request #55 from justyns/fix-templated-prompts
Browse files Browse the repository at this point in the history
Fix templated prompts
  • Loading branch information
justyns authored Aug 2, 2024
2 parents 3ad63ba + 928d045 commit 63c163b
Show file tree
Hide file tree
Showing 9 changed files with 95 additions and 27 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,7 @@ For in-development code from the main branch:
For the latest "release" code, mostly also still in development for now:

```yaml
- ghr:justyns/silverbullet-ai/0.3.0
- ghr:justyns/silverbullet-ai/0.3.1
```

You can also use the `Plugs: Add` command and enter the above url to install.
Expand Down
6 changes: 5 additions & 1 deletion docs/Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@ This page is a brief overview of each version.
---
## Unreleased
- ???

---
## 0.3.1
- Set searchEmbeddings to false by default
- Fix templated prompts not rendering as a template when not using chat-style prompts
- Add new searchEmbeddings function to AICore library for templates to use
---
## 0.3.0
- Don't index and generate embeddings for pages in Library/
Expand Down
2 changes: 1 addition & 1 deletion docs/Installation.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ For in-development code from the main branch:
For the latest "release" code, mostly also still in development for now:
```yaml
- ghr:justyns/silverbullet-ai/0.3.0
- ghr:justyns/silverbullet-ai/0.3.1
```
You can also use the `Plugs: Add` command and enter the above url to install.
Expand Down
21 changes: 21 additions & 0 deletions docs/Library/AICore/Space Script/AI Search Embeddings.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
---
tags:
- spacescript

description: >
This space script allows you to use `{{searchEmbeddings(query)}}` inside of a template. A string
containing the results of the search is returned.
Note that these responses are not cached, so it’s recommended to either immediately bake the rendered template or only use it in a snippet that will be rendered into a note.
---


```space-script
silverbullet.registerFunction({name: "searchEmbeddings"}, async (query) => {
try {
return await syscall("system.invokeFunction", "silverbullet-ai.searchEmbeddingsForChat", query);
} catch (error) {
console.error("Error invoking searchEmbeddingsForChat:", error);
throw error;
}
})
```
2 changes: 2 additions & 0 deletions silverbullet-ai.plug.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ functions:
- slash:complete
queryAI:
path: sbai.ts:queryAI
searchEmbeddingsForChat:
path: src/embeddings.ts:searchEmbeddingsForChat
reloadConfig:
path: sbai.ts:reloadConfig
events:
Expand Down
24 changes: 24 additions & 0 deletions src/embeddings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -509,6 +509,30 @@ export async function debugSearchEmbeddings() {
log("any", "AI: Search results", searchResults);
}

export async function searchEmbeddingsForChat(
query: string | number[],
numResults = 10,
): Promise<string> {
try {
const searchResults = await searchCombinedEmbeddings(query, numResults);
let results = "";
if (searchResults.length > 0) {
for (const r of searchResults) {
results += `>>${r.page}<<\n`;
for (const child of r.children) {
results += `> ${child.text}\n\n`;
}
}
} else {
return "No relevant pages found.";
}
return results;
} catch (error) {
console.error("Error in searchEmbeddingsForChat:", error);
return "An error occurred during the search.";
}
}

/**
* Display an empty "AI: Search" page
*/
Expand Down
2 changes: 1 addition & 1 deletion src/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,7 @@ async function loadAndMergeSettings() {
parseWikiLinks: true,
bakeMessages: true,
customEnrichFunctions: [],
searchEmbeddings: true,
searchEmbeddings: false,
};
const defaultPromptInstructions: PromptInstructions = {
pageRenameSystem: "",
Expand Down
46 changes: 33 additions & 13 deletions src/prompts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
enrichChatMessages,
supportsPlugSlashComplete,
} from "./utils.ts";
import { ChatMessage } from "./types.ts";

// This only works in 0.7.2+, see https://github.com/silverbulletmd/silverbullet/issues/742
export async function aiPromptSlashComplete(
Expand Down Expand Up @@ -161,26 +162,45 @@ export async function insertAiPromptFromTemplate(
cursorPos = await editor.getCursor();
}

if (cursorPos === undefined) {
cursorPos = await getPageLength();
}

console.log("templatetext: ", templateText);

// const renderedTemplate = await renderTemplate(templateText, pageMeta, {
// page: pageMeta,
// });
// console.log("Rendered template:", renderedTemplate);
let messages: ChatMessage[] = [];

let messages = await convertPageToMessages(templateText);
if (selectedTemplate.systemPrompt) {
messages.unshift({
role: "system",
content: selectedTemplate.systemPrompt,
if (!selectedTemplate.chat) {
// non-multi-chat template
const renderedTemplate = await renderTemplate(templateText, pageMeta, {
page: pageMeta,
});
}
if (selectedTemplate.chat && selectedTemplate.enrichMessages) {
messages = await enrichChatMessages(messages);
console.log("Rendered template:", renderedTemplate);
if (selectedTemplate.systemPrompt) {
messages.push({
role: "system",
content: selectedTemplate.systemPrompt,
});
}
messages.push({
role: "user",
content: renderedTemplate.text,
});
} else {
// multi-turn-chat template
messages = await convertPageToMessages(templateText);
if (selectedTemplate.systemPrompt) {
messages.unshift({
role: "system",
content: selectedTemplate.systemPrompt,
});
}
if (selectedTemplate.chat && selectedTemplate.enrichMessages) {
messages = await enrichChatMessages(messages);
}
}

console.log("Messages: ", messages);

await currentAIProvider.streamChatIntoEditor({
messages: messages,
stream: true,
Expand Down
17 changes: 7 additions & 10 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@ import { extractFrontmatter } from "$sb/lib/frontmatter.ts";
import { aiSettings } from "./init.ts";
import { renderTemplate } from "$sbplugs/template/api.ts";
import type { ChatMessage } from "./types.ts";
import { searchCombinedEmbeddings } from "./embeddings.ts";
import {
searchCombinedEmbeddings,
searchEmbeddingsForChat,
} from "./embeddings.ts";

export function folderName(path: string) {
return path.split("/").slice(0, -1).join("/");
Expand Down Expand Up @@ -201,17 +204,11 @@ export async function enrichChatMessages(
if (aiSettings.chat.searchEmbeddings && aiSettings.indexEmbeddings) {
// Search local vector embeddings for relevant context
// TODO: It could be better to turn this into its own message?
const searchResults = await searchCombinedEmbeddings(enrichedContent);
if (searchResults.length > 0) {
const searchResultsText = await searchEmbeddingsForChat(enrichedContent);
if (searchResultsText !== "No relevant pages found.") {
enrichedContent +=
`\n\nThe following pages were found to be relevant to the question. You can use them as context to answer the question. Only partial content is shown. Ask for the whole page if needed. Page name is between >> and <<.\n`;

for (const r of searchResults) {
enrichedContent += `>>${r.page}<<\n`;
for (const child of r.children) {
enrichedContent += `> ${child.text}\n\n`;
}
}
enrichedContent += searchResultsText;
}
}

Expand Down

0 comments on commit 63c163b

Please sign in to comment.