{ "cells": [ { "cell_type": "code", "execution_count": null, "id": "2f9d8993", "metadata": {}, "outputs": [], "source": [ "!pip install -q git+https://github.com/srush/MiniChain\n", "!git clone https://github.com/srush/MiniChain; cp -fr MiniChain/examples/* . " ] }, { "cell_type": "markdown", "id": "c41f0994", "metadata": {}, "source": [ "Answer a math problem with code.\n", "Adapted from Dust [maths-generate-code](https://dust.tt/spolu/a/d12ac33169)" ] }, { "cell_type": "code", "execution_count": null, "id": "ccc349e6", "metadata": {}, "outputs": [], "source": [ "from minichain import Backend, JinjaPrompt, Prompt, start_chain, SimplePrompt, show_log" ] }, { "cell_type": "markdown", "id": "0af5e0ab", "metadata": {}, "source": [ "Prompt that asks LLM for code from math." ] }, { "cell_type": "code", "execution_count": null, "id": "9063448e", "metadata": { "lines_to_next_cell": 2 }, "outputs": [], "source": [ "class ColorPrompt(Prompt[str, bool]):\n", " def parse(inp: str) -> str:\n", " return f\"Answer 'Yes' if this is a color, {inp}. Answer:\"\n", "\n", " def parse(out: str, inp) -> bool:\n", " # Encode the parsing logic\n", " return out.strip() == \"Yes\" \n", "ColorPrompt().show({\"inp\": \"dog\"}, \"No\")" ] }, { "cell_type": "code", "execution_count": null, "id": "13e05a7c", "metadata": { "lines_to_next_cell": 2 }, "outputs": [], "source": [ "with start_chain(\"color\") as backend:\n", " question = 'What is the sum of the powers of 3 (3^i) that are smaller than 100?'\n", " prompt = MathPrompt(backend.OpenAI()).chain(SimplePrompt(backend.Python()))\n", " result = prompt({\"question\": question})\n", " print(result)" ] }, { "cell_type": "code", "execution_count": null, "id": "37249892", "metadata": {}, "outputs": [], "source": [ "show_log(\"math.log\")" ] } ], "metadata": { "jupytext": { "cell_metadata_filter": "-all", "main_language": "python", "notebook_metadata_filter": "-all" } }, "nbformat": 4, "nbformat_minor": 5 }