שאילתות על אינדקסים של גישה לשירותים פרטיים (VPC Network Peering) או של Private Service Connect

אחרי שפורסים נקודת קצה של אינדקס של קישור בין רשתות VPC שכנות (peering) או של Private Service Connect, יש הבדלים קלים בין השאילתות ששולחים אליה, בהתאם לאופן הפריסה:

פריסה באמצעות אוטומציה של Private Service Connect

במקרה של IndexEndpoints פריסה באמצעות אוטומציה של Private Service Connect,‏ Python SDK ימפה באופן אוטומטי את רשת Private Service Connect לנקודת הקצה המתאימה. אם לא משתמשים ב-Python SDK, צריך להתחבר ישירות לכתובת ה-IP שנוצרה לנקודת הקצה, לפי ההוראות לשאילתות על פריסה ידנית של Private Service Connect.

Python

במאמר התקנת Vertex AI SDK ל-Python מוסבר איך להתקין או לעדכן את Vertex AI SDK ל-Python. מידע נוסף מופיע ב מאמרי העזרה של Python API.

def vector_search_match_psc_automation(
    project: str,
    location: str,
    index_endpoint_name: str,
    deployed_index_id: str,
    queries: List[List[float]],
    num_neighbors: int,
    psc_network: str,
) -> List[List[aiplatform.matching_engine.matching_engine_index_endpoint.MatchNeighbor]]:
    """Query the vector search index deployed with PSC automation.

    Args:
        project (str): Required. Project ID
        location (str): Required. The region name
        index_endpoint_name (str): Required. Index endpoint to run the query
        against. The endpoint must be a private endpoint.
        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.
        ip_address (str): Required. The IP address of the PSC endpoint. Obtained
        from the created compute address used in the fordwarding rule to the
        endpoint's service attachment.
        psc_network (str): The network the endpoint was deployed to via PSC
        automation configuration. The format is
        projects/{project_id}/global/networks/{network_name}.

    Returns:
        List[List[aiplatform.matching_engine.matching_engine_index_endpoint.MatchNeighbor]] - A list of nearest neighbors for each query.
    """
    # 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 matches.
    resp = my_index_endpoint.match(
        deployed_index_id=deployed_index_id,
        queries=queries,
        num_neighbors=num_neighbors,
        psc_network=psc_network
    )
    return resp

פריסה עם הגדרה ידנית של Private Service Connect

ב-Private Service Connect IndexEndpoints שנפרס עם חיבור שהוגדר באופן ידני, הגישה לנקודת הקצה מתבצעת באמצעות כתובת ה-IP של כתובת ה-Compute שהועברה לקובץ המצורף של השירות Private Service Connect של נקודת הקצה.

אם עדיין לא ידוע לכם, תוכלו לקבל את כתובת ה-IP שהועברה ל-URI של קובץ השירות באמצעות הפקודות gcloud ai index-endpoints describe ו-gcloud compute forwarding-rules list.

מחליפים את הפרטים הבאים:

  • INDEX_ENDPOINT_ID: מזהה נקודת קצה של אינדקס מוגדר במלואו.
  • ‫REGION: האזור שבו נקודת הקצה של האינדקס פרוסה.
SERVICE_ATTACHMENT_URI=`gcloud ai index-endpoints describe INDEX_ENDPOINT_ID \
  --region=REGION \
  --format="value(deployedIndexes.privateEndpoints.serviceAttachment)"`

gcloud compute forwarding-rules list --filter="TARGET:${SERVICE_ATTACHMENT_URI}"

הפלט יכלול את כתובת ה-IP הפנימית שבה צריך להשתמש כשמבצעים שאילתה ב-IndexEndpoint.

Python

במאמר התקנת Vertex AI SDK ל-Python מוסבר איך להתקין או לעדכן את Vertex AI SDK ל-Python. מידע נוסף מופיע ב מאמרי העזרה של Python API.

def vector_search_match_psc_manual(
    project: str,
    location: str,
    index_endpoint_name: str,
    deployed_index_id: str,
    queries: List[List[float]],
    num_neighbors: int,
    ip_address: str,
) -> List[List[aiplatform.matching_engine.matching_engine_index_endpoint.MatchNeighbor]]:
    """Query the vector search index deployed with PSC manual configuration.

    Args:
        project (str): Required. Project ID
        location (str): Required. The region name
        index_endpoint_name (str): Required. Index endpoint to run the query
        against. The endpoint must be a private endpoint.
        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.
        ip_address (str): Required. The IP address of the PSC endpoint. Obtained
        from the created compute address used in the forwarding rule to the
        endpoint's service attachment.

    Returns:
        List[List[aiplatform.matching_engine.matching_engine_index_endpoint.MatchNeighbor]] - A list of nearest neighbors for each query.
    """
    # 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
    )

    # Set the IP address of the PSC endpoint.
    my_index_endpoint.private_service_connect_ip_address = ip_address

    # Query the index endpoint for matches.
    resp = my_index_endpoint.match(
        deployed_index_id=deployed_index_id,
        queries=queries,
        num_neighbors=num_neighbors
    )
    return resp

שורת פקודה

כדי לשלוח שאילתה ל-DeployedIndex, מתחברים ל-TARGET_IP שלו ביציאה 10000 ומפעילים את method‏ Match או BatchMatch. בנוסף, אפשר להריץ שאילתה באמצעות מזהה הטמעה ספציפי.

בדוגמאות הבאות נעשה שימוש בכלי הקוד הפתוח grpc_cli כדי לשלוח בקשות gRPC לשרת האינדקס שנפרס.

בדוגמה הראשונה, שולחים שאילתה אחת באמצעות השיטה Match.

./grpc_cli call ${TARGET_IP}:10000 google.cloud.aiplatform.container.v1.MatchService.Match 'deployed_index_id: "${DEPLOYED_INDEX_ID}", float_val: [-0.1,..]'
  

בדוגמה השנייה, שתי שאילתות נפרדות משולבות באותה בקשת BatchMatch.

./grpc_cli call ${TARGET_IP}:10000 google.cloud.aiplatform.container.v1.MatchService.BatchMatch 'requests: [{deployed_index_id: "${DEPLOYED_INDEX_ID}", requests: [{deployed_index_id: "${DEPLOYED_INDEX_ID}", float_val: [-0.1,..]}, {deployed_index_id: "${DEPLOYED_INDEX_ID}", float_val: [-0.2,..]}]}]'
  

צריך לשלוח קריאות לממשקי ה-API האלה מלקוח שפועל באותו VPC שאליו השירות קושר באמצעות Peering.

כדי להריץ שאילתה באמצעות embedding_id, משתמשים בדוגמה הבאה.

./grpc_cli call ${TARGET_IP}:10000  google.cloud.aiplatform.container.v1.MatchService.Match "deployed_index_id:'"test_index1"',embedding_id: '"606431"'"

בדוגמה הזו, אתם שולחים שאילתה באמצעות אסימון ומגבלות מספריות.

./grpc_cli call ${TARGET_IP}:10000 google.cloud.aiplatform.container.v1.MatchService.Match 'deployed_index_id: "${DEPLOYED_INDEX_ID}", float_val: [1, 1], "sparse_embedding": {"values": [111.0,111.1,111.2], "dimensions": [10,20,30]}, numeric_restricts: [{name: "double-ns", value_double: 0.3, op: LESS_EQUAL}, {name: "double-ns", value_double: -1.2, op: GREATER}, {name: "double-ns", value_double: 0., op: NOT_EQUAL}], restricts: [{name: "color", allow_tokens: ["red"]}]'

מידע נוסף זמין במאמר הסבר על ספריות לקוח.

המסוף

במאמר הזה מוסבר איך להריץ שאילתות באינדקס של VPC מהמסוף.

  1. בקטע Agent Platform במסוף Cloud de Confiance , עוברים לקטע Deploy and Use. בוחרים באפשרות חיפוש וקטורי.

    לדף חיפוש וקטורי

  2. בוחרים את אינדקס ה-VPC שרוצים לשלוח אליו שאילתה. ייפתח הדף פרטי האינדקס.
  3. גוללים למטה לקטע Deployed indexes (מדדים שנפרסו) ובוחרים את המדד שנפרס שרוצים לשלוח לו שאילתה. ייפתח הדף Deployed index info (פרטי אינדקס שנפרס).
  4. בקטע Query index, בוחרים את הפרמטרים של השאילתה. אפשר לבחור לבצע שאילתה לפי וקטור או לפי נקודת נתונים ספציפית.
  5. מריצים את השאילתה באמצעות הכלי בקוד פתוח grpc_cli, או באמצעות Agent Platform SDK ל-Python.

פריסה באמצעות קישור בין רשתות VPC שכנות (peering)

Python

במאמר התקנת Vertex AI SDK ל-Python מוסבר איך להתקין או לעדכן את Vertex AI SDK ל-Python. מידע נוסף מופיע ב מאמרי העזרה של Python API.

הערה: Python SDK מחפש באופן אוטומטי את כתובת ה-IP של IndexEndpoint שנפרס באמצעות VPC Network Peering.

def vector_search_match_hybrid_queries(
    project: str,
    location: str,
    index_endpoint_name: str,
    deployed_index_id: str,
    num_neighbors: int,
) -> List[List[aiplatform.matching_engine.matching_engine_index_endpoint.MatchNeighbor]]:
    """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. The endpoint must be a private endpoint.
        deployed_index_id (str): Required. The ID of the DeployedIndex to run
        the queries against.
        num_neighbors (int): Required. The number of neighbors to return.

    Returns:
        List[List[aiplatform.matching_engine.matching_engine_index_endpoint.MatchNeighbor]] - A list of nearest neighbors for each query.
    """
    # 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
    )

    # Example queries containing hybrid datapoints, sparse-only datapoints, and
    # dense-only datapoints.
    hybrid_queries = [
        aiplatform.matching_engine.matching_engine_index_endpoint.HybridQuery(
            dense_embedding=[1, 2, 3],
            sparse_embedding_dimensions=[10, 20, 30],
            sparse_embedding_values=[1.0, 1.0, 1.0],
            rrf_ranking_alpha=0.5,
        ),
        aiplatform.matching_engine.matching_engine_index_endpoint.HybridQuery(
            dense_embedding=[1, 2, 3],
            sparse_embedding_dimensions=[10, 20, 30],
            sparse_embedding_values=[0.1, 0.2, 0.3],
        ),
        aiplatform.matching_engine.matching_engine_index_endpoint.HybridQuery(
            sparse_embedding_dimensions=[10, 20, 30],
            sparse_embedding_values=[0.1, 0.2, 0.3],
        ),
        aiplatform.matching_engine.matching_engine_index_endpoint.HybridQuery(
            dense_embedding=[1, 2, 3]
        ),
    ]

    # Query the index endpoint for matches.
    resp = my_index_endpoint.match(
        deployed_index_id=deployed_index_id,
        queries=hybrid_queries,
        num_neighbors=num_neighbors,
    )
    return resp

שורת פקודה

לכל DeployedIndex יש TARGET_IP, שאפשר לאחזר ברשימת IndexEndpoints.

כדי לשלוח שאילתה ל-DeployedIndex, מתחברים ל-TARGET_IP שלו ביציאה 10000 ומפעילים את method‏ Match או BatchMatch. בנוסף, אפשר להריץ שאילתה באמצעות מזהה הטמעה ספציפי.

בדוגמאות הבאות נעשה שימוש בכלי הקוד הפתוח grpc_cli כדי לשלוח בקשות gRPC לשרת האינדקס שנפרס.

בדוגמה הראשונה, שולחים שאילתה אחת באמצעות השיטה Match.

./grpc_cli call ${TARGET_IP}:10000 google.cloud.aiplatform.container.v1.MatchService.Match 'deployed_index_id: "${DEPLOYED_INDEX_ID}", float_val: [-0.1,..]'
  

בדוגמה השנייה, שתי שאילתות נפרדות משולבות באותה בקשת BatchMatch.

./grpc_cli call ${TARGET_IP}:10000 google.cloud.aiplatform.container.v1.MatchService.BatchMatch 'requests: [{deployed_index_id: "${DEPLOYED_INDEX_ID}", requests: [{deployed_index_id: "${DEPLOYED_INDEX_ID}", float_val: [-0.1,..]}, {deployed_index_id: "${DEPLOYED_INDEX_ID}", float_val: [-0.2,..]}]}]'
  

צריך לשלוח קריאות לממשקי ה-API האלה מלקוח שפועל באותו VPC שאליו השירות קושר באמצעות Peering.

כדי להריץ שאילתה באמצעות embedding_id, משתמשים בדוגמה הבאה.

./grpc_cli call ${TARGET_IP}:10000  google.cloud.aiplatform.container.v1.MatchService.Match "deployed_index_id:'"test_index1"',embedding_id: '"606431"'"

בדוגמה הזו, אתם שולחים שאילתה באמצעות אסימון ומגבלות מספריות.

./grpc_cli call ${TARGET_IP}:10000 google.cloud.aiplatform.container.v1.MatchService.Match 'deployed_index_id: "${DEPLOYED_INDEX_ID}", float_val: [1, 1], "sparse_embedding": {"values": [111.0,111.1,111.2], "dimensions": [10,20,30]}, numeric_restricts: [{name: "double-ns", value_double: 0.3, op: LESS_EQUAL}, {name: "double-ns", value_double: -1.2, op: GREATER}, {name: "double-ns", value_double: 0., op: NOT_EQUAL}], restricts: [{name: "color", allow_tokens: ["red"]}]'

מידע נוסף זמין במאמר הסבר על ספריות לקוח.

המסוף

במאמר הזה מוסבר איך להריץ שאילתות באינדקס של VPC מהמסוף.

  1. בקטע Agent Platform במסוף Cloud de Confiance , עוברים לקטע Deploy and Use. בוחרים באפשרות חיפוש וקטורי.

    לדף חיפוש וקטורי

  2. בוחרים את אינדקס ה-VPC שרוצים לשלוח אליו שאילתה. ייפתח הדף פרטי האינדקס.
  3. גוללים למטה לקטע Deployed indexes (מדדים שנפרסו) ובוחרים את המדד שנפרס שרוצים לשלוח לו שאילתה. ייפתח הדף Deployed index info (פרטי אינדקס שנפרס).
  4. בקטע Query index, בוחרים את הפרמטרים של השאילתה. אפשר לבחור לבצע שאילתה לפי וקטור או לפי נקודת נתונים ספציפית.
  5. מריצים את השאילתה באמצעות הכלי בקוד פתוח grpc_cli, או באמצעות Agent Platform SDK ל-Python.

הגדרות שמשפיעות על הביצועים בזמן השאילתה

הפרמטרים הבאים של זמן השאילתה יכולים להשפיע על זמן האחזור, הזמינות והעלות כשמשתמשים בחיפוש וקטורי. ההנחיות האלה רלוונטיות לרוב המקרים. עם זאת, מומלץ תמיד לערוך ניסויים בהגדרות כדי לוודא שהן מתאימות לתרחיש השימוש שלכם.

הגדרות הפרמטרים מפורטות במאמר בנושא פרמטרים של הגדרת אינדקס.

פרמטר מידע כללי השפעה על הביצועים
approximateNeighborsCount

ההגדרה הזו מציינת לאלגוריתם את מספר התוצאות המשוערות שיש לאחזר מכל רסיס.

הערך של approximateNeighborsCount צריך להיות תמיד גדול מהערך של setNeighborsCount. אם הערך של setNeighborsCount קטן, מומלץ להגדיר את הערך של approximateNeighborsCount כ-10 פעמים הערך הזה. אם הערכים של setNeighborsCount גדולים יותר, אפשר להשתמש במכפיל קטן יותר.

השם התואם של ה-API בארכיטקטורת REST בשדה הזה הוא approximate_neighbor_count.

הגדלת הערך של approximateNeighborsCount יכולה להשפיע על הביצועים בדרכים הבאות:

  • זכירה: עלייה
  • זמן האחזור: יכול להיות שיהיה ארוך יותר
  • זמינות: אין השפעה
  • עלות: יכולה לעלות כי יותר נתונים מעובדים במהלך החיפוש

הקטנת הערך של approximateNeighborsCount יכולה להשפיע על הביצועים בדרכים הבאות:

  • Recall: Decreased
  • זמן האחזור: יכול להיות שירד
  • זמינות: אין השפעה
  • עלות: יכולה להפחית את העלות כי פחות נתונים מעובדים במהלך חיפוש
setNeighborCount

מציין את מספר התוצאות שרוצים שהשאילתה תחזיר.

השם התואם של ה-API בארכיטקטורת REST בשדה הזה הוא neighbor_count.

ערכים שקטנים מ-300 או שווים ל-300 לא פוגעים בביצועים ברוב תרחישי השימוש. אם הערכים גדולים יותר, כדאי לבדוק את התרחיש הספציפי שלכם.

fractionLeafNodesToSearch קובע את אחוז הצמתים העלים שיש לבקר בהם כשמחפשים שכנים קרובים. זה קשור לleafNodeEmbeddingCount כי ככל שיש יותר הטמעות לכל צומת עלה, כך נבדקים יותר נתונים לכל עלה.

השם התואם של ה-API בארכיטקטורת REST בשדה הזה הוא fraction_leaf_nodes_to_search_override.

הגדלת הערך של fractionLeafNodesToSearch יכולה להשפיע על הביצועים בדרכים הבאות:

  • זכירה: עלייה
  • זמן טעינה: מוגבר
  • זמינות: אין השפעה
  • עלות: יכולה לעלות כי זמן האחזור הגבוה תופס יותר משאבי מכונה

הקטנת הערך של fractionLeafNodesToSearch יכולה להשפיע על הביצועים בדרכים הבאות:

  • Recall: Decreased
  • זמן טעינה: ירידה
  • זמינות: אין השפעה
  • עלות: יכולה לרדת כי זמן האחזור הנמוך יותר תופס פחות משאבי מכונה

המאמרים הבאים