Inspect Runnables

Open in ColabOpen in GitHub

Overview

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.

Table of Contents


Environment Setup

Set up the environment. You may refer to Environment Setup for more details.

[Note]

  • langchain-opentutorial is a package that provides a set of easy-to-use environment setup, useful functions and utilities for tutorials.

  • You can checkout the langchain-opentutorial for more details.

You can alternatively set OPENAI_API_KEY in .env file and load it.

[Note] This is not necessary if you've already set OPENAI_API_KEY in previous steps.

Introduction to Inspecting Runnables

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.

By inspecting these, you can:

  • Understand the sequence of transformations and data flows.

  • Visualize the graph for debugging.

  • Retrieve or modify prompts or sub-chains as needed.

Graph Inspection

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.

Graph Output

We can inspect the chain’s internal graph of nodes (steps) and edges (data flows).

We can also print the graph in an ASCII-based diagram to visualize the chain flow.

Prompt Retrieval

Finally, we can retrieve the actual prompts used in this chain. This is helpful to see exactly what LLM instructions are being sent.

Last updated