minichain / backtrack.py~
srush's picture
srush HF staff
Upload with huggingface_hub
f5ec828
raw
history blame
758 Bytes
# + tags=["hide_inp"]
desc = """
### Backtrack on Failure
Chain that backtracks on failure. [[Code](https://github.com/srush/MiniChain/blob/main/examples/backtrack.py)]
"""
# -
from minichain import prompt, Mock, show
import minichain
@prompt(Mock(["red", "blue"]))
def prompt_function1(model, x):
return model(x)
@prompt(Mock(["b"]), template_file="test.pmpt.tpl")
def prompt_function2(model, x):
if x == "red":
return model.fail(1)
return model(dict(x=x))
def run(query):
x = prompt_function1(query)
return prompt_function2(prompt_function2(x))
demo = show(run,
examples=["a"],
subprompts=[prompt_function1, prompt_function2, prompt_function2])
if __name__ == "__main__":
demo.launch()