diff --git a/js/plugins/dotprompt/src/prompt.ts b/js/plugins/dotprompt/src/prompt.ts index 6fbe62100..baf7a1388 100644 --- a/js/plugins/dotprompt/src/prompt.ts +++ b/js/plugins/dotprompt/src/prompt.ts @@ -197,11 +197,20 @@ export class Dotprompt implements PromptMetadata { messages: options.messages, docs: options.docs, }); + let renderedPrompt; + let renderedMessages; + if (messages.length > 0 && messages[messages.length - 1].role === 'user') { + renderedPrompt = messages[messages.length - 1].content; + renderedMessages = messages.slice(0, messages.length - 1); + } else { + renderedPrompt = undefined; + renderedMessages = messages; + } return { model: options.model || this.model!, config: { ...this.config, ...options.config }, - messages: messages.slice(0, messages.length - 1), - prompt: messages[messages.length - 1].content, + messages: renderedMessages, + prompt: renderedPrompt, docs: options.docs, output: { format: options.output?.format || this.output?.format || undefined, diff --git a/js/plugins/dotprompt/tests/prompt_test.ts b/js/plugins/dotprompt/tests/prompt_test.ts index 39857923c..dfdcc21ad 100644 --- a/js/plugins/dotprompt/tests/prompt_test.ts +++ b/js/plugins/dotprompt/tests/prompt_test.ts @@ -470,4 +470,25 @@ describe('DotpromptRef', () => { ); }); }); + + it('should render system prompt', () => { + runWithRegistry(registry, () => { + const model = defineModel( + { name: 'echo', supports: { tools: true } }, + async (input) => ({ + message: input.messages[0], + finishReason: 'stop', + }) + ); + const prompt = testPrompt(model, `{{ role "system"}} hi`); + + const rendered = prompt.render({ input: {} }); + assert.deepStrictEqual(rendered.messages, [ + { + content: [{ text: ' hi' }], + role: 'system', + }, + ]); + }); + }); });