वेक्टर एम्बेडिंग की मदद से खोजें

इस पेज में बताया गया है कि K-निकटतम प्रदर्शन करने के लिए Cloud Firestore का इस्तेमाल कैसे किया जा सकता है नेबर (KNN) वेक्टर इन तकनीकों का इस्तेमाल करके खोज करता है:

  • वेक्टर की वैल्यू सेव करें
  • KNN वेक्टर इंडेक्स बनाएं और मैनेज करें
  • समर्थित वेक्टर में से किसी एक का इस्तेमाल करके K-निकटतम-पड़ोसी (KNN) क्वेरी करें दूरी फ़ंक्शन

वेक्टर एम्बेडिंग स्टोर करें

आपके पास अपने कंप्यूटर से, टेक्स्ट एम्बेड करने जैसी वेक्टर वैल्यू बनाने का विकल्प होता है Cloud Firestore डेटा और उन्हें Cloud Firestore दस्तावेज़ों में सेव करना.

वेक्टर एम्बेडिंग के साथ ऑपरेशन लिखें

नीचे दिया उदाहरण दिखाता है कि किसी Cloud Firestore दस्तावेज़:

Python
from google.cloud import firestore
from google.cloud.firestore_v1.vector import Vector

firestore_client = firestore.Client()
collection = firestore_client.collection("coffee-beans")
doc = {
  "name": "Kahawa coffee beans",
  "description": "Information about the Kahawa coffee beans.",
  "embedding_field": Vector([1.0 , 2.0, 3.0])
}

collection.add(doc)
    
Node.js
import {
  Firestore,
  FieldValue,
} from "@google-cloud/firestore";

const db = new Firestore();
const coll = db.collection('coffee-beans');
await coll.add({
  name: "Kahawa coffee beans",
  description: "Information about the Kahawa coffee beans.",
  embedding_field: FieldValue.vector([1.0 , 2.0, 3.0])
});
    

Cloud फ़ंक्शन की मदद से वेक्टर एम्बेड करना कम करें

जब भी कोई दस्तावेज़ अपडेट किया जाता है, तब वेक्टर एम्बेड करने का हिसाब लगाने और उसे सेव करने के लिए या बनाई गई है, तो आप क्लाउड फ़ंक्शन सेट अप कर सकते हैं:

Python
@functions_framework.cloud_event
def store_embedding(cloud_event) -> None:
  """Triggers by a change to a Firestore document.
  """
  firestore_payload = firestore.DocumentEventData()
  payload = firestore_payload._pb.ParseFromString(cloud_event.data)

  collection_id, doc_id = from_payload(payload)
  # Call a function to calculate the embedding
  embedding = calculate_embedding(payload)
  # Update the document
  doc = firestore_client.collection(collection_id).document(doc_id)
  doc.set({"embedding_field": embedding}, merge=True)
    
Node.js
/**
 * A vector embedding will be computed from the
 * value of the `content` field. The vector value
 * will be stored in the `embedding` field. The
 * field names `content` and `embedding` are arbitrary
 * field names chosen for this example.
 */
async function storeEmbedding(event: FirestoreEvent<any>): Promise<void> {
  // Get the previous value of the document's `content` field.
  const previousDocumentSnapshot = event.data.before as QueryDocumentSnapshot;
  const previousContent = previousDocumentSnapshot.get("content");

  // Get the current value of the document's `content` field.
  const currentDocumentSnapshot = event.data.after as QueryDocumentSnapshot;
  const currentContent = currentDocumentSnapshot.get("content");

  // Don't update the embedding if the content field did not change
  if (previousContent === currentContent) {
    return;
  }

  // Call a function to calculate the embedding for the value
  // of the `content` field.
  const embeddingVector = calculateEmbedding(currentContent);

  // Update the `embedding` field on the document.
  await currentDocumentSnapshot.ref.update({
    embedding: embeddingVector,
  });
}
    

वेक्टर इंडेक्स बनाएं और मैनेज करें

वेक्टर एम्बेड करके आस-पास के नतीजे खोजने से पहले, आपको मिलता-जुलता इंडेक्स बनाना होगा. ये उदाहरण दिखाते हैं कि वेक्टर इंडेक्स बनाने और उन्हें मैनेज करने का तरीका जानें.

वेक्टर इंडेक्स बनाना

वेक्टर इंडेक्स बनाने के लिए, इसका इस्तेमाल करें gcloud alpha firestore indexes composite create:

gcloud
gcloud alpha firestore indexes composite create \
--collection-group=collection-group \
--query-scope=COLLECTION \
--field-config field-path=vector-field,vector-config='vector-configuration' \
--database=database-id
    

कहां:

  • collection-group, कलेक्शन ग्रुप का आईडी है.
  • vector-field उस फ़ील्ड का नाम है जिसमें वेक्टर एम्बेड करने की सुविधा होती है.
  • database-id, डेटाबेस का आईडी है.
  • vector-configuration में वेक्टर dimension और इंडेक्स टाइप शामिल होता है. dimension, 2048 तक का पूर्णांक होता है. इंडेक्स टाइप flat होना चाहिए. इंडेक्स कॉन्फ़िगरेशन को इस तरह फ़ॉर्मैट करें: {"dimension":"DIMENSION", "flat": "{}"}.

नीचे दिया गया उदाहरण एक कंपोज़िट इंडेक्स बनाता है, जिसमें vector-field फ़ील्ड के लिए वेक्टर इंडेक्स शामिल है और color फ़ील्ड के लिए बढ़ते क्रम वाला इंडेक्स. प्री-फ़िल्टर करने के लिए, इस तरह के इंडेक्स का इस्तेमाल किया जा सकता है डेटा को आस-पास के पड़ोसी की खोज से पहले.

gcloud
gcloud alpha firestore indexes composite create \
--collection-group=collection-group \
--query-scope=COLLECTION \
--field-config=order=ASCENDING,field-path="color" \
--field-config field-path=vector-field,vector-config='{"dimension":"1024", "flat": "{}"}' \
--database=database-id
    

सभी सदिश इंडेक्स की सूची बनाएं

gcloud
gcloud alpha firestore indexes composite list --database=database-id

database-id को डेटाबेस के आईडी से बदलें.

वेक्टर इंडेक्स मिटाना

gcloud
gcloud alpha firestore indexes composite delete index-id --database=database-id
    

कहां:

  • index-id, मिटाए जाने वाले इंडेक्स का आईडी है. इंडेक्स आईडी को फिर से पाने के लिए, indexes composite list का इस्तेमाल करें.
  • database-id, डेटाबेस का आईडी है.

सदिश सूचकांक के बारे में बताना

gcloud
gcloud alpha firestore indexes composite describe index-id --database=database-id
    

कहां:

  • index-id, इंडेक्स करने के लिए इस्तेमाल किया जाने वाला आईडी है. इस्तेमाल करें या indexes composite list का इस्तेमाल करके इंडेक्स आईडी को फिर से पाएं.
  • database-id, डेटाबेस का आईडी है.

आस-पड़ोस की कोई क्वेरी करें

आप इसके निकटतम पड़ोसियों को खोजने के लिए समानता खोज कर सकते हैं वेक्टर एम्बेड करना. समानता खोजने के लिए वेक्टर इंडेक्स की ज़रूरत होती है. अगर इंडेक्स मौजूद नहीं है, तो Cloud Firestore, इंडेक्स बनाने के लिए एक इंडेक्स का सुझाव देता है तो gcloud सीएलआई का इस्तेमाल किया जा रहा है.

Python
from google.cloud.firestore_v1.base_vector_query import DistanceMeasure

collection = collection("coffee-beans")

# Requires vector index
collection.find_nearest(
   vector_field="embedding_field",
   query_vector=Vector([3.0, 1.0, 2.0]),
   distance_measure=DistanceMeasure.EUCLIDEAN,
   limit=5)
    
Node.js
import {
  Firestore,
  FieldValue,
  VectorQuery,
  VectorQuerySnapshot,
} from "@google-cloud/firestore";

// Requires single-field vector index
const vectorQuery: VectorQuery = coll.findNearest('embedding_field', FieldValue.vector([3.0, 1.0, 2.0]), {
  limit: 5,
  distanceMeasure: 'EUCLIDEAN'
});

const vectorQuerySnapshot: VectorQuerySnapshot = await vectorQuery.get();
    

वेक्टर डिस्टेंस

निकटतम-पड़ोसी क्वेरी में सदिश दूरी के लिए नीचे दिए गए विकल्प होते हैं:

  • EUCLIDEAN: वेक्टर के बीच EUCLIDEAN की दूरी मापता है. इस बारे में ज़्यादा जानने के लिए, यह देखें यूक्लिडीन.
  • COSINE: वेक्टर के बीच के ऐंगल के आधार पर उनकी तुलना करता है, ताकि आप वह समानता मापे जो वेक्टर की तीव्रता पर आधारित नहीं है. हमारा सुझाव है कि इसके बजाय DOT_PRODUCT को यूनिट नॉर्मलाइज़्ड वेक्टर के साथ इस्तेमाल करें COSINE की दूरी, जो गणितीय रूप से बेहतर परफ़ॉर्मेंस. ज़्यादा जानने के लिए देखें कोसाइन के बीच समानता सीखें वगैरह को कॉपी करने का विकल्प है.
  • DOT_PRODUCT: यह COSINE की तरह ही होता है, लेकिन इस पर असर पड़ता है वेक्टर. इस बारे में ज़्यादा जानने के लिए, यह देखें डॉट प्रॉडक्ट.

प्री-फ़िल्टर डेटा

आस-पास के लोगों को खोजने से पहले, डेटा को प्री-फ़िल्टर करने के लिए, असमानता फ़िल्टर को छोड़कर अन्य फ़िल्टर के साथ समानता खोज. and और or कंपोज़िट फ़िल्टर का इस्तेमाल किया जा सकता है. फ़ील्ड फ़िल्टर के लिए, निम्न फ़िल्टर समर्थित हैं:

  • == इसके बराबर है
  • in
  • array_contains
  • array_contains_any
Python
# Similarity search with pre-filter
# Requires composite vector index
collection.where("color", "==", "red").find_nearest(
   vector_field="embedding_field",
   query_vector=Vector([3.0, 1.0, 2.0]),
   distance_measure=DistanceMeasure.EUCLIDEAN,
   limit=5)
    
Node.js
// Similarity search with pre-filter
// Requires composite vector index
const preFilteredVectorQuery: VectorQuery = coll
  .where("color", "==", "red")
  .findNearest("embedding_field", FieldValue.vector([3.0, 1.0, 2.0]), {
    limit: 5,
    distanceMeasure: "EUCLIDEAN",
  });

vectorQueryResults = await preFilteredVectorQuery.get();
    

सीमाएं

वेक्टर एम्बेड करने की सुविधा का इस्तेमाल करते समय, इन सीमाओं का ध्यान रखें:

  • एम्बेड करने के लिए, ज़्यादा से ज़्यादा 2048 डाइमेंशन का इस्तेमाल किया जा सकता है. बड़े इंडेक्स सेव करने के लिए, डाइमेंशन में कमी दिख सकती है.
  • निकटतम आस-पड़ोस की क्वेरी से दिए जाने वाले दस्तावेज़ों की अधिकतम संख्या 1000 है.
  • वेक्टर खोज रीयल-टाइम स्नैपशॉट लिसनर का समर्थन नहीं करती.
  • डेटा को प्री-फ़िल्टर करने के लिए, असमानता फ़िल्टर का इस्तेमाल नहीं किया जा सकता.
  • वेक्टर सर्च की सुविधा सिर्फ़ Python और Node.js क्लाइंट लाइब्रेरी पर काम करती है.

आगे क्या करना है