قياس تشابه التضمينات
افهم مقاييس المسافة والتشابه التي تدعم البحث المتجهي، وكيف تختار المقياس المناسب.
قياس تشابه التضمينات درس مجاني في LangChain / RAG / Vector DBs على CoddyKit. هذا هو الدرس 4 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في LangChain / RAG / Vector DBs، وتقدمك يتزامن عبر الويب وتطبيق CoddyKit. تتضمن دورة LangChain / RAG / Vector DBs 4 دروس في المجموع.
بعض أجزاء هذا الدرس لم تُترجم بعد وتظهر باللغة الإنجليزية.
From Vectors to Meaning
An embedding maps text to a list of numbers in high-dimensional space. Texts with similar meaning land close together. To rank results we need a way to measure that closeness.
Cosine Similarity
Cosine similarity measures the angle between two vectors, ignoring their length. It ranges from -1 (opposite) to 1 (identical direction).
import numpy as np
def cosine(a, b):
a, b = np.array(a), np.array(b)
return a.dot(b) / (np.linalg.norm(a) * np.linalg.norm(b))
print(cosine([1, 0], [1, 1])) # ~0.707Euclidean Distance
Euclidean (L2) distance is the straight-line distance between two points. Smaller means more similar. Unlike cosine, it is sensitive to magnitude.
import numpy as np
def l2(a, b):
return np.linalg.norm(np.array(a) - np.array(b))
print(l2([0, 0], [3, 4])) # 5.0Dot Product
The dot product multiplies matching dimensions and sums them. For normalized vectors it equals cosine similarity, which is why many stores normalize first.
import numpy as np
def dot(a, b):
return float(np.array(a).dot(np.array(b)))
print(dot([1, 2, 3], [4, 5, 6])) # 32.0Normalization
Dividing a vector by its length gives a unit vector. After normalization, dot product and cosine similarity become equivalent, simplifying the math.
import numpy as np
def normalize(v):
v = np.array(v, dtype=float)
return v / np.linalg.norm(v)
print(normalize([3, 4])) # [0.6 0.8]Choosing a Metric
Most modern text embedding models are trained for cosine similarity. Use cosine unless your provider documentation recommends otherwise.
- Cosine: direction matters, length ignored
- L2: absolute position matters
- Dot: cosine on normalized data
Similarity vs. Distance
Beware the inversion: higher cosine = more similar, but higher L2 = less similar. Vector stores expose this difference, sometimes returning a score you must interpret.
Why High Dimensions Help
Embeddings often have hundreds or thousands of dimensions. More dimensions give the model room to separate subtle differences in meaning, at the cost of more storage and compute.
Setting Metric in a Store
When creating a collection you declare the metric. Many libraries default to cosine.
import chromadb
client = chromadb.Client()
col = client.create_collection(
name="docs",
metadata={"hnsw:space": "cosine"}
)Ranking Search Results
Search computes the chosen metric between the query embedding and every stored vector, then returns the top-k closest. The metric directly shapes which documents win.
query_vec = embed("refund policy")
scored = [(cosine(query_vec, d.vec), d) for d in docs]
scored.sort(reverse=True)
top3 = scored[:3]Pitfall: Mixing Models
Vectors from different embedding models live in different spaces and are not comparable. Always embed your query with the same model you used to index the documents.
Quick Check
Test your grasp of similarity metrics.
Recap
You explored how similarity is measured:
- Cosine compares direction (most common for text)
- Euclidean compares position
- Dot product equals cosine on normalized vectors
- Always query and index with the same model
تعلم LangChain / RAG / Vector DBs مع معلم ذكاء اصطناعي — مجانًا
اكتب وقم بتشغيل أكوادك الفعلية في المتصفح، واحصل على مساعدة فورية من معلم ذكاء اصطناعي متاح 24/7، واستمر من حيث توقفت على الويب أو في التطبيق.
- الدورات
- 12
- الدروس
- 48
الأسئلة الشائعة
هل درس «قياس تشابه التضمينات» مجاني؟
نعم — نص درس «قياس تشابه التضمينات» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة LangChain / RAG / Vector DBs، انتقل إلى CoddyKit PRO. تتضمن دورة LangChain / RAG / Vector DBs 4 دروس في المجموع.
ماذا ستتعلم في «قياس تشابه التضمينات»؟
افهم مقاييس المسافة والتشابه التي تدعم البحث المتجهي، وكيف تختار المقياس المناسب. تتمرن على LangChain / RAG / Vector DBs مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.
هل أحتاج إلى خبرة سابقة لأبدأ LangChain / RAG / Vector DBs؟
لا تُشترط خبرة سابقة. LangChain / RAG / Vector DBs على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 4 من أصل 4.
كم من الوقت يستغرق درس «قياس تشابه التضمينات»؟
معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.
هل يمكنني كتابة وتشغيل أكواد في درس LangChain / RAG / Vector DBs هذا؟
نعم. كل درس في LangChain / RAG / Vector DBs يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.
جميع الدروس في هذه الدورة
- فهم تضمينات النصوص
- مقدمة إلى قواعد البيانات المتجهية
- تخزين التضمينات واسترجاعها
- قياس تشابه التضمينات