यह पेज आपको नीचे दी गई तकनीकों का इस्तेमाल करके, K-आस-पास की चीज़ों (KNN) वेक्टर की खोज करने के लिए Cloud Firestore का इस्तेमाल करने का तरीका बताता है:
- वेक्टर की वैल्यू सेव करें
- 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 फ़ंक्शन की मदद से कंप्यूट करें
जब भी कोई दस्तावेज़ अपडेट किया जाता है या बनाया जाता है, तब वेक्टर एम्बेड करने का हिसाब लगाने और उसे सेव करने के लिए, 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
: वेक्टर की तुलना, उनके बीच के ऐंगल के आधार पर की जाती है. इससे आपको समानता का पता लगाने में मदद मिलती है, जो वेक्टर की तीव्रता पर आधारित नहीं होती. हमारा सुझाव है कि COSINE की दूरी के बजाय, यूनिट नॉर्मलाइज़्ड वेक्टर के साथDOT_PRODUCT
का इस्तेमाल करें. यह गणितीय तौर पर बेहतर परफ़ॉर्मेंस के हिसाब से एक जैसा है. अगर आपको इस बारे में ज़्यादा जानना है, तो कोसाइन के बीच समानता देखें.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 क्लाइंट लाइब्रेरी पर काम करती है.
आगे क्या करना है
- Cloud Firestore के लिए सबसे सही तरीकों के बारे में पढ़ें.
- बड़े पैमाने पर पढ़ने और लिखने के बारे में जानकारी