| { |
| "cells": [ |
| { |
| "cell_type": "code", |
| "execution_count": 13, |
| "id": "initial_id", |
| "metadata": { |
| "ExecuteTime": { |
| "start_time": "2024-02-13T06:33:30.183459Z" |
| }, |
| "is_executing": true |
| }, |
| "outputs": [ |
| { |
| "name": "stdout", |
| "output_type": "stream", |
| "text": [ |
| "The hamilton.plugins.jupyter_magic extension is already loaded. To reload it, use:\n", |
| " %reload_ext hamilton.plugins.jupyter_magic\n" |
| ] |
| } |
| ], |
| "source": [ |
| "# load some extensions / magic...\n", |
| "%load_ext hamilton.plugins.jupyter_magic" |
| ] |
| }, |
| { |
| "cell_type": "code", |
| "execution_count": 2, |
| "id": "f829eb1d88585ff", |
| "metadata": { |
| "ExecuteTime": { |
| "end_time": "2024-02-13T06:29:15.104283Z", |
| "start_time": "2024-02-13T06:29:15.098328Z" |
| }, |
| "collapsed": false |
| }, |
| "outputs": [], |
| "source": [ |
| "# import hamilton modules\n", |
| "from hamilton import driver\n", |
| "from hamilton import lifecycle" |
| ] |
| }, |
| { |
| "cell_type": "code", |
| "execution_count": 7, |
| "id": "f84593b0496cadd1", |
| "metadata": { |
| "collapsed": false, |
| "ExecuteTime": { |
| "end_time": "2024-02-13T18:14:06.109282Z", |
| "start_time": "2024-02-13T18:14:06.087426Z" |
| } |
| }, |
| "outputs": [], |
| "source": [ |
| "?%%cell_to_module \n", |
| "# one way to show usage notes" |
| ] |
| }, |
| { |
| "cell_type": "code", |
| "outputs": [ |
| { |
| "name": "stdout", |
| "output_type": "stream", |
| "text": [ |
| "Help for %%cell_to_module magic:\n", |
| " -m, --module_name: Module name to provide. Default is jupyter_module.\n", |
| " -c, --config: JSON config string, or variable name containing config to use.\n", |
| " -r, --rebuild-drivers: Flag to rebuild drivers.\n", |
| " -d, --display: Flag to visualize dataflow.\n", |
| " -v, --verbosity: of standard output. 0 to hide. 1 is normal, default.\n" |
| ] |
| } |
| ], |
| "source": [ |
| "%%cell_to_module --help \n", |
| "# shows --help message" |
| ], |
| "metadata": { |
| "collapsed": false, |
| "ExecuteTime": { |
| "end_time": "2024-02-13T18:14:07.660691Z", |
| "start_time": "2024-02-13T18:14:07.644540Z" |
| } |
| }, |
| "id": "a7907aac424f56f4", |
| "execution_count": 8 |
| }, |
| { |
| "cell_type": "code", |
| "execution_count": 4, |
| "id": "f76707690893f061", |
| "metadata": { |
| "ExecuteTime": { |
| "end_time": "2024-02-13T06:29:19.482875Z", |
| "start_time": "2024-02-13T06:29:17.737719Z" |
| }, |
| "collapsed": false |
| }, |
| "outputs": [ |
| { |
| "data": { |
| "image/svg+xml": [ |
| "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n", |
| "<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\"\n", |
| " \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\n", |
| "<!-- Generated by graphviz version 9.0.0 (20230911.1827)\n", |
| " -->\n", |
| "<!-- Pages: 1 -->\n", |
| "<svg width=\"527pt\" height=\"211pt\"\n", |
| " viewBox=\"0.00 0.00 526.52 210.80\" xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\">\n", |
| "<g id=\"graph0\" class=\"graph\" transform=\"scale(1 1) rotate(0) translate(4 206.8)\">\n", |
| "<polygon fill=\"white\" stroke=\"none\" points=\"-4,4 -4,-206.8 522.52,-206.8 522.52,4 -4,4\"/>\n", |
| "<g id=\"clust1\" class=\"cluster\">\n", |
| "<title>cluster__legend</title>\n", |
| "<polygon fill=\"none\" stroke=\"black\" points=\"8,-64.8 8,-194.8 92.85,-194.8 92.85,-64.8 8,-64.8\"/>\n", |
| "<text text-anchor=\"middle\" x=\"50.42\" y=\"-177.5\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">Legend</text>\n", |
| "</g>\n", |
| "<!-- joke_messages -->\n", |
| "<g id=\"node1\" class=\"node\">\n", |
| "<title>joke_messages</title>\n", |
| "<path fill=\"#b4d8e4\" stroke=\"black\" d=\"M361.43,-63.6C361.43,-63.6 263.32,-63.6 263.32,-63.6 257.32,-63.6 251.32,-57.6 251.32,-51.6 251.32,-51.6 251.32,-12 251.32,-12 251.32,-6 257.32,0 263.32,0 263.32,0 361.43,0 361.43,0 367.43,0 373.43,-6 373.43,-12 373.43,-12 373.43,-51.6 373.43,-51.6 373.43,-57.6 367.43,-63.6 361.43,-63.6\"/>\n", |
| "<text text-anchor=\"start\" x=\"262.12\" y=\"-40.5\" font-family=\"Helvetica,sans-Serif\" font-weight=\"bold\" font-size=\"14.00\">joke_messages</text>\n", |
| "<text text-anchor=\"start\" x=\"304.12\" y=\"-12.5\" font-family=\"Helvetica,sans-Serif\" font-style=\"italic\" font-size=\"14.00\">list</text>\n", |
| "</g>\n", |
| "<!-- joke_response -->\n", |
| "<g id=\"node2\" class=\"node\">\n", |
| "<title>joke_response</title>\n", |
| "<path fill=\"#b4d8e4\" stroke=\"black\" d=\"M506.52,-104.6C506.52,-104.6 414.42,-104.6 414.42,-104.6 408.42,-104.6 402.42,-98.6 402.42,-92.6 402.42,-92.6 402.42,-53 402.42,-53 402.42,-47 408.42,-41 414.42,-41 414.42,-41 506.52,-41 506.52,-41 512.52,-41 518.52,-47 518.52,-53 518.52,-53 518.52,-92.6 518.52,-92.6 518.52,-98.6 512.52,-104.6 506.52,-104.6\"/>\n", |
| "<text text-anchor=\"start\" x=\"413.22\" y=\"-81.5\" font-family=\"Helvetica,sans-Serif\" font-weight=\"bold\" font-size=\"14.00\">joke_response</text>\n", |
| "<text text-anchor=\"start\" x=\"452.97\" y=\"-53.5\" font-family=\"Helvetica,sans-Serif\" font-style=\"italic\" font-size=\"14.00\">str</text>\n", |
| "</g>\n", |
| "<!-- joke_messages->joke_response -->\n", |
| "<g id=\"edge3\" class=\"edge\">\n", |
| "<title>joke_messages->joke_response</title>\n", |
| "<path fill=\"none\" stroke=\"black\" d=\"M373.64,-48.71C379.54,-50.37 385.55,-52.05 391.51,-53.73\"/>\n", |
| "<polygon fill=\"black\" stroke=\"black\" points=\"390.13,-56.98 400.71,-56.31 392.03,-50.24 390.13,-56.98\"/>\n", |
| "</g>\n", |
| "<!-- llm_client -->\n", |
| "<g id=\"node3\" class=\"node\">\n", |
| "<title>llm_client</title>\n", |
| "<path fill=\"#b4d8e4\" stroke=\"black\" d=\"M342.68,-145.6C342.68,-145.6 282.07,-145.6 282.07,-145.6 276.07,-145.6 270.07,-139.6 270.07,-133.6 270.07,-133.6 270.07,-94 270.07,-94 270.07,-88 276.07,-82 282.07,-82 282.07,-82 342.68,-82 342.68,-82 348.68,-82 354.68,-88 354.68,-94 354.68,-94 354.68,-133.6 354.68,-133.6 354.68,-139.6 348.68,-145.6 342.68,-145.6\"/>\n", |
| "<text text-anchor=\"start\" x=\"280.88\" y=\"-122.5\" font-family=\"Helvetica,sans-Serif\" font-weight=\"bold\" font-size=\"14.00\">llm_client</text>\n", |
| "<text text-anchor=\"start\" x=\"289.12\" y=\"-94.5\" font-family=\"Helvetica,sans-Serif\" font-style=\"italic\" font-size=\"14.00\">OpenAI</text>\n", |
| "</g>\n", |
| "<!-- llm_client->joke_response -->\n", |
| "<g id=\"edge2\" class=\"edge\">\n", |
| "<title>llm_client->joke_response</title>\n", |
| "<path fill=\"none\" stroke=\"black\" d=\"M354.85,-102.16C366.2,-98.97 378.82,-95.43 391.24,-91.95\"/>\n", |
| "<polygon fill=\"black\" stroke=\"black\" points=\"391.94,-95.39 400.62,-89.32 390.05,-88.65 391.94,-95.39\"/>\n", |
| "</g>\n", |
| "<!-- joke_prompt -->\n", |
| "<g id=\"node4\" class=\"node\">\n", |
| "<title>joke_prompt</title>\n", |
| "<path fill=\"#b4d8e4\" stroke=\"black\" d=\"M210.32,-63.6C210.32,-63.6 130.97,-63.6 130.97,-63.6 124.97,-63.6 118.97,-57.6 118.97,-51.6 118.97,-51.6 118.97,-12 118.97,-12 118.97,-6 124.97,0 130.97,0 130.97,0 210.32,0 210.32,0 216.32,0 222.32,-6 222.32,-12 222.32,-12 222.32,-51.6 222.32,-51.6 222.32,-57.6 216.32,-63.6 210.32,-63.6\"/>\n", |
| "<text text-anchor=\"start\" x=\"129.77\" y=\"-40.5\" font-family=\"Helvetica,sans-Serif\" font-weight=\"bold\" font-size=\"14.00\">joke_prompt</text>\n", |
| "<text text-anchor=\"start\" x=\"163.15\" y=\"-12.5\" font-family=\"Helvetica,sans-Serif\" font-style=\"italic\" font-size=\"14.00\">str</text>\n", |
| "</g>\n", |
| "<!-- joke_prompt->joke_messages -->\n", |
| "<g id=\"edge1\" class=\"edge\">\n", |
| "<title>joke_prompt->joke_messages</title>\n", |
| "<path fill=\"none\" stroke=\"black\" d=\"M222.49,-31.8C228.1,-31.8 233.89,-31.8 239.69,-31.8\"/>\n", |
| "<polygon fill=\"black\" stroke=\"black\" points=\"239.32,-35.3 249.32,-31.8 239.32,-28.3 239.32,-35.3\"/>\n", |
| "</g>\n", |
| "<!-- _joke_prompt_inputs -->\n", |
| "<g id=\"node5\" class=\"node\">\n", |
| "<title>_joke_prompt_inputs</title>\n", |
| "<polygon fill=\"none\" stroke=\"black\" stroke-dasharray=\"5,2\" points=\"89.97,-54.1 10.87,-54.1 10.87,-9.5 89.97,-9.5 89.97,-54.1\"/>\n", |
| "<text text-anchor=\"start\" x=\"25.67\" y=\"-26\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">topic</text>\n", |
| "<text text-anchor=\"start\" x=\"60.17\" y=\"-26\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">str</text>\n", |
| "</g>\n", |
| "<!-- _joke_prompt_inputs->joke_prompt -->\n", |
| "<g id=\"edge4\" class=\"edge\">\n", |
| "<title>_joke_prompt_inputs->joke_prompt</title>\n", |
| "<path fill=\"none\" stroke=\"black\" d=\"M90.46,-31.8C95.96,-31.8 101.71,-31.8 107.5,-31.8\"/>\n", |
| "<polygon fill=\"black\" stroke=\"black\" points=\"107.12,-35.3 117.12,-31.8 107.12,-28.3 107.12,-35.3\"/>\n", |
| "</g>\n", |
| "<!-- input -->\n", |
| "<g id=\"node6\" class=\"node\">\n", |
| "<title>input</title>\n", |
| "<polygon fill=\"none\" stroke=\"black\" stroke-dasharray=\"5,2\" points=\"77.42,-164.1 23.42,-164.1 23.42,-127.5 77.42,-127.5 77.42,-164.1\"/>\n", |
| "<text text-anchor=\"middle\" x=\"50.42\" y=\"-140\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">input</text>\n", |
| "</g>\n", |
| "<!-- function -->\n", |
| "<g id=\"node7\" class=\"node\">\n", |
| "<title>function</title>\n", |
| "<path fill=\"#b4d8e4\" stroke=\"black\" d=\"M72.85,-109.1C72.85,-109.1 28,-109.1 28,-109.1 22,-109.1 16,-103.1 16,-97.1 16,-97.1 16,-84.5 16,-84.5 16,-78.5 22,-72.5 28,-72.5 28,-72.5 72.85,-72.5 72.85,-72.5 78.85,-72.5 84.85,-78.5 84.85,-84.5 84.85,-84.5 84.85,-97.1 84.85,-97.1 84.85,-103.1 78.85,-109.1 72.85,-109.1\"/>\n", |
| "<text text-anchor=\"middle\" x=\"50.42\" y=\"-85\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">function</text>\n", |
| "</g>\n", |
| "</g>\n", |
| "</svg>\n" |
| ], |
| "text/plain": [ |
| "<graphviz.graphs.Digraph at 0x14c19a730>" |
| ] |
| }, |
| "execution_count": 4, |
| "metadata": {}, |
| "output_type": "execute_result" |
| } |
| ], |
| "source": [ |
| "%%cell_to_module -m joke --display --rebuild-drivers \n", |
| "# The above directive does three things: \n", |
| "# 1. it creates a module with the contents of this cell\n", |
| "# 2. it imports the module under the name `joke`.\n", |
| "# 3. it displays the contents of the module.\n", |
| "# 4. if changes are made to the module, it will be reloaded automatically. If you constructed a driver with this module, you won't need to re-create it (in most cases).\n", |
| "# 5. if there is configuration passed, it will be used to help display the module.\n", |
| "# %%write_file joke.py\n", |
| "# Once you are happy with your code, you can write it to a file using the `write_file` magic command, thereby creating a module that can be imported elsewhere.\n", |
| "from typing import List\n", |
| "\n", |
| "import openai\n", |
| "\n", |
| "\n", |
| "def llm_client() -> openai.OpenAI:\n", |
| " return openai.OpenAI()\n", |
| "\n", |
| "\n", |
| "def joke_prompt(topic: str) -> str:\n", |
| " return f\"Tell me a short joke about {topic}\"\n", |
| "\n", |
| "\n", |
| "def joke_messages(joke_prompt: str) -> List[dict]:\n", |
| " return [{\"role\": \"user\", \"content\": joke_prompt}]\n", |
| "\n", |
| "\n", |
| "def joke_response(llm_client: openai.OpenAI,\n", |
| " joke_messages: List[dict]) -> str:\n", |
| " response = llm_client.chat.completions.create(\n", |
| " model=\"gpt-3.5-turbo\",\n", |
| " messages=joke_messages,\n", |
| " )\n", |
| " return response.choices[0].message.content" |
| ] |
| }, |
| { |
| "cell_type": "code", |
| "execution_count": 5, |
| "id": "42b6eb0c9ac397bc", |
| "metadata": { |
| "ExecuteTime": { |
| "end_time": "2024-02-13T06:29:19.517474Z", |
| "start_time": "2024-02-13T06:29:19.502194Z" |
| }, |
| "collapsed": false |
| }, |
| "outputs": [], |
| "source": [ |
| "# create a driver --- this will be auto rebuilt (if you turn that on)\n", |
| "dr = (\n", |
| " driver.Builder()\n", |
| " .with_modules(joke)\n", |
| " .with_config({\"dummy\": \"config\"})\n", |
| " .build()\n", |
| ")" |
| ] |
| }, |
| { |
| "cell_type": "code", |
| "execution_count": 6, |
| "id": "e372203e0f3ab1b6", |
| "metadata": { |
| "ExecuteTime": { |
| "end_time": "2024-02-13T06:29:20.866327Z", |
| "start_time": "2024-02-13T06:29:19.569013Z" |
| }, |
| "collapsed": false |
| }, |
| "outputs": [ |
| { |
| "name": "stdout", |
| "output_type": "stream", |
| "text": [ |
| "{'joke_response': 'Sure, here it is:\\n\\nWhy did the ice cream go to therapy?\\nBecause it had too many scoops of emotions!'}\n" |
| ] |
| } |
| ], |
| "source": [ |
| "print(dr.execute([\"joke_response\"],\n", |
| " inputs={\"topic\": \"ice cream\"}))" |
| ] |
| }, |
| { |
| "cell_type": "code", |
| "execution_count": 7, |
| "id": "5d20afdf40d37441", |
| "metadata": { |
| "ExecuteTime": { |
| "end_time": "2024-02-13T06:29:20.897770Z", |
| "start_time": "2024-02-13T06:29:20.886666Z" |
| }, |
| "collapsed": false |
| }, |
| "outputs": [], |
| "source": [ |
| "dr2 = (\n", |
| " driver.Builder()\n", |
| " .with_modules(joke)\n", |
| " .with_adapters(lifecycle.PrintLn()) # this driver will not be rebuilt because of the adapter.\n", |
| " .build()\n", |
| ")" |
| ] |
| }, |
| { |
| "cell_type": "code", |
| "execution_count": 8, |
| "id": "fcedb47a8a4c1792", |
| "metadata": { |
| "ExecuteTime": { |
| "end_time": "2024-02-13T06:29:22.187332Z", |
| "start_time": "2024-02-13T06:29:21.011468Z" |
| }, |
| "collapsed": false |
| }, |
| "outputs": [ |
| { |
| "name": "stdout", |
| "output_type": "stream", |
| "text": [ |
| "Executing node: llm_client.\n", |
| "Finished debugging node: llm_client in 9.78ms. Status: Success.\n", |
| "Executing node: joke_prompt.\n", |
| "Finished debugging node: joke_prompt in 15.7μs. Status: Success.\n", |
| "Executing node: joke_messages.\n", |
| "Finished debugging node: joke_messages in 11μs. Status: Success.\n", |
| "Executing node: joke_response.\n", |
| "Finished debugging node: joke_response in 1.25s. Status: Success.\n", |
| "{'joke_response': 'Why did the corn chips go to therapy?\\n\\nBecause they were feeling a little \"salty\" about always being \"dipped\" in salsa without any appreciation!'}\n" |
| ] |
| } |
| ], |
| "source": [ |
| "print(dr2.execute([\"joke_response\"],\n", |
| " inputs={\"topic\": \"corn chips\"}))" |
| ] |
| }, |
| { |
| "cell_type": "code", |
| "execution_count": 9, |
| "id": "cfd9cbe7f9630ade", |
| "metadata": { |
| "ExecuteTime": { |
| "end_time": "2024-02-13T06:29:22.195026Z", |
| "start_time": "2024-02-13T06:29:22.187770Z" |
| }, |
| "collapsed": false |
| }, |
| "outputs": [], |
| "source": [ |
| "# some configuration \n", |
| "conf = {\"some_key\":\"some_value\"}" |
| ] |
| }, |
| { |
| "cell_type": "code", |
| "execution_count": 10, |
| "id": "f7c21d5eaa95cc0a", |
| "metadata": { |
| "ExecuteTime": { |
| "end_time": "2024-02-13T06:29:22.701173Z", |
| "start_time": "2024-02-13T06:29:22.399709Z" |
| }, |
| "collapsed": false |
| }, |
| "outputs": [ |
| { |
| "data": { |
| "image/svg+xml": [ |
| "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n", |
| "<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\"\n", |
| " \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\n", |
| "<!-- Generated by graphviz version 9.0.0 (20230911.1827)\n", |
| " -->\n", |
| "<!-- Pages: 1 -->\n", |
| "<svg width=\"187pt\" height=\"287pt\"\n", |
| " viewBox=\"0.00 0.00 186.77 287.00\" xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\">\n", |
| "<g id=\"graph0\" class=\"graph\" transform=\"scale(1 1) rotate(0) translate(4 283)\">\n", |
| "<polygon fill=\"white\" stroke=\"none\" points=\"-4,4 -4,-283 182.77,-283 182.77,4 -4,4\"/>\n", |
| "<g id=\"clust1\" class=\"cluster\">\n", |
| "<title>cluster__legend</title>\n", |
| "<polygon fill=\"none\" stroke=\"black\" points=\"8,-142 8,-271 92.85,-271 92.85,-142 8,-142\"/>\n", |
| "<text text-anchor=\"middle\" x=\"50.42\" y=\"-253.7\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">Legend</text>\n", |
| "</g>\n", |
| "<!-- some_key -->\n", |
| "<g id=\"node1\" class=\"node\">\n", |
| "<title>some_key</title>\n", |
| "<polygon fill=\"none\" stroke=\"black\" points=\"85.42,-50 9.42,-50 9.42,0 91.42,0 91.42,-44 85.42,-50\"/>\n", |
| "<polyline fill=\"none\" stroke=\"black\" points=\"85.42,-50 85.42,-44\"/>\n", |
| "<polyline fill=\"none\" stroke=\"black\" points=\"91.42,-44 85.42,-44\"/>\n", |
| "<text text-anchor=\"start\" x=\"17.42\" y=\"-33.7\" font-family=\"Helvetica,sans-Serif\" font-weight=\"bold\" font-size=\"14.00\">some_key</text>\n", |
| "<text text-anchor=\"start\" x=\"18.92\" y=\"-5.7\" font-family=\"Helvetica,sans-Serif\" font-style=\"italic\" font-size=\"14.00\">typing.Any</text>\n", |
| "</g>\n", |
| "<!-- world -->\n", |
| "<g id=\"node2\" class=\"node\">\n", |
| "<title>world</title>\n", |
| "<path fill=\"#b4d8e4\" stroke=\"black\" d=\"M166.77,-131.8C166.77,-131.8 132.42,-131.8 132.42,-131.8 126.42,-131.8 120.42,-125.8 120.42,-119.8 120.42,-119.8 120.42,-80.2 120.42,-80.2 120.42,-74.2 126.42,-68.2 132.42,-68.2 132.42,-68.2 166.77,-68.2 166.77,-68.2 172.77,-68.2 178.77,-74.2 178.77,-80.2 178.77,-80.2 178.77,-119.8 178.77,-119.8 178.77,-125.8 172.77,-131.8 166.77,-131.8\"/>\n", |
| "<text text-anchor=\"start\" x=\"131.22\" y=\"-108.7\" font-family=\"Helvetica,sans-Serif\" font-weight=\"bold\" font-size=\"14.00\">world</text>\n", |
| "<text text-anchor=\"start\" x=\"142.1\" y=\"-80.7\" font-family=\"Helvetica,sans-Serif\" font-style=\"italic\" font-size=\"14.00\">str</text>\n", |
| "</g>\n", |
| "<!-- hello -->\n", |
| "<g id=\"node3\" class=\"node\">\n", |
| "<title>hello</title>\n", |
| "<path fill=\"#b4d8e4\" stroke=\"black\" d=\"M65.42,-131.8C65.42,-131.8 35.42,-131.8 35.42,-131.8 29.42,-131.8 23.42,-125.8 23.42,-119.8 23.42,-119.8 23.42,-80.2 23.42,-80.2 23.42,-74.2 29.42,-68.2 35.42,-68.2 35.42,-68.2 65.42,-68.2 65.42,-68.2 71.42,-68.2 77.42,-74.2 77.42,-80.2 77.42,-80.2 77.42,-119.8 77.42,-119.8 77.42,-125.8 71.42,-131.8 65.42,-131.8\"/>\n", |
| "<text text-anchor=\"start\" x=\"34.67\" y=\"-108.7\" font-family=\"Helvetica,sans-Serif\" font-weight=\"bold\" font-size=\"14.00\">hello</text>\n", |
| "<text text-anchor=\"start\" x=\"42.92\" y=\"-80.7\" font-family=\"Helvetica,sans-Serif\" font-style=\"italic\" font-size=\"14.00\">str</text>\n", |
| "</g>\n", |
| "<!-- hello->world -->\n", |
| "<g id=\"edge1\" class=\"edge\">\n", |
| "<title>hello->world</title>\n", |
| "<path fill=\"none\" stroke=\"black\" d=\"M77.72,-100C87.28,-100 98.3,-100 108.77,-100\"/>\n", |
| "<polygon fill=\"black\" stroke=\"black\" points=\"108.72,-103.5 118.72,-100 108.72,-96.5 108.72,-103.5\"/>\n", |
| "</g>\n", |
| "<!-- config -->\n", |
| "<g id=\"node4\" class=\"node\">\n", |
| "<title>config</title>\n", |
| "<polygon fill=\"none\" stroke=\"black\" points=\"71.42,-240 23.42,-240 23.42,-204 77.42,-204 77.42,-234 71.42,-240\"/>\n", |
| "<polyline fill=\"none\" stroke=\"black\" points=\"71.42,-240 71.42,-234\"/>\n", |
| "<polyline fill=\"none\" stroke=\"black\" points=\"77.42,-234 71.42,-234\"/>\n", |
| "<text text-anchor=\"middle\" x=\"50.42\" y=\"-216.2\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">config</text>\n", |
| "</g>\n", |
| "<!-- function -->\n", |
| "<g id=\"node5\" class=\"node\">\n", |
| "<title>function</title>\n", |
| "<path fill=\"#b4d8e4\" stroke=\"black\" d=\"M72.85,-186.3C72.85,-186.3 28,-186.3 28,-186.3 22,-186.3 16,-180.3 16,-174.3 16,-174.3 16,-161.7 16,-161.7 16,-155.7 22,-149.7 28,-149.7 28,-149.7 72.85,-149.7 72.85,-149.7 78.85,-149.7 84.85,-155.7 84.85,-161.7 84.85,-161.7 84.85,-174.3 84.85,-174.3 84.85,-180.3 78.85,-186.3 72.85,-186.3\"/>\n", |
| "<text text-anchor=\"middle\" x=\"50.42\" y=\"-162.2\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">function</text>\n", |
| "</g>\n", |
| "</g>\n", |
| "</svg>\n" |
| ], |
| "text/plain": [ |
| "<graphviz.graphs.Digraph at 0x14c237790>" |
| ] |
| }, |
| "execution_count": 10, |
| "metadata": {}, |
| "output_type": "execute_result" |
| } |
| ], |
| "source": [ |
| "%%cell_to_module -m hello --display --config conf --rebuild-drivers\n", |
| "# shows how to pass in configuration for display\n", |
| "from hamilton.function_modifiers import config\n", |
| "\n", |
| "def hello()->str:\n", |
| " return \"hi\"\n", |
| "\n", |
| "@config.when(some_key=\"some_value\")\n", |
| "def world__1(hello: str)-> str:\n", |
| " return f\"{hello} world\"\n", |
| "\n", |
| "@config.when_not(some_key=\"some_value\")\n", |
| "def world__2(hello: str)-> str:\n", |
| " return f\"World {hello}\"" |
| ] |
| }, |
| { |
| "cell_type": "code", |
| "execution_count": 11, |
| "id": "fb7135d8a7078a40", |
| "metadata": { |
| "ExecuteTime": { |
| "end_time": "2024-02-13T06:29:23.617271Z", |
| "start_time": "2024-02-13T06:29:23.339236Z" |
| }, |
| "collapsed": false |
| }, |
| "outputs": [ |
| { |
| "data": { |
| "image/svg+xml": [ |
| "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n", |
| "<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\"\n", |
| " \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\n", |
| "<!-- Generated by graphviz version 9.0.0 (20230911.1827)\n", |
| " -->\n", |
| "<!-- Pages: 1 -->\n", |
| "<svg width=\"528pt\" height=\"424pt\"\n", |
| " viewBox=\"0.00 0.00 527.98 423.80\" xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\">\n", |
| "<g id=\"graph0\" class=\"graph\" transform=\"scale(1 1) rotate(0) translate(4 419.8)\">\n", |
| "<polygon fill=\"white\" stroke=\"none\" points=\"-4,4 -4,-419.8 523.98,-419.8 523.98,4 -4,4\"/>\n", |
| "<g id=\"clust1\" class=\"cluster\">\n", |
| "<title>cluster__legend</title>\n", |
| "<polygon fill=\"none\" stroke=\"black\" points=\"8,-223.8 8,-407.8 92.85,-407.8 92.85,-223.8 8,-223.8\"/>\n", |
| "<text text-anchor=\"middle\" x=\"50.43\" y=\"-390.5\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">Legend</text>\n", |
| "</g>\n", |
| "<!-- joke_messages -->\n", |
| "<g id=\"node1\" class=\"node\">\n", |
| "<title>joke_messages</title>\n", |
| "<path fill=\"#b4d8e4\" stroke=\"black\" d=\"M362.88,-63.6C362.88,-63.6 264.78,-63.6 264.78,-63.6 258.78,-63.6 252.78,-57.6 252.78,-51.6 252.78,-51.6 252.78,-12 252.78,-12 252.78,-6 258.78,0 264.78,0 264.78,0 362.88,0 362.88,0 368.88,0 374.88,-6 374.88,-12 374.88,-12 374.88,-51.6 374.88,-51.6 374.88,-57.6 368.88,-63.6 362.88,-63.6\"/>\n", |
| "<text text-anchor=\"start\" x=\"263.58\" y=\"-40.5\" font-family=\"Helvetica,sans-Serif\" font-weight=\"bold\" font-size=\"14.00\">joke_messages</text>\n", |
| "<text text-anchor=\"start\" x=\"305.58\" y=\"-12.5\" font-family=\"Helvetica,sans-Serif\" font-style=\"italic\" font-size=\"14.00\">list</text>\n", |
| "</g>\n", |
| "<!-- joke_response -->\n", |
| "<g id=\"node2\" class=\"node\">\n", |
| "<title>joke_response</title>\n", |
| "<path fill=\"#b4d8e4\" stroke=\"black\" d=\"M507.98,-104.6C507.98,-104.6 415.88,-104.6 415.88,-104.6 409.88,-104.6 403.88,-98.6 403.88,-92.6 403.88,-92.6 403.88,-53 403.88,-53 403.88,-47 409.88,-41 415.88,-41 415.88,-41 507.98,-41 507.98,-41 513.98,-41 519.98,-47 519.98,-53 519.98,-53 519.98,-92.6 519.98,-92.6 519.98,-98.6 513.98,-104.6 507.98,-104.6\"/>\n", |
| "<text text-anchor=\"start\" x=\"414.68\" y=\"-81.5\" font-family=\"Helvetica,sans-Serif\" font-weight=\"bold\" font-size=\"14.00\">joke_response</text>\n", |
| "<text text-anchor=\"start\" x=\"454.43\" y=\"-53.5\" font-family=\"Helvetica,sans-Serif\" font-style=\"italic\" font-size=\"14.00\">str</text>\n", |
| "</g>\n", |
| "<!-- joke_messages->joke_response -->\n", |
| "<g id=\"edge3\" class=\"edge\">\n", |
| "<title>joke_messages->joke_response</title>\n", |
| "<path fill=\"none\" stroke=\"black\" d=\"M375.09,-48.71C380.99,-50.37 387,-52.05 392.96,-53.73\"/>\n", |
| "<polygon fill=\"black\" stroke=\"black\" points=\"391.58,-56.98 402.16,-56.31 393.48,-50.24 391.58,-56.98\"/>\n", |
| "</g>\n", |
| "<!-- world -->\n", |
| "<g id=\"node3\" class=\"node\">\n", |
| "<title>world</title>\n", |
| "<path fill=\"#b4d8e4\" stroke=\"black\" d=\"M189.28,-145.6C189.28,-145.6 154.93,-145.6 154.93,-145.6 148.93,-145.6 142.93,-139.6 142.93,-133.6 142.93,-133.6 142.93,-94 142.93,-94 142.93,-88 148.93,-82 154.93,-82 154.93,-82 189.28,-82 189.28,-82 195.28,-82 201.28,-88 201.28,-94 201.28,-94 201.28,-133.6 201.28,-133.6 201.28,-139.6 195.28,-145.6 189.28,-145.6\"/>\n", |
| "<text text-anchor=\"start\" x=\"153.73\" y=\"-122.5\" font-family=\"Helvetica,sans-Serif\" font-weight=\"bold\" font-size=\"14.00\">world</text>\n", |
| "<text text-anchor=\"start\" x=\"164.6\" y=\"-94.5\" font-family=\"Helvetica,sans-Serif\" font-style=\"italic\" font-size=\"14.00\">str</text>\n", |
| "</g>\n", |
| "<!-- some_key -->\n", |
| "<g id=\"node4\" class=\"node\">\n", |
| "<title>some_key</title>\n", |
| "<polygon fill=\"none\" stroke=\"black\" points=\"85.43,-213.8 9.43,-213.8 9.43,-163.8 91.43,-163.8 91.43,-207.8 85.43,-213.8\"/>\n", |
| "<polyline fill=\"none\" stroke=\"black\" points=\"85.43,-213.8 85.43,-207.8\"/>\n", |
| "<polyline fill=\"none\" stroke=\"black\" points=\"91.43,-207.8 85.43,-207.8\"/>\n", |
| "<text text-anchor=\"start\" x=\"17.43\" y=\"-197.5\" font-family=\"Helvetica,sans-Serif\" font-weight=\"bold\" font-size=\"14.00\">some_key</text>\n", |
| "<text text-anchor=\"start\" x=\"18.93\" y=\"-169.5\" font-family=\"Helvetica,sans-Serif\" font-style=\"italic\" font-size=\"14.00\">typing.Any</text>\n", |
| "</g>\n", |
| "<!-- joke_prompt -->\n", |
| "<g id=\"node5\" class=\"node\">\n", |
| "<title>joke_prompt</title>\n", |
| "<path fill=\"#b4d8e4\" stroke=\"black\" d=\"M211.78,-63.6C211.78,-63.6 132.43,-63.6 132.43,-63.6 126.43,-63.6 120.43,-57.6 120.43,-51.6 120.43,-51.6 120.43,-12 120.43,-12 120.43,-6 126.43,0 132.43,0 132.43,0 211.78,0 211.78,0 217.78,0 223.78,-6 223.78,-12 223.78,-12 223.78,-51.6 223.78,-51.6 223.78,-57.6 217.78,-63.6 211.78,-63.6\"/>\n", |
| "<text text-anchor=\"start\" x=\"131.23\" y=\"-40.5\" font-family=\"Helvetica,sans-Serif\" font-weight=\"bold\" font-size=\"14.00\">joke_prompt</text>\n", |
| "<text text-anchor=\"start\" x=\"164.6\" y=\"-12.5\" font-family=\"Helvetica,sans-Serif\" font-style=\"italic\" font-size=\"14.00\">str</text>\n", |
| "</g>\n", |
| "<!-- joke_prompt->joke_messages -->\n", |
| "<g id=\"edge1\" class=\"edge\">\n", |
| "<title>joke_prompt->joke_messages</title>\n", |
| "<path fill=\"none\" stroke=\"black\" d=\"M223.94,-31.8C229.55,-31.8 235.34,-31.8 241.14,-31.8\"/>\n", |
| "<polygon fill=\"black\" stroke=\"black\" points=\"240.77,-35.3 250.77,-31.8 240.77,-28.3 240.77,-35.3\"/>\n", |
| "</g>\n", |
| "<!-- llm_client -->\n", |
| "<g id=\"node6\" class=\"node\">\n", |
| "<title>llm_client</title>\n", |
| "<path fill=\"#b4d8e4\" stroke=\"black\" d=\"M344.13,-145.6C344.13,-145.6 283.53,-145.6 283.53,-145.6 277.53,-145.6 271.53,-139.6 271.53,-133.6 271.53,-133.6 271.53,-94 271.53,-94 271.53,-88 277.53,-82 283.53,-82 283.53,-82 344.13,-82 344.13,-82 350.13,-82 356.13,-88 356.13,-94 356.13,-94 356.13,-133.6 356.13,-133.6 356.13,-139.6 350.13,-145.6 344.13,-145.6\"/>\n", |
| "<text text-anchor=\"start\" x=\"282.33\" y=\"-122.5\" font-family=\"Helvetica,sans-Serif\" font-weight=\"bold\" font-size=\"14.00\">llm_client</text>\n", |
| "<text text-anchor=\"start\" x=\"290.58\" y=\"-94.5\" font-family=\"Helvetica,sans-Serif\" font-style=\"italic\" font-size=\"14.00\">OpenAI</text>\n", |
| "</g>\n", |
| "<!-- llm_client->joke_response -->\n", |
| "<g id=\"edge2\" class=\"edge\">\n", |
| "<title>llm_client->joke_response</title>\n", |
| "<path fill=\"none\" stroke=\"black\" d=\"M356.3,-102.16C367.65,-98.97 380.27,-95.43 392.69,-91.95\"/>\n", |
| "<polygon fill=\"black\" stroke=\"black\" points=\"393.39,-95.39 402.07,-89.32 391.5,-88.65 393.39,-95.39\"/>\n", |
| "</g>\n", |
| "<!-- hello -->\n", |
| "<g id=\"node7\" class=\"node\">\n", |
| "<title>hello</title>\n", |
| "<path fill=\"#b4d8e4\" stroke=\"black\" d=\"M65.43,-145.6C65.43,-145.6 35.43,-145.6 35.43,-145.6 29.43,-145.6 23.43,-139.6 23.43,-133.6 23.43,-133.6 23.43,-94 23.43,-94 23.43,-88 29.43,-82 35.43,-82 35.43,-82 65.43,-82 65.43,-82 71.43,-82 77.43,-88 77.43,-94 77.43,-94 77.43,-133.6 77.43,-133.6 77.43,-139.6 71.43,-145.6 65.43,-145.6\"/>\n", |
| "<text text-anchor=\"start\" x=\"34.68\" y=\"-122.5\" font-family=\"Helvetica,sans-Serif\" font-weight=\"bold\" font-size=\"14.00\">hello</text>\n", |
| "<text text-anchor=\"start\" x=\"42.93\" y=\"-94.5\" font-family=\"Helvetica,sans-Serif\" font-style=\"italic\" font-size=\"14.00\">str</text>\n", |
| "</g>\n", |
| "<!-- hello->world -->\n", |
| "<g id=\"edge4\" class=\"edge\">\n", |
| "<title>hello->world</title>\n", |
| "<path fill=\"none\" stroke=\"black\" d=\"M77.69,-113.8C93.42,-113.8 113.72,-113.8 131.38,-113.8\"/>\n", |
| "<polygon fill=\"black\" stroke=\"black\" points=\"131.3,-117.3 141.3,-113.8 131.3,-110.3 131.3,-117.3\"/>\n", |
| "</g>\n", |
| "<!-- _joke_prompt_inputs -->\n", |
| "<g id=\"node8\" class=\"node\">\n", |
| "<title>_joke_prompt_inputs</title>\n", |
| "<polygon fill=\"none\" stroke=\"black\" stroke-dasharray=\"5,2\" points=\"89.98,-54.1 10.88,-54.1 10.88,-9.5 89.98,-9.5 89.98,-54.1\"/>\n", |
| "<text text-anchor=\"start\" x=\"25.68\" y=\"-26\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">topic</text>\n", |
| "<text text-anchor=\"start\" x=\"60.18\" y=\"-26\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">str</text>\n", |
| "</g>\n", |
| "<!-- _joke_prompt_inputs->joke_prompt -->\n", |
| "<g id=\"edge5\" class=\"edge\">\n", |
| "<title>_joke_prompt_inputs->joke_prompt</title>\n", |
| "<path fill=\"none\" stroke=\"black\" d=\"M90.28,-31.8C96.25,-31.8 102.54,-31.8 108.87,-31.8\"/>\n", |
| "<polygon fill=\"black\" stroke=\"black\" points=\"108.72,-35.3 118.72,-31.8 108.72,-28.3 108.72,-35.3\"/>\n", |
| "</g>\n", |
| "<!-- config -->\n", |
| "<g id=\"node9\" class=\"node\">\n", |
| "<title>config</title>\n", |
| "<polygon fill=\"none\" stroke=\"black\" points=\"71.43,-376.8 23.43,-376.8 23.43,-340.8 77.43,-340.8 77.43,-370.8 71.43,-376.8\"/>\n", |
| "<polyline fill=\"none\" stroke=\"black\" points=\"71.43,-376.8 71.43,-370.8\"/>\n", |
| "<polyline fill=\"none\" stroke=\"black\" points=\"77.43,-370.8 71.43,-370.8\"/>\n", |
| "<text text-anchor=\"middle\" x=\"50.43\" y=\"-353\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">config</text>\n", |
| "</g>\n", |
| "<!-- input -->\n", |
| "<g id=\"node10\" class=\"node\">\n", |
| "<title>input</title>\n", |
| "<polygon fill=\"none\" stroke=\"black\" stroke-dasharray=\"5,2\" points=\"77.43,-323.1 23.43,-323.1 23.43,-286.5 77.43,-286.5 77.43,-323.1\"/>\n", |
| "<text text-anchor=\"middle\" x=\"50.43\" y=\"-299\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">input</text>\n", |
| "</g>\n", |
| "<!-- function -->\n", |
| "<g id=\"node11\" class=\"node\">\n", |
| "<title>function</title>\n", |
| "<path fill=\"#b4d8e4\" stroke=\"black\" d=\"M72.85,-268.1C72.85,-268.1 28,-268.1 28,-268.1 22,-268.1 16,-262.1 16,-256.1 16,-256.1 16,-243.5 16,-243.5 16,-237.5 22,-231.5 28,-231.5 28,-231.5 72.85,-231.5 72.85,-231.5 78.85,-231.5 84.85,-237.5 84.85,-243.5 84.85,-243.5 84.85,-256.1 84.85,-256.1 84.85,-262.1 78.85,-268.1 72.85,-268.1\"/>\n", |
| "<text text-anchor=\"middle\" x=\"50.43\" y=\"-244\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">function</text>\n", |
| "</g>\n", |
| "</g>\n", |
| "</svg>\n" |
| ], |
| "text/plain": [ |
| "<graphviz.graphs.Digraph at 0x14c1931c0>" |
| ] |
| }, |
| "execution_count": 11, |
| "metadata": {}, |
| "output_type": "execute_result" |
| } |
| ], |
| "source": [ |
| "# shows multiple modules -- this will be rebuilt correctly if either \"module\" is updated if you\n", |
| "# turn rebuilding drivers on (`--rebuild-drivers`).\n", |
| "dr3 = (\n", |
| " driver.Builder()\n", |
| " .with_modules(joke, hello)\n", |
| " .with_config(conf)\n", |
| " .build()\n", |
| " )\n", |
| "dr3.display_all_functions()" |
| ] |
| }, |
| { |
| "cell_type": "code", |
| "execution_count": 12, |
| "id": "a2b844b28df52160", |
| "metadata": { |
| "ExecuteTime": { |
| "end_time": "2024-02-13T06:29:24.456838Z", |
| "start_time": "2024-02-13T06:29:24.447092Z" |
| }, |
| "collapsed": false |
| }, |
| "outputs": [ |
| { |
| "data": { |
| "text/plain": [ |
| "{'world': 'hi world'}" |
| ] |
| }, |
| "execution_count": 12, |
| "metadata": {}, |
| "output_type": "execute_result" |
| } |
| ], |
| "source": [ |
| "dr3.execute([\"world\"], inputs={})" |
| ] |
| }, |
| { |
| "cell_type": "code", |
| "execution_count": null, |
| "id": "f8ef467eeab94c4d", |
| "metadata": { |
| "collapsed": false |
| }, |
| "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.9.13" |
| } |
| }, |
| "nbformat": 4, |
| "nbformat_minor": 5 |
| } |