查询公共索引以获取最近邻

创建和部署索引后,您可以运行查询以获取最近邻。

以下是匹配查询的一些示例,这些查询使用 k 最近邻算法 (k-NN) 查找最近邻。

公共端点查询示例

Python

Python

def vector_search_find_neighbors(
    project: str,
    location: str,
    index_endpoint_name: str,
    deployed_index_id: str,
    queries: List[List[float]],
    num_neighbors: int,
) -> None:
    """Query the vector search index.

    Args:
        project (str): Required. Project ID
        location (str): Required. The region name
        index_endpoint_name (str): Required. Index endpoint to run the query
        against.
        deployed_index_id (str): Required. The ID of the DeployedIndex to run
        the queries against.
        queries (List[List[float]]): Required. A list of queries. Each query is
        a list of floats, representing a single embedding.
        num_neighbors (int): Required. The number of neighbors to return.
    """
    # Initialize the Vertex AI client
    aiplatform.init(project=project, location=location)

    # Create the index endpoint instance from an existing endpoint.
    my_index_endpoint = aiplatform.MatchingEngineIndexEndpoint(
        index_endpoint_name=index_endpoint_name
    )

    # Query the index endpoint for the nearest neighbors.
    resp = my_index_endpoint.find_neighbors(
        deployed_index_id=deployed_index_id,
        queries=queries,
        num_neighbors=num_neighbors,
    )
    print(resp)

Curl

您可以在部署中找到下面列出的 publicEndpointDomainName


  $ curl -X POST -H "Content-Type: application/json" -H "Authorization: Bearer `gcloud auth print-access-token`" http://1957880287.us-central1-181224308459.vdb.vertexai.goog/v1/projects/181224308459/locations/us-central1/indexEndpoints/3370566089086861312:findNeighbors -d '{deployed_index_id: "test_index_public1", queries: [{datapoint: {datapoint_id: "0", feature_vector: [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]}, neighbor_count: 5}]}'
  

此 curl 示例展示了如何从 http(s) 客户端进行调用,虽然公共端点支持 RESTful 和 grpc_cli 的双协议。


  $ curl -X POST -H "Content-Type: application/json" -H "Authorization: Bearer `gcloud auth print-access-token`" http://1957880287.us-central1-181224308459.vdb.vertexai.goog/v1/projects/${PROJECT_ID}/locations/us-central1/indexEndpoints/${INDEX_ENDPOINT_ID}:readIndexDatapoints -d '{deployed_index_id:"test_index_public1", ids: ["606431", "896688"]}'
  

控制台

按照以下说明从控制台查询部署到公共端点的索引。

  1. 在 Google Cloud 控制台的 Vertex AI 部分中,前往部署和使用部分。选择 Vector Search

    前往 Vector Search

  2. 选择要查询的索引。此时会打开索引信息页面。
  3. 向下滚动到已部署的索引部分,然后选择要查询的已部署索引。此时会打开已部署的索引信息页面。
  4. 查询索引部分中,选择查询参数。您可以选择按向量或特定数据点进行查询。
  5. 使用提供的 curl 命令或使用 Cloud Shell 运行来执行查询。
  6. 如果使用 Cloud Shell,请选择在 Cloud Shell 中运行
  7. 在 Cloud Shell 中运行。
  8. 结果会返回最近邻。

如需查看端到端示例,了解如何创建索引、如何将其部署到公共端点以及如何查询,请参阅官方笔记本:使用 Vector Search 和 Vertex AI Embeddings for Text 处理 StackOverflow 问题

影响性能的查询时间设置

使用 Vector Search 时,以下查询时参数可能会影响延迟时间、可用性和费用。本指南适用于大多数情况。 但是,请始终对您的配置进行实验,以确保它们适用于您的用例。

如需了解参数定义,请参阅索引配置参数

参数 简介 性能影响
approximateNeighborsCount 告知算法要从每个分片中检索的近似结果数。

增加 approximateNeighborsCount 的值可能会通过以下方式影响性能:

  • 召回率:提高了
  • 延迟时间:可能增加
  • 可用性:无影响
  • 费用:可能会增加,因为搜索期间处理的数据会更多

降低 approximateNeighborsCount 的值可能会通过以下方式影响性能:

  • 召回率:降低
  • 延迟时间:可能会缩短
  • 可用性:无影响
  • 费用:可以降低费用,因为搜索期间处理的数据量较少
setNeighborCount 指定您希望查询返回的结果数。

在大多数用例中,小于或等于 300 的值仍然保持高性能。 对于较大的值,请针对具体用例进行测试。

approximateNeighborsCount 应始终大于 setNeighborsCount。如果 setNeighborsCount 的值较小,则建议为 approximateNeighborsCount 使用该值的 10 倍。对于较大的 setNeighborsCount 值,可以使用较小的乘数。

fractionLeafNodesToSearch 控制搜索最近邻时要访问的叶节点的百分比。这与 leafNodeEmbeddingCount 相关,因为每个叶节点的嵌入越多,每个叶节点检查的数据就越多。

增加 fractionLeafNodesToSearch 的值可通过以下方式影响性能:

  • 召回率:提高了
  • 延迟时间:增加
  • 可用性:无影响
  • 费用:可能会增加,因为延迟时间较长会占用更多的机器资源

降低 fractionLeafNodesToSearch 的值可能会通过以下方式影响性能:

  • 召回率:降低
  • 延迟时间:缩短
  • 可用性:无影响
  • 费用:可能会降低,因为较短的延迟时间会占用较少的机器资源

后续步骤