{ "cells": [ { "metadata": {}, "cell_type": "markdown", "source": "Python Code Executor", "id": "f96e89bb67a640f5" }, { "cell_type": "code", "id": "initial_id", "metadata": { "collapsed": true, "ExecuteTime": { "end_time": "2025-06-10T15:11:36.621661Z", "start_time": "2025-06-10T15:11:36.617741Z" } }, "source": [ "from langchain_core.tools import Tool\n", "from langchain_experimental.utilities import PythonREPL\n", "import subprocess" ], "outputs": [], "execution_count": 10 }, { "metadata": { "ExecuteTime": { "end_time": "2025-06-10T14:54:42.141759Z", "start_time": "2025-06-10T14:54:42.138854Z" } }, "cell_type": "code", "source": "file_dir = 'files/f918266a-b3e0-4914-865d-4faa564f1aef.py'", "id": "1f0aed71e203012d", "outputs": [], "execution_count": 6 }, { "metadata": { "ExecuteTime": { "end_time": "2025-06-10T14:44:45.063803Z", "start_time": "2025-06-10T14:44:45.061618Z" } }, "cell_type": "code", "source": "python_repl = PythonREPL()", "id": "acea852beeb1035e", "outputs": [], "execution_count": 2 }, { "metadata": { "ExecuteTime": { "end_time": "2025-06-10T14:55:05.802715Z", "start_time": "2025-06-10T14:55:05.797934Z" } }, "cell_type": "code", "source": [ "\n", "with open(file_dir, \"r\", encoding=\"utf-8\") as f:\n", " code = f.read()\n", " print(code)\n" ], "id": "620b184072e95086", "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "from random import randint\n", "import time\n", "\n", "class UhOh(Exception):\n", " pass\n", "\n", "class Hmm:\n", " def __init__(self):\n", " self.value = randint(-100, 100)\n", "\n", " def Yeah(self):\n", " if self.value == 0:\n", " return True\n", " else:\n", " raise UhOh()\n", "\n", "def Okay():\n", " while True:\n", " yield Hmm()\n", "\n", "def keep_trying(go, first_try=True):\n", " maybe = next(go)\n", " try:\n", " if maybe.Yeah():\n", " return maybe.value\n", " except UhOh:\n", " if first_try:\n", " print(\"Working...\")\n", " print(\"Please wait patiently...\")\n", " time.sleep(0.1)\n", " return keep_trying(go, first_try=False)\n", "\n", "if __name__ == \"__main__\":\n", " go = Okay()\n", " print(f\"{keep_trying(go)}\")\n", "\n" ] } ], "execution_count": 8 }, { "metadata": { "ExecuteTime": { "end_time": "2025-06-10T14:55:08.665931Z", "start_time": "2025-06-10T14:55:08.661026Z" } }, "cell_type": "code", "source": "print(python_repl.run(code))", "id": "c55e78888adc58f2", "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n" ] } ], "execution_count": 9 }, { "metadata": { "ExecuteTime": { "end_time": "2025-06-10T15:13:00.580769Z", "start_time": "2025-06-10T15:12:56.064064Z" } }, "cell_type": "code", "source": [ "def run_as_main(filename):\n", " result = subprocess.run([\"python\", filename], capture_output=True, text=True)\n", " return result.stdout\n", "print(run_as_main(file_dir))" ], "id": "c7b145cdb13aea01", "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Working...\n", "Please wait patiently...\n", "0\n", "\n" ] } ], "execution_count": 13 }, { "metadata": {}, "cell_type": "code", "outputs": [], "execution_count": null, "source": [ "repl_tool = Tool(\n", " name=\"python_repl\",\n", " description=\"A Python shell. Use this to execute python commands. Input should be a valid python command. If you want to see the output of a value, you should print it out with `print(...)`.\",\n", " func=python_repl.run,\n", ")" ], "id": "1789b291aa070b8b" } ], "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 }