From 957bcf135ddfd60cc3c7f1e423e8e59904119b96 Mon Sep 17 00:00:00 2001 From: chester Date: Wed, 8 Jan 2025 21:33:36 +0900 Subject: [PATCH 1/3] [E-4] 13-LangChain-Expression-Language / 02-InspectRunnables.ipynb MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit {PR Message - Write freely, remove this bracket if unnecessary} ### Author Checklist - [x] **PR Title Format**: I have confirmed that the PR title follows the correct format. _(e.g., [N-2] 07-Text Splitter / 07-RecursiveCharacterTextSplitter)_ - [x] **Committed Files**: I have ensured that no unnecessary files _(e.g., .bin, .gitignore, poetry.lock, pyproject.toml)_ are included. These files are not allowed. - [ ] **(Optional) Related Issue**: If this PR is linked to an issue, I have referenced the issue number in the PR message. _(e.g., Fixes #123)_ - ❌ Do not include unnecessary files (e.g., .bin, .gitignore, poetry.lock, pyproject.toml) or other people's code. If included, close the PR and create a new PR. ### Review Template (Intial PR) If no one reviews your PR within a few days, please @-mention one of teddylee777, musangk, BAEM1N --- .../02-InspectRunnables.ipynb | 292 ++++++++++++++++++ 1 file changed, 292 insertions(+) create mode 100644 13-LangChain-Expression-Language/02-InspectRunnables.ipynb diff --git a/13-LangChain-Expression-Language/02-InspectRunnables.ipynb b/13-LangChain-Expression-Language/02-InspectRunnables.ipynb new file mode 100644 index 000000000..0935a2702 --- /dev/null +++ b/13-LangChain-Expression-Language/02-InspectRunnables.ipynb @@ -0,0 +1,292 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "id": "intro-inspect-runnables", + "metadata": {}, + "source": [ + "# Inspect Runnables\n", + "\n", + "- Author: [ranian963](https://github.com/ranian963)\n", + "- Design: []()\n", + "- Peer Review: []()\n", + "- This is a part of [LangChain Open Tutorial](https://github.com/LangChain-OpenTutorial/LangChain-OpenTutorial)\n", + "\n", + "[![Open in Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/) [![Open in GitHub](https://img.shields.io/badge/Open%20in%20GitHub-181717?style=flat-square&logo=github&logoColor=white)](https://github.com/)\n", + "\n", + "## Overview\n", + "In this tutorial, we introduce how to **inspect** and visualize various components (including the graph structure) of a `Runnable` chain. Understanding the underlying graph structure can help diagnose and optimize complex chain flows.\n" + ] + }, + { + "cell_type": "markdown", + "id": "table-of-contents", + "metadata": {}, + "source": [ + "### Table of Contents\n", + "- [Overview](#overview)\n", + "- [Environment Setup](#environment-setup)\n", + "- [Introduction to Inspecting Runnables](#introduction-to-inspecting-runnables)\n", + " - [Graph Inspection](#graph-inspection)\n", + " - [Graph Output](#graph-output)\n", + " - [Prompt Retrieval](#prompt-retrieval)" + ] + }, + { + "cell_type": "markdown", + "id": "references", + "metadata": {}, + "source": [ + "### References\n", + "- [LangChain: Runnables](https://python.langchain.com/api_reference/core/runnables.html)\n", + "- [LangChain Open Tutorial](https://github.com/LangChain-OpenTutorial/LangChain-OpenTutorial)\n", + "----" + ] + }, + { + "cell_type": "markdown", + "id": "env-setup", + "metadata": {}, + "source": [ + "## Environment Setup\n", + "\n", + "Set up the environment. You may refer to [Environment Setup](https://wikidocs.net/257836) for more details.\n", + "\n", + "**[Note]**\n", + "- `langchain-opentutorial` is a package that provides a set of easy-to-use environment setup, useful functions and utilities for tutorials. \n", + "- You can checkout the [`langchain-opentutorial`](https://github.com/LangChain-OpenTutorial/langchain-opentutorial-pypi) for more details." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "install-libraries", + "metadata": {}, + "outputs": [], + "source": [ + "%%capture --no-stderr\n", + "%pip install langchain-opentutorial" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "install-req", + "metadata": {}, + "outputs": [], + "source": [ + "from langchain_opentutorial import package\n", + "\n", + "package.install(\n", + " [\n", + " \"langsmith\",\n", + " \"langchain\",\n", + " \"langchain_community\",\n", + " \"langchain_openai\",\n", + " \"faiss-cpu\",\n", + " \"grandalf\",\n", + " ],\n", + " verbose=False,\n", + " upgrade=False,\n", + ")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "set-env", + "metadata": {}, + "outputs": [], + "source": [ + "# Set environment variables\n", + "from langchain_opentutorial import set_env\n", + "\n", + "set_env(\n", + " {\n", + " \"OPENAI_API_KEY\": \"\",\n", + " \"LANGCHAIN_API_KEY\": \"\",\n", + " \"LANGCHAIN_TRACING_V2\": \"true\",\n", + " \"LANGCHAIN_ENDPOINT\": \"https://api.smith.langchain.com\",\n", + " \"LANGCHAIN_PROJECT\": \"02-InspectRunnables\",\n", + " }\n", + ")" + ] + }, + { + "cell_type": "markdown", + "id": "dotenv-option", + "metadata": {}, + "source": [ + "You can alternatively set `OPENAI_API_KEY` in `.env` file and load it. \n", + "\n", + "[Note] This is not necessary if you've already set `OPENAI_API_KEY` in previous steps." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "load-dotenv", + "metadata": {}, + "outputs": [], + "source": [ + "from dotenv import load_dotenv\n", + "\n", + "load_dotenv(override=True)" + ] + }, + { + "cell_type": "markdown", + "id": "introduction-to-inspecting-runnables", + "metadata": {}, + "source": [ + "## Introduction to Inspecting Runnables\n", + "\n", + "LangChain `Runnable` objects can be composed into pipelines, commonly referred to as **chains** or **flows**. After setting up a `runnable`, you might want to **inspect its structure** to see what's happening under the hood.\n", + "\n", + "By inspecting these, you can:\n", + "- Understand the sequence of transformations and data flows.\n", + "- Visualize the graph for debugging.\n", + "- Retrieve or modify prompts or sub-chains as needed.\n" + ] + }, + { + "cell_type": "markdown", + "id": "graph-inspection", + "metadata": {}, + "source": [ + "### Graph Inspection\n", + "\n", + "We'll create a runnable chain that includes a retriever from FAISS, a prompt template, and a ChatOpenAI model. Then we’ll **inspect the chain’s graph** to understand how data flows between these components.\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "chain-setup", + "metadata": {}, + "outputs": [], + "source": [ + "from langchain.prompts import ChatPromptTemplate\n", + "from langchain_community.vectorstores import FAISS\n", + "from langchain_core.output_parsers import StrOutputParser\n", + "from langchain_core.runnables import RunnablePassthrough\n", + "from langchain_openai import ChatOpenAI, OpenAIEmbeddings\n", + "\n", + "# Create a FAISS vector store from simple text data\n", + "vectorstore = FAISS.from_texts(\n", + " [\"Teddy is an AI engineer who loves programming!\"], embedding=OpenAIEmbeddings()\n", + ")\n", + "\n", + "# Create a retriever based on the vector store\n", + "retriever = vectorstore.as_retriever()\n", + "\n", + "template = \"\"\"Answer the question based only on the following context:\\n{context}\\n\\nQuestion: {question}\"\"\"\n", + "# Create a prompt template\n", + "prompt = ChatPromptTemplate.from_template(template)\n", + "\n", + "# Initialize ChatOpenAI model\n", + "model = ChatOpenAI(model=\"gpt-4o-mini\")\n", + "\n", + "# Construct the chain: (dictionary format) => prompt => model => output parser\n", + "chain = (\n", + " {\n", + " \"context\": retriever,\n", + " \"question\": RunnablePassthrough(),\n", + " } # Search context and question\n", + " | prompt\n", + " | model\n", + " | StrOutputParser()\n", + ")" + ] + }, + { + "cell_type": "markdown", + "id": "graph-output", + "metadata": {}, + "source": [ + "### Graph Output\n", + "We can **inspect** the chain’s internal graph of nodes (steps) and edges (data flows)." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "node-inspection", + "metadata": {}, + "outputs": [], + "source": [ + "# Get nodes from the chain's graph\n", + "chain.get_graph().nodes" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "edge-inspection", + "metadata": {}, + "outputs": [], + "source": [ + "# Get edges from the chain's graph\n", + "chain.get_graph().edges" + ] + }, + { + "cell_type": "markdown", + "id": "graph-print-ascii", + "metadata": {}, + "source": [ + "We can also print the graph in an ASCII-based diagram to visualize the chain flow." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "print-ascii", + "metadata": {}, + "outputs": [], + "source": [ + "chain.get_graph().print_ascii()" + ] + }, + { + "cell_type": "markdown", + "id": "prompt-retrieval", + "metadata": {}, + "source": [ + "### Prompt Retrieval\n", + "Finally, we can retrieve the **actual prompts** used in this chain. This is helpful to see exactly what LLM instructions are being sent.\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "get-prompts", + "metadata": {}, + "outputs": [], + "source": [ + "chain.get_prompts()" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "python3", + "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.11.9" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} From ea6855c7550485ef25b56fdc33e46130620c2459 Mon Sep 17 00:00:00 2001 From: ranian963 Date: Thu, 9 Jan 2025 19:12:55 +0900 Subject: [PATCH 2/3] Update 02-InspectRunnables.ipynb MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 최신 import로 변경하였습니다. 리뷰 감사합니다! --- 13-LangChain-Expression-Language/02-InspectRunnables.ipynb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/13-LangChain-Expression-Language/02-InspectRunnables.ipynb b/13-LangChain-Expression-Language/02-InspectRunnables.ipynb index 0935a2702..11bc80df0 100644 --- a/13-LangChain-Expression-Language/02-InspectRunnables.ipynb +++ b/13-LangChain-Expression-Language/02-InspectRunnables.ipynb @@ -81,6 +81,7 @@ " [\n", " \"langsmith\",\n", " \"langchain\",\n", + " \"langchain_core\",\n", " \"langchain_community\",\n", " \"langchain_openai\",\n", " \"faiss-cpu\",\n", @@ -166,7 +167,7 @@ "metadata": {}, "outputs": [], "source": [ - "from langchain.prompts import ChatPromptTemplate\n", + "from langchain_core.prompts import ChatPromptTemplate\n", "from langchain_community.vectorstores import FAISS\n", "from langchain_core.output_parsers import StrOutputParser\n", "from langchain_core.runnables import RunnablePassthrough\n", From 2f76bb7c99bd3b3c89a6640e4d4bd59c6808838c Mon Sep 17 00:00:00 2001 From: ranian963 Date: Thu, 9 Jan 2025 20:16:18 +0900 Subject: [PATCH 3/3] Update 02-InspectRunnables.ipynb MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 말씀주신대로 위쪽을 하나로 합치고 디자인을 제거하였습니다. 감사합니다. --- .../02-InspectRunnables.ipynb | 25 +++++-------------- 1 file changed, 6 insertions(+), 19 deletions(-) diff --git a/13-LangChain-Expression-Language/02-InspectRunnables.ipynb b/13-LangChain-Expression-Language/02-InspectRunnables.ipynb index 11bc80df0..f5966098e 100644 --- a/13-LangChain-Expression-Language/02-InspectRunnables.ipynb +++ b/13-LangChain-Expression-Language/02-InspectRunnables.ipynb @@ -8,39 +8,26 @@ "# Inspect Runnables\n", "\n", "- Author: [ranian963](https://github.com/ranian963)\n", - "- Design: []()\n", "- Peer Review: []()\n", "- This is a part of [LangChain Open Tutorial](https://github.com/LangChain-OpenTutorial/LangChain-OpenTutorial)\n", "\n", "[![Open in Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/) [![Open in GitHub](https://img.shields.io/badge/Open%20in%20GitHub-181717?style=flat-square&logo=github&logoColor=white)](https://github.com/)\n", "\n", "## Overview\n", - "In this tutorial, we introduce how to **inspect** and visualize various components (including the graph structure) of a `Runnable` chain. Understanding the underlying graph structure can help diagnose and optimize complex chain flows.\n" - ] - }, - { - "cell_type": "markdown", - "id": "table-of-contents", - "metadata": {}, - "source": [ + "In this tutorial, we introduce how to **inspect** and visualize various components (including the graph structure) of a `Runnable` chain. Understanding the underlying graph structure can help diagnose and optimize complex chain flows.\n", + "\n", "### Table of Contents\n", "- [Overview](#overview)\n", "- [Environment Setup](#environment-setup)\n", "- [Introduction to Inspecting Runnables](#introduction-to-inspecting-runnables)\n", " - [Graph Inspection](#graph-inspection)\n", " - [Graph Output](#graph-output)\n", - " - [Prompt Retrieval](#prompt-retrieval)" - ] - }, - { - "cell_type": "markdown", - "id": "references", - "metadata": {}, - "source": [ - "### References\n", + " - [Prompt Retrieval](#prompt-retrieval)\n", + "\n", + " ### References\n", "- [LangChain: Runnables](https://python.langchain.com/api_reference/core/runnables.html)\n", "- [LangChain Open Tutorial](https://github.com/LangChain-OpenTutorial/LangChain-OpenTutorial)\n", - "----" + "----\n" ] }, {