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", + " | query | \n", + "code | \n", + "result | \n", + "errors | \n", + "
---|---|---|---|---|
0 | \n", + "what is obama's age raised to the power of 0.44 divided by his wifes age? | \n",
+ " var obama_age = search( | \n",
+ " 0.10344109655764026 | \n", + "[] | \n", + "
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",
+ "