You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
- The ``base_retriever`` must implement the ``AutoRetriever`` interface.
156
+
- You can use any compatible retriever, e.g., ``NgramRetriever``, ``Word2VecRetriever``,
157
+
or your own custom retriever.
158
+
- This allows combining semantic, n-gram, or hybrid retrieval pipelines easily.
159
+
160
+
161
+
Retriever Collection
162
+
--------------------------
163
+
164
+
NgramRetriever
165
+
~~~~~~~~~~~~~~~~~~~~~~~
166
+
167
+
168
+
.. sidebar:: **Supported vectorizers**
169
+
170
+
- ``count``: Converts a collection of text documents to a matrix of token counts.
171
+
- ``tfidf``: Converts a collection of raw documents to a matrix of TF-IDF features.
172
+
173
+
174
+
The ``NgramRetriever`` is a simple, interpretable text retriever based on traditional n-gram vectorization methods, such as `CountVectorizer <https://scikit-learn.org/stable/modules/generated/sklearn.feature_extraction.text.CountVectorizer.html>`_ and `TfidfVectorizer <https://scikit-learn.org/stable/modules/generated/sklearn.feature_extraction.text.TfidfVectorizer.html>`_. It ranks documents using cosine similarity of n-gram vectors. This is useful for baseline retrieval, keyword matching, or small-scale text search tasks. The following code shows how to import ``NgramRetriever`` and load desired model with desired arguments.
175
+
176
+
.. code-block:: python
177
+
178
+
from ontolearner.learner import AutoRetrieverLearner
179
+
from ontolearner.learner.retriever import NgramRetriever
For desired arguments refer to `scikit-learn > TfidfVectorizer <https://scikit-learn.org/stable/modules/generated/sklearn.feature_extraction.text.TfidfVectorizer.html>`_ or `scikit-learn > CountVectorizer <https://scikit-learn.org/stable/modules/generated/sklearn.feature_extraction.text.CountVectorizer.html>`_
190
+
191
+
Word2VecRetriever
192
+
~~~~~~~~~~~~~~~~~~~~~~~
193
+
194
+
.. sidebar:: How to Download Word2Vec?
195
+
196
+
Download the word2vec from `GoogleNews-vectors-negative300.bin.gz <https://drive.google.com/file/d/0B7XkCwpI5KDYNlNUTTlSS21pQmM/edit?resourcekey=0-wjGZdNAUop6WykTtMip30g>`_ and then you can provide the path inside the ``.load(...)``.
197
+
198
+
`Word2Vec <https://arxiv.org/abs/1301.3781>`_ retriever encode documents and queries using pre-trained word embeddings. Each document is represented by the average of its word vectors, and retrieval is done via cosine similarity between query vectors and document vectors. The following code shows how to use ``Word2VecRetriever`` inside learner model:
199
+
200
+
.. code-block:: python
201
+
202
+
from ontolearner.learner import AutoRetrieverLearner
203
+
from ontolearner.learner.retriever import Word2VecRetriever
Learn more about Word2Vec at `https://www.tensorflow.org/text/tutorials/word2vec <https://www.tensorflow.org/text/tutorials/word2vec>`_
214
+
215
+
GloveRetriever
216
+
~~~~~~~~~~~~~~~~~~~~~~~
217
+
.. sidebar:: How to Download GloVe?
218
+
219
+
Download the desired GloVe models from `https://nlp.stanford.edu/projects/glove/ <https://nlp.stanford.edu/projects/glove/>`_ and then you can provide the path inside the ``.load(...)``.
220
+
221
+
`GloVe <https://nlp.stanford.edu/projects/glove/>`_ is an unsupervised learning algorithm for obtaining vector representations for words. Training is performed on aggregated global word-word co-occurrence statistics from a corpus, and the resulting representations showcase interesting linear substructures of the word vector space. Here, the ``GloveRetriever`` operates based on GloVe model as shown in the following:
222
+
223
+
224
+
.. code-block:: python
225
+
226
+
from ontolearner.learner import AutoRetrieverLearner
227
+
from ontolearner.learner.retriever import GloveRetriever
In both **Word2Vec** and **GloVe** retrievers, If a word in a word is not in the embedding vocabulary, it is ignored.
239
+
240
+
.. note::
241
+
242
+
Refer to the GloVe paper at `GloVe: Global Vectors for Word Representation <https://aclanthology.org/D14-1162/>`_ to learn more about this model.
243
+
244
+
CrossEncoderRetriever
245
+
~~~~~~~~~~~~~~~~~~~~~~~
246
+
247
+
248
+
.. sidebar:: Cross-Encoder Models
249
+
250
+
Collections of publicly available cross-encoder models are available at: `🤗 Sentence Transformers - Cross-Encoders <https://huggingface.co/cross-encoder>`_.
251
+
252
+
253
+
Untill now, the OntoLearner ``AutoRetriever`` (base retriever for ``AutoRetrieverLearner``) were using a Bi-Encoder architecture for retrievals. It is important to understand the difference between Bi- and Cross-Encoder. The following diagram shows the differences:
254
+
255
+
.. raw:: html
256
+
257
+
<divalign="center">
258
+
<imgsrc="https://raw.githubusercontent.com/sciknoworg/OntoLearner/refs/heads/dev/docs/source/learners/images/Bi_vs_Cross-Encoder.jpg"alt="Bi-Encoder vs Cross-Encoder "width="40%"/>
259
+
</div>
260
+
<br>
261
+
262
+
263
+
Bi-Encoders produce for a given sentence a sentence embedding. We pass to a BERT independently the sentences A and B, which result in the sentence embeddings u and v. These sentence embedding can then be compared using cosine similarity. In contrast, for a Cross-Encoder, we pass both sentences simultaneously to the Transformer network. It produces then an output value between 0 and 1 indicating the similarity of the input sentence pair. A Cross-Encoder does not produce a sentence embedding. Also, we are not able to pass individual sentences to a Cross-Encoder (Reference: `Sentence-BERT > Cross-Encoder <https://sbert.net/examples/cross_encoder/applications/README.html>`_).
264
+
265
+
266
+
Here, in the OntoLearner, we implemented a ``CrossEncoderRetriever``, a hybrid dense retriever that combines a BiEncoder for fast candidate retrieval and a CrossEncoder for accurate reranking. Overall ``CrossEncoderRetriever`` uses Bi-Encoder based model for retrieval and Cross-Encoder model for reranking. This provides an efficient and accurate alternative to pure Cross-Encoder or pure Bi-Encoder approaches. To use ``CrossEncoderRetriever`` simply follow the following steps:
267
+
268
+
269
+
.. code-block:: python
270
+
271
+
from ontolearner.learner import AutoRetrieverLearner
272
+
from ontolearner.learner.retriever import CrossEncoderRetriever
273
+
274
+
retriever = CrossEncoderRetriever(bi_encoder_model_id='Qwen/Qwen3-Embedding-8B') # pass the bi-encoder model ID used in the first-stage
learner.load(model_id="cross-encoder/ms-marco-MiniLM-L12-v2") # Model ID for the CrossEncoder (reranking model) here!
279
+
# When .load(...) is instantiated, both the bi-encoder and cross-encoder models will be loaded.
280
+
281
+
282
+
.. note::
283
+
284
+
Learn more about Retrieve and Rerank approach at `Sentence Transformers > Usage > Retrieve & Re-Rank <https://sbert.net/examples/sentence_transformer/applications/retrieve_rerank/README.html>`_.
285
+
286
+
LLMAugmentedRetriever
287
+
~~~~~~~~~~~~~~~~~~~~~~~~
288
+
The LLM-Augmented retriever improves retrieval quality by expanding each query into multiple augmented variants using an LLM (e.g., GPT-4). The following diagram shows how LLM-Augmented retriever operates in comparison to usual retriever approach.
The online augmentation is designed to avoid multiple calls to the models that may lead into expensive API usage and waiting time. Once the augmenter generator output is stored, it can be used for next stage.
319
+
320
+
**2. Offline augmentation (recommended for large experiments):** Instead of calling the LLM repeatedly, you load the previously saved augmentations.
321
+
322
+
323
+
.. code-block:: python
324
+
325
+
# Step 1 — Load augmenter
326
+
from ontolearner.learner.retriever import LLMAugmenter
327
+
augmenter = LLMAugmenter("augment.json")
328
+
329
+
# Step 2 — Attach it to the retriever
330
+
from ontolearner.learner.retriever import LLMAugmentedRetriever
331
+
from ontolearner.learner import LLMAugmentedRetrieverLearner
learner.load(model_id="Qwen/Qwen3-Embedding-8B") # path to desired retriever model.
337
+
338
+
Here the ``LLMAugmentedRetrieverLearner`` is the high-level wrapper that orchestrates the loading a retriever model, attaching the ``LLMAugmentedRetriever``, automatically applying LLM-based query expansion during training and prediction, and computing ground truth and returning predictions.
339
+
340
+
341
+
342
+
.. list-table:: Summary of Components:
343
+
:header-rows: 1
344
+
:widths: 25 75
345
+
346
+
* - Component
347
+
- Purpose
348
+
* - ``LLMAugmenterGenerator``
349
+
- Calls an LLM (GPT-4, GPT-3.5, etc.) to generate augmentation data.
350
+
* - ``LLMAugmenter``
351
+
- Loads offline augmentations (``augment.json``).
352
+
* - ``LLMAugmentedRetriever``
353
+
- Expands each query using augmentations before retrieval.
354
+
* - ``LLMAugmentedRetrieverLearner``
355
+
- Applies the learner pipeline using the augmented retriever.
356
+
357
+
.. rubric:: Example: Using LLMAugmentedRetrieverLearner for Taxonomy Discovery
358
+
359
+
.. code-block:: python
360
+
361
+
from ontolearner.learner.retriever import LLMAugmenterGenerator, LLMAugmentedRetriever, LLMAugmenter
362
+
from ontolearner import LLMAugmentedRetrieverLearner, Wine, train_test_split, evaluation_report
0 commit comments