Skip to content

Commit

Permalink
Importtemplate (#684)
Browse files Browse the repository at this point in the history
* support for template propmpts

* Update dependencies and integrate variable interpolation in markdown processing

* Refactor interpolation logic by moving it from markdown.ts to a new file mustache.ts and update imports in promptdom.ts

* Refactor test generation script and implement interpolation tests for mustache module

* Add support for importing template data in prompt rendering functions

* Added mustache template rendering and updated types and sample files for improved template interpolation

* Update keywords and add hint function in import-template.genai.mts

* Add docstring to interpolateVariables function in mustache.ts

* Add importTemplate documentation and improve guidelines in pr-review-commit script

* front matter
  • Loading branch information
pelikhan authored Sep 3, 2024
1 parent 9c9d65b commit c0e3673
Show file tree
Hide file tree
Showing 34 changed files with 1,052 additions and 468 deletions.
1 change: 1 addition & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
"pelikhan",
"promptdom",
"promptfoo",
"prompty",
"stringifying",
"sysr",
"tabletojson",
Expand Down
42 changes: 21 additions & 21 deletions THIRD_PARTY_LICENSES.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ https://www.npmjs.com/package/generate-license-file

The following npm package may be included in this product:

- pdfjs-dist@4.5.136
- pdfjs-dist@4.6.82

This package contains the following license and notice below:

Expand Down Expand Up @@ -1458,7 +1458,7 @@ The following npm packages may be included in this product:
- @types/http-cache-semantics@4.0.4
- @types/jsonfile@6.1.4
- @types/mute-stream@0.0.4
- @types/node@22.5.1
- @types/node@22.5.2
- @types/turndown@5.0.5
- @types/yauzl@2.10.3

Expand Down Expand Up @@ -1520,7 +1520,7 @@ MIT License

The following npm package may be included in this product:

- genaiscript-vscode@1.52.0
- genaiscript-vscode@1.53.1

This package contains the following license and notice below:

Expand Down Expand Up @@ -2044,7 +2044,7 @@ software or this license, under any kind of legal claim._**

The following npm package may be included in this product:

- [email protected].5
- [email protected].7

This package contains the following license and notice below:

Expand Down Expand Up @@ -2921,7 +2921,7 @@ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
The following npm packages may be included in this product:

- [email protected]
- [email protected].1
- [email protected].2
- [email protected]
- [email protected]

Expand Down Expand Up @@ -3320,20 +3320,20 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

The following npm packages may be included in this product:

- @inquirer/checkbox@2.4.7
- @inquirer/confirm@3.1.22
- @inquirer/core@9.0.10
- @inquirer/editor@2.1.22
- @inquirer/expand@2.1.22
- @inquirer/checkbox@2.5.0
- @inquirer/confirm@3.2.0
- @inquirer/core@9.1.0
- @inquirer/editor@2.2.0
- @inquirer/expand@2.2.0
- @inquirer/figures@1.0.5
- @inquirer/input@2.2.9
- @inquirer/number@1.0.10
- @inquirer/password@2.1.22
- @inquirer/prompts@5.3.8
- @inquirer/rawlist@2.2.4
- @inquirer/search@1.0.7
- @inquirer/select@2.4.7
- @inquirer/type@1.5.2
- @inquirer/input@2.3.0
- @inquirer/number@1.1.0
- @inquirer/password@2.2.0
- @inquirer/prompts@5.4.0
- @inquirer/rawlist@2.3.0
- @inquirer/search@1.1.0
- @inquirer/select@2.5.0
- @inquirer/type@1.5.3

These packages each contain the following license and notice below:

Expand Down Expand Up @@ -4582,9 +4582,9 @@ The following npm packages may be included in this product:
- [email protected]
- [email protected]
- [email protected]
- genaiscript-core-internal@1.52.0
- genaiscript-sample@1.52.0
- genaiscript@1.52.0
- genaiscript-core-internal@1.53.1
- genaiscript-sample@1.53.1
- genaiscript@1.53.1
- [email protected]
- [email protected]
- [email protected]
Expand Down
20 changes: 20 additions & 0 deletions docs/genaisrc/genaiscript.d.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions docs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
},
"dependencies": {
"@astrojs/check": "^0.9.3",
"@astrojs/starlight": "^0.26.1",
"astro": "^4.15.1",
"@astrojs/starlight": "^0.26.2",
"astro": "^4.15.2",
"typescript": "5.5.4"
},
"devDependencies": {
Expand Down
79 changes: 79 additions & 0 deletions docs/src/content/docs/reference/scripts/import-template.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
---
title: Import Template
sidebar:
order: 50
description: Learn how to import prompt templates into GenAIScript using
`importTemplate` with support for mustache variable interpolation and file
globs.
keywords: importTemplate, prompts, mustache, variable interpolation, file globs

---

Various LLM tools allow to store prompts in text or markdown files.
You can use `importTemplate` to import these files into the prompt.

```markdown title="cot.md"
Explain your answer step by step.
```

```js title="tool.genai.mjs"
importTemplate("cot.md");
```

## Variable interpolation

`importTemplate` supports [mustache](https://mustache.github.io/) variable interpolation. You can use variables in the imported template and pass them as arguments to the `importTemplate` function.

```markdown title="time.md"
The current time is {{time}}.
```

```js title="tool.genai.mjs"
importTemplate("time.md", { time: "12:00" });
```

Mustache supports arguments as functions. This allows you to pass dynamic values to the template.

```js title="tool.genai.mjs"
importTemplate("time.md", { time: () => Date.now() });
```


## File globs

You can specify an array of files or glob patterns.

```js
importTemplate("*.prompt")
```

## Other File formats

Aside from the basic text or markdown formats, `importTemplate` also automatically supports these variations.

### Prompty

[Prompty](https://prompty.ai/) provides a simple markdown-based format for prompts. It adds the concept of role sections to the markdown format.

```markdown
---
name: Basic Prompt
description: A basic prompt that uses the chat API to answer questions
...
inputs:
question:
type: string
sample:
"question": "Who is the most famous person in the world?"
---
system:
You are an AI assistant who helps people find information.
As the assistant, you answer questions briefly, succinctly.

user:
{{question}}
```

```js title="tool.genai.mjs"
importTemplate("basic.prompty", { question: "what is the capital of France?" });
```
Loading

0 comments on commit c0e3673

Please sign in to comment.