pgvector: تخزين المتجهات والاستعلام عنها
في هذا الدرس 4 موضوعات
هذا الدرس مخطط له. يعرض المخطط أدناه الموضوعات التي سيغطيها، ولم يُنشر الدرس الكامل بعد.
نص الدروس والتمارين باللغة الإنجليزية، بينما تبقى واجهة الموقع وعناوين الدروس بالعربية.
في هذا الدرس
store embeddings in PostgreSQL and run a nearest-neighbour query.
الموضوعات المخطط لها
- pgvector adds a
vector(n)column type and distance operators; enable it withCREATE EXTENSION vector. - The dimension
nmust match the model; keep amodelcolumn so you know what produced each row. - Query: order by distance,
LIMIT k. Without an index this is an exact scan, fine for small tables. - Embeddings stay next to relational data: filter by language or product with a normal
WHERE. - Store source text, a content hash, and the article id with the vector so rows can be refreshed.
تفاصيل المخطط المبدئي
عرض عملي
CREATE EXTENSION IF NOT EXISTS vector;
CREATE TABLE help_chunk (
id bigserial PRIMARY KEY, article_id bigint NOT NULL, lang text NOT NULL,
content text NOT NULL, content_hash text NOT NULL,
model text NOT NULL, embedding vector(768) NOT NULL);
SELECT id, article_id, embedding <=> :q AS distance
FROM help_chunk WHERE lang = 'en'
ORDER BY embedding <=> :q LIMIT 5;Dimension 768 is an example; verify against the model.
اختبر فهمك
Why store a model column?
أظهر الإجابة
To detect and re-embed rows made with a different model.
التطبيق
طبّق هذا الدرس بمختبر الوحدة وتمارينها واختبارها.
تعليم الدرس كمكتمل اختياري ويمكنك التراجع عنه في أي وقت. يبقى تقدمك محفوظًا في هذا المتصفح.