Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AIMon reranker integration into LlamaIndex node postprocessors #18087

Merged
merged 29 commits into from
Mar 14, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
9917b9a
aimon-reranker-integration
devvratbhardwaj Mar 4, 2025
f5ac629
Declaring task_definition as a Field
devvratbhardwaj Mar 5, 2025
da63c0a
updating the arguments for aimon-reranker call
devvratbhardwaj Mar 5, 2025
b097195
modified post_processing according to aimon reranking results
devvratbhardwaj Mar 5, 2025
5fc7089
top_n filtering added
devvratbhardwaj Mar 5, 2025
a1d67dd
update to post processing
devvratbhardwaj Mar 5, 2025
289a02f
update+=2
devvratbhardwaj Mar 5, 2025
b171cb2
POST request approach
devvratbhardwaj Mar 5, 2025
b408ba4
printing response_data received from AIMon
devvratbhardwaj Mar 5, 2025
5d4139d
update+=3
devvratbhardwaj Mar 5, 2025
47cb601
model name modified
devvratbhardwaj Mar 5, 2025
8111d09
removed _client as POST call is directly being used
devvratbhardwaj Mar 5, 2025
a1ce2d7
added print statements for debugging
devvratbhardwaj Mar 5, 2025
42ffc29
checking document lengths
devvratbhardwaj Mar 5, 2025
5e73783
added \n for easier reading
devvratbhardwaj Mar 5, 2025
618c17c
text split len
devvratbhardwaj Mar 5, 2025
aeb0dea
Client call to RR instead of POST
devvratbhardwaj Mar 5, 2025
6a757e5
removed redundant imports
devvratbhardwaj Mar 5, 2025
3c7c997
normalized the absolute relevance scores
devvratbhardwaj Mar 5, 2025
ea8cd1f
added support for batch processing
devvratbhardwaj Mar 6, 2025
b699c27
updated print statements for better readability during debugging
devvratbhardwaj Mar 6, 2025
2254528
Merge branch 'run-llama:main' into aimon-reranker
devvratbhardwaj Mar 6, 2025
44b245c
Merge branch 'run-llama:main' into aimon-reranker
devvratbhardwaj Mar 10, 2025
be1d0e3
passing the pre-commit checks
devvratbhardwaj Mar 10, 2025
44b5a55
removed print statements
devvratbhardwaj Mar 10, 2025
36acff9
added unit tests and tests for batch processing
devvratbhardwaj Mar 10, 2025
8c16856
Merge branch 'run-llama:main' into aimon-reranker
devvratbhardwaj Mar 12, 2025
bb32b92
Merge branch 'run-llama:main' into aimon-reranker
devvratbhardwaj Mar 12, 2025
8a4d6c3
updated the AIMonRerank README.md with instructions on installation a…
devvratbhardwaj Mar 12, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
llama_index/postprocessor/aimon_rerank/features_to_add.txt

llama_index/_static
.DS_Store
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class

# C extensions
*.so

# Distribution / packaging
.Python
bin/
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
etc/
include/
lib/
lib64/
parts/
sdist/
share/
var/
wheels/
pip-wheel-metadata/
share/python-wheels/
*.egg-info/
.installed.cfg
*.egg
MANIFEST

# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec

# Installer logs
pip-log.txt
pip-delete-this-directory.txt

# Unit test / coverage reports
htmlcov/
.tox/
.nox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*.cover
*.py,cover
.hypothesis/
.pytest_cache/
.ruff_cache

# Translations
*.mo
*.pot

# Django stuff:
*.log
local_settings.py
db.sqlite3
db.sqlite3-journal

# Flask stuff:
instance/
.webassets-cache

# Scrapy stuff:
.scrapy

# Sphinx documentation
docs/_build/

# PyBuilder
target/

# Jupyter Notebook
.ipynb_checkpoints
notebooks/

# IPython
profile_default/
ipython_config.py

# pyenv
.python-version

# pipenv
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
# However, in case of collaboration, if having platform-specific dependencies or dependencies
# having no cross-platform support, pipenv may install dependencies that don't work, or not
# install all needed dependencies.
#Pipfile.lock

# PEP 582; used by e.g. github.com/David-OConnor/pyflow
__pypackages__/

# Celery stuff
celerybeat-schedule
celerybeat.pid

# SageMath parsed files
*.sage.py

# Environments
.env
.venv
env/
venv/
ENV/
env.bak/
venv.bak/
pyvenv.cfg

# Spyder project settings
.spyderproject
.spyproject

# Rope project settings
.ropeproject

# mkdocs documentation
/site

# mypy
.mypy_cache/
.dmypy.json
dmypy.json

# Pyre type checker
.pyre/

# Jetbrains
.idea
modules/
*.swp

# VsCode
.vscode

# pipenv
Pipfile
Pipfile.lock

# pyright
pyrightconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
poetry_requirements(
name="poetry",
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License

Copyright (c) Jerry Liu

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
GIT_ROOT ?= $(shell git rev-parse --show-toplevel)

help: ## Show all Makefile targets.
@grep -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[33m%-30s\033[0m %s\n", $$1, $$2}'

format: ## Run code autoformatters (black).
pre-commit install
git ls-files | xargs pre-commit run black --files

lint: ## Run linters: pre-commit (black, ruff, codespell) and mypy
pre-commit install && git ls-files | xargs pre-commit run --show-diff-on-failure --files

test: ## Run tests via pytest.
pytest tests

watch-docs: ## Build and watch documentation.
sphinx-autobuild docs/ docs/_build/html --open-browser --watch $(GIT_ROOT)/llama_index/
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
# AIMon Rerank

AIMon Rerank is a postprocessor for [LlamaIndex](https://github.com/run-llama/llama_index) that leverages the AIMon API to rerank retrieved documents based on contextual relevance. It refines document retrieval by applying a custom task definition and returning the most contextually relevant nodes.

## Features

- **Domain Adaptable Reranking:** Applies a user-defined task to assess document relevance.
- **Batch Processing:** Efficiently handles text in batches to stay within word count limit of 10000 per batch.
- **Seamless Integration:** Easily integrates with LlamaIndex query engine.

## Installation

Ensure you have Python 3.8+ installed. Then, install the required packages:

```bash
pip install llama-index
pip install llama-index-postprocessor-aimon-rerank
```

## Setup

Set your AIMon API key as an environment variable (or pass it directly when instantiating the reranker):

```bash
export AIMON_API_KEY="your_aimon_api_key_here"
```

## Basic Usage

Below is a minimal example demonstrating how to use AIMon Rerank with LlamaIndex:

```python
import os
from llama_index.postprocessor.aimon_rerank import AIMonRerank
from llama_index.core.response.pprint_utils import pprint_response
from llama_index.core import VectorStoreIndex, SimpleDirectoryReader

# Load documents from a directory.
documents = SimpleDirectoryReader(
"data/your_documents/example_of_afforestion"
).load_data()

# Build a vector store index from the documents.
index = VectorStoreIndex.from_documents(documents=documents)

# Define a task for the reranker.
task_definition = "Determine the relevance of context documents with respect to the user query."

# Initialize AIMonRerank, with the following parameters:
# top_n: After reranking, the top_n most contextually relevant nodes are selected for response generation.
# api_key: Ensure the AIMON_API_KEY is set, either directly or as an environment variable.
# task_definition: The task definition serves as an explicit instruction that defines what the reranking evaluation should focus on.
aimon_rerank = AIMonRerank(
top_n=2,
api_key=os.environ["AIMON_API_KEY"],
task_definition=task_definition,
)

# Create a query engine with the AIMon reranking postprocessor.
# For example, the query engine retrieves top 10 most relevant nodes, out of which only top_n are selected after reranking.
query_engine = index.as_query_engine(
similarity_top_k=10, node_postprocessors=[aimon_rerank]
)

# Execute a query.
response = query_engine.query("What did the protagonist do in this essay?")
pprint_response(response, show_source=True)
```

## Output

### Final Response

The protagonist was responsible for planting 1000 trees.

---

#### Source Node 1/2

**Node ID:** 2940ea4a-69ec-4fc4-9dd4-8ed54a9d4f1b
**Similarity:** 0.49260445005911023

**Text:**
The protagonist took on the responsibility of afforestation in their village, initiating a large-scale tree-planting campaign. Over several months, they coordinated volunteers, secured funding, and ensured the successful planting of 1000 trees in barren lands to restore the local ecosystem.

---

#### Source Node 2/2

**Node ID:** 0baaf5af-6e6b-4889-8407-e49d1753980c
**Similarity:** 0.45151918284717965

**Text:**
Determined to combat deforestation, the protagonist spearheaded a green initiative, setting an ambitious goal of planting 1000 trees. Through meticulous planning and relentless effort, they managed to achieve their objective, significantly improving the area's biodiversity and air quality.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
python_sources()
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from llama_index.postprocessor.aimon_rerank.base import AIMonRerank

__all__ = ["AIMonRerank"]
Loading