Skip to content

Commit

Permalink
Update docs for new post processor functions
Browse files Browse the repository at this point in the history
  • Loading branch information
justyns committed Sep 9, 2024
1 parent 1488c4e commit 8179126
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 5 deletions.
10 changes: 9 additions & 1 deletion docs/Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,19 @@ For the full changelog, please refer to the individual release notes on https://
This page is a brief overview of each version.

---
## Unreleased
## 0.4.0 (Unreleased)
- Use a separate queue for indexing embeddings and summaries, to prevent blocking the main SB indexing thread
- Refactor to use JSR for most Silverbullet imports, and lots of related changes
- Reduced bundle size
- Add support for [space-config](https://silverbullet.md/Space%20Config)
- Add support for [[Templated Prompts|Post Processor]] functions in [[Templated Prompts]].
- AICore Library: Updated all library files to have the meta tag.
- AICore Library: Add space-script functions to be used as post processors:
- **indentOneLevel** - Indent entire response one level deeper than the previous line.
- **removeDuplicateStart** - Remove the first line from the response if it matches the line before the response started.
- **convertToBulletList** - Convert response to a markdown list.
- **convertToTaskList** - Convert response to a markdown list of tasks.
- AICore Library: Add `aiSplitTodo` slash command and [[^Library/AICore/AIPrompt/AI Split Task]] templated prompt to split a task into smaller subtasks.

---
## 0.3.2
Expand Down
2 changes: 1 addition & 1 deletion docs/Library/AICore/AIPrompt/AI Create Space Script.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ aiprompt:

SilverBullet space script documentation:

[[!silverbullet.md/Space%20Script]]
[Space%20Script](https://silverbullet.md/Space%20Script)

Using the above documentation, please create a space-script following the users description in the note below. Output only valid markdown with a code block using space-script. No explanations, code in a markdown space-script block only. Must contain **silverbullet.registerFunction** or **silverbullet.registerCommand**. Use syscalls where available, but only if you know for sure they exist.

Expand Down
2 changes: 1 addition & 1 deletion docs/Library/AICore/New Page/AI New Chat.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@ frontmatter:

**assistant**: Hello, how can I help you?

**user**: |^|
**user**: |^|
78 changes: 76 additions & 2 deletions docs/Templated Prompts.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ To be a templated prompt, the note must have the following frontmatter:
- Optionally, `aiprompt.systemPrompt` can be specified to override the system prompt
- Optionally, `aiprompt.chat` can be specified to treat the template as a multi-turn chat instead of single message
- Optionally, `aiprompt.enrichMessages` can be set to true to enrich each chat message
-
- Optionally, `aiprompt.postProcessors` can be set to a list of space-script function names to manipulate text returned by the llm

For example, here is a templated prompt to summarize the current note and insert the summary at the cursor:

Expand Down Expand Up @@ -65,6 +65,23 @@ Everything below is the content of the note:
{{readPage(@page.ref)}}
```


## Template Metadata

As of version 0.4.0, the following global metadata is available for use inside of an aiPrompt template:

* **`page`**: Metadata about the current page.
* **`currentItemBounds`**: Start and end positions of the current item. An item may be a bullet point or task.
* **`currentItemText`**: Full text of the current item.
* **`currentLineNumber`**: Line number of the current cursor position.
* **`lineStartPos`**: Starting character position of the current line.
* **`lineEndPos`**: Ending character position of the current line.
* **`currentPageText`**: Entire text of the current page.
* **`parentItemBounds`**: Start and end positions of the parent item.
* **`parentItemText`**: Full text of the parent item. A parent item may contain child items.

All of these can be accessed by prefixing the variable name with `@`, like `@lineEndPos` or `@currentLineNumber`.

## Chat-style prompts

As of version 0.3.0, `aiprompt.chat` can be set to true in the template frontmatter to treat the template similar to a page using [[Commands/AI: Chat on current page]].
Expand Down Expand Up @@ -96,4 +113,61 @@ Everything below is the content of the note:

These messages will be parsed into multiple chat messages when calling the LLM’s api. Only the response from the LLM will be included in the note where the template is triggered from.

The `enrich` attribute can also be toggled on or off per message. By default it is either disabled or goes off of the `aiPrompt.enrichMessages` frontmatter attribute. Assistant and system messages are never enriched.
The `enrich` attribute can also be toggled on or off per message. By default it is either disabled or goes off of the `aiPrompt.enrichMessages` frontmatter attribute. Assistant and system messages are never enriched.

## Post Processors

As of version 0.4.0, `aiPrompt.postProcessors` can be set to a list of space-script function names like in the example below. Once the LLM finishes streaming its response, the entire response will be sent to each post processor function in order.

Each function must accept a single data parameter. Currently, the parameter follows this typing:

```javascript
export type PostProcessorData = {
// The full response text
response: string;
// The line before where the response was inserted
lineBefore: string;
// The line after where the response was inserted
lineAfter: string;
// The line where the cursor was before the response was inserted
lineCurrent: string;
};
```

A simple post processing function looks like this:

```javascript
silverbullet.registerFunction({ name: "aiFooBar" }, async (data) => {

// Extract variables from PostProcessorData
const { response, lineBefore, lineCurrent, lineAfter } = data;

// Put the current response between FOO and BAR and return it
const newResponse = `FOO ${response} BAR`;
return newResponse
}
```
This function could be used in a template prompt like this:
```yaml
---
tags:
- template
- aiPrompt
- meta

description: "Generate a random pet name"
aiprompt:
description: "Generate a random pet name."
slashCommand: aiGeneratePetName
insertAt: cursor
postProcessors:
- aiFooBar
---

Generate a random name for a pet. Only generate a single name. Return nothing but that name.
```
Running this prompt, the LLM may return `Henry` as the name and then aiFooBar will transform it into `FOO Henry BAR` which is what will ultimately be placed in the note the templated was executed from.
4 changes: 4 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,8 +152,12 @@ export type EmbeddingModelConfig = {
};

export type PostProcessorData = {
// The full response text
response: string;
// The line before where the response was inserted
lineBefore: string;
// The line after where the response was inserted
lineAfter: string;
// The line where the cursor was before the response was inserted
lineCurrent: string;
};

0 comments on commit 8179126

Please sign in to comment.