minichain / backtrack.py~
srush's picture
srush HF staff
Upload with huggingface_hub
5b30d27
raw
history blame
1.04 kB
# + tags=["hide_inp"]
desc = """
### Backtrack on Failure
Chain that backtracks on failure. [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/srush/MiniChain/blob/master/examples/backtrack.ipynb)
"""
# -
# $
from minichain import prompt, Mock, show, OpenAI
import minichain
@prompt(Mock(["dog", "blue", "cat"]))
def prompt_generation(model):
return model("")
@prompt(OpenAI(), template="Answer 'yes' is {{query}} is a color. Answer:")
def prompt_validation(model, x):
out = model(dict(query=x))
if out.strip().lower().startswith("yes"):
return x
return model.fail(1)
def run():
x = prompt_generation()
return prompt_validation(x)
# $
gradio = show(run,
examples = [],
subprompts=[prompt_generation, prompt_validation],
code=open("backtrack.py", "r").read().split("$")[1].strip().strip("#").strip(),
out_type="markdown"
)
if __name__ == "__main__":
gradio.launch()