Published on

LangChain Visualizer - A Beautiful UI for LangChain Interactions

Authors
  • avatar
    Twitter
Langchain Visualiser

LangChain Visualizer is a library that adapts Ought's ICE visualizer for use with LangChain, enabling users to view LangChain interactions with a beautiful user interface (UI). In this blog post, we will provide a quickstart guide on how to use the LangChain Visualizer library and explain its benefits.

Quickstart

To get started with LangChain Visualizer, follow these steps:

  1. Install the library by running the following command:
pip install langchain-visualizer
  1. If you're using a Linux distribution, you may need to install libyaml first. Run the following command to install it:
apt install -y libyaml-dev
  1. Import the langchain_visualizer library as the first import in your Python entrypoint file.

  2. Write an asynchronous function to visualize the workflow you want to run.

  3. Call the langchain_visualizer.visualize function on your function.

For a practical example, let's reproduce the screenshot shown above.

Running the Example Screenshot

To run the example shown in the screenshot, first install the LangChain Visualizer library and its optional dependencies by running the following command:

pip install langchain-visualizer google-search-results openai

If you haven't set up your OpenAI API keys or SERP API keys yet, you can clone the vcr-langchain repository and follow the instructions to replay the recorded interactions.

If you have set up your API keys, you can run the following script (adapted from the LangChain docs):

import langchain_visualizer
import asyncio
from langchain.agents import initialize_agent, load_tools
from langchain.llms import OpenAI

llm = OpenAI(temperature=0.7)
tools = load_tools(["serpapi", "llm-math"], llm=llm)
agent = initialize_agent(tools, llm, agent="zero-shot-react-description", verbose=True)

async def search_agent_demo():
    return agent.run(
        "Who is Olivia Wilde's boyfriend? What is his current age raised to the 0.23 power?"
    )

langchain_visualizer.visualize(search_agent_demo)

By running this script, a browser window will open, allowing you to see the agent's execution in real-time.

Jupyter Notebook Support

LangChain Visualizer now supports Jupyter notebooks! To use it within a Jupyter notebook, make sure to import the visualize function from langchain_visualizer.jupyter.

For an example of how to use LangChain Visualizer in a Jupyter notebook, please refer to the demo notebook.

Visualizing Embeddings

If you want to visualize documents being chunked up for embeddings, you can call the visualize_embeddings function before visualizing the main chain. Here's an example:

from langchain_visualizer import visualize, visualize_embeddings

async def run_chain():
    ...

visualize_embeddings()
visualize(run_chain)

Why Use LangChain Visualizer?

LangChain Visualizer offers several advantages over LangChain's built-in tracer:

  • Beautiful UI: LangChain Visualizer leverages the ICE UI, providing colored highlighting of filled-in template variables and the ability to inspect different LLM calls without leaving the trace page.
  • Static Visualization: The visualization of agent logic remains static even when LLM calls are cached, allowing for easier analysis and debugging.
  • Granular Execution Flow: LangChain Visualizer shows when each function goes up the stack, providing a clear view of the execution flow.
  • Tool Invocation Visibility: Unlike LangChain's tracer, LangChain Visualizer displays when tools, such as PythonREPL, are called, giving a more comprehensive understanding of the execution.

Please note that LangChain Visualizer is a community project and may not support all LangChain functionality. If there's anything you need to show up in the execution trace, feel free to open a pull request or issue on the project's GitHub repository.

Future Directions

LangChain Visualizer is an evolving project, and there are plans to enhance its functionality further. Some potential future directions include:

  • Adding support for additional visualization features, such as step-by-step execution highlighting and variable tracking.
  • Integrating with other popular AI libraries and frameworks to provide a seamless visualization experience.
  • Improving performance and optimizing resource usage to ensure smooth execution even with large-scale workflows.

Conclusion

LangChain Visualizer is a powerful library that brings a beautiful UI to LangChain interactions. With its easy-to-use interface and rich visualization capabilities, it enables users to gain deeper insights into their LangChain workflows. By leveraging the ICE UI and providing static visualization, LangChain Visualizer offers a superior experience compared to LangChain's built-in tracer. As the project continues to evolve, we can expect even more exciting features and enhancements in the future. Give LangChain Visualizer a try and unlock the full potential of your LangChain workflows!