Skip to content

TogetherReranker

A reranker that uses Together AI's API for document reranking with various model options.

Installation

pip install datapizza-ai-rerankers-together

datapizza.modules.rerankers.together.TogetherReranker

Bases: Reranker

A reranker that uses the Together API to rerank documents.

__init__

__init__(api_key, model, top_n=10, threshold=None)

Initialize the TogetherReranker.

Parameters:

Name Type Description Default
api_key str

Together API key

required
model str

Model name to use for reranking

required
top_n int

Number of top documents to return

10
threshold Optional[float]

Minimum relevance score threshold. If None, no filtering is applied.

None

rerank

rerank(query, documents)

Rerank documents based on query.

Parameters:

Name Type Description Default
query str

The query to rerank documents by.

required
documents list[Chunk]

The documents to rerank.

required

Returns:

Type Description
list[Chunk]

The reranked documents.

Usage

from datapizza.modules.rerankers.together import TogetherReranker

reranker = TogetherReranker(
    api_key="your-together-api-key",
    model="sentence-transformers/msmarco-bert-base-dot-v5",
    top_n=15,
    threshold=0.3
)

# Rerank documents
query = "How to implement neural networks?"
reranked_results = reranker.rerank(query, document_chunks)

Features

  • Access to multiple reranking model options
  • Flexible model selection for different use cases
  • Score-based filtering with configurable thresholds
  • Support for various domain-specific models
  • Integration with Together AI's model ecosystem
  • Automatic model initialization and management

Available Models

Common reranking models available through Together AI:

  • sentence-transformers/msmarco-bert-base-dot-v5
  • sentence-transformers/all-MiniLM-L6-v2
  • sentence-transformers/all-mpnet-base-v2
  • Custom fine-tuned models for specific domains

Examples

Basic Usage

import uuid

from datapizza.modules.rerankers.together import TogetherReranker
from datapizza.type import Chunk

# Initialize with specific model
reranker = TogetherReranker(
    api_key="TOGETHER_API_KEY",
    model="Salesforce/Llama-Rank-V1",
    top_n=10,
    threshold=0.4
)

# Sample chunks
chunks = [
    Chunk(id=str(uuid.uuid4()), text="Neural networks are computational models inspired by biological brains..."),
    Chunk(id=str(uuid.uuid4()), text="Deep learning uses multiple layers to learn complex patterns..."),
    Chunk(id=str(uuid.uuid4()), text="Backpropagation is the algorithm used to train neural networks..."),
    Chunk(id=str(uuid.uuid4()), text="The weather is sunny today with mild temperatures..."),
    Chunk(id=str(uuid.uuid4()), text="Convolutional neural networks excel at image recognition tasks...")
]

query = "How do neural networks learn?"

# Rerank based on relevance
reranked_results = reranker.rerank(query, chunks)

# Display results
for i, chunk in enumerate(reranked_results):
    score = chunk.metadata.get('relevance_score', 'N/A')
    print(f"Rank {i+1} (Score: {score}): {chunk.text[:70]}...")