Use Vertex AI Vector Search

This document describes how to use the Vertex AI Vector Search to perform vector similarity searches, by using the Vertex AI SDK for ABAP. With Vector Search, you can find semantically similar data points within large datasets by using high-dimensional vectors. It excels in tasks such as image and text retrieval, prioritizing meaning over exact keyword matches.

Vector Search lets you harness the power of your enterprise data to improve efficiency, enhance customer experiences, and gain a competitive edge. It can bring significant value to your SAP environment, providing benefits such as the following:

  • Enhance search capabilities: Improve the accuracy and relevance of search results across vast amounts of SAP data, including both structured and unstructured information scattered across disparate systems. This leads to faster and more effective information retrieval of relevant enterprise data.

  • Unlock insights from unstructured data: Utilize Vector Search to extract valuable insights from previously untapped unstructured data within SAP systems, such as product data, customer data, customer reviews, support tickets, or internal documents.

  • Improve customer experience: Provide personalized and context-aware interactions with customers by using Vector Search for product recommendations, intelligent chatbots, and other customer-facing applications.

  • Streamline business processes: Optimize internal processes by using Vector Search to quickly find relevant information, identify patterns, and make data-driven decisions.

  • Accelerate innovation: Combine Vector Search with other Vertex AI capabilities, such as generative AI, to develop new and innovative solutions that drive business growth.

How to use Vertex AI Vector Search

Semantic matching by using Vector Search can be simplified into the following steps:

  1. Generate embedding representations of your enterprise data.
  2. Upload the embeddings to a Cloud Storage bucket.
  3. Create a vector index and associate the vector index to the Cloud Storage bucket containing embeddings. You can create two types of indexes, depending on how you plan to update them with most recent data:

    • Batch index: A batch index is for when you want to update your index in a batch, with data which has been stored over a set amount of time, like data which is processed weekly or monthly.
    • Stream index: A streaming index is for when you want index data to be updated as soon as new data is added to your datastore.
  4. Create an index endpoint and deploy the vector index to the endpoint to run queries to get recommendations or results. An index endpoint works as a server instance accepting query requests for your index.

  5. Query the index endpoint by using an entity ID, a search string, or an embedding.

The Vertex AI SDK for ABAP covers all of these aspects of Vertex AI Vector Search for you to build Vertex AI driven Vector Search applications in your ABAP environment.

Create and manage a vector index

This section explains how to create and manage a vector index by using the Vertex AI SDK for ABAP.

Before you begin

Make sure that you or your administrators have completed the following prerequisites:

Prepare your embeddings

You need to have your embeddings ready. For information about how to generate embeddings for your enterprise data by using the Vertex AI SDK for ABAP, see Generate embeddings.

Upload your embeddings to Cloud Storage

Upload your embeddings to a Cloud Storage bucket so that you can associate the embeddings with a vector index. Even if you have generated your embedding files outside of Google Cloud, you can upload those embedding files to a Cloud Storage bucket.

For information about how to send the embeddings that are generated by using the Vertex AI SDK for ABAP to a Cloud Storage bucket, see Store embeddings in Cloud Storage.

Instantiate the class for vector index

To create and manage your vector index, you can instantiate the /GOOG/CL_VECTOR_INDEX class. You instantiate the class by passing the client key configured for authentication.

DATA(lo_vector_index) = NEW /goog/cl_vector_index( iv_key_name = 'CLIENT_KEY' ).

Replace CLIENT_KEY with the client key configured for authentication.

Create a vector index

To create a vector index, you can use the CREATE_TREE_AH_INDEX method of the /GOOG/CL_VECTOR_INDEX class. It creates an index based on the tree-AH algorithm.

lo_vector_index->create_tree_ah_index( iv_display_name                = 'INDEX_NAME'
                                       iv_description                 = 'INDEX_DESCRIPTION'
                                       iv_location_id                 = 'LOCATION_ID'
                                       iv_gcs_content_uri             = 'CONTENT_URI'
                                       iv_dimensions                  = 'DIMENSIONS'
                                       iv_approximate_neighbors_count = 'NEIGHBORS_COUNT'
                                       iv_index_update_method         = 'INDEX_UPDATE_METHOD'
                                       iv_distance_measure_type       = 'DISTANCE_MEASURE_TYPE'
                                       iv_shard_size                  = 'SHARD_SIZE'
                                       iv_leaf_node_embedding_count   = 'LEAF_NODE_EMBEDDING_COUNT'
                                       iv_leaf_nodes_to_search        = 'LEAF_NODE_TO_SEARCH'
                                       iv_etag                        = 'ETAG'
                                       iv_cloud_kms_encryption_key     = 'KEY_FOR_ENCRYPTION' ).

Replace the following:

  • INDEX_NAME: Display name of the index.
  • INDEX_DESCRIPTION: Description of the index.
  • LOCATION_ID: Google Cloud region where you want to store the index. For information about the available locations, see Vertex AI locations.
  • CONTENT_URI: URI of the Cloud Storage bucket containing the embeddings, which is used to build the index.
  • DIMENSIONS: Number of dimensions of the input vectors.
  • NEIGHBORS_COUNT: Number of neighbors to find through approximate search before reordering is performed.
  • INDEX_UPDATE_METHOD: Update mode of the index: BATCH_UPDATE or STREAM_UPDATE.
  • DISTANCE_MEASURE_TYPE: Determines the algorithm used for distance calculation between data points and the query vector. For more information, see Distance measure type.
  • SHARD_SIZE: Size of each shard. When an index is large, it is sharded based on the specified shard size. During serving, each shard is served on a separate node and scales independently.
  • LEAF_NODE_EMBEDDING_COUNT: Number of embeddings on each leaf node. The default value is 1000, if not set.
  • LEAF_NODE_TO_SEARCH: The default percentage of leaf nodes that any query may be searched. Must be in range 1-100, inclusive. The default value is 10 (means 10%), if not set.
  • ETAG: An ETag value to perform consistent read-modify-write updates.
  • KEY_FOR_ENCRYPTION: Cloud KMS resource identifier of the customer managed encryption key.

Create a vector index endpoint

To create a vector index endpoint, you can use the CREATE_INDEX_ENDPOINT method of the /GOOG/CL_VECTOR_INDEX class.

lo_vector_index->create_index_endpoint( iv_display_name             = 'INDEX_ENDPOINT_NAME'
                                        iv_description              = 'INDEX_ENDPOINT_DESCRIPTION'
                                        iv_location_id              = 'LOCATION_ID'
                                        iv_public_endpoint_enabled  = 'ENABLE_PUBLIC_ENDPOINT'
                                        iv_etag                     = 'ETAG'
                                        iv_cloud_kms_encryption_key = 'KEY_FOR_ENCRYPTION' ).

Replace the following:

  • INDEX_ENDPOINT_NAME: Display name of the index endpoint.
  • INDEX_ENDPOINT_DESCRIPTION: Description of the index endpoint.
  • LOCATION_ID: Google Cloud region where you want to create the index endpoint. For information about the available locations, see Vertex AI locations.
  • ENABLE_PUBLIC_ENDPOINT: If the deployed index is accessible through a public endpoint, then set this parameter value to ABAP_TRUE.
  • ETAG: An ETag value to perform consistent read-modify-write updates.
  • KEY_FOR_ENCRYPTION: Cloud KMS resource identifier of the customer managed encryption key.

Deploy a vector index to an index endpoint

To deploy a vector index to an index endpoint, you can use the DEPLOY_INDEX method of the /GOOG/CL_VECTOR_INDEX class.

lo_vector_index->deploy_index( iv_deployed_index_id     = 'DEPLOYMENT_ID'
                               iv_location_id           = 'LOCATION_ID'
                               iv_index_id              = 'INDEX_ID'
                               iv_index_endpoint_id     = 'INDEX_ENDPOINT_ID>'
                               iv_min_replica_count     = 'MIN_REPLICA_COUNT'
                               iv_max_replica_count     = 'MAX_REPLICA_COUNT'
                               iv_enable_access_logging = 'ENABLE_ACCESS_LOGGING'
                               iv_deployment_group      = 'DEPLOYMENT_GROUP' ).

Replace the following:

  • DEPLOYMENT_ID: ID of the index deployment.
  • LOCATION_ID: Google Cloud region where you want to deploy the index. For information about the available locations, see Vertex AI locations.
  • INDEX_ID: Resource name of the index.
  • INDEX_ENDPOINT_ID: Resource name of the index endpoint to deploy to.
  • MIN_REPLICA_COUNT: Minimum number of machine replicas for the deployed model.
  • MAX_REPLICA_COUNT: Maximum number of machine replicas for the deployed model.
  • ENABLE_ACCESS_LOGGING: To send private endpoint's access logs to Cloud Logging, set this parameter value to ABAP_TRUE.
  • DEPLOYMENT_GROUP: Name of the deployment group, for example, test, prod.

Update and rebuild a vector index

To get the most accurate Vector Search results for an enterprise AI solution, you also need to keep the index updated with the most recent enterprise data.

Update a batch index

To update a batch index with updated information present in a Cloud Storage bucket, you can use the PATCH_TREE_AH_INDEX method of the /GOOG/CL_VECTOR_INDEX class.

lo_vector_index->patch_tree_ah_index( iv_index_id              = 'INDEX_ID'
                                      iv_gcs_content_uri       = 'CONTENT_URI'
                                      iv_location_id           = 'LOCATION_ID'
                                      iv_is_complete_overwrite = 'IS_COMPLETE_OVERWRITE' ).

Replace the following:

  • INDEX_ID: Resource name of the index.
  • CONTENT_URI: URI of the Cloud Storage bucket having the embeddings for the most recent enterprise data.
  • LOCATION_ID: Google Cloud region of the index.
  • IS_COMPLETE_OVERWRITE: To completely overwrite the index with data in the given Cloud Storage bucket, set this parameter value to ABAP_TRUE.

You can use the following example architecture flow to update a batch index with your SAP data:

  • Update the Cloud Storage bucket with embeddings for most recent data by using the /GOOG/CL_STORAGE_V1 class, through an SAP background job. For non-SAP data, this can also be done through a process outside SAP.
  • Trigger batch index updates by using the PATCH_TREE_AH_INDEX method of the /GOOG/CL_VECTOR_INDEX class, through a background job in SAP, run at a frequency.

Update a stream index

To upsert information for a data point for a stream index, you can use the UPSERT_DATAPOINTS method of the /GOOG/CL_VECTOR_INDEX class.

lo_vector_index->upsert_datapoints( iv_index_id     = 'INDEX_ID'
                                    iv_location_id  = 'LOCATION_ID'
                                    iv_datapoint_id = 'ENTITY_ID'
                                    it_embeddings   = 'EMBEDDINGS' ).

Replace the following:

  • INDEX_ID: Resource name of the index.
  • LOCATION_ID: Google Cloud region of the index.
  • ENTITY_ID: Entity ID to upsert to.
  • EMBEDDINGS: Embeddings to upsert for the data point.

To remove information for a data point from a stream index, you can use the REMOVE_DATAPOINTS method of the /GOOG/CL_VECTOR_INDEX class.

lo_vector_index->remove_datapoints( iv_index_id     = 'INDEX_ID'
                                    iv_location_id  = 'LOCATION_ID'
                                    iv_datapoint_id = 'ENTITY_ID' ).

Replace the following:

  • INDEX_ID: Resource name of the index.
  • LOCATION_ID: Google Cloud region of the index.
  • ENTITY_ID: Entity ID to remove.

Upsert and remove operations are done in real time for the index.

You can use the following example architecture flow to update a stream index with your SAP data:

  • Identify the placeholders in your enterprise SAP solution, such as BADIs, exits, enhancements, and custom SAP logic, which are points for SAP data change.
  • Trigger the upsert or remove operation for SAP data from the stream index using the UPSERT_DATAPOINTS and REMOVE_DATAPOINTS methods of the /GOOG/CL_VECTOR_INDEX class, from your identified placeholder.

Get the status of the vector index operations

Google Cloud performs the following as a long-running operation: creation of vector index and index endpoint, deployment of index to an index endpoint, or update of a vector batch index.

To determine the long-running operation ID, resource ID of the Vertex AI artifacts, and error messages, the SDK provides the /GOOG/CL_VECTORINDEX_RESPONSE class.

The response captured by the /GOOG/CL_VECTORINDEX_RESPONSE class is chained to the requests made through the methods of the /GOOG/CL_VECTOR_INDEX class, so that you can directly access the response in a single statement without requiring variables to store the intermediate results.

Get the long-running operation ID

To get the long-running operation ID for each vector search task, you can use the GET_LRO method of the /GOOG/CL_VECTORINDEX_RESPONSE class. After executing a task, you can call the GET_LRO method with the same instance to get the corresponding long-running operation ID.

The following code sample illustrates how to get the long-running operation ID for a batch index update task:

DATA(lv_patch_index_lro) = lo_vector_index->patch_tree_ah_index(
                                              iv_index_id              = 'INDEX_ID'
                                              iv_gcs_content_uri       = 'CONTENT_URI'
                                              iv_location_id           = 'LOCATION_ID'
                                              iv_is_complete_overwrite = 'IS_COMPLETE_OVERWRITE'
                                         )->get_lro( ).

Replace the following:

  • INDEX_ID: Resource name of the index.
  • CONTENT_URI: URI of the Cloud Storage bucket having the embeddings for the most recent enterprise data.
  • LOCATION_ID: Google Cloud region of the index.
  • IS_COMPLETE_OVERWRITE: To completely overwrite the index with data in the given Cloud Storage bucket, set this parameter value to ABAP_TRUE.

Get the status of the long-running operation

Every long-running operation has a status associated with it. To validate if the task scheduled as a long-running operation is successful or not, you can check the status of the operation.

To determine or continually monitor the status of a long-running operation for any Vertex AI related tasks, you can use the GET_LRO_STATUS method of the /GOOG/CL_VERTEX_AI_SDK_UTILITY class.

/goog/cl_vertex_ai_sdk_utility=>get_lro_status(
    EXPORTING
        iv_key           = 'CLIENY_KEY'
        iv_operations_id = 'LONG_RUNNING_OPERATION_ID'
        iv_location_id   = 'LOCATION_ID'
    IMPORTING
        ev_is_done       = DATA(lv_is_done)          "Is the long-running operation complete
        ev_is_successful = DATA(lv_is_successful)    "Is the long-running operation successful
        ev_error_code    = DATA(lv_error_code)       "Error code in the long-running operation in case of errors
        ev_error_message = DATA(lv_error_message) ). "Error message in the long-running operation in case of errors

Replace the following:

  • CLIENY_KEY: Client key for authentication.
  • LONG_RUNNING_OPERATION_ID: Long-running operation ID.
  • LOCATION_ID: Google Cloud region of the index.

Get the resource ID of the Vertex AI artifact

To determine the resource ID of the created index, created index endpoint, deployed index, index ID for which patch is triggered, you can use the GET_ID method of the /GOOG/CL_VECTORINDEX_RESPONSE class.

After executing a task, you can call the GET_ID method with the same instance to get the corresponding resource ID.

The following code sample illustrates how to get the ID of the index being created after calling the CREATE_TREE_AH_INDEX method of the /GOOG/CL_VECTOR_INDEX class:

DATA(lv_id) = lo_vector_index->create_tree_ah_index(
                                 iv_display_name                = 'INDEX_NAME'
                                 iv_location_id                 = 'LOCATION_ID'
                                 iv_gcs_content_uri             = 'CONTENT_URI'
                                 iv_dimensions                  = 'DIMENSIONS'
                                 iv_approximate_neighbors_count = 'NEIGHBORS_COUNT'
                                 iv_leaf_node_embedding_count   = 'LEAF_NODE_EMBEDDING_COUNT'
                                 iv_leaf_nodes_to_search        = 'LEAF_NODE_TO_SEARCH'
                            )->get_id( ).

Replace the following:

  • INDEX_NAME: Display name of the index.
  • LOCATION_ID: Google Cloud region of the index.
  • CONTENT_URI: URI of the Cloud Storage bucket containing the embeddings, which is used to build the index.
  • DIMENSIONS: Number of dimensions of the input vectors.
  • NEIGHBORS_COUNT: Neighbors to find through approximate search before reordering is performed.
  • LEAF_NODE_EMBEDDING_COUNT: Number of embeddings on each leaf node. The default value is 1000, if not set.
  • LEAF_NODE_TO_SEARCH: The default percentage of leaf nodes that any query may be searched. Must be in range 1-100, inclusive. The default value is 10 (means 10%), if not set.

Get the error code and the error message

Errors might occur during the trigger of vector index tasks through the Vertex AI SDK for ABAP.

To get the error code and the error message respectively (if any) after the trigger of a task, you can use the GET_ERROR_CODE and GET_ERROR_MESSAGE methods of the `/GOOG/CL_VECTORINDEX_RESPONSE class.

After executing a task, you can call the GET_ERROR_CODE and GET_ERROR_MESSAGE methods with the same instance to get the corresponding error code or error message.

The following code sample illustrates how to display error code and error message if creation of an index fails:

DATA(lo_vectorindex_response) = lo_vector_index->create_tree_ah_index(
                                                   iv_display_name                = 'INDEX_NAME'
                                                   iv_location_id                 = 'LOCATION_ID'
                                                   iv_gcs_content_uri             = 'CONTENT_URI'
                                                   iv_dimensions                  = 'DIMENSIONS'
                                                   iv_approximate_neighbors_count = 'NEIGHBORS_COUNT'
                                                   iv_leaf_node_embedding_count   = 'LEAF_NODE_EMBEDDING_COUNT'
                                                   iv_leaf_nodes_to_search        = 'LEAF_NODE_TO_SEARCH' ).
IF lo_vectorindex_response->get_error_code( ) IS NOT INITIAL.
  cl_demo_output=>display( 'Error Code-' && lo_vectorindex_response->get_error_code( ) &&
                           'Error Message-' && lo_vectorindex_response->get_error_message( ) ).
ENDIF.

Replace the following:

  • INDEX_NAME: Display name of the index.
  • LOCATION_ID: Google Cloud region of the index.
  • CONTENT_URI: URI of the Cloud Storage bucket containing the embeddings, which is used to build the index.
  • DIMENSIONS: Number of dimensions of the input vectors.
  • NEIGHBORS_COUNT: Neighbors to find through approximate search before reordering is performed.
  • LEAF_NODE_EMBEDDING_COUNT: Number of embeddings on each leaf node. The default value is 1000, if not set.
  • LEAF_NODE_TO_SEARCH: The default percentage of leaf nodes that any query may be searched. Must be in range 1-100, inclusive. The default value is 10 (means 10%), if not set.

Search nearest neighbors

This section explains how to search nearest neighbors by using the Vertex AI SDK for ABAP.

Before you begin

Make sure that you or your administrators have completed the following prerequisites:

Create an RFC destination

You need to create an RFC destination for the index endpoint to which you have deployed your index having your enterprise data in the form of embedding files.

  1. Retrieve the hostname for the index endpoint:

    1. In SAP GUI, execute the transaction code /GOOG/SDK_IMG.

      Alternatively, execute the transaction code SPRO, and then click SAP Reference IMG.

    2. Click ABAP SDK for Google Cloud > Utilities > Vertex AI SDK: Get RFC information for Feature Store and Vector Search.
    3. Select Get Vector Search Details.
    4. Select one of the following options:

      • Search key
      • Vector index endpoint ID
    5. If you've selected the Search key option, enter the search key, which is configured in the Vector Search parameters.

    6. If you've selected the Vector index endpoint ID option, enter the following parameters:

      • Google Cloud Key Name: The client key for authentication to Google Cloud.
      • Location: The region where the index endpoint is located.
      • Index Endpoint ID: The name of the index endpoint.
    7. Click Execute to view the details. Make a note of the endpoint against the Host label.

  2. Create an RFC destination for the index endpoint:

    1. In SAP GUI, execute the transaction code SM59.

    2. Create a new RFC destination. For the RFC destination that you created, make sure the Connection Type is set as follows: G - HTTP connection to external server.

    3. Go to the Technical Settings tab and enter the following details:

      • Target Host: Provide the hostname for the index endpoint.

      • Service No.: Enter 443. This port number is used for secure communication.

    4. Go to the Logon & Security tab and make sure that the SSL Certificate field is set with the option DFAULT SSL Client (Standard).

    5. Save your changes.

  3. Configure the service mapping table for the Vertex AI API:

    1. In SAP GUI, execute the transaction code /GOOG/SDK_IMG.

      Alternatively, execute the transaction code SPRO, and then click SAP Reference IMG.

    2. Click ABAP SDK for Google Cloud > Basic Settings > Configure Service Mapping.

    3. Click New Entries.

    4. Specify RFC destination for the Vertex AI API:

      Name Service Name RFC Destination
      Client key for authentication. apiinvoker:v1 Name of the RFC destination.
    5. Save the new entry.

To invoke Vector Search, you can instantiate the /GOOG/CL_VECTOR_SEARCH class. You instantiate the class by passing the search key, which is configured in the Vector Search parameters.

DATA(lo_vector_search) = NEW /goog/cl_vector_search( iv_search_name = 'SEARCH_KEY' ).

Replace SEARCH_KEY with the search key, which is configured in the Vector Search parameters.

Search by using an entity ID

To query a vector index with respect to a specific entity ID, you can use the FIND_NEIGHBORS_BY_ENTITY_ID method of the /GOOG/CL_VECTOR_SEARCH class. Entity IDs correspond to the data points stored in your vector index.

lo_vector_search->find_neighbors_by_entity_id( iv_entity_id             = 'ENTITY_ID'
                                               iv_neighbor_count        = 'NEIGHBOR_COUNT'
                                               iv_return_full_datapoint = 'RETURN_FULL_DATAPOINT' ).

Replace the following:

  • ENTITY_ID: Entity ID to search.
  • NEIGHBOR_COUNT: Number of nearest neighbors to be retrieved for the query.
  • RETURN_FULL_DATAPOINT: To return the vector embeddings for the retrieved data points, set this parameter value to ABAP_TRUE.

Search by using a search string

To query a vector index with respect to a search string, you can use the FIND_NEIGHBORS_BY_STRING method of the /GOOG/CL_VECTOR_SEARCH class.

The SDK first converts your search string into embeddings, and then retrieves the nearest neighbors for the query.

DATA(lo_response) = lo_vector_search->find_neighbors_by_string(
                                        iv_search_string         = 'SEARCH_STRING'
                                        iv_embeddings_model_key  = 'MODEL_KEY'
                                        iv_neighbor_count        = 'NEIGHBOR_COUNT'
                                        iv_return_full_datapoint = 'RETURN_FULL_DATAPOINT' ).

Replace the following:

  • SEARCH_STRING: Input search string.
  • MODEL_KEY: The model key for generating embeddings, which is configured in the model generation parameters.
  • NEIGHBOR_COUNT: Number of nearest neighbors to be retrieved for the query.
  • RETURN_FULL_DATAPOINT: To return the vector embeddings for the retrieved data points, set this parameter value to ABAP_TRUE.

Search by using an embedding

To query a vector index with respect to embeddings, you can use the FIND_NEIGHBORS_BY_EMBEDDING method of the /GOOG/CL_VECTOR_SEARCH class.

DATA(lo_response) = lo_vector_search->find_neighbors_by_embeddings(
                                        iv_neighbor_count        = 'NEIGHBOR_COUNT'
                                        iv_return_full_datapoint = 'RETURN_FULL_DATAPOINT'
                                        it_embeddings            = 'EMBEDDINGS' ).

Replace the following:

  • NEIGHBOR_COUNT: Number of nearest neighbors to be retrieved for the query.
  • RETURN_FULL_DATAPOINT: To return the vector embeddings for the retrieved data points, set this parameter value to ABAP_TRUE.
  • EMBEDDINGS: Input embeddings.

Get the search response

To receive processed responses from the model and present them in a meaningful way, the SDK provides the ABAP class /GOOG/CL_VECTORSEARCH_RESPONSE.

The response captured by the /GOOG/CL_VECTORSEARCH_RESPONSE class is chained to the requests made through the methods of the /GOOG/CL_VECTOR_SEARCH class, so that you can directly access the response in a single statement without requiring variables to store the intermediate results.

Get the nearest neighbors for the search query

To determine the nearest neighbors found for the executed search query with respect to a data point, search string, or embeddings, you can use the GET_NEAREST_NEIGHBOR and GET_NEAREST_NEIGHBORS methods of the class /GOOG/CL_VECTORSEARCH_RESPONSE.

These methods can be called in a chain after executing the search query by using the methods FIND_NEIGHBORS_BY_STRING, FIND_NEIGHBORS_BY_EMBEDDING, and FIND_NEIGHBORS_BY_ENTITY_ID of the class /GOOG/CL_VECTOR_SEARCH.

  • To get the nearest neighbor closest to the searched data point, string, or embeddings, call the GET_NEAREST_NEIGHBOR method. The response of this method lists the ID of the closest data point, its distance from the search query, and the feature vector associated with it in the index.

    The following code sample illustrates how to get the nearest neighbor for a search string:

    DATA(ls_nearest_neighbor) = lo_vector_search->find_neighbors_by_string(
                                                    iv_search_string         = 'SEARCH_STRING'
                                                    iv_embeddings_model_key  = 'MODEL_KEY'
                                                    iv_neighbor_count        = 'NEIGHBOR_COUNT'
                                                    iv_return_full_datapoint = 'RETURN_FULL_DATAPOINT'
                                               )->get_nearest_neighbor( ).
    

    Replace the following:

    • SEARCH_STRING: Input search string.
    • MODEL_KEY: The model key for generating embeddings, which is configured in the model generation parameters.
    • NEIGHBOR_COUNT: Number of nearest neighbors to be retrieved for the query.
    • RETURN_FULL_DATAPOINT: To return the vector embeddings for the retrieved data points, set this parameter value to ABAP_TRUE.
  • To get the set of nearest neighbors closest to the searched data point, string, or embeddings, call the GET_NEAREST_NEIGHBORS method. The response of this method is a table of closest data points, listing the data point IDs, their distance from the search query and the feature vector associated with them in the index.

    The following code sample illustrates how to get the nearest neighbors for a search string:

    DATA(lt_nearest_neighbors) = lo_vector_search->find_neighbors_by_string(
                                                     iv_search_string         = 'SEARCH_STRING'
                                                     iv_embeddings_model_key  = 'MODEL_KEY'
                                                     iv_neighbor_count        = 'NEIGHBOR_COUNT'
                                                     iv_return_full_datapoint = 'RETURN_FULL_DATAPOINT'
                                                )->get_nearest_neighbors( ).
    

    Replace the following:

    • SEARCH_STRING: Input search string.
    • MODEL_KEY: The model key for generating embeddings, which is configured in the model generation parameters.
    • NEIGHBOR_COUNT: Number of nearest neighbors to be retrieved for the query.
    • RETURN_FULL_DATAPOINT: To return the vector embeddings for the retrieved data points, set this parameter value to ABAP_TRUE.

Get the distance of the fetched neighbor

To determine the distance of a fetched response from the searched query, you can use the GET_DATAPOINT_DISTANCE method of the /GOOG/CL_VECTORSEARCH_RESPONSE class.

Distance in the search response represents a measure of dissimilarity between the query (string, embedding, or entity ID) and a retrieved result vector. The specific meaning of distance depends on the "Distance Measure" used when creating an index.

The distance value helps you understand how relevant each search result is to your query. Results with lower distances are generally considered more similar or relevant, while those with higher distances are less similar or relevant.

The following code sample illustrates how to get the distance of the nearest neighbor for a search string:

DATA(lo_vectorsearch_response) = lo_vector_search->find_neighbors_by_string(
                                                     iv_search_string         = 'SEARCH_STRING'
                                                     iv_embeddings_model_key  = 'MODEL_KEY'
                                                     iv_neighbor_count        = 'NEIGHBOR_COUNT'
                                                     iv_return_full_datapoint = 'RETURN_FULL_DATAPOINT' ).
DATA(ls_nearest_neighbor) = lo_vectorsearch_response->get_nearest_neighbor( ).
DATA(lv_distance) = lo_vectorsearch_response->get_datapoint_distance( iv_datapoint_id = ls_nearest_neighbor-datapoint_id ).

Replace the following:

  • SEARCH_STRING: Input search string.
  • MODEL_KEY: The model key for generating embeddings, which is configured in the model generation parameters.
  • NEIGHBOR_COUNT: Number of nearest neighbors to be retrieved for the query.
  • RETURN_FULL_DATAPOINT: To return the vector embeddings for the retrieved data points, set this parameter value to ABAP_TRUE.

Get the embeddings vector of the fetched neighbor

To determine the embeddings vector of the fetched response from the searched query, you can use the GET_DATAPOINT_FEATURE_VECTOR method of the /GOOG/CL_VECTORSEARCH_RESPONSE class.

To get an embeddings vector, the IV_RETURN_FULL_DATAPOINT parameter of the FIND_NEIGHBORS_BY_STRING, FIND_NEIGHBORS_BY_EMBEDDING, and FIND_NEIGHBORS_BY_ENTITY_ID methods must be set to ABAP_TRUE.

The following code sample illustrates how to get the feature vector of the nearest neighbor for a search string:

DATA(lo_vectorsearch_response) = lo_vector_search->find_neighbors_by_string(
                                                     iv_search_string         = 'SEARCH_STRING'
                                                     iv_embeddings_model_key  = 'MODEL_KEY'
                                                     iv_neighbor_count        = 'NEIGHBOR_COUNT'
                                                     iv_return_full_datapoint = 'RETURN_FULL_DATAPOINT' ).
DATA(ls_nearest_neighbor) = lo_vectorsearch_response->get_nearest_neighbor( ).
DATA(lt_feature_vector) = lo_vectorsearch_response->get_datapoint_feature_vector( iv_datapoint_id = ls_nearest_neighbor-datapoint_id ).

Replace the following:

  • SEARCH_STRING: Input search string.
  • MODEL_KEY: The model key for generating embeddings, which is configured in the model generation parameters.
  • NEIGHBOR_COUNT: Number of nearest neighbors to be retrieved for the query.
  • RETURN_FULL_DATAPOINT: To return the vector embeddings for the retrieved data points, set this parameter value to ABAP_TRUE.

What's next