Skip to content

Commit c427633

Browse files
authored
Merge pull request #53 from ferrislucas/use-liquid-in-prompts
Support Liquidjs templating in user prompts
2 parents 554587e + 033c5e7 commit c427633

File tree

2 files changed

+23
-6
lines changed

2 files changed

+23
-6
lines changed

src/services/TemplateLoader.js

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,24 @@ class TemplateLoader {
1010
await this.loadTemplateFromUrl(template) :
1111
await this.loadTemplateFromPath(template)
1212
}
13+
const expandedPrompt = await this.parseTemplate(prompt, context)
1314
const engine = new Liquid()
1415
engine.registerFilter("jsonToObject", (json) => JSON.parse(json))
15-
const tpl = engine.parse(templateText)
16+
const tpl = engine.parse(templateText)
1617
const content = await engine.render(tpl, {
1718
context: context,
18-
prompt: prompt
19+
prompt: expandedPrompt
20+
})
21+
return content
22+
}
23+
24+
static async parseTemplate(text, context) {
25+
const engine = new Liquid()
26+
engine.registerFilter("jsonToObject", (json) => JSON.parse(json))
27+
const tpl = engine.parse(text)
28+
const content = await engine.render(tpl, {
29+
context: context,
30+
prompt: text
1931
})
2032
return content
2133
}
@@ -38,7 +50,7 @@ class TemplateLoader {
3850

3951
static getTemplateText(template) {
4052
if (template === 'refactor') {
41-
return `The user's request is: {{prompt}}
53+
return `{{prompt}}
4254
{% if context.files.size > 0 %}
4355
###
4456
The following are file paths and content related to this request.
@@ -54,9 +66,9 @@ File: {{ item.filename }} """
5466
if (template === 'empty') {
5567
return `{% if context.files.size > 0 %}You will be provided the contents of some files.
5668
The contents of each file begin with "--BEGIN-FILE:" followed by the file path.
57-
The contents of each file end with "--END-FILE--".{% endif %}
58-
59-
Your instructions are: {{prompt}}
69+
The contents of each file end with "--END-FILE--".
70+
{% endif %}
71+
{{prompt}}
6072
{% if context.files.size > 0 %}The file contents are below:{% endif %}
6173
{% for item in context.files %}
6274
--BEGIN-FILE: {{ item.filename }}

test/templateLoader.test.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,11 @@ describe('TemplateLoader', () => {
2424

2525
loadTemplateFromPathStub.restore();
2626
});
27+
28+
it('should expand liduidjs templating tags in the prompt', async () => {
29+
const result = await TemplateLoader.loadTemplate('prompt: {{context.test}}', { test: 42 }, 'empty')
30+
assert.strictEqual(result.trim(), 'prompt: 42')
31+
})
2732
});
2833

2934
describe('loadTemplateFromUrl', () => {

0 commit comments

Comments
 (0)