Milvus
Zilliz
الصفحة الرئيسية
  • عمليات الدمج
  • Home
  • Docs
  • عمليات الدمج

  • التنسيق

  • لاما إندكس

  • البحث في النص الكامل

Open In Colab GitHub Repository

استخدام البحث عن النص الكامل مع LlamaIndex و Milvus

يستخدمالبحث عن النص الكامل مطابقة الكلمات المفتاحية بشكل دقيق، وغالبًا ما يستفيد من خوارزميات مثل BM25 لترتيب المستندات حسب الصلة. في أنظمة التوليد المعزز للاسترجاع (RAG) ، تسترجع هذه الطريقة النص ذا الصلة لتعزيز الاستجابات التي يتم إنشاؤها بواسطة الذكاء الاصطناعي.

وفي الوقت نفسه، يفسر البحث الدلالي المعنى السياقي لتقديم نتائج أوسع نطاقًا. يؤدي الجمع بين كلا النهجين إلى إنشاء بحث هجين يحسن استرجاع المعلومات - خاصةً في الحالات التي تقصر فيها طريقة واحدة.

باستخدام نهج Sparse-BM25 في Milvus 2.5، يتم تحويل النص الخام تلقائيًا إلى متجهات متفرقة. هذا يلغي الحاجة إلى توليد التضمين المتناثر يدويًا ويتيح استراتيجية بحث هجينة توازن بين الفهم الدلالي وأهمية الكلمات الرئيسية.

في هذا البرنامج التعليمي، سوف تتعلم كيفية استخدام LlamaIndex وMilvus لبناء نظام RAG باستخدام البحث في النص الكامل والبحث الهجين. سنبدأ بتنفيذ البحث بالنص الكامل وحده ثم نعززه من خلال دمج البحث الدلالي للحصول على نتائج أكثر شمولاً.

قبل الشروع في هذا البرنامج التعليمي، تأكد من أنك على دراية بالبحث في النص الكامل وأساسيات استخدام Milvus في LlamaIndex.

المتطلبات الأساسية

تثبيت التبعيات

قبل البدء، تأكد من تثبيت التبعيات التالية:

$ $pip install llama-index-vector-stores-milvus
$ $pip install llama-index-embeddings-openai
$ $pip install llama-index-llms-openai

إذا كنت تستخدم Google Colab، فقد تحتاج إلى إعادة تشغيل وقت التشغيل (انتقل إلى قائمة "وقت التشغيل" في أعلى الواجهة، وحدد "إعادة تشغيل الجلسة" من القائمة المنسدلة).

إعداد الحسابات

يستخدم هذا البرنامج التعليمي OpenAI لتضمين النص وتوليد الإجابات. تحتاج إلى إعداد مفتاح OpenAI API.

import openai

openai.api_key = "sk-"

لاستخدام مخزن ناقلات Milvus، حدد خادم Milvus الخاص بك URI (واختيارياً مع TOKEN). لبدء تشغيل خادم Milvus، يمكنك إعداد خادم Milvus باتباع دليل تثبيت Milvus أو ببساطة تجربة Zilliz Cloud مجانًا.

البحث في النص الكامل مدعوم حاليًا في Milvus Standalone وMilvus Distributed وZilliz Cloud، ولكن ليس بعد في Milvus Lite (من المخطط تنفيذه في المستقبل). تواصل مع support@zilliz.com لمزيد من المعلومات.

URI = "http://localhost:19530"
# TOKEN = ""

تنزيل أمثلة للبيانات

قم بتشغيل الأوامر التالية لتنزيل نماذج المستندات في دليل "data/paul_graham":

$ mkdir -p 'data/paul_graham/'
$ $wget 'https://raw.githubusercontent.com/run-llama/llama_index/main/docs/docs/examples/data/paul_graham/paul_graham_essay.txt' -O 'data/paul_graham/paul_graham_essay.txt'
--2025-03-27 07:49:01--  https://raw.githubusercontent.com/run-llama/llama_index/main/docs/docs/examples/data/paul_graham/paul_graham_essay.txt
Resolving raw.githubusercontent.com (raw.githubusercontent.com)... 185.199.108.133, 185.199.109.133, 185.199.110.133, ...
Connecting to raw.githubusercontent.com (raw.githubusercontent.com)|185.199.108.133|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 75042 (73K) [text/plain]
Saving to: ‘data/paul_graham/paul_graham_essay.txt’

data/paul_graham/pa 100%[===================>]  73.28K  --.-KB/s    in 0.07s   

2025-03-27 07:49:01 (1.01 MB/s) - ‘data/paul_graham/paul_graham_essay.txt’ saved [75042/75042]

يعمل دمج البحث بالنص الكامل في نظام RAG على تحقيق التوازن بين البحث الدلالي والاسترجاع الدقيق والمتوقع القائم على الكلمات المفتاحية. يمكنك أيضًا اختيار استخدام البحث بالنص الكامل فقط على الرغم من أنه يوصى بدمج البحث بالنص الكامل مع البحث الدلالي للحصول على نتائج بحث أفضل. هنا لغرض العرض التوضيحي سنعرض البحث بالنص الكامل وحده والبحث المختلط.

للبدء، استخدم SimpleDirectoryReaderLoad لتحميل مقال "ما عملت عليه" للكاتب بول غراهام:

from llama_index.core import SimpleDirectoryReader

documents = SimpleDirectoryReader("./data/paul_graham/").load_data()

# Let's take a look at the first document
print("Example document:\n", documents[0])
Example document:
 Doc ID: 16b7942f-bf1a-4197-85e1-f31d51ea25a9
Text: What I Worked On  February 2021  Before college the two main
things I worked on, outside of school, were writing and programming. I
didn't write essays. I wrote what beginning writers were supposed to
write then, and probably still are: short stories. My stories were
awful. They had hardly any plot, just characters with strong feelings,
which I ...

البحث بالنص الكامل مع BM25

يدعم موقع LlamaIndex MilvusVectorStore البحث في النص الكامل، مما يتيح الاسترجاع الفعال القائم على الكلمات الرئيسية. باستخدام دالة مدمجة كـ sparse_embedding_function ، فإنه يطبق تصنيف BM25 لترتيب نتائج البحث.

سنوضح في هذا القسم كيفية تنفيذ نظام RAG باستخدام BM25 للبحث في النص الكامل.

from llama_index.core import VectorStoreIndex, StorageContext
from llama_index.vector_stores.milvus import MilvusVectorStore
from llama_index.vector_stores.milvus.utils import BM25BuiltInFunction
from llama_index.core import Settings

# Skip dense embedding model
Settings.embed_model = None

# Build Milvus vector store creating a new collection
vector_store = MilvusVectorStore(
    uri=URI,
    # token=TOKEN,
    enable_dense=False,
    enable_sparse=True,  # Only enable sparse to demo full text search
    sparse_embedding_function=BM25BuiltInFunction(),
    overwrite=True,
)

# Store documents in Milvus
storage_context = StorageContext.from_defaults(vector_store=vector_store)
index = VectorStoreIndex.from_documents(documents, storage_context=storage_context)
Embeddings have been explicitly disabled. Using MockEmbedding.

يقوم الرمز أعلاه بإدراج أمثلة على المستندات في Milvus وإنشاء فهرس لتمكين تصنيف BM25 للبحث في النص الكامل. يقوم بتعطيل التضمين الكثيف ويستخدم BM25BuiltInFunction مع المعلمات الافتراضية.

يمكنك تحديد حقول الإدخال والإخراج في معلمات BM25BuiltInFunction:

  • input_field_names (str): حقل نص الإدخال (افتراضي: "نص"). يشير إلى حقل النص الذي تطبق عليه خوارزمية BM25. قم بتغيير هذا إذا كنت تستخدم مجموعتك الخاصة باسم حقل نصي مختلف.
  • output_field_names (str): الحقل حيث يتم تخزين مخرجات دالة BM25 هذه (افتراضي: "sparse_embedding").

بمجرد إعداد مخزن المتجهات، يمكنك إجراء استعلامات بحث عن النص الكامل باستخدام Milvus مع وضع الاستعلام "متناثر" أو "text_search":

import textwrap

query_engine = index.as_query_engine(
    vector_store_query_mode="sparse", similarity_top_k=5
)
answer = query_engine.query("What did the author learn at Viaweb?")
print(textwrap.fill(str(answer), 100))
The author learned several important lessons at Viaweb. They learned about the importance of growth
rate as the ultimate test of a startup, the value of building stores for users to understand retail
and software usability, and the significance of being the "entry level" option in a market.
Additionally, they discovered the accidental success of making Viaweb inexpensive, the challenges of
hiring too many people, and the relief felt when the company was acquired by Yahoo.

تخصيص محلل النص

تلعب المحللات دورًا حيويًا في البحث في النص الكامل من خلال تقسيم الجمل إلى رموز وإجراء معالجة معجمية، مثل الجذعية وإزالة كلمات التوقف. وهي عادةً ما تكون خاصة باللغة. لمزيد من التفاصيل، راجع دليل محلل ميلفوس.

يدعم ميلفوس نوعين من المحللات: المحللات المدمجة والمحللات المخصصة. بشكل افتراضي، يستخدم BM25BuiltInFunction المحلل المدمج القياسي، الذي يقوم بترميز النص بناءً على علامات الترقيم.

لاستخدام محلل مختلف أو تخصيص المحلل الموجود، يمكنك تمرير قيمة إلى الوسيطة analyzer_params:

bm25_function = BM25BuiltInFunction(
    analyzer_params={
        "tokenizer": "standard",
        "filter": [
            "lowercase",  # Built-in filter
            {"type": "length", "max": 40},  # Custom cap size of a single token
            {"type": "stop", "stop_words": ["of", "to"]},  # Custom stopwords
        ],
    },
    enable_match=True,
)

البحث الهجين مع أداة إعادة التصنيف

يجمع نظام البحث الهجين بين البحث الدلالي والبحث في النص الكامل، مما يحسن أداء الاسترجاع في نظام RAG.

يستخدم المثال التالي تضمين OpenAI للبحث الدلالي و BM25 للبحث في النص الكامل:

# Create index over the documnts
vector_store = MilvusVectorStore(
    uri=URI,
    # token=TOKEN,
    # enable_dense=True,  # enable_dense defaults to True
    dim=1536,
    enable_sparse=True,
    sparse_embedding_function=BM25BuiltInFunction(),
    overwrite=True,
    # hybrid_ranker="RRFRanker",  # hybrid_ranker defaults to "RRFRanker"
    # hybrid_ranker_params={},  # hybrid_ranker_params defaults to {}
)

storage_context = StorageContext.from_defaults(vector_store=vector_store)
index = VectorStoreIndex.from_documents(
    documents,
    storage_context=storage_context,
    embed_model="default",  # "default" will use OpenAI embedding
)

كيف يعمل

يقوم هذا النهج بتخزين المستندات في مجموعة Milvus مع كلا الحقلين المتجهين:

  • embedding: التضمينات الكثيفة التي تم إنشاؤها بواسطة نموذج التضمين OpenAI للبحث الدلالي.
  • sparse_embedding: التضمينات المتفرقة المحسوبة باستخدام BM25BuiltInFunction للبحث في النص الكامل.

بالإضافة إلى ذلك، قمنا بتطبيق استراتيجية إعادة الترتيب باستخدام "RRFRanker" بمعلماته الافتراضية. لتخصيص أداة إعادة التصنيف، يمكنك تهيئة hybrid_ranker و hybrid_ranker_params باتباع دليل إعادة التصنيف الخاص بـ Milvus Reranking.

والآن، دعنا نختبر نظام RAG باستخدام نموذج استعلام:

# Query
query_engine = index.as_query_engine(
    vector_store_query_mode="hybrid", similarity_top_k=5
)
answer = query_engine.query("What did the author learn at Viaweb?")
print(textwrap.fill(str(answer), 100))
The author learned several important lessons at Viaweb. These included the importance of
understanding growth rate as the ultimate test of a startup, the impact of hiring too many people,
the challenges of being at the mercy of investors, and the relief experienced when Yahoo bought the
company. Additionally, the author learned about the significance of user feedback, the value of
building stores for users, and the realization that growth rate is crucial for the long-term success
of a startup.

يضمن هذا النهج الهجين استجابات أكثر دقة وإدراكًا للسياق في نظام RAG من خلال الاستفادة من كل من الاسترجاع الدلالي والاسترجاع القائم على الكلمات الرئيسية.

جرب Managed Milvus مجاناً

Zilliz Cloud خالي من المتاعب، ويعمل بواسطة Milvus ويعمل بسرعة 10 أضعاف.

ابدأ
التعليقات

هل كانت هذه الصفحة مفيدة؟