Skip to content

运行时重建图

您可能需要根据新的运行配置来重建图。例如,您可能需要根据不同的配置使用不同的图状态或图结构。本指南将展示如何实现这一点。

注意

在大多数情况下,基于配置自定义行为应由单个图处理,每个节点都可以读取配置并根据配置更改其行为。

准备工作

请首先查阅此操作指南以了解如何为部署设置您的应用程序。

定义图

假设你有一个应用程序,其中包含一个简单的图,该图调用了一个LLM并返回响应给用户。应用程序文件目录如下所示:

my-app/
|-- requirements.txt
|-- .env
|-- openai_agent.py     # 图的代码

其中图在 openai_agent.py 中定义。

不重建

在标准的LangGraph API配置中,服务器使用在 openai_agent.py 的顶层定义的编译后的图实例,该实例看起来如下所示:

API Reference: END | START

from langchain_openai import ChatOpenAI
from langgraph.graph import END, START, MessageGraph

model = ChatOpenAI(temperature=0)

graph_workflow = MessageGraph()

graph_workflow.add_node("agent", model)
graph_workflow.add_edge("agent", END)
graph_workflow.add_edge(START, "agent")

agent = graph_workflow.compile()

为了让服务器知道你的图,你需要在LangGraph API配置(langgraph.json)中指定变量的路径,该变量包含 CompiledStateGraph 实例,例如:

{
    "dependencies": ["."],
    "graphs": {
        "openai_agent": "./openai_agent.py:agent",
    },
    "env": "./.env"
}

重建

为了让你的图在每次新运行时根据自定义配置重建,你需要重写 openai_agent.py ,以提供一个接受配置并返回图(或编译后的图)实例的函数。假设我们希望为用户ID '1' 返回现有的图,并为其他用户提供工具调用代理。我们可以修改 openai_agent.py 如下:

API Reference: END | START | StateGraph | add_messages | ToolNode | tool | BaseMessage | RunnableConfig

from typing import Annotated
from typing_extensions import TypedDict
from langchain_openai import ChatOpenAI
from langgraph.graph import END, START, MessageGraph
from langgraph.graph.state import StateGraph
from langgraph.graph.message import add_messages
from langgraph.prebuilt import ToolNode
from langchain_core.tools import tool
from langchain_core.messages import BaseMessage
from langchain_core.runnables import RunnableConfig


class State(TypedDict):
    messages: Annotated[list[BaseMessage], add_messages]


model = ChatOpenAI(temperature=0)

def make_default_graph():
    """创建一个简单的LLM代理"""
    graph_workflow = StateGraph(State)
    def call_model(state):
        return {"messages": [model.invoke(state["messages"])]}

    graph_workflow.add_node("agent", call_model)
    graph_workflow.add_edge("agent", END)
    graph_workflow.add_edge(START, "agent")

    agent = graph_workflow.compile()
    return agent


def make_alternative_graph():
    """创建一个工具调用代理"""

    @tool
    def add(a: float, b: float):
        """加两个数字。"""
        return a + b

    tool_node = ToolNode([add])
    model_with_tools = model.bind_tools([add])
    def call_model(state):
        return {"messages": [model_with_tools.invoke(state["messages"])]}

    def should_continue(state: State):
        if state["messages"][-1].tool_calls:
            return "tools"
        else:
            return END

    graph_workflow = StateGraph(State)

    graph_workflow.add_node("agent", call_model)
    graph_workflow.add_node("tools", tool_node)
    graph_workflow.add_edge("tools", "agent")
    graph_workflow.add_edge(START, "agent")
    graph_workflow.add_conditional_edges("agent", should_continue)

    agent = graph_workflow.compile()
    return agent


# 这是构建图的函数,它将根据提供的配置决定构建哪个图
def make_graph(config: RunnableConfig):
    user_id = config.get("configurable", {}).get("user_id")
    # 根据用户ID路由到不同的图状态/结构
    if user_id == "1":
        return make_default_graph()
    else:
        return make_alternative_graph()

最后,你需要在 langgraph.json 中指定构建图的函数(make_graph)的路径:

{
    "dependencies": ["."],
    "graphs": {
        "openai_agent": "./openai_agent.py:make_graph",
    },
    "env": "./.env"
}

有关LangGraph API配置文件的更多信息,请参阅此处

Comments