From 53421907d6d33bcba1e5a4ee34c118b8ca3d34ba Mon Sep 17 00:00:00 2001 From: Eugene Yurtsev Date: Mon, 15 May 2023 14:51:19 -0400 Subject: [PATCH] x --- docs/source/examples/search_and_math.ipynb | 786 +++++++++++++++++++++ 1 file changed, 786 insertions(+) create mode 100644 docs/source/examples/search_and_math.ipynb diff --git a/docs/source/examples/search_and_math.ipynb b/docs/source/examples/search_and_math.ipynb new file mode 100644 index 0000000..ab3df84 --- /dev/null +++ b/docs/source/examples/search_and_math.ipynb @@ -0,0 +1,786 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "id": "7193140d", + "metadata": { + "pycharm": { + "name": "#%% md\n" + } + }, + "source": [ + "# Chain with Search and Math functionality\n", + "\n", + "Let's see how we can expand the complexity of the code that gets run.\n", + "\n", + "Here we will write a `Kork` chain that can combine search functionality with math." + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "id": "56029d65", + "metadata": { + "collapsed": false, + "jupyter": { + "outputs_hidden": false + }, + "pycharm": { + "name": "#%%\n" + }, + "tags": [ + "remove_cell" + ] + }, + "outputs": [], + "source": [ + "%load_ext autoreload\n", + "%autoreload 2\n", + "\n", + "import sys\n", + "\n", + "sys.path.insert(0, \"../\")" + ] + }, + { + "cell_type": "markdown", + "id": "28fd2e2c", + "metadata": { + "pycharm": { + "name": "#%% md\n" + } + }, + "source": [ + "For the search component, we'll be using a langchain agent that can search using the serpapi." + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "id": "4ea4e0b0", + "metadata": { + "collapsed": false, + "jupyter": { + "outputs_hidden": false + }, + "pycharm": { + "name": "#%%\n" + }, + "tags": [] + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "\u001b[1m[\u001b[0m\u001b[34;49mnotice\u001b[0m\u001b[1;39;49m]\u001b[0m\u001b[39;49m A new release of pip is available: \u001b[0m\u001b[31;49m23.1\u001b[0m\u001b[39;49m -> \u001b[0m\u001b[32;49m23.1.2\u001b[0m\n", + "\u001b[1m[\u001b[0m\u001b[34;49mnotice\u001b[0m\u001b[1;39;49m]\u001b[0m\u001b[39;49m To update, run: \u001b[0m\u001b[32;49mpython3.10 -m pip install --upgrade pip\u001b[0m\n" + ] + } + ], + "source": [ + "!pip install google-search-results > /dev/null" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "id": "975ce14f", + "metadata": { + "collapsed": false, + "jupyter": { + "outputs_hidden": false + }, + "pycharm": { + "name": "#%%\n" + }, + "tags": [] + }, + "outputs": [], + "source": [ + "from typing import List, Any, Optional\n", + "\n", + "import langchain\n", + "from langchain import OpenAI\n", + "from langchain.agents import initialize_agent, AgentType, load_tools\n", + "\n", + "from kork.parser import parse\n", + "from kork import CodeChain, InterpreterResult, Environment" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "id": "82ad4500", + "metadata": { + "collapsed": false, + "jupyter": { + "outputs_hidden": false + }, + "pycharm": { + "name": "#%%\n" + }, + "tags": [] + }, + "outputs": [], + "source": [ + "llm = OpenAI(temperature=0)\n", + "tools = load_tools([\"serpapi\"], llm=llm)\n", + "agent = initialize_agent(\n", + " tools, llm, agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION, verbose=True\n", + ")" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "id": "80592ac5", + "metadata": { + "collapsed": false, + "jupyter": { + "outputs_hidden": false + }, + "pycharm": { + "name": "#%%\n" + }, + "tags": [] + }, + "outputs": [], + "source": [ + "def concat(str1: str, str2: str) -> str:\n", + " \"\"\"Concatenate two strings together.\"\"\"\n", + " return str1 + str2\n", + "\n", + "\n", + "def search(query: str, expected_type: str) -> str:\n", + " \"\"\"Search the internet for a result. The result is always of a string type, and may need to be cast.\"\"\"\n", + " if expected_type == \"int\":\n", + " return int(agent.run(query + \" Output your answer as a number.\"))\n", + " return agent.run(query)" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "id": "7ff30e6f-dc87-4da5-b70d-36c1b3bf63f4", + "metadata": { + "pycharm": { + "name": "#%%\n" + }, + "tags": [] + }, + "outputs": [], + "source": [ + "import math\n", + "import operator\n", + "\n", + "funcs = [\n", + " math.sin,\n", + " math.sinh,\n", + " math.asin,\n", + " math.atan,\n", + " math.acos,\n", + " math.cos,\n", + " math.cosh,\n", + " math.tan,\n", + " math.tanh,\n", + " math.ceil,\n", + " math.floor,\n", + " math.dist,\n", + " math.degrees,\n", + " math.radians,\n", + " math.exp,\n", + " math.log10,\n", + " math.log2,\n", + " math.pow,\n", + " operator.ge,\n", + " operator.le,\n", + " operator.eq,\n", + " operator.gt,\n", + " operator.lt,\n", + " search,\n", + " concat,\n", + "]" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "id": "72cd9233", + "metadata": { + "collapsed": false, + "jupyter": { + "outputs_hidden": false + }, + "pycharm": { + "name": "#%%\n" + }, + "tags": [] + }, + "outputs": [], + "source": [ + "examples = [\n", + " (\"calculate the sqrt of 2\", \"let result = pow(2, 0.5)\"),\n", + " (\"2*5 + 1\", \"let result = 2 * 5 + 1\"),\n", + " (\"1.3e-3\", \"let result = 1.3 * pow(10, -3)\"),\n", + " (\"2**5\", \"let result = pow(2, 5)\"),\n", + " (\"calculate log of 2\", \"let result = log2(2)\"),\n", + " (\"tan of 0.3\", \"let result = tan(0.3)\"),\n", + " (\n", + " \"every day i eat 3 donuts. how many donuts do i eat during the week\",\n", + " \"let days_in_week = 7; let result = days_in_week * 3;\",\n", + " ),\n", + " (\"is 2 > 1?\", \"let result = gt(2, 1);\"),\n", + " (\n", + " \"where is the eiffel tower?\",\n", + " 'let result = search(\"Where is the eiffel tower?\", \"str\")',\n", + " ),\n", + " (\n", + " \"how many people have been to the moon?\",\n", + " 'let result = search(\"how many people have been to the moon?\", \"int\")',\n", + " ),\n", + " (\n", + " \"the sin of the the distance to the moon \",\n", + " 'let result = sin(search(\"how many people have been to the moon?\", \"int\"))',\n", + " ),\n", + " (\n", + " \"how old is the moon?\",\n", + " 'let result = search(\"how old is the moon?\", \"int\")',\n", + " ),\n", + " ('add \"hello\" and \"goodbye\"?', 'let result = concat(\"hello\", \"goodbye\")'),\n", + "]" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "id": "78cc4c0c", + "metadata": { + "collapsed": false, + "jupyter": { + "outputs_hidden": false + }, + "pycharm": { + "name": "#%%\n" + }, + "tags": [] + }, + "outputs": [], + "source": [ + "examples_in_ast = [(query, parse(code)) for query, code in examples]" + ] + }, + { + "cell_type": "markdown", + "id": "0013f74b", + "metadata": { + "pycharm": { + "name": "#%% md\n" + } + }, + "source": [ + "## Create chain" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "id": "ee86454c", + "metadata": { + "collapsed": false, + "jupyter": { + "outputs_hidden": false + }, + "pycharm": { + "name": "#%%\n" + }, + "tags": [] + }, + "outputs": [], + "source": [ + "chain = CodeChain.from_defaults(llm=llm, examples=examples_in_ast, context=funcs)" + ] + }, + { + "cell_type": "markdown", + "id": "81f89e79", + "metadata": { + "pycharm": { + "name": "#%% md\n" + } + }, + "source": [ + "use to toggle verbosity" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "id": "ffb1a897", + "metadata": { + "collapsed": false, + "jupyter": { + "outputs_hidden": false + }, + "pycharm": { + "name": "#%%\n" + }, + "tags": [] + }, + "outputs": [], + "source": [ + "langchain.verbose = False" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "id": "f996d596", + "metadata": { + "collapsed": false, + "jupyter": { + "outputs_hidden": false + }, + "pycharm": { + "name": "#%%\n" + }, + "tags": [] + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "\n", + "\u001b[1m> Entering new AgentExecutor chain...\u001b[0m\n", + "\u001b[32;1m\u001b[1;3m I need to find out how old Obama is\n", + "Action: Search\n", + "Action Input: \"Obama age\"\u001b[0m\n", + "Observation: \u001b[36;1m\u001b[1;3m61 years\u001b[0m\n", + "Thought:\u001b[32;1m\u001b[1;3m I now know the final answer\n", + "Final Answer: 61\u001b[0m\n", + "\n", + "\u001b[1m> Finished chain.\u001b[0m\n", + "\n", + "\n", + "\u001b[1m> Entering new AgentExecutor chain...\u001b[0m\n", + "\u001b[32;1m\u001b[1;3m I should search for the answer\n", + "Action: Search\n", + "Action Input: \"Michelle Obama age\"\u001b[0m\n", + "Observation: \u001b[36;1m\u001b[1;3m59 years\u001b[0m\n", + "Thought:\u001b[32;1m\u001b[1;3m I now know the final answer\n", + "Final Answer: 59\u001b[0m\n", + "\n", + "\u001b[1m> Finished chain.\u001b[0m\n" + ] + } + ], + "source": [ + "response = chain(\n", + " inputs={\n", + " \"query\": \"what is obama's age raised to the power of 0.44 divided by his wifes age?\"\n", + " }\n", + ")" + ] + }, + { + "cell_type": "code", + "execution_count": 56, + "id": "0c333a18-f885-492c-a0a1-43a8c613c066", + "metadata": { + "tags": [] + }, + "outputs": [], + "source": [ + "from kork.display import display_code_result" + ] + }, + { + "cell_type": "code", + "execution_count": 57, + "id": "a34d6750-9629-4712-a9f8-426a80e5cf60", + "metadata": { + "tags": [] + }, + "outputs": [ + { + "data": { + "text/html": [ + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
 querycoderesulterrors
0what is obama's age raised to the
power of 0.44 divided by his wifes age?
var obama_age = search(
"what is obama's age?",
"int"
)
var michelle_age = search(
"what is michelle obama's age?",
"int"
)
var result = pow(
obama_age,
0.44
) / michelle_age
0.10344109655764026[]
\n" + ], + "text/plain": [ + "" + ] + }, + "execution_count": 57, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "display_code_result(response, columns=[\"query\", \"code\", \"result\", \"errors\"])" + ] + }, + { + "cell_type": "code", + "execution_count": 25, + "id": "55a2f26f-3e1e-425f-97f7-0850a696376f", + "metadata": { + "tags": [] + }, + "outputs": [], + "source": [ + "from kork.parser import parse\n", + "from kork.ast_printer import AstPrinter" + ] + }, + { + "cell_type": "code", + "execution_count": 28, + "id": "90b3f113-2d04-437b-87b7-a4f192615084", + "metadata": { + "tags": [] + }, + "outputs": [], + "source": [ + "from IPython.display import Code" + ] + }, + { + "cell_type": "code", + "execution_count": 30, + "id": "f2674677", + "metadata": { + "collapsed": false, + "jupyter": { + "outputs_hidden": false + }, + "pycharm": { + "name": "#%%\n" + }, + "tags": [] + }, + "outputs": [ + { + "data": { + "text/html": [ + "
var obama_age = search(\n",
+       "    "what is obama's age?",\n",
+       "    "int"\n",
+       ")\n",
+       "var michelle_age = search(\n",
+       "    "what is michelle obama's age?",\n",
+       "    "int"\n",
+       ")\n",
+       "var result = pow(\n",
+       "    obama_age,\n",
+       "    0.44\n",
+       ") / michelle_age\n",
+       "
\n" + ], + "text/latex": [ + "\\begin{Verbatim}[commandchars=\\\\\\{\\}]\n", + "\\PY{k}{var}\\PY{+w}{ }\\PY{n}{obama\\PYZus{}age}\\PY{+w}{ }\\PY{o}{=}\\PY{+w}{ }\\PY{n}{search}\\PY{p}{(}\n", + "\\PY{+w}{ }\\PY{l+s+s2}{\\PYZdq{}}\\PY{l+s+s2}{what is obama}\\PY{l+s+s2}{\\PYZsq{}}\\PY{l+s+s2}{s age?}\\PY{l+s+s2}{\\PYZdq{}}\\PY{p}{,}\n", + "\\PY{+w}{ }\\PY{l+s+s2}{\\PYZdq{}}\\PY{l+s+s2}{int}\\PY{l+s+s2}{\\PYZdq{}}\n", + "\\PY{p}{)}\n", + "\\PY{k}{var}\\PY{+w}{ }\\PY{n}{michelle\\PYZus{}age}\\PY{+w}{ }\\PY{o}{=}\\PY{+w}{ }\\PY{n}{search}\\PY{p}{(}\n", + "\\PY{+w}{ }\\PY{l+s+s2}{\\PYZdq{}}\\PY{l+s+s2}{what is michelle obama}\\PY{l+s+s2}{\\PYZsq{}}\\PY{l+s+s2}{s age?}\\PY{l+s+s2}{\\PYZdq{}}\\PY{p}{,}\n", + "\\PY{+w}{ }\\PY{l+s+s2}{\\PYZdq{}}\\PY{l+s+s2}{int}\\PY{l+s+s2}{\\PYZdq{}}\n", + "\\PY{p}{)}\n", + "\\PY{k}{var}\\PY{+w}{ }\\PY{n}{result}\\PY{+w}{ }\\PY{o}{=}\\PY{+w}{ }\\PY{n+nb}{pow}\\PY{p}{(}\n", + "\\PY{+w}{ }\\PY{n}{obama\\PYZus{}age}\\PY{p}{,}\n", + "\\PY{+w}{ }\\PY{l+m+mf}{0.44}\n", + "\\PY{p}{)}\\PY{+w}{ }\\PY{o}{/}\\PY{+w}{ }\\PY{n}{michelle\\PYZus{}age}\n", + "\\end{Verbatim}\n" + ], + "text/plain": [ + "var obama_age = search(\n", + " \"what is obama's age?\",\n", + " \"int\"\n", + ")\n", + "var michelle_age = search(\n", + " \"what is michelle obama's age?\",\n", + " \"int\"\n", + ")\n", + "var result = pow(\n", + " obama_age,\n", + " 0.44\n", + ") / michelle_age" + ] + }, + "execution_count": 30, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "Code(AstPrinter().visit(parse(response[\"code\"]), pretty_print=True))" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "f0682051", + "metadata": { + "collapsed": false, + "jupyter": { + "outputs_hidden": false + }, + "pycharm": { + "name": "#%%\n" + } + }, + "outputs": [], + "source": [ + "response[\"environment\"].get_symbol(\"result\")" + ] + }, + { + "cell_type": "markdown", + "id": "748e9c40", + "metadata": { + "pycharm": { + "name": "#%% md\n" + } + }, + "source": [ + "## Test chain\n", + "\n", + "Let's see how to create a test chain. \n", + "\n", + "This chain can be used to check that generated programs look correct without having\n", + "to execute their code." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "f1622195", + "metadata": { + "collapsed": false, + "jupyter": { + "outputs_hidden": false + }, + "pycharm": { + "name": "#%%\n" + } + }, + "outputs": [], + "source": [ + "from kork.interpreter import no_op_interpreter" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "7474940b", + "metadata": { + "collapsed": false, + "jupyter": { + "outputs_hidden": false + }, + "pycharm": { + "name": "#%%\n" + } + }, + "outputs": [], + "source": [ + "chain = CodeChain.from_defaults(\n", + " llm=llm,\n", + " examples=examples_in_ast,\n", + " context=funcs,\n", + " interpreter=no_op_interpreter,\n", + ")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "8457d4f2", + "metadata": { + "collapsed": false, + "jupyter": { + "outputs_hidden": false + }, + "pycharm": { + "name": "#%%\n" + } + }, + "outputs": [], + "source": [ + "queries = [\n", + " \"how old is obama?\",\n", + " \"what is obamas age raised to the power of 0.44\",\n", + " \"is barack obama older than michele obama?\",\n", + " \"what is leonardo decaprio's girlfriends name?\",\n", + " \"what is sin of leonardo decaprio's girlfriend's\",\n", + " \"what is leonardo decaprio's girlfriend name? What is her age to the power of 0.44\",\n", + "]\n", + "\n", + "results = []\n", + "\n", + "for query in queries:\n", + " results.append(chain(inputs={\"query\": query}))" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "3ff04885", + "metadata": { + "collapsed": false, + "jupyter": { + "outputs_hidden": false + }, + "pycharm": { + "name": "#%%\n" + } + }, + "outputs": [], + "source": [ + "from kork.display import as_html_dict, display_html_results\n", + "\n", + "display_html_results(\n", + " [as_html_dict(r) for r in results], columns=[\"query\", \"code\", \"result\", \"errors\"]\n", + ")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "98bf6101", + "metadata": { + "collapsed": false, + "jupyter": { + "outputs_hidden": false + }, + "pycharm": { + "name": "#%%\n" + } + }, + "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.10.2" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +}