LangGraph 平台快速入门¶
本指南将展示如何在本地运行一个 LangGraph 应用程序。
先决条件¶
在开始之前,请确保您具备以下内容:
- LangSmith 的 API 密钥 - 注册免费
1. 安装 LangGraph CLI¶
2. 创建一个 LangGraph 应用 🌱¶
从 new-langgraph-project-python
模板 或 new-langgraph-project-js
模板 创建一个新的应用。这个模板演示了一个单节点应用,你可以用自己的逻辑进行扩展。
其他模板
如果你使用 langgraph new
而不指定模板,将会出现一个交互式菜单,允许你从可用的模板列表中进行选择。
3. 安装依赖项¶
在你的新 LangGraph 应用的根目录下,以 edit
模式安装依赖项,这样服务器就可以使用你本地的更改:
4. 创建 .env
文件¶
在你新创建的 LangGraph 应用的根目录中,你会找到一个 .env.example
文件。在你新创建的 LangGraph 应用的根目录中创建一个 .env
文件,并将 .env.example
文件的内容复制到其中,填写必要的 API 密钥:
5. 启动 LangGraph 服务器 🚀¶
在本地启动 LangGraph API 服务器:
示例输出:
> 已准备就绪!
>
> - API: [http://localhost:2024](http://localhost:2024/)
>
> - 文档: http://localhost:2024/docs
>
> - LangGraph Studio Web 界面: https://smith.langchain.com/studio/?baseUrl=http://127.0.0.1:2024
langgraph dev
命令以内存模式启动 LangGraph 服务器。此模式适用于开发和测试目的。对于生产环境使用,请部署 LangGraph 服务器并连接到持久化存储后端。有关更多信息,请参见 部署选项。
6. 在 LangGraph Studio 中测试你的应用程序¶
LangGraph Studio 是一个专门的用户界面,你可以将其连接到 LangGraph API 服务器,以本地可视化、交互和调试你的应用程序。通过访问 langgraph dev
命令输出中的 URL,在 LangGraph Studio 中测试你的图:
对于运行在自定义主机/端口上的 LangGraph 服务器,请更新 baseURL 参数。
7. 测试 API¶
-
安装 LangGraph Python SDK:
-
向助手发送消息(无线程运行):
from langgraph_sdk import get_client import asyncio client = get_client(url="http://localhost:2024") async def main(): async for chunk in client.runs.stream( None, # Threadless run "agent", # Name of assistant. Defined in langgraph.json. input={ "messages": [{ "role": "human", "content": "What is LangGraph?", }], }, ): print(f"Receiving new event of type: {chunk.event}...") print(chunk.data) print("\n\n") asyncio.run(main())
-
安装 LangGraph Python SDK:
-
向助手发送消息(无线程运行):
from langgraph_sdk import get_sync_client client = get_sync_client(url="http://localhost:2024") for chunk in client.runs.stream( None, # Threadless run "agent", # Name of assistant. Defined in langgraph.json. input={ "messages": [{ "role": "human", "content": "What is LangGraph?", }], }, stream_mode="messages-tuple", ): print(f"Receiving new event of type: {chunk.event}...") print(chunk.data) print("\n\n")
-
安装 LangGraph JS SDK:
-
向助手发送消息(无线程运行):
const { Client } = await import("@langchain/langgraph-sdk"); // only set the apiUrl if you changed the default port when calling langgraph dev const client = new Client({ apiUrl: "http://localhost:2024"}); const streamResponse = client.runs.stream( null, // Threadless run "agent", // Assistant ID { input: { "messages": [ { "role": "user", "content": "What is LangGraph?"} ] }, streamMode: "messages-tuple", } ); for await (const chunk of streamResponse) { console.log(`Receiving new event of type: ${chunk.event}...`); console.log(JSON.stringify(chunk.data)); console.log("\n\n"); }
下一步¶
现在你已经在本地运行了一个 LangGraph 应用,继续你的旅程,探索部署和高级功能:
- 部署快速入门: 使用 LangGraph 平台部署你的 LangGraph 应用。
- LangGraph 平台概述: 了解 LangGraph 平台的基础概念。
- LangGraph 服务器 API 参考: 探索 LangGraph 服务器的 API 文档。
- Python SDK 参考: 探索 Python SDK 的 API 参考。
- JS/TS SDK 参考: 探索 JS/TS SDK 的 API 参考。