Skip to main content

llama-index postprocessor aimon rerank integration

Project description

AIMon Rerank

AIMon Rerank is a postprocessor for LlamaIndex 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:

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):

export AIMON_API_KEY="your_aimon_api_key_here"

Basic Usage

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

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.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

File details

Details for the file llama_index_postprocessor_aimon_rerank-0.2.0.tar.gz.

File metadata

File hashes

Hashes for llama_index_postprocessor_aimon_rerank-0.2.0.tar.gz
Algorithm Hash digest
SHA256 f3dee0ab44f3bae5d84e704fae5c48ea751ceb1ab8b0963ede3e33c8c206e9c0
MD5 3929dd07dcda26edeb5f9eba52ecc328
BLAKE2b-256 1f9d2ae84c8fd4dc04eba69fea17ce0316d70aba7611269e2e86305b840f3417

See more details on using hashes here.

File details

Details for the file llama_index_postprocessor_aimon_rerank-0.2.0-py3-none-any.whl.

File metadata

File hashes

Hashes for llama_index_postprocessor_aimon_rerank-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 e905a0cdec7abea29c4334d881e813e7517d4f28b4c33762366deadeaadeceec
MD5 b982df5e11c31f04dd88b7e6797f9a42
BLAKE2b-256 4a68dd17273d477c6a231653a239b771d07ec81090a82dce99cfddfa99b3a929

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page