Documentation Index Fetch the complete documentation index at: https://nvd-54.mintlify.app/llms.txt
Use this file to discover all available pages before exploring further.
本指南向你展示如何在本地运行 LangGraph 应用。
前提条件
开始之前,请确保你具备以下条件:
1. 安装 LangGraph CLI
# 需要 Python >= 3.11。
pip install -U "langgraph-cli[inmem]"
2. 创建 LangGraph 应用
从 new-langgraph-project-python 模板 创建一个新应用。此模板演示了一个可以用你自己的逻辑扩展的单节点应用。
langgraph new path/to/your/app --template new-langgraph-project-python
更多模板
如果你使用 langgraph new 而不指定模板,将会显示一个交互式菜单,让你从可用模板列表中选择。
3. 安装依赖
在新 LangGraph 应用的根目录中,以 edit 模式安装依赖,这样你的本地更改会被服务器使用:
cd path/to/your/app
pip install -e .
4. 创建 .env 文件
你会在新 LangGraph 应用的根目录中找到一个 .env.example 文件。在新 LangGraph 应用的根目录中创建一个 .env 文件,将 .env.example 文件的内容复制到其中,并填写必要的 API 密钥:
LANGSMITH_API_KEY = lsv2...
5. 启动智能体服务器
在本地启动 LangGraph API 服务器:
示例输出:
INFO:langgraph_api.cli:
Welcome to
╦ ┌─┐┌┐┌┌─┐╔═╗┬─┐┌─┐┌─┐┬ ┬
║ ├─┤││││ ┬║ ╦├┬┘├─┤├─┘├─┤
╩═╝┴ ┴┘└┘└─┘╚═╝┴└─┴ ┴┴ ┴ ┴
- 🚀 API: http://127.0.0.1:2024
- 🎨 Studio UI: https://smith.langchain.com/studio/?baseUrl=http://127.0.0.1:2024
- 📚 API Docs: http://127.0.0.1:2024/docs
This in-memory server is designed for development and testing.
For production use, please use LangSmith Deployment.
langgraph dev 命令以内存模式启动 Agent Server。此模式适合开发和测试目的。对于生产使用,请部署带有持久存储后端访问权限的 Agent Server。更多信息请参阅平台设置概述 。
6. 在 Studio 中测试你的应用
Studio 是一个专用 UI,你可以将其连接到 LangGraph API 服务器,以可视化、交互和调试你的本地应用。通过访问 langgraph dev 命令输出中提供的 URL 在 Studio 中测试你的图:
> - LangGraph Studio Web UI: https://smith.langchain.com/studio/?baseUrl=http://127.0.0.1:2024
对于在自定义主机/端口上运行的 Agent Server,请更新 URL 中的 baseUrl 查询参数。例如,如果你的服务器运行在 http://myhost:3000:
https://smith.langchain.com/studio/?baseUrl=http://myhost:3000
在命令中使用 --tunnel 标志创建安全隧道,因为 Safari 在连接 localhost 服务器时存在限制:
7. 测试 API
Python SDK(异步)
Python SDK(同步)
Rest API
安装 LangGraph Python SDK:
pip install langgraph-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 , # 无线程运行
"agent" , # 助手名称。在 langgraph.json 中定义。
input = {
"messages" : [{
"role" : "human" ,
"content" : "What is LangGraph?" ,
}],
},
):
print ( f "接收到新事件,类型为: { chunk . event } ..." )
print ( chunk . data )
print ( " \n\n " )
asyncio . run ( main ())
安装 LangGraph Python SDK:
pip install langgraph-sdk
向助手发送消息(无线程运行):
from langgraph_sdk import get_sync_client
client = get_sync_client ( url = "http://localhost:2024" )
for chunk in client . runs . stream (
None , # 无线程运行
"agent" , # 助手名称。在 langgraph.json 中定义。
input = {
"messages" : [{
"role" : "human" ,
"content" : "What is LangGraph?" ,
}],
},
stream_mode = "messages-tuple" ,
):
print ( f "接收到新事件,类型为: { chunk . event } ..." )
print ( chunk . data )
print ( " \n\n " )
curl -s --request POST \
--url "http://localhost:2024/runs/stream" \
--header 'Content-Type: application/json' \
--data "{
\" assistant_id \" : \" agent \" ,
\" input \" : {
\" messages \" : [
{
\" role \" : \" human \" ,
\" content \" : \" What is LangGraph? \"
}
]
},
\" stream_mode \" : \" messages-tuple \"
}"
后续步骤
现在你已经在本地运行了 LangGraph 应用,可以通过探索部署和高级功能来进一步深入:
将这些文档连接 到 Claude、VSCode 等工具,通过 MCP 获取实时答案。