نظرة عامة على أدوات إعادة الترتيب
في مجال استرجاع المعلومات والذكاء الاصطناعي التوليدي، تُعد أداة إعادة التصنيف أداة أساسية تعمل على تحسين ترتيب النتائج من عمليات البحث الأولية. تختلف أدوات إعادة التصنيف عن نماذج التضمين التقليدية من خلال أخذ استعلام ومستند كمدخلات وإرجاع درجة تشابه مباشرةً بدلاً من التضمينات. تشير هذه الدرجة إلى الصلة بين الاستعلام المدخل والمستند.
وغالبًا ما يتم استخدام أدوات إعادة التصنيف بعد المرحلة الأولى من الاسترجاع، وعادةً ما يتم ذلك من خلال تقنيات الجار القريب المتجه (ANN). في حين أن عمليات البحث باستخدام ANN فعّالة في جلب مجموعة واسعة من النتائج المحتملة ذات الصلة، إلا أنها قد لا تعطي دائمًا الأولوية للنتائج من حيث القرب الدلالي الفعلي من الاستعلام. وهنا، تُستخدم أدوات إعادة الترتيب لتحسين ترتيب النتائج باستخدام تحليلات سياقية أعمق، وغالبًا ما تستفيد من نماذج التعلم الآلي المتقدمة مثل BERT أو النماذج الأخرى القائمة على المحولات. من خلال القيام بذلك، يمكن لـ rerankers تحسين دقة وملاءمة النتائج النهائية المقدمة للمستخدم بشكل كبير.
تدمج مكتبة نماذج PyMilvus وظائف إعادة الترتيب لتحسين ترتيب النتائج التي يتم إرجاعها من عمليات البحث الأولية. بعد استرداد أقرب التضمينات من Milvus، يمكنك الاستفادة من أدوات إعادة الترتيب هذه لتحسين نتائج البحث لتحسين دقة نتائج البحث.
وظيفة إعادة الترتيب | واجهة برمجة التطبيقات أو المصادر المفتوحة |
---|---|
BGE | مفتوحة المصدر |
أداة التشفير المتقاطع | مفتوح المصدر |
رحلة | واجهة برمجة التطبيقات |
كوهير | واجهة برمجة التطبيقات |
جينا للذكاء الاصطناعي | API |
قبل استخدام برامج إعادة الترتيب مفتوحة المصدر، تأكد من تنزيل وتثبيت جميع التبعيات والنماذج المطلوبة.
بالنسبة لأدوات إعادة الترتيب المستندة إلى واجهة برمجة التطبيقات، احصل على مفتاح واجهة برمجة التطبيقات من الموفر وقم بتعيينه في متغيرات البيئة أو الوسيطات المناسبة.
مثال 1: استخدم دالة إعادة تصنيف BGE لإعادة تصنيف المستندات وفقًا لاستعلام
في هذا المثال، نوضح في هذا المثال كيفية إعادة ترتيب نتائج البحث باستخدام أداة إعادة ترتيب BGE استنادًا إلى استعلام محدد.
لاستخدام أداة إعادة الترتيب مع مكتبة نموذج PyMilvus، ابدأ بتثبيت مكتبة نموذج PyMilvus مع الحزمة الفرعية للنموذج التي تحتوي على جميع أدوات إعادة الترتيب الضرورية:
pip install pymilvus[model]
# or pip install "pymilvus[model]" for zsh.
لاستخدام أداة إعادة الترتيب BGE، قم أولاً باستيراد فئة BGERerankFunction
:
from pymilvus.model.reranker import BGERerankFunction
ثم، قم بإنشاء مثيل BGERerankFunction
لإعادة الترتيب:
bge_rf = BGERerankFunction(
model_name="BAAI/bge-reranker-v2-m3", # Specify the model name. Defaults to `BAAI/bge-reranker-v2-m3`.
device="cpu" # Specify the device to use, e.g., 'cpu' or 'cuda:0'
)
لإعادة تصنيف المستندات بناءً على استعلام، استخدم الكود التالي:
query = "What event in 1956 marked the official birth of artificial intelligence as a discipline?"
documents = [
"In 1950, Alan Turing published his seminal paper, 'Computing Machinery and Intelligence,' proposing the Turing Test as a criterion of intelligence, a foundational concept in the philosophy and development of artificial intelligence.",
"The Dartmouth Conference in 1956 is considered the birthplace of artificial intelligence as a field; here, John McCarthy and others coined the term 'artificial intelligence' and laid out its basic goals.",
"In 1951, British mathematician and computer scientist Alan Turing also developed the first program designed to play chess, demonstrating an early example of AI in game strategy.",
"The invention of the Logic Theorist by Allen Newell, Herbert A. Simon, and Cliff Shaw in 1955 marked the creation of the first true AI program, which was capable of solving logic problems, akin to proving mathematical theorems."
]
bge_rf(query, documents)
الناتج المتوقع مشابه لما يلي:
[RerankResult(text="The Dartmouth Conference in 1956 is considered the birthplace of artificial intelligence as a field; here, John McCarthy and others coined the term 'artificial intelligence' and laid out its basic goals.", score=0.9911615761470803, index=1),
RerankResult(text="In 1950, Alan Turing published his seminal paper, 'Computing Machinery and Intelligence,' proposing the Turing Test as a criterion of intelligence, a foundational concept in the philosophy and development of artificial intelligence.", score=0.0326971950177779, index=0),
RerankResult(text='The invention of the Logic Theorist by Allen Newell, Herbert A. Simon, and Cliff Shaw in 1955 marked the creation of the first true AI program, which was capable of solving logic problems, akin to proving mathematical theorems.', score=0.006514905766152258, index=3),
RerankResult(text='In 1951, British mathematician and computer scientist Alan Turing also developed the first program designed to play chess, demonstrating an early example of AI in game strategy.', score=0.0042116724917325935, index=2)]
مثال 2: استخدم أداة إعادة الترتيب لتحسين ملاءمة نتائج البحث
في هذا الدليل، سنستكشف كيفية استخدام طريقة search()
في PyMilvus لإجراء عمليات البحث عن التشابه، وكيفية تحسين ملاءمة نتائج البحث باستخدام أداة إعادة الترتيب. سيستخدم عرضنا التوضيحي مجموعة البيانات التالية:
entities = [
{'doc_id': 0, 'doc_vector': [-0.0372721,0.0101959,...,-0.114994], 'doc_text': "In 1950, Alan Turing published his seminal paper, 'Computing Machinery and Intelligence,' proposing the Turing Test as a criterion of intelligence, a foundational concept in the philosophy and development of artificial intelligence."},
{'doc_id': 1, 'doc_vector': [-0.00308882,-0.0219905,...,-0.00795811], 'doc_text': "The Dartmouth Conference in 1956 is considered the birthplace of artificial intelligence as a field; here, John McCarthy and others coined the term 'artificial intelligence' and laid out its basic goals."},
{'doc_id': 2, 'doc_vector': [0.00945078,0.00397605,...,-0.0286199], 'doc_text': 'In 1951, British mathematician and computer scientist Alan Turing also developed the first program designed to play chess, demonstrating an early example of AI in game strategy.'},
{'doc_id': 3, 'doc_vector': [-0.0391119,-0.00880096,...,-0.0109257], 'doc_text': 'The invention of the Logic Theorist by Allen Newell, Herbert A. Simon, and Cliff Shaw in 1955 marked the creation of the first true AI program, which was capable of solving logic problems, akin to proving mathematical theorems.'}
]
مكونات مجموعة البيانات:
doc_id
: معرّف فريد لكل مستند.doc_vector
: التضمينات المتجهة التي تمثل المستند. للحصول على إرشادات حول إنشاء التضمينات، راجع التضمينات.doc_text
: المحتوى النصي للمستند.
التحضيرات
قبل بدء البحث عن التشابه، تحتاج إلى إنشاء اتصال مع ميلفوس وإنشاء مجموعة وإعداد البيانات وإدراجها في تلك المجموعة. يوضح مقتطف الكود التالي هذه الخطوات التمهيدية.
from pymilvus import MilvusClient, DataType
client = MilvusClient(
uri="http://10.102.6.214:19530" # replace with your own Milvus server address
)
client.drop_collection('test_collection')
# define schema
schema = client.create_schema(auto_id=False, enabel_dynamic_field=True)
schema.add_field(field_name="doc_id", datatype=DataType.INT64, is_primary=True, description="document id")
schema.add_field(field_name="doc_vector", datatype=DataType.FLOAT_VECTOR, dim=384, description="document vector")
schema.add_field(field_name="doc_text", datatype=DataType.VARCHAR, max_length=65535, description="document text")
# define index params
index_params = client.prepare_index_params()
index_params.add_index(field_name="doc_vector", index_type="IVF_FLAT", metric_type="IP", params={"nlist": 128})
# create collection
client.create_collection(collection_name="test_collection", schema=schema, index_params=index_params)
# insert data into collection
client.insert(collection_name="test_collection", data=entities)
# Output:
# {'insert_count': 4, 'ids': [0, 1, 2, 3]}
إجراء بحث تشابه
بعد إدراج البيانات، قم بإجراء عمليات البحث عن التشابه باستخدام الطريقة search
.
# search results based on our query
res = client.search(
collection_name="test_collection",
data=[[-0.045217834, 0.035171617, ..., -0.025117004]], # replace with your query vector
limit=3,
output_fields=["doc_id", "doc_text"]
)
for i in res[0]:
print(f'distance: {i["distance"]}')
print(f'doc_text: {i["entity"]["doc_text"]}')
الناتج المتوقع مشابه لما يلي:
distance: 0.7235960960388184
doc_text: The Dartmouth Conference in 1956 is considered the birthplace of artificial intelligence as a field; here, John McCarthy and others coined the term 'artificial intelligence' and laid out its basic goals.
distance: 0.6269873976707458
doc_text: In 1950, Alan Turing published his seminal paper, 'Computing Machinery and Intelligence,' proposing the Turing Test as a criterion of intelligence, a foundational concept in the philosophy and development of artificial intelligence.
distance: 0.5340118408203125
doc_text: The invention of the Logic Theorist by Allen Newell, Herbert A. Simon, and Cliff Shaw in 1955 marked the creation of the first true AI program, which was capable of solving logic problems, akin to proving mathematical theorems.
استخدم أداة إعادة الترتيب لتحسين نتائج البحث
بعد ذلك، قم بتحسين ملاءمة نتائج البحث من خلال خطوة إعادة الترتيب. في هذا المثال، نستخدم في هذا المثال CrossEncoderRerankFunction
المدمج في PyMilvus لإعادة ترتيب النتائج لتحسين الدقة.
# use reranker to rerank search results
from pymilvus.model.reranker import CrossEncoderRerankFunction
ce_rf = CrossEncoderRerankFunction(
model_name="cross-encoder/ms-marco-MiniLM-L-6-v2", # Specify the model name.
device="cpu" # Specify the device to use, e.g., 'cpu' or 'cuda:0'
)
reranked_results = ce_rf(
query='What event in 1956 marked the official birth of artificial intelligence as a discipline?',
documents=[
"In 1950, Alan Turing published his seminal paper, 'Computing Machinery and Intelligence,' proposing the Turing Test as a criterion of intelligence, a foundational concept in the philosophy and development of artificial intelligence.",
"The Dartmouth Conference in 1956 is considered the birthplace of artificial intelligence as a field; here, John McCarthy and others coined the term 'artificial intelligence' and laid out its basic goals.",
"In 1951, British mathematician and computer scientist Alan Turing also developed the first program designed to play chess, demonstrating an early example of AI in game strategy.",
"The invention of the Logic Theorist by Allen Newell, Herbert A. Simon, and Cliff Shaw in 1955 marked the creation of the first true AI program, which was capable of solving logic problems, akin to proving mathematical theorems."
],
top_k=3
)
# print the reranked results
for result in reranked_results:
print(f'score: {result.score}')
print(f'doc_text: {result.text}')
الناتج المتوقع مشابه لما يلي:
score: 6.250532627105713
doc_text: The Dartmouth Conference in 1956 is considered the birthplace of artificial intelligence as a field; here, John McCarthy and others coined the term 'artificial intelligence' and laid out its basic goals.
score: -2.9546022415161133
doc_text: In 1950, Alan Turing published his seminal paper, 'Computing Machinery and Intelligence,' proposing the Turing Test as a criterion of intelligence, a foundational concept in the philosophy and development of artificial intelligence.
score: -4.771512031555176
doc_text: The invention of the Logic Theorist by Allen Newell, Herbert A. Simon, and Cliff Shaw in 1955 marked the creation of the first true AI program, which was capable of solving logic problems, akin to proving mathematical theorems.