https://academy.langchain.com/courses/intro-to-langgraph
https://github.com/shangxiang0907/langchain-academy
文章目录
- [Creating a deployment 创建部署](#Creating a deployment 创建部署)
-
- [Code structure 代码结构](#Code structure 代码结构)
- [CLI 命令行界面(CLI)](#CLI 命令行界面(CLI))
-
- [Build Docker Image for LangGraph Server 为 LangGraph 服务器构建 Docker 镜像](#Build Docker Image for LangGraph Server 为 LangGraph 服务器构建 Docker 镜像)
- [Set Up Redis and PostgreSQL 设置 Redis 和 PostgreSQL](#Set Up Redis and PostgreSQL 设置 Redis 和 PostgreSQL)
Creating a deployment 创建部署
Let's create a deployment of the task_maistro app that we created in module 5.
让我们为我们在模块 5 中创建的 task_maistro 应用创建一个部署。
Code structure 代码结构
The following information should be provided to create a LangGraph Platform deployment:
以下信息应被提供,以创建 LangGraph 平台部署:
-
A LangGraph API Configuration file -
langgraph.json- 一个 LangGraph API 配置文件 ---
langgraph.json
- 一个 LangGraph API 配置文件 ---
-
The graphs that implement the logic of the application - e.g.,
task_maistro.py- 实现应用逻辑的图(graph)------例如,
task_maistro.py
- 实现应用逻辑的图(graph)------例如,
-
A file that specifies dependencies required to run the application -
requirements.txt- 指定运行应用所需依赖项的文件 ---
requirements.txt
- 指定运行应用所需依赖项的文件 ---
-
Supply environment variables needed for the application to run -
.envordocker-compose.yml- 提供应用运行所需的环境变量 ---
.env或docker-compose.yml
- 提供应用运行所需的环境变量 ---
We have this already in the module-6/deployment directory!
这些文件我们已在 module-6/deployment 目录中准备就绪!
CLI 命令行界面(CLI)
The LangGraph CLI is a command-line interface for creating a LangGraph Platform deployment.
LangGraph CLI 是用于创建 LangGraph 平台部署的命令行接口。
python
%%capture --no-stderr
%pip install -U langgraph-cli
To create a self-hosted deployment, we'll follow a few steps.
为创建 自托管部署,我们将按几个步骤操作。
Build Docker Image for LangGraph Server 为 LangGraph 服务器构建 Docker 镜像
We first use the langgraph CLI to create a Docker image for the LangGraph Server.
我们首先使用 langgraph CLI 为 LangGraph 服务器 创建一个 Docker 镜像。
This will package our graph and dependencies into a Docker image.
该操作会将我们的图及其依赖项打包进一个 Docker 镜像。
A Docker image is a template for a Docker container that contains the code and dependencies required to run the application.
Docker 镜像是 Docker 容器的模板,其中包含运行应用所需的代码和依赖项。
Ensure that Docker is installed and then run the following command to create the Docker image, my-image:
请确保已安装 Docker,然后运行以下命令创建名为 my-image 的 Docker 镜像:
$ cd module-6/deployment
$ langgraph build -t my-image
Set Up Redis and PostgreSQL 设置 Redis 和 PostgreSQL
If you already have Redis and PostgreSQL running (e.g., locally or on other servers), then create and run the LangGraph Server container by itself with the URIs for Redis and PostgreSQL:
如果你已有正在运行的 Redis 和 PostgreSQL(例如,在本地或其他服务器上),则可 单独 创建并运行 LangGraph 服务器容器,并传入 Redis 和 PostgreSQL 的 URI。
docker run \
--env-file .env \
-p 8123:8000 \
-e REDIS_URI="foo" \
-e DATABASE_URI="bar" \
-e LANGSMITH_API_KEY="baz" \
my-image
Alternatively, you can use the provided docker-compose.yml file to create three separate containers based on the services defined:
或者,你可以使用提供的 docker-compose.yml 文件,基于所定义的服务创建三个独立的容器:
-
langgraph-redis: Creates a new container using the official Redis image.langgraph-redis:使用官方 Redis 镜像创建新容器。
-
langgraph-postgres: Creates a new container using the official Postgres image.langgraph-postgres:使用官方 Postgres 镜像创建新容器。
-
langgraph-api: Creates a new container using your pre-built image.langgraph-api:使用你预先构建的镜像创建新容器。
Simply copy the docker-compose-example.yml and add the following environment variables to run the deployed task_maistro app:
只需复制 docker-compose-example.yml,并添加以下环境变量,即可运行已部署的 task_maistro 应用:
-
IMAGE_NAME(e.g.,my-image) -
LANGSMITH_API_KEY -
OPENAI_API_KEY
Then, launch the deployment!
然后,启动部署!
$ cd module-6/deployment
$ docker compose up