创建或重复使用示例商店实例

本页面介绍了如何创建新的 Example Store 实例或重复使用现有的 Example Store 实例。您可以在开发 LLM 应用时将示例存储在 Example Store 中,并动态检索这些示例以在 LLM 提示中使用。

如需使用少样本示例来训练 LLM 或代理,您必须先为您的项目和位置创建或重复使用 Example Store 实例,然后将示例上传到相应实例。

对于每个项目和位置,您最多可以有 50 个 Example Store 实例。创建 Example Store 实例后,您可以在多个 LLM 应用和代理之间共享该实例。

您可以通过以下两种方式预配 Example Store 实例:

  • 创建新的 Example Store 实例:创建新的 Example Store 实例时,您需要指定嵌入模型,Example Store 会使用该模型来确定哪些示例与用户的查询相关。Example Store 支持以下嵌入模型:

    • text-embedding-005

    • text-multilingual-embedding-002

    创建 Example Store 实例后,您无法更改嵌入模型。如果您想使用其他嵌入模型,则必须创建另一个 Example Store 实例。如需详细了解文本嵌入,请参阅获取文本嵌入

  • 重复使用现有的 Example Store 实例:Example Store 实例设计为可供多个代理使用,因此您可以在不同的 LLM 应用中访问存储的示例。重复使用现有的 Example Store 实例时,您无法更改嵌入模型。

前提条件

在使用本页面上的 Python 示例之前,请在本地 Python 环境中安装并初始化 Vertex AI SDK for Python。

  1. 运行以下命令以安装适用于示例商店的 Python 版 Vertex AI SDK。

    pip install --upgrade google-cloud-aiplatform>=1.87.0
  2. 使用以下代码示例导入并初始化适用于示例商店的 SDK。

    import vertexai
    from vertexai.preview import example_stores
    
    vertexai.init(
      project="PROJECT_ID",
      location="LOCATION"
    )
    

    替换以下内容:

    • PROJECT_ID:您的项目 ID。

    • LOCATION:您的区域。 仅支持 us-central1

创建 Example Store 实例

使用以下示例可为指定项目和位置创建 Example Store 实例。请注意,创建 Example Store 实例需要几分钟时间。

Python

在尝试此示例之前,请按照《Vertex AI 快速入门:使用客户端库》中的 Python 设置说明执行操作。 如需了解详情,请参阅 Vertex AI Python API 参考文档

如需向 Vertex AI 进行身份验证,请设置应用默认凭证。如需了解详情,请参阅为本地开发环境设置身份验证

import vertexai
from vertexai.preview import example_stores

vertexai.init(
    project="PROJECT_ID",
    location="LOCATION"
)

my_example_store = example_stores.ExampleStore.create(
    example_store_config=example_stores.ExampleStoreConfig(
        vertex_embedding_model="EMBEDDING_MODEL"
    )
)

替换以下内容:

  • PROJECT_ID:您的项目 ID。
  • LOCATION:要在其中创建示例商店的区域。仅支持区域 us-central1
  • EMBEDDING_MODEL:Example Store 实例用于确定哪些示例与用户查询相关的嵌入模型。Example Store 支持以下嵌入模型:
    • text-embedding-004
    • text-multilingual-embedding-002

REST

如需创建 ExampleStore 资源,请使用 exampleStores.create 方法发送 POST 请求。

在使用任何请求数据之前,请先进行以下替换:

  • PROJECT_ID:您的项目 ID。
  • LOCATION:要在其中创建 Example Store 实例的区域。仅支持区域 us-central1
  • DISPLAY_NAME:Example Store 实例的名称。
  • EMBEDDING_MODEL:Example Store 实例用于确定哪些示例与用户查询相关的嵌入模型。Example Store 支持以下嵌入模型:
    • textembedding-gecko@003
    • text-embedding-004
    • text-multilingual-embedding-002

HTTP 方法和网址:

POST https://LOCATION-aiplatform.googleapis.com/v1beta1/projects/PROJECT_ID/locations/LOCATION/exampleStores

请求 JSON 正文:

{
  "display_name": "DISPLAY_NAME",
  "example_store_config": {"vertex_embedding_model": EMBEDDING_MODEL}
}

如需发送请求,请选择以下方式之一:

curl

将请求正文保存在名为 request.json 的文件中,然后执行以下命令:

curl -X POST \
-H "Authorization: Bearer $(gcloud auth print-access-token)" \
-H "Content-Type: application/json; charset=utf-8" \
-d @request.json \
"https://LOCATION-aiplatform.googleapis.com/v1beta1/projects/PROJECT_ID/locations/LOCATION/exampleStores"

PowerShell

将请求正文保存在名为 request.json 的文件中,然后执行以下命令:

$cred = gcloud auth print-access-token
$headers = @{ "Authorization" = "Bearer $cred" }

Invoke-WebRequest `
-Method POST `
-Headers $headers `
-ContentType: "application/json; charset=utf-8" `
-InFile request.json `
-Uri "https://LOCATION-aiplatform.googleapis.com/v1beta1/projects/PROJECT_ID/locations/LOCATION/exampleStores" | Select-Object -Expand Content

您应该会收到类似以下内容的 JSON 响应,其中 EXAMPLE_STORE_ID 表示 Example Store 实例的 ID。

重复使用现有的 Example Store 实例

使用以下示例可对指定项目和位置重复使用现有的 Example Store 实例。

Python

在尝试此示例之前,请按照《Vertex AI 快速入门:使用客户端库》中的 Python 设置说明执行操作。 如需了解详情,请参阅 Vertex AI Python API 参考文档

如需向 Vertex AI 进行身份验证,请设置应用默认凭证。如需了解详情,请参阅为本地开发环境设置身份验证

import vertexai
from vertexai.preview import example_stores

vertexai.init(
    project="PROJECT_ID",
    location="LOCATION"
)

example_store = example_stores.ExampleStore(
    "EXAMPLE_STORE_NAME")

替换以下内容:

  • PROJECT_ID:您的项目 ID。
  • LOCATION:要在其中创建示例商店的区域。仅支持区域 us-central1
  • EXAMPLE_STORE_NAME:要重复使用的 Example Store 实例的名称。

后续步骤