Skip to content

Commit

Permalink
Merge branch 'main' into isaac/monorepocallouts
Browse files Browse the repository at this point in the history
  • Loading branch information
isahers1 authored Aug 9, 2024
2 parents f2b57fe + 350adae commit e20bac4
Show file tree
Hide file tree
Showing 46 changed files with 6,464 additions and 3,228 deletions.
1 change: 1 addition & 0 deletions docs/_scripts/copy_notebooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
"human_in_the_loop/time-travel.ipynb",
"human_in_the_loop/edit-graph-state.ipynb",
"human_in_the_loop/wait-user-input.ipynb",
"human_in_the_loop/review-tool-calls.ipynb",
"node-retries.ipynb",
],
"tutorials": [
Expand Down
129 changes: 85 additions & 44 deletions docs/docs/cloud/deployment/setup.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,30 @@

A LangGraph application must be configured with a [LangGraph API configuration file](../reference/cli.md#configuration-file) in order to be deployed to LangGraph Cloud (or to be self-hosted). This how-to guide discusses the basic steps to setup a LangGraph application for deployment using `requirements.txt` to specify project dependencies.

!!! tip "Setup with a Monorepo"
If you are interested in deploying a graph located inside a monorepo, take a look at [this](https://github.com/langchain-ai/langgraph-example-monorepo) repository for an example of how to do so.

This walkthrough is based on [this repository](https://github.com/langchain-ai/langgraph-example), which you can play around with to learn more about how to setup your LangGraph application for deployment.

!!! tip "Setup with pyproject.toml"
If you prefer using poetry for dependency management, check out [this how-to guide](./setup_pyproject.md) on using `pyproject.toml` for LangGraph Cloud.

!!! tip "Setup with a Monorepo"
If you are interested in deploying a graph located inside a monorepo, take a look at [this](https://github.com/langchain-ai/langgraph-example-monorepo) repository for an example of how to do so.


The final repo structure will look something like this:

```bash
my-app/
|-- requirements.txt # package dependencies
|-- .env # environment variables
|-- openai_agent.py # code for an agent
|-- anthropic_agent.py # code for another agent
|-- langgraph.json # configuration file for LangGraph
├── my_agent # all project code lies within here
│ ├── utils # utilities for your graph
│ │ ├── __init__.py
│ │ ├── tools.py # tools for your graph
│ │ ├── nodes.py # node functions for you graph
│ │ └── state.py # state definition of your graph
│   ├── requirements.txt # package dependencies
│   ├── __init__.py
│   └── agent.py # code for constructing your graph
├── .env # environment variables
└── langgraph.json # configuration file for LangGraph
```

After each step, an example file directory is provided to demonstrate how code can be organized.
Expand All @@ -28,8 +36,8 @@ Dependencies can optionally be specified in one of the following files: `pyproje

The dependencies below will be included in the image, you can also use them in your code, as long as with a compatible version range:
```
langgraph>=0.1.19,<0.2.0
langchain-core>=0.2.8,<0.3.0
langgraph>=0.2.0,<0.3.0
langchain-core>=0.2.27,<0.3.0
langsmith>=0.1.63
orjson>=3.10.1
httpx>=0.27.0
Expand All @@ -40,18 +48,24 @@ uvloop>=0.19.0
httptools>=0.6.1
jsonschema-rs>=0.18.0
croniter>=1.0.1
structlog>=24.4.0
```

Example `requirements.txt` file:
```
langgraph
langchain_anthropic
tavily-python
langchain_community
langchain_openai
```

Example file directory:
```
```bash
my-app/
|-- requirements.txt # Python packages required for your graph
├── my_agent # all project code lies within here
│   └── requirements.txt # package dependencies
```

## Specify Environment Variables
Expand All @@ -66,42 +80,66 @@ OPENAI_API_KEY=key
```

Example file directory:
```

```bash
my-app/
|-- requirements.txt
|-- .env # file with environment variables
├── my_agent # all project code lies within here
│   └── requirements.txt # package dependencies
└── .env # environment variables
```

## Define Graphs

Implement your graphs! Graphs can be defined in a single file or multiple files. Make note of the variable names of each [CompiledGraph][compiledgraph] to be included in the LangGraph application. The variable names will be used later when creating the [LangGraph API configuration file](../reference/cli.md#configuration-file).

Example `openai_agent.py` file:
```python
from langchain_openai import ChatOpenAI
from langgraph.graph import END, MessageGraph

model = ChatOpenAI(temperature=0)
Example `agent.py` file, which shows how to import from other modules you define (code for the modules is not shown here, please see [this repo](https://github.com/langchain-ai/langgraph-example) to see their implementation):

graph_workflow = MessageGraph()

graph_workflow.add_node("agent", model)
graph_workflow.add_edge("agent", END)
graph_workflow.set_entry_point("agent")
```python
# my_agent/agent.py
from typing import TypedDict, Literal

from langgraph.graph import StateGraph, END
from my_agent.utils.nodes import call_model, should_continue, tool_node # import nodes
from my_agent.utils.state import AgentState # import state

# Define the config
class GraphConfig(TypedDict):
model_name: Literal["anthropic", "openai"]

workflow = StateGraph(AgentState, config_schema=GraphConfig)
workflow.add_node("agent", call_model)
workflow.add_node("action", tool_node)
workflow.set_entry_point("agent")
workflow.add_conditional_edges(
"agent",
should_continue,
{
"continue": "action",
"end": END,
},
)
workflow.add_edge("action", "agent")

agent = graph_workflow.compile()
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 (alternatively, you can provide [a function that creates a graph](./graph_rebuild.md)).

Example file directory:
```
```bash
my-app/
|-- requirements.txt
|-- .env
|-- openai_agent.py # code for your graph
|-- anthropic_agent.py # code for your graph
├── my_agent # all project code lies within here
│ ├── utils # utilities for your graph
│ │ ├── __init__.py
│ │ ├── tools.py # tools for your graph
│ │ ├── nodes.py # node functions for you graph
│ │ └── state.py # state definition of your graph
│   ├── requirements.txt # package dependencies
│   ├── __init__.py
│   └── agent.py # code for constructing your graph
└── .env # environment variables
```

## Create LangGraph API Config
Expand All @@ -111,14 +149,11 @@ Create a [LangGraph API configuration file](../reference/cli.md#configuration-fi
Example `langgraph.json` file:
```json
{
"dependencies": [
"."
],
"graphs": {
"openai_agent": "./openai_agent.py:agent",
"anthropic_agent": "./anthropic_agent.py:agent"
},
"env": "./.env"
"dependencies": ["./my_agent"],
"graphs": {
"agent": "./my_agent/agent.py:graph"
},
"env": ".env"
}
```

Expand All @@ -131,11 +166,17 @@ Example file directory:

```bash
my-app/
|-- requirements.txt
|-- .env
|-- openai_agent.py
|-- anthropic_agent.py
|-- langgraph.json # configuration file for LangGraph
├── my_agent # all project code lies within here
│ ├── utils # utilities for your graph
│ │ ├── __init__.py
│ │ ├── tools.py # tools for your graph
│ │ ├── nodes.py # node functions for you graph
│ │ └── state.py # state definition of your graph
│   ├── requirements.txt # package dependencies
│   ├── __init__.py
│   └── agent.py # code for constructing your graph
├── .env # environment variables
└── langgraph.json # configuration file for LangGraph
```

## Next
Expand Down
Loading

0 comments on commit e20bac4

Please sign in to comment.