Files
wehub-resource-sync a0c8464e58
Build Package / build (ubuntu-latest) (push) Failing after 1s
CodeQL / Analyze (python) (push) Failing after 1s
Core Typecheck / core-typecheck (push) Failing after 1s
Linting / lint (push) Failing after 1s
llama-dev tests / test-llama-dev (push) Failing after 1s
Publish Sub-Package to PyPI if Needed / publish_subpackage_if_needed (push) Has been skipped
Sync Docs to Developer Hub / sync-docs (push) Failing after 0s
Build Package / build (windows-latest) (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 12:26:52 +08:00

175 lines
5.2 KiB
Plaintext

{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<a href=\"https://colab.research.google.com/github/run-llama/llama_index/blob/main/docs/examples/evaluation/BeirEvaluation.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# BEIR Out of Domain Benchmark"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"About [BEIR](https://github.com/beir-cellar/beir):\n",
"\n",
"BEIR is a heterogeneous benchmark containing diverse IR tasks. It also provides a common and easy framework for evaluation of your retrieval methods within the benchmark.\n",
"\n",
"Refer to the repo via the link for a full list of supported datasets."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Here, we test the `all-MiniLM-L6-v2` sentence-transformer embedding, which is one of the fastest for the given accuracy range. We set the top_k value for the retriever to 30. We also use the nfcorpus dataset."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"If you're opening this Notebook on colab, you will probably need to install LlamaIndex 🦙."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"%pip install llama-index-embeddings-huggingface"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"!pip install llama-index"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"/home/jonch/.pyenv/versions/3.10.6/lib/python3.10/site-packages/beir/datasets/data_loader.py:2: TqdmWarning: IProgress not found. Please update jupyter and ipywidgets. See https://ipywidgets.readthedocs.io/en/stable/user_install.html\n",
" from tqdm.autonotebook import tqdm\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Dataset: nfcorpus downloaded at: /home/jonch/.cache/llama_index/datasets/BeIR__nfcorpus\n",
"Evaluating on dataset: nfcorpus\n",
"-------------------------------------\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"100%|███████████████████████████████████| 3633/3633 [00:00<00:00, 141316.79it/s]\n",
"Parsing documents into nodes: 100%|████████| 3633/3633 [00:06<00:00, 569.35it/s]\n",
"Generating embeddings: 100%|████████████████| 3649/3649 [04:22<00:00, 13.92it/s]\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Retriever created for: nfcorpus\n",
"Evaluating retriever on questions against qrels\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"100%|█████████████████████████████████████████| 323/323 [01:26<00:00, 3.74it/s]"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Results for: nfcorpus\n",
"{'NDCG@3': 0.35476, 'MAP@3': 0.07489, 'Recall@3': 0.08583, 'precision@3': 0.33746}\n",
"{'NDCG@10': 0.31403, 'MAP@10': 0.11003, 'Recall@10': 0.15885, 'precision@10': 0.23994}\n",
"{'NDCG@30': 0.28636, 'MAP@30': 0.12794, 'Recall@30': 0.21653, 'precision@30': 0.14716}\n",
"-------------------------------------\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"\n"
]
}
],
"source": [
"from llama_index.embeddings.huggingface import HuggingFaceEmbedding\n",
"from llama_index.core.evaluation.benchmarks import BeirEvaluator\n",
"from llama_index.core import VectorStoreIndex\n",
"\n",
"\n",
"def create_retriever(documents):\n",
" embed_model = HuggingFaceEmbedding(model_name=\"BAAI/bge-small-en-v1.5\")\n",
" index = VectorStoreIndex.from_documents(\n",
" documents, embed_model=embed_model, show_progress=True\n",
" )\n",
" return index.as_retriever(similarity_top_k=30)\n",
"\n",
"\n",
"BeirEvaluator().run(\n",
" create_retriever, datasets=[\"nfcorpus\"], metrics_k_values=[3, 10, 30]\n",
")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Higher is better for all the evaluation metrics.\n",
"\n",
"This [towardsdatascience article](https://towardsdatascience.com/ranking-evaluation-metrics-for-recommender-systems-263d0a66ef54) covers NDCG, MAP and MRR in greater depth."
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3"
}
},
"nbformat": 4,
"nbformat_minor": 4
}