Skip to content

LangGraph 工作室中的提示工程

概述

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

配置

第一步是定义你的配置,以便 LangGraph Studio 了解你想要迭代的提示以及它们与哪些节点相关联。

参考

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

langgraph_nodes

  • 描述:指定配置字段与哪些图节点相关联。
  • 值类型:字符串数组,其中每个字符串是图中节点的名称。
  • 使用场景:包含在 Pydantic 模型的 json_schema_extra 字典中,或包含在数据类的 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 字典中,或包含在数据类的 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 的节点,并且想要对其系统提示进行迭代,你可以定义如下配置。

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

## 使用数据类
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 节点右上角的配置图标

Studio 中的图

点击此图标将打开一个模态框,你可以在其中编辑与 call_model 节点相关的所有字段的配置。在此处,你可以保存更改并将其应用到图中。请注意,这些值反映的是当前活动的助手,保存操作将使用新值更新助手。

配置模态框

playground(实验场)

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

  1. 打开一个现有的线程或创建一个新线程。
  2. 在线程日志中,任何进行过 LLM 调用的节点都会有一个“查看 LLM 运行记录”按钮。点击此按钮将打开一个弹出框,显示该节点的 LLM 运行记录。
  3. 选择你想要编辑的 LLM 运行记录。这将在 LangSmith Playground 中打开所选的 LLM 运行记录。

Studio 中的 playground

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

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

Comments