From 2ceac211e7f839933b27992f889d23a3b01f1732 Mon Sep 17 00:00:00 2001 From: vbarda Date: Wed, 24 Jul 2024 17:25:21 -0400 Subject: [PATCH] docs: add how to for graph factory + update cli --- docs/docs/cloud/deployment/graph_rebuild.md | 87 +++++++++++++++++++++ docs/docs/cloud/deployment/setup.md | 2 +- docs/docs/cloud/reference/cli.md | 4 +- docs/mkdocs.yml | 1 + 4 files changed, 91 insertions(+), 3 deletions(-) create mode 100644 docs/docs/cloud/deployment/graph_rebuild.md diff --git a/docs/docs/cloud/deployment/graph_rebuild.md b/docs/docs/cloud/deployment/graph_rebuild.md new file mode 100644 index 000000000..d700b0822 --- /dev/null +++ b/docs/docs/cloud/deployment/graph_rebuild.md @@ -0,0 +1,87 @@ +# Rebuild Graph at Runtime + +You might need to rebuild your graph with a different configuration for a new run. This guide shows how you can do this. + +## Prerequisites + +Make sure to check out [this how-to guide](./setup.md) on setting up your app for deployment first. + +## Define graphs + +Let's say you have an app with a simple graph that calls an LLM and returns the response to the user. The app file directory looks like the following: + +``` +my-app/ +|-- requirements.txt +|-- .env +|-- openai_agent.py # code for your graph +``` + +where the graph is defined in `openai_agent.py`. + +### No rebuild + +In the standard LangGraph API configuration, the server uses the compiled graph instance that's defined at the top level of `openai_agent.py`, which looks like the following: + +```python +from langchain_openai import ChatOpenAI +from langgraph.graph import END, MessageGraph + +model = ChatOpenAI(temperature=0) + +graph_workflow = MessageGraph() + +graph_workflow.add_node("agent", model) +graph_workflow.add_edge("agent", END) +graph_workflow.set_entry_point("agent") + +agent = graph_workflow.compile() +``` + +To make the server aware of your graph, you need to specify a path to the variable that contains the `CompiledStateGraph` instance in your LangGraph API configuration (`langgraph.json`), e.g.: + +``` +{ + "dependencies": ["."], + "graphs": { + "openai_agent": "./openai_agent.py:agent", + }, + "env": "./.env" +} +``` + +### Rebuild + +To make your graph rebuild on each new run with custom configuration, you need to rewrite `openai_agent.py` to instead provide a _function_ that takes a config and returns a graph (or compiled graph) instance as follows: + +```python +from langchain_openai import ChatOpenAI +from langgraph.graph import END, MessageGraph +from langchain_core.runnables import RunnableConfig + +model = ChatOpenAI(temperature=0) + +def make_graph(config: RunnableConfig) + graph_workflow = MessageGraph() + + graph_workflow.add_node("agent", model) + graph_workflow.add_edge("agent", END) + graph_workflow.set_entry_point("agent") + + agent = graph_workflow.compile() + return agent +``` + +Finally, you need to specify the path to your graph-making function (`make_graph`) in `langgraph.json`: + +``` +{ + "dependencies": ["."], + "graphs": { + "openai_agent": "./openai_agent.py:make_graph", + }, + "env": "./.env" +} +``` + +See more info on LangGraph API configuration file [here](../reference/cli.md#configuration-file) \ No newline at end of file diff --git a/docs/docs/cloud/deployment/setup.md b/docs/docs/cloud/deployment/setup.md index 0ab7900d0..1aa7da3f8 100644 --- a/docs/docs/cloud/deployment/setup.md +++ b/docs/docs/cloud/deployment/setup.md @@ -88,7 +88,7 @@ agent = graph_workflow.compile() ``` !!! warning "Assign `CompiledGraph` to Variable" - The build process for LangGraph Cloud requires that the `CompiledGraph` object be assigned to a variable at the top-level of a Python module. + The build process for LangGraph Cloud requires that the `CompiledGraph` object be assigned to a variable at the top-level of a Python module (alternatively, you can provide [a function that creates a graph](./graph_rebuild.md)). Example file directory: ``` diff --git a/docs/docs/cloud/reference/cli.md b/docs/docs/cloud/reference/cli.md index 5e77ad113..ee481ea20 100644 --- a/docs/docs/cloud/reference/cli.md +++ b/docs/docs/cloud/reference/cli.md @@ -13,7 +13,7 @@ The LangGraph CLI requires a JSON configuration file with the following keys: | Key | Description | | --- | ----------- | | `dependencies` | **Required**. Array of dependencies for LangGraph Cloud API server. Dependencies can be one of the following: (1) `"."`, which will look for local Python packages, (2) `pyproject.toml`, `setup.py` or `requirements.txt` in the app directory `"./local_package"`, or (3) a package name. | -| `graphs` | **Required**. Mapping from graph ID to path where the compiled graph is defined. Example: `./your_package/your_file.py:variable`, where `variable` is an instance of `langgraph.graph.graph.CompiledGraph`. | +| `graphs` | **Required**. Mapping from graph ID to path where the compiled graph or a function that makes a graph is defined. Example: | | `env` | Path to `.env` file or a mapping from environment variable to its value. | | `python_version` | `3.11` or `3.12`. Defaults to `3.11`. | | `pip_config_file`| Path to `pip` config file. | @@ -49,7 +49,7 @@ Example: "." ], "graphs": { - "my_graph_id": "./your_package/your_file.py:variable" + "my_graph_id": "./your_package/your_file.py:make_graph" }, "env": { "OPENAI_API_KEY": "secret-key" diff --git a/docs/mkdocs.yml b/docs/mkdocs.yml index 083661975..a3bfb34e7 100644 --- a/docs/mkdocs.yml +++ b/docs/mkdocs.yml @@ -192,6 +192,7 @@ nav: - Deployment: - Setup App: "cloud/deployment/setup.md" - Setup App (pyproject.toml): "cloud/deployment/setup_pyproject.md" + - Rebuild Graph at Runtime: "cloud/deployment/graph_rebuild.md" - Test App Locally: "cloud/deployment/test_locally.md" - Deploy to Cloud: "cloud/deployment/cloud.md" - Self-Host: "cloud/deployment/self_hosted.md"