शुरू करेंमुफ़्त में शुरू करें

Similarity के आधार पर sorting

अब जब आपने अपनी सभी features को embed कर लिया है, अगला कदम similarities निकालना है. इस अभ्यास में, आप find_n_closest() नाम का एक फंक्शन परिभाषित करेंगे, जो एक query vector और embeddings की सूची के बीच cosine distances निकालता है और n सबसे छोटे distances और उनके indexes लौटाता है.

अगले अभ्यास में, आप इसी फंक्शन का उपयोग करके अपना semantic product search application सक्षम करेंगे.

distance को scipy.spatial से import किया गया है.

यह अभ्यास पाठ्यक्रम का हिस्सा है

OpenAI API के साथ Embeddings परिचय

पाठ्यक्रम देखें

अभ्यास निर्देश

  • query_vector और embedding के बीच cosine distance की गणना करें.
  • dist और उसका index शामिल करते हुए एक dictionary को distances सूची में append करें.
  • distances सूची को प्रत्येक dictionary की 'distance' key के आधार पर sort करें.
  • distances_sorted के पहले n एलिमेंट्स लौटाएँ.

इंटरैक्टिव व्यावहारिक अभ्यास

इस अभ्यास को इस नमूना कोड को पूरा करके आज़माएँ।

def find_n_closest(query_vector, embeddings, n=3):
  distances = []
  for index, embedding in enumerate(embeddings):
    # Calculate the cosine distance between the query vector and embedding
    dist = ____
    # Append the distance and index to distances
    distances.append({"distance": ____, "index": ____})
  # Sort distances by the distance key
  distances_sorted = ____
  # Return the first n elements in distances_sorted
  return ____
कोड संपादित करें और चलाएँ