跳到内容

LangGraph Studio 中的提示词工程

概览

代理开发的一个核心方面是提示词工程。LangGraph Studio 使您可以在 UI 中直接轻松地迭代图中使用到的提示词。

设置

第一步是定义您的配置,以便 LangGraph Studio 知道您想要迭代哪些提示词以及它们与哪些节点关联。

参考

定义配置时,您可以使用特殊的元数据键来指示 LangGraph Studio 如何处理不同的字段。以下是可用配置选项的参考信息:

langgraph_nodes

  • 描述:指定配置字段与哪些图节点关联。
  • 值类型:字符串数组,其中每个字符串是图中的节点名称。
  • 使用场景:包含在 Pydantic 模型的 json_schema_extra 字典中或 dataclasses 的 metadata["json_schema_extra"] 字典中。
  • 必需:否,但如果您希望某个字段在 UI 中对特定节点可编辑,则必需。
  • 示例:
    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",
        },
    )
    

示例

例如,如果您有一个名为 call_model 的节点,并且想迭代其系统提示词,可以定义如下配置。

## 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-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"]},
    )

## 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"]},
        },
    )

迭代提示词

节点配置

完成此设置后,运行图并在 LangGraph Studio 中查看将呈现如下所示的图。

请注意 call_model 节点右上角的配置图标:

Graph in Studio

点击此图标将打开一个模态框,您可以在其中编辑与 call_model 节点关联的所有字段的配置。在此处,您可以保存更改并将其应用于图。请注意,这些值反映了当前激活的助手,保存后会用新值更新助手。

Configuration modal

Playground

LangGraph Studio 还通过与 LangSmith Playground 集成来支持提示词工程。具体操作如下:

  1. 打开现有会话或创建新会话。
  2. 在会话日志中,任何执行过 LLM 调用的节点都会有一个“查看 LLM 运行”按钮。点击此按钮将打开一个弹窗,显示该节点的 LLM 运行信息。
  3. 选择您想要编辑的 LLM 运行。这将打开 LangSmith Playground,并显示选定的 LLM 运行。

Playground in Studio

在这里,您可以编辑提示词、测试不同的模型配置,并仅重新运行此 LLM 调用,而无需重新运行整个图。当您对更改满意后,可以将更新后的提示词复制回您的图中。

有关如何使用 LangSmith Playground 的更多信息,请参阅 LangSmith Playground 文档

评论