microsoft / sammo

A library for prompt engineering and optimization (SAMMO = Structure-aware Multi-Objective Metaprompt Optimization)
MIT License
581 stars 29 forks source link

Add MultiStepRewrite Mutator #47

Open pbourke opened 2 months ago

pbourke commented 2 months ago

This issue proposes the addition of a MultiStepRewrite mutator, which allows a single content segment to be rewritten using multiple templated generation calls. This class can be used as a basis for subsequent library mutators or used directly from application code to support a specific mutation scenario.

This mutator will support more sophisticated, deep mutations by allowing successive generation calls that "thread" the prior prompt texts and results into the next template. This allows for a larger context to be accumulated before a final call to generate rewritten text.

usage example:

step1_template = """
Given a set of instructions and a critical style, produce a set of notes detailing how the instructions fall short of the critical style.

Instructions: {{{content}}}

Critical Style: {{{critical_style}}}

Notes about Critical Style:
"""

step2_template = """
{{{last_prompt}}}

{{{last_result}}}

Using your notes about the critical style, generate an updated set of instructions that addresses the shortcomings.

Updated Instructions:
"""
mutator = MultiStepRewrite(
    path_descriptor={ "id": "instructions" },
    templates=[ step1_template, step2_template ],
    critical_style="Instructions should be precise and explicit, and proceed step by step"
)

The mutation specified above will be executed as follows:

  1. The segment content is resolved from the node at id=instructions for the content variable
  2. A Template is instantiated from the contents of step1_template and the content and critical_style variables will be passed to the template render function
  3. Text is generated using the rendered template
  4. A Template is instantiated from step2_template and will receive the content and critical_style variables in addition to last_prompt and last_result which contains the rendered prompt and generated text from the prior step
  5. Text is generated using the rendered template and mutation proceeds as per normal, with the instructions node updated with the new contents

In addition to the special variables last_prompt and last_result, which are populated for all template renders after the first one, the caller can specify arbitrary additional template variables that will be passed to all template renders including the first one. critical_style is an example of such a variable.