{ "cells": [ { "cell_type": "code", "execution_count": null, "id": "20a65655", "metadata": {}, "outputs": [], "source": [ "!pip install -q git+https://github.com/srush/MiniChain\n", "!git clone git+https://github.com/srush/MiniChain; cp -fr MiniChain/examples/* . " ] }, { "cell_type": "code", "execution_count": null, "id": "261f764b", "metadata": { "tags": [ "hide_inp" ] }, "outputs": [], "source": [ "desc = \"\"\"\n", "### Backtrack on Failure\n", "\n", "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)\n", "\n", "\"\"\"" ] }, { "cell_type": "code", "execution_count": null, "id": "b6617fa6", "metadata": { "lines_to_next_cell": 1 }, "outputs": [], "source": [ "from minichain import prompt, Mock, show, OpenAI\n", "import minichain" ] }, { "cell_type": "code", "execution_count": null, "id": "36c42ea3", "metadata": { "lines_to_next_cell": 1 }, "outputs": [], "source": [ "@prompt(Mock([\"dog\", \"blue\", \"cat\"]))\n", "def prompt_generation(model):\n", " return model(\"\")" ] }, { "cell_type": "code", "execution_count": null, "id": "283167b5", "metadata": { "lines_to_next_cell": 1 }, "outputs": [], "source": [ "@prompt(OpenAI(), template=\"Answer 'yes' is {{query}} is a color. Answer:\")\n", "def prompt_validation(model, x):\n", " out = model(dict(query=x))\n", " if out.strip().lower().startswith(\"yes\"):\n", " return x\n", " return model.fail(1)" ] }, { "cell_type": "code", "execution_count": null, "id": "76e99eee", "metadata": { "lines_to_next_cell": 1 }, "outputs": [], "source": [ "def run():\n", " x = prompt_generation()\n", " return prompt_validation(x)" ] }, { "cell_type": "code", "execution_count": null, "id": "45a2e713", "metadata": {}, "outputs": [], "source": [ "gradio = show(run,\n", " examples = [],\n", " subprompts=[prompt_generation, prompt_validation],\n", " out_type=\"markdown\"\n", " )" ] }, { "cell_type": "code", "execution_count": null, "id": "24791bf0", "metadata": {}, "outputs": [], "source": [ "if __name__ == \"__main__\":\n", " gradio.launch()" ] } ], "metadata": { "jupytext": { "cell_metadata_filter": "tags,-all", "main_language": "python", "notebook_metadata_filter": "-all" } }, "nbformat": 4, "nbformat_minor": 5 }