Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions docs/dotprompt.md
Original file line number Diff line number Diff line change
Expand Up @@ -442,6 +442,34 @@ The name of the variant is included in the metadata of generation traces, so you
can compare and contrast actual performance between variants in the Genkit trace
inspector.

## Defining Custom Helpers

You can define custom helpers to process and manage data inside of a prompt. Helpers
are registered globally using `defineHelper`:

```ts
import { defineHelper } from '@genkit-ai/dotprompt';

defineHelper('shout', (text: string) => text.toUpperCase());
```

Once a helper is defined you can use it in any prompt:

```none
---
model: vertexai/gemini-1.5-pro
input:
schema:
name: string
---

HELLO, {{shout name}}!!!
```

For more information about the arguments passed into helpers, see the
[Handlebars documentation](https://handlebarsjs.com/guide/#custom-helpers) on creating
custom helpers.

## Alternate ways to load and define prompts

Dotprompt is optimized for organization in the prompt directory. However, there
Expand Down
2 changes: 1 addition & 1 deletion js/plugins/dotprompt/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import { basename } from 'path';
import { defineDotprompt, Dotprompt } from './prompt.js';
import { loadPromptFolder, lookupPrompt } from './registry.js';

export { definePartial } from './template.js';
export { defineHelper, definePartial } from './template.js';
export { defineDotprompt, Dotprompt };

export interface DotpromptPluginOptions {
Expand Down
5 changes: 4 additions & 1 deletion js/plugins/dotprompt/src/template.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,6 @@ export function compile<Variables = any>(
role: true,
history: true,
},
knownHelpersOnly: true,
});

return (
Expand All @@ -172,6 +171,10 @@ export function compile<Variables = any>(
};
}

export function defineHelper(name: string, fn: Handlebars.HelperDelegate) {
Promptbars.registerHelper(name, fn);
}

export function definePartial(name: string, source: string) {
Promptbars.registerPartial(name, source);
}
8 changes: 7 additions & 1 deletion js/testapps/prompt-file/prompts/recipe.prompt
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,16 @@ model: googleai/gemini-pro
input:
schema:
food: string
ingredients?(array): string
output:
schema: Recipe
---

You are a chef famous for making creative recipes that can be prepared in 45 minutes or less.

Generate a recipe for {{food}}.
Generate a recipe for {{food}}.

{{#if ingredients}}
Make sure to include the following ingredients:
{{list ingredients}}
{{/if}}
9 changes: 8 additions & 1 deletion js/testapps/prompt-file/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*/

import { configureGenkit, defineSchema } from '@genkit-ai/core';
import { dotprompt, prompt } from '@genkit-ai/dotprompt';
import { defineHelper, dotprompt, prompt } from '@genkit-ai/dotprompt';
import { defineFlow } from '@genkit-ai/flow';
import { googleAI } from '@genkit-ai/googleai';
import * as z from 'zod';
Expand Down Expand Up @@ -49,6 +49,13 @@ const RecipeSchema = defineSchema(
// If it fails, due to the prompt file being invalid, the process will crash,
// instead of us getting a more mysterious failure later when the flow runs.

defineHelper('list', (data: any) => {
if (!Array.isArray(data)) {
return '';
}
return data.map((item) => `- ${item}`).join('\n');
});

prompt('recipe').then((recipePrompt) => {
defineFlow(
{
Expand Down