| { |
| "cells": [ |
| { |
| "cell_type": "code", |
| "execution_count": null, |
| "id": "initial_id", |
| "metadata": { |
| "collapsed": true |
| }, |
| "outputs": [], |
| "source": [ |
| "from hamilton import driver, dataflows" |
| ] |
| }, |
| { |
| "cell_type": "code", |
| "execution_count": null, |
| "outputs": [], |
| "source": [ |
| "# import module -- pick one\n", |
| "# translate_to_hamilton = dataflows.import_module('translate_to_hamilton')\n", |
| "# from hamilton.contrib.dagworks import translate_to_hamilton" |
| ], |
| "metadata": { |
| "collapsed": false |
| }, |
| "id": "2e5cf49a8f5f4c09" |
| }, |
| { |
| "cell_type": "code", |
| "execution_count": 6, |
| "outputs": [], |
| "source": [ |
| "# find some code to change.\n", |
| "def my_func(b, c):\n", |
| " \"\"\"Pretend this is a big function and we want to conver the body of it to hamilton.\"\"\"\n", |
| " a = b + c\n", |
| " d = some_func(a)\n", |
| " e = d + 1\n", |
| " return e" |
| ], |
| "metadata": { |
| "collapsed": false, |
| "ExecuteTime": { |
| "end_time": "2023-12-11T04:53:48.501891Z", |
| "start_time": "2023-12-11T04:53:48.230975Z" |
| } |
| }, |
| "id": "8a56177f67c84a6a" |
| }, |
| { |
| "cell_type": "code", |
| "execution_count": 7, |
| "outputs": [], |
| "source": [ |
| "# get the code\n", |
| "import inspect\n", |
| "user_code = inspect.getsource(my_func)" |
| ], |
| "metadata": { |
| "collapsed": false, |
| "ExecuteTime": { |
| "end_time": "2023-12-11T04:56:15.313971Z", |
| "start_time": "2023-12-11T04:56:14.930079Z" |
| } |
| }, |
| "id": "68d94fe011259977" |
| }, |
| { |
| "cell_type": "code", |
| "execution_count": 8, |
| "outputs": [ |
| { |
| "data": { |
| "text/plain": "'def my_func(b, c):\\n \"\"\"Pretend this is a big function.\"\"\"\\n a = b + c\\n d = some_func(a)\\n e = d + 1\\n return e\\n'" |
| }, |
| "execution_count": 8, |
| "metadata": {}, |
| "output_type": "execute_result" |
| } |
| ], |
| "source": [ |
| "user_code" |
| ], |
| "metadata": { |
| "collapsed": false, |
| "ExecuteTime": { |
| "end_time": "2023-12-11T04:56:40.058551Z", |
| "start_time": "2023-12-11T04:56:39.312301Z" |
| } |
| }, |
| "id": "3b4ea454b23a9878" |
| }, |
| { |
| "cell_type": "code", |
| "execution_count": 9, |
| "outputs": [ |
| { |
| "name": "stderr", |
| "output_type": "stream", |
| "text": [ |
| "Note: Hamilton collects completely anonymous data about usage. This will help us improve Hamilton over time. See https://github.com/dagworks-inc/hamilton#usage-analytics--data-privacy for details.\n" |
| ] |
| } |
| ], |
| "source": [ |
| "# create a driver\n", |
| "dr = (\n", |
| " driver.Builder()\n", |
| " .with_config({})\n", |
| " .with_modules(translate_to_hamilton)\n", |
| " .build()\n", |
| ")" |
| ], |
| "metadata": { |
| "collapsed": false, |
| "ExecuteTime": { |
| "end_time": "2023-12-11T04:59:14.902769Z", |
| "start_time": "2023-12-11T04:59:14.408612Z" |
| } |
| }, |
| "id": "993d821780a4ae76" |
| }, |
| { |
| "cell_type": "code", |
| "execution_count": 10, |
| "outputs": [], |
| "source": [ |
| "result = dr.execute(\n", |
| " [\"code_segments\", \"translated_code_response\"], # request these as outputs\n", |
| " inputs={\"user_code\": user_code, \"model_name\": \"gpt-4-1106-preview\"}\n", |
| ")" |
| ], |
| "metadata": { |
| "collapsed": false, |
| "ExecuteTime": { |
| "end_time": "2023-12-11T04:59:55.748817Z", |
| "start_time": "2023-12-11T04:59:19.193779Z" |
| } |
| }, |
| "id": "446ea53bd3406d1f" |
| }, |
| { |
| "cell_type": "code", |
| "execution_count": 14, |
| "outputs": [ |
| { |
| "name": "stdout", |
| "output_type": "stream", |
| "text": [ |
| "# functions.py\n", |
| "def a(b: float, c: float) -> float:\n", |
| " \"\"\"Computes the sum of b and c.\"\"\"\n", |
| " return b + c\n", |
| "\n", |
| "def d(a: float) -> float:\n", |
| " \"\"\"Applies some_func to a.\"\"\"\n", |
| " return some_func(a)\n", |
| "\n", |
| "def e(d: float) -> float:\n", |
| " \"\"\"Increments d by 1.\"\"\"\n", |
| " return d + 1\n", |
| "\n", |
| "# run.py\n", |
| "from hamilton import driver\n", |
| "import functions\n", |
| "\n", |
| "# Instantiate a Driver\n", |
| "dr = driver.Driver(config={}, module=functions)\n", |
| "\n", |
| "# Specify the final output we want from the Driver\n", |
| "output = [\"e\"]\n", |
| "\n", |
| "# Suppose we know the values of `b` and `c` we want to use\n", |
| "inputs = {\"b\": 1, \"c\": 2}\n", |
| "\n", |
| "# Execute the data flow to produce the output\n", |
| "result = dr.execute(output, inputs=inputs)\n", |
| "\n", |
| "print(result['e']) # The result is a dictionary with keys as the output names we specified earlier\n" |
| ] |
| } |
| ], |
| "source": [ |
| "for code in result[\"code_segments\"]:\n", |
| " print(code)" |
| ], |
| "metadata": { |
| "collapsed": false, |
| "ExecuteTime": { |
| "end_time": "2023-12-11T05:01:49.575797Z", |
| "start_time": "2023-12-11T05:01:49.570792Z" |
| } |
| }, |
| "id": "3c47292eb6f24a22" |
| }, |
| { |
| "cell_type": "code", |
| "execution_count": 12, |
| "outputs": [ |
| { |
| "name": "stdout", |
| "output_type": "stream", |
| "text": [ |
| "In Hamilton, each part of the procedural function `my_func` that assigns a value to a variable can be refactored into its own Hamilton function. Here's how you might represent this code with separate functions:\n", |
| "\n", |
| "```python\n", |
| "# functions.py\n", |
| "def a(b: float, c: float) -> float:\n", |
| " \"\"\"Computes the sum of b and c.\"\"\"\n", |
| " return b + c\n", |
| "\n", |
| "def d(a: float) -> float:\n", |
| " \"\"\"Applies some_func to a.\"\"\"\n", |
| " return some_func(a)\n", |
| "\n", |
| "def e(d: float) -> float:\n", |
| " \"\"\"Increments d by 1.\"\"\"\n", |
| " return d + 1\n", |
| "```\n", |
| "\n", |
| "In this example, `some_func` is assumed to be an existing function that takes a single argument and returns a single result. In actual implementation, `some_func` would be defined elsewhere, or you would need to create a Hamilton function that correctly replicates its functionality.\n", |
| "\n", |
| "Now, you'd want to set up the driver script (`run.py`) to execute these functions:\n", |
| "\n", |
| "```python\n", |
| "# run.py\n", |
| "from hamilton import driver\n", |
| "import functions\n", |
| "\n", |
| "# Instantiate a Driver\n", |
| "dr = driver.Driver(config={}, module=functions)\n", |
| "\n", |
| "# Specify the final output we want from the Driver\n", |
| "output = [\"e\"]\n", |
| "\n", |
| "# Suppose we know the values of `b` and `c` we want to use\n", |
| "inputs = {\"b\": 1, \"c\": 2}\n", |
| "\n", |
| "# Execute the data flow to produce the output\n", |
| "result = dr.execute(output, inputs=inputs)\n", |
| "\n", |
| "print(result['e']) # The result is a dictionary with keys as the output names we specified earlier\n", |
| "```\n", |
| "\n", |
| "Please note that we directly put the values for `b` and `c` in the `inputs` dictionary as an example. In an actual Hamilton application, these might come from other data sources or computations within the directed acyclic graph (DAG).\n" |
| ] |
| } |
| ], |
| "source": [ |
| "print(result[\"translated_code_response\"])" |
| ], |
| "metadata": { |
| "collapsed": false, |
| "ExecuteTime": { |
| "end_time": "2023-12-11T05:00:11.575020Z", |
| "start_time": "2023-12-11T05:00:11.539851Z" |
| } |
| }, |
| "id": "94355a6dd80f5138" |
| }, |
| { |
| "cell_type": "code", |
| "execution_count": null, |
| "outputs": [], |
| "source": [], |
| "metadata": { |
| "collapsed": false |
| }, |
| "id": "49720b418f9a7971" |
| } |
| ], |
| "metadata": { |
| "kernelspec": { |
| "display_name": "Python 3", |
| "language": "python", |
| "name": "python3" |
| }, |
| "language_info": { |
| "codemirror_mode": { |
| "name": "ipython", |
| "version": 2 |
| }, |
| "file_extension": ".py", |
| "mimetype": "text/x-python", |
| "name": "python", |
| "nbconvert_exporter": "python", |
| "pygments_lexer": "ipython2", |
| "version": "2.7.6" |
| } |
| }, |
| "nbformat": 4, |
| "nbformat_minor": 5 |
| } |