diff --git a/examples/visualization.ipynb b/examples/visualization.ipynb index 1766b3adc..bcca44734 100644 --- a/examples/visualization.ipynb +++ b/examples/visualization.ipynb @@ -24,26 +24,11 @@ { "cell_type": "code", "execution_count": 1, - "outputs": [], - "source": [ - "!pip install langchain-openai" - ], - "metadata": { - "collapsed": false, - "ExecuteTime": { - "start_time": "2024-04-18T12:18:27.746359Z", - "end_time": "2024-04-18T12:18:30.105332Z" - } - } - }, - { - "cell_type": "code", - "execution_count": 2, "id": "efb7e3c0-c63f-40f6-93ce-19681d650fc2", "metadata": { "ExecuteTime": { - "start_time": "2024-04-18T12:18:30.106849Z", - "end_time": "2024-04-18T12:18:30.466861Z" + "start_time": "2024-04-19T11:25:28.531482Z", + "end_time": "2024-04-19T11:25:30.217991Z" } }, "outputs": [], @@ -55,18 +40,23 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 2, "id": "a7025f33-3160-41cf-868b-17ebc916fb1d", - "metadata": {}, + "metadata": { + "ExecuteTime": { + "start_time": "2024-04-19T11:25:32.168821Z", + "end_time": "2024-04-19T11:25:32.431922Z" + } + }, "outputs": [], "source": [ "# Optional to not need .env\n", - "import os\n", - "os.environ['TAVILY_API_KEY']='foo'\n", - "os.environ['OPENAI_API_KEY'] = 'foo'\n", + "# import os\n", + "# os.environ['TAVILY_API_KEY'] = 'foo'\n", + "# os.environ['OPENAI_API_KEY'] = 'foo'\n", "\n", - "tools=[TavilySearchResults(max_results=1)]\n", - "model=ChatOpenAI()" + "tools = [TavilySearchResults(max_results=1)]\n", + "model = ChatOpenAI()" ] }, { @@ -88,12 +78,17 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 3, "id": "32b4ae66-f667-4a8b-a602-503fd0effcd9", - "metadata": {}, + "metadata": { + "ExecuteTime": { + "start_time": "2024-04-19T11:25:36.098462Z", + "end_time": "2024-04-19T11:25:36.231169Z" + } + }, "outputs": [], "source": [ - "app=chat_agent_executor.create_function_calling_executor(model, tools)" + "app = chat_agent_executor.create_function_calling_executor(model, tools)" ] }, { @@ -143,10 +138,43 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 4, "id": "ca9b980d-1f0a-4286-9157-a870e3d55134", - "metadata": {}, - "outputs": [], + "metadata": { + "ExecuteTime": { + "start_time": "2024-04-19T11:25:37.273032Z", + "end_time": "2024-04-19T11:25:37.303260Z" + } + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + " +-----------+ \n", + " | __start__ | \n", + " +-----------+ \n", + " * \n", + " * \n", + " * \n", + " +-------+ \n", + " | agent | \n", + " +-------+* \n", + " *** *** \n", + " * * \n", + " ** *** \n", + "+-----------------+ * \n", + "| should_continue | * \n", + "+-----------------+. * \n", + " . ..... * \n", + " . ... * \n", + " . ... * \n", + " +---------+ +--------+ \n", + " | __end__ | | action | \n", + " +---------+ +--------+ \n" + ] + } + ], "source": [ "app.get_graph().print_ascii()" ] @@ -193,15 +221,38 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 5, "id": "66007b2d", "metadata": { "ExecuteTime": { - "start_time": "2024-04-18T12:16:55.014109Z", - "end_time": "2024-04-18T12:16:55.036649Z" + "start_time": "2024-04-19T11:25:38.726838Z", + "end_time": "2024-04-19T11:25:38.733126Z" } }, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "%%{init: {'flowchart': {'curve': 'linear'}}}%%\n", + "graph TD;\n", + "\t__start__[__start__]:::startclass;\n", + "\t__end__[__end__]:::endclass;\n", + "\tagent([agent]):::otherclass;\n", + "\taction([action]):::otherclass;\n", + "\tshould_continue([should_continue]):::otherclass;\n", + "\t__start__ --> agent;\n", + "\taction --> agent;\n", + "\tagent --> should_continue;\n", + "\tshould_continue -. continue .-> action;\n", + "\tshould_continue -. end .-> __end__;\n", + "\tclassDef startclass fill:#ffdfba;\n", + "\tclassDef endclass fill:#baffc9;\n", + "\tclassDef otherclass fill:#fad7de;\n", + "\n" + ] + } + ], "source": [ "print(app.get_graph().draw_mermaid())" ] @@ -229,18 +280,24 @@ }, { "cell_type": "code", - "execution_count": null, - "id": "ea909e33-6a72-4d9f-87bf-faab9ab578f1", - "metadata": { - "ExecuteTime": { - "start_time": "2024-04-18T12:16:55.021318Z", - "end_time": "2024-04-18T12:16:55.036774Z" - } - }, + "execution_count": 6, "outputs": [], "source": [ - "from IPython.display import Image" - ] + "from IPython.display import display, HTML\n", + "import base64\n", + "\n", + "def display_image(image_bytes: bytes, width=300):\n", + " decoded_img_bytes = base64.b64encode(image_bytes).decode('utf-8')\n", + " html = f''\n", + " display(HTML(html))" + ], + "metadata": { + "collapsed": false, + "ExecuteTime": { + "start_time": "2024-04-19T11:25:40.351636Z", + "end_time": "2024-04-19T11:25:40.358604Z" + } + } }, { "cell_type": "markdown", @@ -259,12 +316,27 @@ }, { "cell_type": "code", - "execution_count": 8, + "execution_count": 7, "id": "d4234400-75cd-4b13-aeff-828f7fb68ab1", "metadata": { "ExecuteTime": { - "start_time": "2024-04-18T12:18:30.624386Z", - "end_time": "2024-04-18T12:18:30.870326Z" + "start_time": "2024-04-19T11:25:42.019017Z", + "end_time": "2024-04-19T11:25:42.057704Z" + } + }, + "outputs": [], + "source": [ + "#!pip install pygraphviz" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "id": "ee026342-f560-4ce0-ab43-1718bd19a366", + "metadata": { + "ExecuteTime": { + "start_time": "2024-04-19T11:25:42.452377Z", + "end_time": "2024-04-19T11:25:42.631675Z" } }, "outputs": [ @@ -278,22 +350,7 @@ } ], "source": [ - "# !pip install pygraphviz" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "ee026342-f560-4ce0-ab43-1718bd19a366", - "metadata": { - "ExecuteTime": { - "start_time": "2024-04-18T12:16:55.029258Z", - "end_time": "2024-04-18T12:16:55.261297Z" - } - }, - "outputs": [], - "source": [ - "Image(app.get_graph().draw_png())" + "display_image(app.get_graph().draw_png())" ] }, { @@ -313,12 +370,28 @@ }, { "cell_type": "code", - "execution_count": 10, + "execution_count": 9, "id": "d403e1e7", "metadata": { "ExecuteTime": { - "start_time": "2024-04-18T12:18:30.876810Z", - "end_time": "2024-04-18T12:18:33.630723Z" + "start_time": "2024-04-19T11:25:44.793438Z", + "end_time": "2024-04-19T11:25:44.798703Z" + } + }, + "outputs": [], + "source": [ + "# !pip install pyppeteer\n", + "# !pip install nest_asyncio" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "id": "058546ee", + "metadata": { + "ExecuteTime": { + "start_time": "2024-04-19T11:25:45.405158Z", + "end_time": "2024-04-19T11:25:47.412695Z" } }, "outputs": [ @@ -332,24 +405,12 @@ } ], "source": [ - "# !pip install pyppeteer" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "058546ee", - "metadata": { - "ExecuteTime": { - "start_time": "2024-04-18T12:16:55.268313Z", - "end_time": "2024-04-18T12:16:57.848469Z" - } - }, - "outputs": [], - "source": [ + "import nest_asyncio\n", "from langchain_core.runnables.graph import CurveStyle, NodeColors, MermaidDrawMethod\n", "\n", - "Image(app.get_graph().draw_mermaid_png(\n", + "nest_asyncio.apply() # Required for Jupyter Notebook to run async functions\n", + "\n", + "display_image(app.get_graph().draw_mermaid_png(\n", " curve_style=CurveStyle.LINEAR,\n", " node_colors=NodeColors(start=\"#ffdfba\", end=\"#baffc9\", other=\"#fad7de\"),\n", " wrap_label_n_words=9,\n", @@ -360,28 +421,6 @@ "))" ] }, - { - "cell_type": "markdown", - "id": "dc4b618c", - "metadata": { - "ExecuteTime": { - "start_time": "2024-04-18T12:18:33.633459Z", - "end_time": "2024-04-18T12:18:33.849053Z" - } - }, - "source": [], - "outputs": [ - { - "data": { - "text/plain": "", - "text/html": "" - }, - "metadata": {}, - "output_type": "display_data" - } - ], - "execution_count": 11 - }, { "cell_type": "markdown", "id": "2dd71a7c", @@ -408,26 +447,26 @@ }, { "cell_type": "code", - "execution_count": 12, + "execution_count": 11, "id": "be37d419", "metadata": { "ExecuteTime": { - "start_time": "2024-04-18T12:18:33.851902Z", - "end_time": "2024-04-18T12:18:34.007726Z" + "start_time": "2024-04-19T11:25:51.640462Z", + "end_time": "2024-04-19T11:25:51.865932Z" } }, "outputs": [ { "data": { "text/plain": "", - "text/html": "" + "text/html": "" }, "metadata": {}, "output_type": "display_data" } ], "source": [ - "Image(app.get_graph().draw_mermaid_png(\n", + "display_image(app.get_graph().draw_mermaid_png(\n", " draw_method=MermaidDrawMethod.API,\n", "))" ] @@ -443,7 +482,7 @@ }, "source": [ "## Excluding condition nodes\n", - "By default, condition nods like 'should_continue' will be added. In case you want to exclude these, you can use add_condition_nodes parameter" + "By default, condition nods like 'should_continue' will be added. In case you have a big graph and want to exclude these or simplicity, you can use add_condition_nodes parameter" ], "outputs": [ { @@ -459,33 +498,29 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 21, "id": "9f2773dd", "metadata": { "ExecuteTime": { - "start_time": "2024-04-18T12:16:58.771228Z", - "end_time": "2024-04-18T12:17:00.428677Z" + "start_time": "2024-04-19T17:28:35.404649Z", + "end_time": "2024-04-19T17:28:37.844424Z" } }, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": "", + "text/html": "" + }, + "metadata": {}, + "output_type": "display_data" + } + ], "source": [ - "Image(app.get_graph(add_condition_nodes=False).draw_mermaid_png(\n", - " draw_method=MermaidDrawMethod.API,\n", - "))" + "display_image(app.get_graph(add_condition_nodes=False).draw_mermaid_png(\n", + " draw_method=MermaidDrawMethod.PYPPETEER,\n", + "))\n" ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "36fbdc44", - "metadata": { - "ExecuteTime": { - "start_time": "2024-04-18T12:05:15.456711Z", - "end_time": "2024-04-18T12:05:15.483115Z" - } - }, - "outputs": [], - "source": [] } ], "metadata": {