Skip to content

Commit

Permalink
fix variable format in the document check node
Browse files Browse the repository at this point in the history
  • Loading branch information
au-re committed Oct 31, 2023
1 parent 3787827 commit 6065461
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export const documentCheck: NodeConfig = {
name: "Prompt",
description: "Prompt to check the document with",
type: "text",
defaultValue: `Extract information in the document below and insert them in the csv table, don't overwrite existing values and keep things empty if you cannot find information in the document:\n\nTABLE EXAMPLE:\ncharacters, age\nmickey mouse, 10\ndonald duck, -\n\nTABLE:\n[[table]]\n\nDOCUMENT:\n[[document]]\n\nTABLE:\n`,
defaultValue: `Extract information in the document below and insert them in the csv table, don't overwrite existing values and keep things empty if you cannot find information in the document:\n\nTABLE EXAMPLE:\ncharacters, age\nmickey mouse, 10\ndonald duck, -\n\nTABLE:\n{{table}}\n\nDOCUMENT:\n{{document}}\n\nTABLE:\n`,
},
{
id: "table",
Expand Down
14 changes: 13 additions & 1 deletion packages/@pufflig/ps-nodes/src/data/prompt/prompt.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,19 @@ test("execute - no variables", async () => {
});
expect(variables).toMatchInlineSnapshot(`
{
"prompt": "summarize ",
"prompt": "summarize {{longText}}",
}
`);
});

test("execute - empty variables", async () => {
const variables = await execute({
template: `summarize {{longText}}`,
longText: "",
});
expect(variables).toMatchInlineSnapshot(`
{
"prompt": "summarize {{longText}}",
}
`);
});
Expand Down
13 changes: 12 additions & 1 deletion packages/@pufflig/ps-nodes/src/data/prompt/prompt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,18 @@ export interface TemplateTextOutput {

export const execute = async (input: TemplateTextInput) => {
const { template, ...variables } = input;
const renderedTemplate = Mustache.render(template, variables);

// keep template values for undefined variables
const extractedVariables = extractVariables(template) || [];
const variablesWithDefaults = extractedVariables.reduce((acc, param) => {
return {
...acc,
[param.id]:
variables[param.id] === undefined || variables[param.id] === "" ? `{{${param.id}}}` : variables[param.id],
};
}, {} as Record<string, string>);

const renderedTemplate = Mustache.render(template, variablesWithDefaults);
return {
prompt: renderedTemplate,
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ export const execute: Execute<LLMCompletionInput, LLMCompletionOutput> = async (
const { modelId, parameters } = model;
const { globals } = options;

const renderedPrompt = Mustache.render(prompt, variables);
// render the prompt without overwriting the document and table variables
const renderedPrompt = Mustache.render(prompt, { ...variables, document: "{{document}}", table: "{{table}}" });

const { result } = await refineCompletion({
apiKey: getPromptStudioKey(globals || {}),
Expand Down

0 comments on commit 6065461

Please sign in to comment.