跳到内容

如何对助手进行版本控制

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

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. 单击“创建新助手”按钮

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

使用工作室

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

使用工作室

要更改版本,您只需单击助手的编辑窗格,选择您要更改到的版本,然后单击“设置为当前版本”按钮

set version

使用您的助手版本

无论您是不编写代码的业务用户,还是使用 SDK 的开发人员 - 助手版本控制都允许您在受控环境中快速测试不同的代理,从而轻松快速迭代。您可以像使用普通助手一样使用任何助手版本,并且可以通过阅读这些指南这篇指南(如果您正在使用 Studio)来了解有关如何从这些助手流式传输输出的更多信息。

删除助手

删除助手将删除其所有版本,因为它们都指向相同的助手 ID。目前没有办法仅删除单个版本,但是通过将您的助手指向正确的版本,您可以跳过任何您不希望使用的版本。