跳转至

迭代提示

概述

LangGraph Studio 支持两种修改图中提示的方法:直接节点编辑和 LangSmith Playground 界面。

直接节点编辑

Studio 允许你直接从图界面编辑各个节点内部使用的提示。

先决条件

图配置

定义你的配置以使用 langgraph_nodeslanggraph_type 键指定提示字段及其关联的节点。

配置参考

langgraph_nodes
  • 描述:指定配置字段与图的哪些节点相关联。
  • 值类型:字符串数组,其中每个字符串是图中节点的名称。
  • 使用上下文:包含在 Pydantic 模型的 json_schema_extra 字典或 dataclasses 的 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 字典或 dataclasses 的 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",
        },
    )
    

配置示例

## 使用 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-3-7-sonnet-latest",
            "anthropic/claude-3-5-haiku-latest",
            "openai/o1",
            "openai/gpt-4o-mini",
            "openai/o1-mini",
            "openai/o3-mini",
        ],
        {"__template_metadata__": {"kind": "llm"}},
    ] = Field(
        default="openai/gpt-4o-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"]},
    )

## 使用 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. 保存以更新当前助手版本或创建新版本

LangSmith Playground

LangSmith Playground 界面允许测试单个 LLM 调用,而无需运行完整的图:

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

对于高级 Playground 功能,请点击右上角的展开按钮。