{ "cells": [ { "cell_type": "markdown", "id": "32e022a2", "metadata": {}, "source": [ "# PAL\n", "\n", "Implements Program-Aided Language Models, as in https://arxiv.org/pdf/2211.10435.pdf.\n" ] }, { "cell_type": "code", "execution_count": 1, "id": "1370e40f", "metadata": {}, "outputs": [], "source": [ "from langchain.chains import PALChain\n", "from langchain import OpenAI" ] }, { "cell_type": "code", "execution_count": 2, "id": "beddcac7", "metadata": {}, "outputs": [], "source": [ "llm = OpenAI(model_name='code-davinci-002', temperature=0, max_tokens=512)\n", "pal_chain = PALChain.from_math_prompt(llm, verbose=True)" ] }, { "cell_type": "code", "execution_count": 3, "id": "e2eab9d4", "metadata": {}, "outputs": [], "source": [ "question = \"Jan has three times the number of pets as Marcia. Marcia has two more pets than Cindy. If Cindy has four pets, how many total pets do the three have?\"" ] }, { "cell_type": "code", "execution_count": 4, "id": "3ef64b27", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "\n", "\u001b[1m> Entering new chain...\u001b[0m\n", "\u001b[32;1m\u001b[1;3mdef solution():\n", " \"\"\"Jan has three times the number of pets as Marcia. Marcia has two more pets than Cindy. If Cindy has four pets, how many total pets do the three have?\"\"\"\n", " cindy_pets = 4\n", " marcia_pets = cindy_pets + 2\n", " jan_pets = marcia_pets * 3\n", " total_pets = cindy_pets + marcia_pets + jan_pets\n", " result = total_pets\n", " return result\u001b[0m\n", "\n", "\u001b[1m> Finished chain.\u001b[0m\n" ] }, { "data": { "text/plain": [ "'28'" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "pal_chain.run(question)" ] }, { "cell_type": "code", "execution_count": 5, "id": "e524f81f", "metadata": {}, "outputs": [], "source": [ "llm = OpenAI(model_name='code-davinci-002', temperature=0, max_tokens=512)\n", "pal_chain = PALChain.from_colored_object_prompt(llm, verbose=True)" ] }, { "cell_type": "code", "execution_count": 6, "id": "03a237b8", "metadata": {}, "outputs": [], "source": [ "question = \"On the desk, you see two blue booklets, two purple booklets, and two yellow pairs of sunglasses. If I remove all the pairs of sunglasses from the desk, how many purple items remain on it?\"" ] }, { "cell_type": "code", "execution_count": 7, "id": "a84a4352", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "\n", "\u001b[1m> Entering new chain...\u001b[0m\n", "\u001b[32;1m\u001b[1;3m# Put objects into a list to record ordering\n", "objects = []\n", "objects += [('booklet', 'blue')] * 2\n", "objects += [('booklet', 'purple')] * 2\n", "objects += [('sunglasses', 'yellow')] * 2\n", "\n", "# Remove all pairs of sunglasses\n", "objects = [object for object in objects if object[0] != 'sunglasses']\n", "\n", "# Count number of purple objects\n", "num_purple = len([object for object in objects if object[1] == 'purple'])\n", "answer = num_purple\u001b[0m\n", "\n", "\u001b[1m> Finished chain.\u001b[0m\n" ] }, { "data": { "text/plain": [ "'2'" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "pal_chain.run(question)" ] }, { "cell_type": "code", "execution_count": null, "id": "4ab20fec", "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.8.7" } }, "nbformat": 4, "nbformat_minor": 5 }