跳到内容

如何对助手进行版本控制

在本操作指南中,我们将逐步介绍如何创建和管理不同的助手版本。如果你还没有这样做,可以阅读这篇概念指南,以更好地理解助手版本控制是什么。本操作指南假设你有一个可配置的图,这意味着你已经定义了一个配置模式并将其传递给你的图,如下所示

class Config(BaseModel):
    model_name: Literal["anthropic", "openai"] = "anthropic"
    system_prompt: str

agent = StateGraph(State, config_schema=Config)
const ConfigAnnotation = Annotation.Root({
    modelName: Annotation<z.enum(["openai", "anthropic"])>({
        default: () => "anthropic",
    }),
    systemPrompt: Annotation<String>
});

// the rest of your code

const agent = new StateGraph(StateAnnotation, ConfigAnnotation);

设置

首先,让我们设置客户端和线程。如果你使用的是 Studio,只需打开应用程序到名为“agent”的图即可。如果使用 cURL,你无需执行任何操作,只需记下你的部署 URL 和你想使用的图的名称。

from langgraph_sdk import get_client

client = get_client(url=<DEPLOYMENT_URL>)
# Using the graph deployed with the name "agent"
graph_name = "agent"
import { Client } from "@langchain/langgraph-sdk";

const client = new Client({ apiUrl: <DEPLOYMENT_URL> });
// Using the graph deployed with the name "agent"
const graphName = "agent";

创建助手

在本例中,我们将通过修改图中使用的模型名称来创建一个助手。我们可以为此创建一个名为“openai_assistant”的新助手

openai_assistant = await client.assistants.create(graph_name, config={"configurable": {"model_name": "openai"}}, name="openai_assistant")
const openaiAssistant = await client.assistants.create({graphId: graphName, config: { configurable: {"modelName": "openai"}}, name: "openaiAssistant"});
curl --request POST \
--url <DEPOLYMENT_URL>/assistants \
--header 'Content-Type: application/json' \
--data '{
"graph_id": "agent",
"config": {"model_name": "openai"},
"name": "openai_assistant"
}'

使用 Studio

要使用 Studio 创建助手,请执行以下步骤

  1. 点击“创建新助手”按钮

    click create

  2. 使用创建助手面板输入你想创建的助手信息,然后点击创建

    create

  3. 查看你的助手是否已创建并在 Studio 中显示

    view create

  4. 点击选中助手旁边的编辑按钮来管理你创建的助手

    create edit

为助手创建新版本

现在假设我们想为助手添加一个系统提示。我们可以如下使用 update 端点来做到这一点。请注意,你必须传入完整的配置(以及元数据,如果你使用它)。更新端点完全从头开始创建新版本,不依赖于之前输入的配置。在这种情况下,我们需要继续告诉助手使用“openai”作为模型。

openai_assistant_v2 = await client.assistants.update(openai_assistant['assistant_id'], config={"configurable": {"model_name": "openai", "system_prompt": "You are a helpful assistant!"}})
const openaiAssistantV2 = await client.assistants.update(openaiAssistant['assistant_id'], {config: { configurable: {"modelName": "openai", "systemPrompt": "You are a helpful assistant!"}}});
curl --request PATCH \
--url <DEPOLYMENT_URL>/assistants/<ASSISTANT_ID> \
--header 'Content-Type: application/json' \
--data '{
"config": {"model_name": "openai", "system_prompt": "You are a helpful assistant!"}
}'

使用 Studio

  1. 首先,点击 openai_assistant 旁边的编辑按钮。然后,添加一个系统提示并点击“保存新版本”

    create new version

  2. 然后你可以在助手下拉列表中看到它被选中

    see version dropdown

  3. 你可以在助手的编辑面板中看到所有的版本历史

    see versions

将助手指向不同版本

在创建了多个版本后,我们可以通过使用 SDK 和 Studio 来更改助手指向的版本。在本例中,我们将把刚刚创建了两个版本的 openai_assistant 重置回第一个版本。当你创建新版本(通过使用 update 端点)时,助手会自动指向新创建的版本,因此按照上面的代码,我们的 openai_assistant 正指向第二个版本。在这里,我们将它更改为指向第一个版本

await client.assistants.set_latest(openai_assistant['assistant_id'], 1)
await client.assistants.setLatest(openaiAssistant['assistant_id'], 1);
curl --request POST \
--url <DEPLOYMENT_URL>/assistants/<ASSISTANT_ID>/latest \
--header 'Content-Type: application/json' \
--data '{
"version": 1
}'

使用 Studio

要更改版本,你只需点击助手的编辑面板,选择你想切换到的版本,然后点击“设为当前版本”按钮

set version

使用你的助手版本

无论你是无需编写代码的商业用户,还是使用 SDK 的开发者,助手版本控制都能让你在受控环境中快速测试不同的智能体,从而轻松实现快速迭代。你可以像使用普通助手一样使用任何助手版本,并且可以通过阅读这些指南或(如果你正在使用 Studio)阅读这篇指南来了解如何从这些助手流式输出。

删除助手

删除助手将删除其所有版本,因为它们都指向同一个助手 ID。目前无法只删除单个版本,但通过将助手指向正确的版本,你可以跳过任何你不想使用的版本。

评论