开启左侧

LangGraph Studio

[复制链接]
创想小编 发表于 4 小时前 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题
作者:CSDN博客
概述

本文档介绍如何在本地使用 LangGraph Studio 对智能体进行可视化、交互与调试。LangGraph Studio 是免费使用、功能强大的智能体 IDE,与 LangSmith 集成,提供追踪、评估和提示词工程能力,可查看智能体的完整思考过程、追踪每一步决策,用于开发更可靠的智能体。
Prerequisites(前置条件)

开始前需准备:
LangSmith API Key(免费注册获取)
    访问 LangSmith 官网:https://smith.langchain.com/。点击 Sign Up,使用邮箱注册账户。登录后,进入 Settings > API Keys,生成一个 API Key(稍后需要用到)。
Setup local LangGraph server(搭建本地 LangGraph 服务)

1. Install the LangGraph CLI

要求:Python ≥ 3.11
  1. pip install --upgrade "langgraph-cli[inmem]"
复制代码
2. Prepare your agent(准备智能体)

示例代码(agent.py):
  1. from langchain.agents import create_agent
  2. model = ChatOpenAI(model="gpt-4o")
  3. def send_email(to: str, subject: str, body: str):
  4.     """Send an email"""
  5.     email = {
  6.         "to": to,
  7.         "subject": subject,
  8.         "body": body
  9.     }
  10.     # ... email sending logic
  11.     return f"Email sent to {to}"
  12. agent = create_agent(
  13.     "openai:gpt-4o",
  14.     tools=[send_email],
  15.     prompt="You are an email assistant. Always use the send_email tool.",
  16. )
复制代码
3. Environment variables(环境变量)

在项目根目录创建 .env 文件,填入 LangSmith API Key:
  1. LANGSMITH_API_KEY=lsv2...
复制代码
注意:不要将 .env 提交到 Git 等版本控制系统。
4. Make your app LangGraph-compatible(使应用兼容 LangGraph)

在应用目录创建配置文件 langgraph.json:
  1. {
  2.   "dependencies": ["."],
  3.   "graphs": {
  4.     "agent": "./src/agent.py:agent"
  5.   },
  6.   "env": ".env"
  7. }
复制代码
create_agent() 会自动返回已编译的 LangGraph 图,可直接配置到 graphs 字段。
项目结构:
  1. my-app/
  2. ├── src
  3. │   └── agent.py
  4. ├── .env
  5. └── langgraph.json
复制代码
5. Install dependencies(安装依赖)

在项目根目录执行:
bash
运行
  1. pip install -e .
复制代码
6. View your agent in Studio(在 Studio 中查看智能体)

启动 LangGraph 服务:
bash
运行
  1. langgraph dev
复制代码
    Safari 会阻止 localhost 连接,可添加 --tunnel 通过安全隧道访问智能体 API 地址:http://127.0.0.1:2024Studio 访问地址:https://smith.langchain.com/studio/?baseUrl=http://127.0.0.1:2024
Studio 核心能力

    可视化智能体每一步执行流程,可回放任意输入查看精确的提示词、工具参数、返回值展示 Token 与延迟指标捕获工具异常并记录上下文状态支持热重载,修改提示词 / 工具后自动更新可从任意步骤重新运行对话线程支持从单工具示例扩展至多节点图

原文地址:https://blog.csdn.net/weixin_65875490/article/details/159205525
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

发布主题
阅读排行更多+

Powered by Discuz! X3.4© 2001-2013 Discuz Team.( 京ICP备17022993号-3 )