Ironclad / rivet

The open-source visual AI programming environment and TypeScript library
https://rivet.ironcladapp.com
MIT License
2.55k stars 226 forks source link

[Feature]: Load a prompt from an external file and use inputs #418

Open selimrbd opened 2 weeks ago

selimrbd commented 2 weeks ago

Feature Request

I'd like to load my prompts from external text files, so they are versioned cleanly.

The Read File doesn't solve my problem: The variables can't be linked to inputs in the resulting text node.

Example:

Screenshot 2024-06-12 at 21 17 24

I would like {{userInstructions}} and {{context}} to be available as variables / inputs.

Is it possible to do this currently in Rivet ?

If not, what workaround do you use to properly version your prompts ?

Code of Conduct

selimrbd commented 2 weeks ago

This is my current solution, using a Code Node to replace the inputs in the prompt.

Screenshot 2024-06-12 at 22 06 18

My prompt text file (prompt.txt):

This is the beginning of my prompt

{{userInstructions}}
{{userContext}}

Here are additional instructions.

And the parsing code :

const prompt = inputs.prompt.value
const variables = Object.fromEntries(
    Object.entries(inputs).filter(([key, _]) => key !== "prompt")
);

function replacePlaceholders(prompt, variables) {
    var result = prompt
    for(let placeholder in variables) {
        let regex = new RegExp(`{{${placeholder}}}`, 'g')
        result = result.replace(regex, variables[placeholder].value);
    }
    return result;
}

const outputString = replacePlaceholders(prompt, variables);

return {
    output: {
        type: 'string',
        value: outputString
    }
};

Does anybody have a more elegant / less complicated solution ?