Skip to content

如何对助手进行版本管理

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

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"
}'

使用工作室

要使用工作室创建助手,请执行以下步骤:

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

    点击创建

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

    创建

  3. 确认你的助手已创建并显示在工作室中

    查看创建

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

    创建编辑

为你的助手创建新版本

现在假设我们想为我们的助手添加一个系统提示。我们可以通过使用 update 端点来实现,如下所示。请注意,你必须传入完整的配置(如果你使用了元数据,还需传入元数据)。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!"}
}'

使用工作室

  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

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

设置版本

使用你的助手版本

无论你是无需编写代码即可进行迭代的企业用户,还是使用 SDK 的开发者,助手版本控制都能让你在可控环境中快速测试不同的智能体,从而轻松实现快速迭代。你可以像使用普通助手一样使用任何助手版本,如果你想了解如何从这些助手流式输出内容,可以阅读 这些指南;如果你使用的是 Studio,也可以阅读 这篇指南

删除助手

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

Comments