AI知识库

53AI知识库

学习大模型的前沿技术与行业应用场景


【AI Agent系列】【LangGraph】3. 一行代码让你的 LangGraph 结构可视化!
发布日期:2024-05-06 16:21:12 浏览次数: 1870


可视化是个非常非常非常有用和友好的东西。本文我们来实现 LangGraph 结构的可视化,当你创建的 LangGraph 结构越来越复杂时,可以利用它来方便地调查和优化逻辑。

一行代码即可搞定,非常简单。

0. 示例Demo

0.1 完整代码

先上完整代码,跑通再说

from langchain_openai import ChatOpenAIfrom langchain_community.tools.tavily_search import TavilySearchResultsfrom langgraph.prebuilt import chat_agent_executorfrom langchain_core.messages import HumanMessagetools = [TavilySearchResults(max_results=1)]model = ChatOpenAI()app = chat_agent_executor.create_function_calling_executor(model, tools)app.get_graph().print_ascii()

0.2 踩坑:No module named 'grandalf'

运行时遇到问题:

安装 grandalf 即可:

pip install -U grandalf -i https://pypi.tuna.tsinghua.edu.cn/simple

0.3 运行结果

可以看到它可视化的图与上篇文章我自己画的图一样:

1. 代码详解

代码很简单,就两行有效代码:

(1)app = chat_agent_executor.create_function_calling_executor(model, tools),创建一个 Graph。

(2)app.get_graph().print_ascii(),以 ASCII 的形式打印出图形。

1.1 create_function_calling_executor

这个其实就是将我们上篇文章实现的 LangGraph 创建的过程做了一下封装而已,源码如下:

1.2 print_ascii

从运行结果来看,它最终实现的效果其实就是将节点和边打印出来,多了一些空格和星号。实现原理并不难,但是想要组织好这个这个显示的效果(空格和星号的数量等),感觉很难。部分源码如下,看看就好,会用就行:

def draw_ascii(self) -> str:    return draw_ascii(        {node.id: node_data_str(node) for node in self.nodes.values()},        [(edge.source, edge.target) for edge in self.edges],    )def print_ascii(self) -> None:    print(self.draw_ascii())  # noqa: T201def draw_ascii(vertices: Mapping[str, str], edges: Sequence[Tuple[str, str]]) -> str:    """Build a DAG and draw it in ASCII.    Args:        vertices (list): list of graph vertices.        edges (list): list of graph edges.    Returns:        str: ASCII representation    Example:        >>> from dvc.dagascii import draw        >>> vertices = [1, 2, 3, 4]        >>> edges = [(1, 2), (2, 3), (2, 4), (1, 4)]        >>> print(draw(vertices, edges))        +---+     +---+        | 3 |     | 4 |        +---+    *+---+          *    **   *          *  **     *          * *       *        +---+       *        | 2 |      *        +---+     *             *    *              *  *               **             +---+             | 1 |             +---+    """

2. 更好看一点的可视化

教程中还写了另一种可视化的方式,结果如下:

代码如下:

# app.get_graph().print_ascii() ## 替换掉这一句from IPython.display import ImageImage(app.get_graph().draw_png())

运行前需要先安装如下依赖库:

pip install -U prompt_toolkit  -i https://pypi.tuna.tsinghua.edu.cn/simplepip install -U grandalf -i https://pypi.tuna.tsinghua.edu.cn/simple

安装过程中你可能会遇到如下问题:ERROR: Could not build wheels for pygraphviz, which is required to install pyproject.toml-based projects

Windows平台的解决方法可参考这篇文章:
https://savleen307.medium.com/pygraphviz-installation-in-windows-f45cc6fed981


3. 总结

本文介绍了两种将 LangGraph 可视化的方法,一行代码就可以搞定:

app.get_graph().print_ascii()

Image(app.get_graph().draw_png())

其中 app 是你构建的 LangGraph:

workflow = StateGraph(AgentState)......app = workflow.compile()

4. 参考

  • • https://github.com/langchain-ai/langgraph/blob/main/examples/visualization.ipynb



53AI,企业落地应用大模型首选服务商

产品:大模型应用平台+智能体定制开发+落地咨询服务

承诺:先做场景POC验证,看到效果再签署服务协议。零风险落地应用大模型,已交付160+中大型企业

联系我们

售前咨询
186 6662 7370
预约演示
185 8882 0121

微信扫码

与创始人交个朋友

回到顶部

 
扫码咨询