إنتقل إلى المحتوى الرئيسي

الفصل 1: ما هي Embeddings؟

مقدمة​

الـ embeddings تمثيلات متجهية كثيفة للنص تلتقط المعنى الدلالي. تمكّن الحواسيب من فهم معنى النص ومقارنته، لا الكلمات فقط.

المفهوم​

تخيّل embeddings كإحداثيات في «فضاء المعنى»:

"happy" → [0.2, 0.8, -0.1, 0.5, ...]
"joyful" → [0.21, 0.79, -0.12, 0.48, ...] // Similar!
"sad" → [-0.3, 0.1, 0.7, -0.4, ...] // Different!

معانٍ متشابهة = متجهات متشابهة = قريبة في الفضاء

كيف تعمل Embeddings​

1. من النص إلى المتجه​

from openai import OpenAI

client = OpenAI()

# Text becomes a vector
text = "Machine learning is fascinating"
response = client.embeddings.create(
input=text,
model="text-embedding-3-small"
)

embedding = response.data[0].embedding
# Result: [0.023, -0.891, 0.156, ..., 0.342]
# 1536 dimensions

2. حساب التشابه​

import numpy as np

def cosine_similarity(vec1, vec2):
"""Calculate cosine similarity between two vectors"""
dot_product = np.dot(vec1, vec2)
norm1 = np.linalg.norm(vec1)
norm2 = np.linalg.norm(vec2)
return dot_product / (norm1 * norm2)

# Example
similarity = cosine_similarity(
embed("What is RAG?"),
embed("Explain Retrieval Augmented Generation")
)
# Result: ~0.92 (very similar!)

similarity = cosine_similarity(
embed("What is RAG?"),
embed("How to cook pasta")
)
# Result: ~0.15 (not similar)

أبعاد Embedding​

النموذجالأبعادحجم المتجه
text-embedding-3-small15366KB
text-embedding-3-large307212KB
Cohere embed-v310244KB
BGE-small3841.5KB
BGE-large10244KB

لماذا تهم الأبعاد​

  • أبعاد أكثر = تمثيلات أدق وأغنى
  • أبعاد أقل = بحث أسرع وتخزين أقل
  • مقايضة بين الجودة والكفاءة

مقاييس التشابه​

Cosine Similarity​

الأكثر شيوعًا لـ embeddings النص:

# Range: -1 to 1
# 1 = identical direction
# 0 = orthogonal (unrelated)
# -1 = opposite direction

cosine_sim = np.dot(a, b) / (np.linalg.norm(a) * np.linalg.norm(b))

المسافة الإقليدية​

تقيس المسافة المستقيمة:

# Lower = more similar
euclidean_dist = np.linalg.norm(a - b)

الجداء الداخلي (Dot Product)​

عندما تكون المتجهات معيارية:

# For normalized vectors, same as cosine similarity
dot_product = np.dot(a, b)

تصوّر Embeddings​

from sklearn.manifold import TSNE
import matplotlib.pyplot as plt

# Reduce to 2D for visualization
texts = ["cat", "dog", "car", "truck", "happy", "sad"]
embeddings = [embed(t) for t in texts]

tsne = TSNE(n_components=2, random_state=42)
reduced = tsne.fit_transform(embeddings)

plt.scatter(reduced[:, 0], reduced[:, 1])
for i, text in enumerate(texts):
plt.annotate(text, (reduced[i, 0], reduced[i, 1]))
plt.show()

تجمّعات متوقعة:

  • "cat"، "dog" ← تجمّع الحيوانات
  • "car"، "truck" ← تجمّع المركبات
  • "happy"، "sad" ← تجمّع المشاعر

خصائص embeddings الجيدة​

الخاصيةالوصف
التشابه الدلاليمعانٍ متشابهة ← متجهات قريبة
التركيبيةالعبارات مرمَّزة بمعنى
عبر اللغاتالمفهوم نفسه متشابه عبر اللغات
كثافةكل الأبعاد تحمل معنى

Embeddings في RAG​

ملخص​

  • تحوّل Embeddings النص إلى متجهات رقمية
  • المعاني المتشابهة تنتج متجهات متشابهة
  • Cosine similarity هو المقياس القياسي
  • الأبعاد تؤثّر في الجودة مقابل الكفاءة
  • تمكّن Embeddings البحث الدلالي في RAG

الخطوات التالية​

تابع إلى الفصل 2: نماذج Embedding لاستكشاف الخيارات المتاحة.