MiniChain icon indicating copy to clipboard operation
MiniChain copied to clipboard

access the rendered template during development/testing

Open gureckis opened this issue 1 year ago • 1 comments

Hi great looking library. Love the simplicity!
I'm playing with things and exploring the examples but I'm sort of curious how I could print out my rendered template prior to running it. For example from this basic example:

@prompt(OpenAI(), template_file="math.pmpt.tpl")
def math_prompt(model, question):
    "Prompt to call GPT with a Jinja template"
    return model(dict(question=question))

I see that a call to math_prompt('what is 1+1?').run() will execute but was thinking with the lazy loading that something like math_prompt("what is 1+1?").render() would print out the rendered jinja template. I see how could just use jinja for that but in the flow of debugging it seems a useful utility function. Happy to make a PR if you give me a hint or let me know how you would think about this.

<inor update... part of the reason is that the function might itself provide new inputs to the template using hard to predict computations. As a simple example, I look up the date today and add it to the template:

@prompt(Mock(answers=["blah"]), template_file="test.pmpt.tpl")
def date_prompt(model, question):
    "Prompt to call GPT with a Jinja template"
    today = date.today().strftime("%A %B %d, %Y")
    print(model.prompt.template_fill(dict(question=question, today=today)))
    return model(dict(question=question, today=today))

where test.pmpt.tpl is

Todays date is {{today}}
{{question}}

The snippet above worked to print out the final template during the run but maybe could be done prompt or chain itself.

gureckis avatar Apr 02 '23 19:04 gureckis