以编程方式使用文档

提示引导大型语言模型(LLM)的行为。 提示工程 是创建、测试和改进你给LLM的指令的过程,使其产生可靠且有用的响应。

LangSmith 提供创建、版本控制、测试和协作提示的工具。你还会遇到常见的概念,如 提示模板,它允许你重复使用结构化提示,以及 变量,它允许你动态地将值(如用户的问题)插入到提示中。

在这个快速入门中,你将使用 UI 或 SDK 创建、测试和改进提示。此快速入门将使用 OpenAI 作为示例 LLM 提供商,但相同的工作流程也适用于其他提供商。

前提条件

在开始之前,请确保你拥有:

选择 UI 或 SDK 工作流程的标签:

UI

1. 设置工作区密钥

2. 创建提示

  1. LangSmith UI中,导航到左侧菜单中的 **提示** 部分。
  2. 点击 **+ 提示** 来创建提示。
  3. 根据需要通过编辑或添加提示和输入变量来修改提示。
Playground with the system prompt ready for editing. Playground with the system prompt ready for editing.

3. 测试提示

  1. 在 **提示** 标题下选择模型名称旁边的齿轮 图标,这将启动 **提示设置** 窗口在 **模型配置** tab.
  2. 设置 模型配置 你想使用的。在 **提供商** 和 **模型** 您选择的内容将决定此配置页面上可配置的参数。设置完成后,点击 **另存为**.
LangSmith UI 中的模型配置窗口,包含 Provider、Model、Temperature、Max Output Tokens、Top P、Presence Penalty、Frequency Penalty、Reasoning Effort 等设置。 LangSmith UI 中的模型配置窗口,包含 Provider、Model、Temperature、Max Output Tokens、Top P、Presence Penalty、Frequency Penalty、Reasoning Effort 等设置。
  1. 在 **输入** 框中指定要测试的输入变量,然后点击 **开始**.
输入框中已输入问题。输出框包含对提示的响应。 输入框中已输入问题。输出框包含对提示的响应。

要了解在 Playground 中配置提示的更多选项,请参阅 配置提示设置.

  1. 测试和完善提示后,点击 **保存** 以存储以供将来使用。

4. 迭代提示

LangSmith 支持团队协作进行提示迭代。 工作区 成员可以在 Playground 中尝试提示,并在准备好时将更改保存为新的 提交

要改进您的提示:

- 参考模型提供商的文档了解提示创建的最佳实践,例如: - OpenAI API 提示工程最佳实践 - Gemini 提示设计简介 - 使用 Prompt Canvas 构建和完善提示——这是 LangSmith 中的一个交互式工具。在 Prompt Canvas 指南. - 为特定提交添加标签,以标记提交历史中的重要时刻。 1. 要创建提交,请导航至 **Playground** 并选择 **提交**。选择要提交更改的提示,然后 **提交**. 1. 导航至 **提示** 在左侧菜单中。选择提示。在提示详情页面上,选择 **标签** 在右上角添加 提交标签.

SDK

1. 设置您的环境

  1. 在终端中,准备您的环境:
    mkdir ls-prompt-quickstart && cd ls-prompt-quickstart
    python -m venv .venv
    source .venv/bin/activate
    pip install -qU langsmith openai langchain_core
    
    mkdir ls-prompt-quickstart-ts && cd ls-prompt-quickstart-ts
    npm init -y
    npm install langsmith openai typescript ts-node
    npx tsc --init
    
  1. 设置您的 API 密钥:
    

2. 创建一个提示词

要创建提示词,您需要定义提示词中所需的消息列表,然后将其推送到 LangSmith。

使用特定语言的构造函数和推送方法:

  1. 将以下代码添加到 create_prompt file:
    from langsmith import Client
    from langchain_core.prompts import ChatPromptTemplate

    client = Client()

    prompt = ChatPromptTemplate([
        ("system", "You are a helpful chatbot."),
        ("user", "{question}"),
    ])

    client.push_prompt("prompt-quickstart", object=prompt)
    
    const client = new Client();

    const prompt = ChatPromptTemplate.fromMessages([
    ["system", "You are a helpful chatbot."],
    ["user", "{question}"],
    ]);

    await client.pushPrompt("prompt-quickstart", {
    object: prompt,
    });
    

这会创建一个有序的消息列表,将它们包装在 ChatPromptTemplate中,然后按名称将提示词推送到您的 工作空间 中以进行版本控制和重用。

  1. 运行 create_prompt:
    python create_prompt.py
    
    npx tsx create_prompt.ts
    

跟随生成的链接查看 LangSmith UI 中新创建的 Prompt Hub 提示词。

3. 测试提示词

在此步骤中,您将从 第 2 步 按名称拉取("prompt-quickstart"),使用测试输入对其进行格式化,转换为 OpenAI 的聊天格式,然后调用 OpenAI Chat Completions API。

然后,您将通过创建新版本迭代提示词。您工作空间的成员可以打开现有提示词,在 UI中尝试更改,并将这些更改保存为同一提示词上的新提交,从而为整个团队保留历史记录。

  1. 将以下内容添加到 test_prompt file:
    from langsmith import Client
    from openai import OpenAI
    from langchain_core.messages import convert_to_openai_messages

    client = Client()
    oai_client = OpenAI()

    prompt = client.pull_prompt("prompt-quickstart")

    # Since the prompt only has one variable you could also pass in the value directly
    # Equivalent to formatted_prompt = prompt.invoke("What is the color of the sky?")
    formatted_prompt = prompt.invoke({"question": "What is the color of the sky?"})

    response = oai_client.chat.completions.create(
        model="gpt-5.5",
        messages=convert_to_openai_messages(formatted_prompt.messages),
    )
    
    const oaiClient = new OpenAI();

    const prompt = await pull("prompt-quickstart");

    // Format the prompt with the question
    const formattedPrompt = await prompt.invoke({ question: "What is the color of the sky?" });

    const response = await oaiClient.chat.completions.create({
        model: "gpt-5.5",
        messages: convertPromptToOpenAI(formattedPrompt).messages,
    });
    

这会使用 pull 按名称加载提示词以获取您正在测试的提示词的最新提交版本。您还可以通过传递提交哈希 "<prompt-name>:<commit-hash>"

  1. 运行 test_prompt :
    python test_prompt.py
    
    npx tsx test_prompt.ts
    
  1. 要创建新版本的提示词,请使用相同的提示词名称和更新后的模板调用最初使用的推送方法。LangSmith 会将其记录为新提交并保留之前的版本。

将以下代码复制到 iterate_prompt file:

    from langsmith import Client
    from langchain_core.prompts import ChatPromptTemplate

    client = Client()

    new_prompt = ChatPromptTemplate([
        ("system", "You are a helpful chatbot. Respond in Spanish."),
        ("user", "{question}"),
    ])

    client.push_prompt("prompt-quickstart", object=new_prompt)
    
    const client = new Client();

    const newPrompt = ChatPromptTemplate.fromMessages([
        ["system", "You are a helpful chatbot. Speak in Spanish."],
        ["user", "{question}"]
    ]);

    await client.pushPrompt("prompt-quickstart", {
        object: newPrompt
    });
    
  1. 运行 iterate_prompt :
    python iterate_prompt.py
    
    npx tsx iterate_prompt.ts
    

现在您的提示词将包含两个提交。

改进您的提示词:

后续步骤