以编程方式使用文档

LangSmith Studio 提供工具用于检查、调试和改进您的应用,不仅仅是执行。通过使用追踪、数据集和提示词,您可以详细了解应用程序的行为、衡量其性能并优化其输出:

迭代提示词

Studio 支持以下方法来修改图形中的提示词:

直接节点编辑

Studio 允许您直接从图形界面编辑各个节点中使用的提示词。

图形配置

定义您的 配置 来指定提示词字段及其关联的节点,使用 langgraph_nodeslanggraph_type keys.

langgraph_nodes

- **描述**:指定配置字段关联的图形节点。 - **值类型**:字符串数组,其中每个字符串都是图形中节点的名称。 - **使用上下文**:包含在 Pydantic 模型的 json_schema_extra 字典中,或 dataclass 的 metadata["json_schema_extra"] 字典中。 - **示例**:

  system_prompt: str = Field(
      default="You are a helpful AI assistant.",
      json_schema_extra={"langgraph_nodes": ["call_model", "other_node"]},
  )
  

langgraph_type

- **描述**:指定配置字段的类型,决定其在 UI 中的处理方式。 - **值类型**:字符串 - **支持的值**: * "prompt":表示该字段包含提示词文本,应在 UI 中进行特殊处理。 - **使用上下文**:包含在 Pydantic 模型的 json_schema_extra 字典中,或 dataclass 的 metadata["json_schema_extra"] 字典中。 - **示例**:

  system_prompt: str = Field(
      default="You are a helpful AI assistant.",
      json_schema_extra={
          "langgraph_nodes": ["call_model"],
          "langgraph_type": "prompt",
      },
  )
  

Full example configuration

## Using Pydantic
from pydantic import BaseModel, Field
from typing import Annotated, Literal

class Configuration(BaseModel):
    """The configuration for the agent."""

    system_prompt: str = Field(
        default="You are a helpful AI assistant.",
        description="The system prompt to use for the agent's interactions. "
        "This prompt sets the context and behavior for the agent.",
        json_schema_extra={
            "langgraph_nodes": ["call_model"],
            "langgraph_type": "prompt",
        },
    )

    model: Annotated[
        Literal[
            "anthropic/claude-sonnet-4-6",
            "anthropic/claude-haiku-4-5-20251001",
            "openai/o1",
            "openai/gpt-5.4-mini",
            "openai/o1-mini",
            "openai/o3-mini",
        ],
        {"__template_metadata__": {"kind": "llm"}},
    ] = Field(
        default="openai/gpt-5.4-mini",
        description="The name of the language model to use for the agent's main interactions. "
        "Should be in the form: provider/model-name.",
        json_schema_extra={"langgraph_nodes": ["call_model"]},
    )

## Using Dataclasses
from dataclasses import dataclass, field

@dataclass(kw_only=True)
class Configuration:
    """The configuration for the agent."""

    system_prompt: str = field(
        default="You are a helpful AI assistant.",
        metadata={
            "description": "The system prompt to use for the agent's interactions. "
            "This prompt sets the context and behavior for the agent.",
            "json_schema_extra": {"langgraph_nodes": ["call_model"]},
        },
    )

    model: Annotated[str, {"__template_metadata__": {"kind": "llm"}}] = field(
        default="anthropic/claude-3-5-sonnet-20240620",
        metadata={
            "description": "The name of the language model to use for the agent's main interactions. "
            "Should be in the form: provider/model-name.",
            "json_schema_extra": {"langgraph_nodes": ["call_model"]},
        },
    )

在 UI 中编辑提示词

  1. 在具有关联配置字段的节点上找到齿轮图标。
  2. 点击打开配置模态框。
  3. 编辑值。
  4. 保存以更新当前助手版本或创建新版本。

Playground

游乐场 界面允许测试单个 LLM 调用,无需运行完整图:

  1. 选择一个线程。
  2. 点击 **查看 LLM 运行** 在节点上。这会列出节点内所有 LLM 调用(如果有)。
  3. 选择一个 LLM 运行以在游乐场中打开。
  4. 修改提示并测试不同的模型和工具设置。
  5. 将更新的提示复制回您的图。

在数据集上运行实验

Studio 让您运行 评估 通过针对预定义的 LangSmith 数据集执行您的助手。这允许您测试各种输入的性能,将输出与参考答案进行比较,并使用配置的 评估器.

本指南向您展示如何直接从 Studio 运行完整的端到端实验。

前提条件

在运行实验之前,请确保您满足以下条件:

  • LangSmith 数据集:您的数据集应包含要测试的输入,以及可选的参考输出用于比较。输入的架构必须与助手所需的输入架构匹配。有关架构的更多信息,请参阅 图 API 架构文档。有关创建数据集的更多信息,请参阅 如何管理数据集.
  • (可选)评估器:您可以在 LangSmith 中向数据集附加评估器(例如 LLM-as-a-Judge、启发式方法或自定义函数)。这些将在图处理完所有输入后自动运行。
  • 正在运行的应用程序:可以针对以下项运行实验:
  • - 部署在 LangSmith.
  • - 上的应用程序通过 langgraph-cli.

实验设置

  1. 启动实验。点击 **运行实验** 按钮在 Studio 页面右上角。
  2. 选择您的数据集。在出现的模态框中,选择用于实验的数据集(或特定数据集拆分)并点击 **开始**.
  3. 监控进度。数据集中的所有输入现在将在活动助手上运行。通过右上角的徽章监控实验进度。
  4. 您可以在实验于后台运行时继续在Studio中工作。随时点击箭头图标按钮导航到LangSmith并查看详细的实验结果。

调试LangSmith追踪

本指南说明如何在Studio中打开LangSmith追踪进行交互式调查和调试。

打开已部署的线程

  1. 打开LangSmith追踪,选择根运行。
  2. 点击 **在Studio中运行**.

这将打开Studio并连接到相关部署,同时选中追踪的父线程。

使用远程追踪测试本地代理

本节说明如何使用LangSmith中的远程追踪测试本地代理。这使您能够将生产追踪作为本地测试的输入,允许您在开发环境中调试和验证代理修改。

前置条件

克隆线程

  1. 打开LangSmith追踪,选择根运行。
  2. 点击 **在Studio中运行**.
  3. 输入您本地代理的URL。
  4. 选择 **在本地克隆线程**.
  5. 如果存在多个图,请选择目标图。

将在您的本地代理中创建一个新线程,其中线程历史从远程线程推断和复制,并将导航到Studio查看您本地运行的应用程序。

将节点添加到数据集

添加 示例 to LangSmith数据集 从线程日志中的节点。这对于评估代理的各个步骤非常有用。

  1. 选择一个线程。
  2. 点击 **添加到数据集**.
  3. Select nodes whose input/output you want to add to a dataset.
  4. 对于每个选中的节点,选择要创建示例的目标数据集。默认情况下,将选择特定助手和节点的数据集。如果此数据集尚不存在,将会被创建。
  5. Edit the example's input/output as needed before adding it to the dataset.
  6. 选择 **添加到数据集** 在页面底部将所有选中的节点添加到各自的数据集。

更多详细信息,请参阅 如何评估应用程序的中间步骤.