Reranking

Improve Agent retrieval accuracy with reranking

Reranking helps Agents pick the most relevant documents for RAG.

Diagram

Agent

Documents

Ranked Context

Quick Start

  1. Simple Usage
    typescript
    import { Agent, createCohereReranker, createPineconeStore } from 'praisonai';
    
    const vectorStore = createPineconeStore({ apiKey: process.env.PINECONE_API_KEY! });
    const reranker = createCohereReranker({ apiKey: process.env.COHERE_API_KEY! });
    
    // Agent with reranking-enhanced knowledge
    const agent = new Agent({
      name: 'Research Assistant',
      instructions: 'Answer questions accurately using the knowledge base.',
      knowledge: {
        vectorStore,
        indexName: 'documents',
        reranker,  // Rerank results before using as context
        topK: 5
      }
    });
    
    // Agent gets better context = better answers
    const response = await agent.chat('What is our return policy for electronics?');
  2. With Configuration

    Add a reranking tool or LLM scorer — see sections below.


Agent with Reranking Tool

Give your Agent explicit control over reranking:

typescript
import { Agent, createTool, createCohereReranker, createPineconeStore } from 'praisonai';

const vectorStore = createPineconeStore({ apiKey: process.env.PINECONE_API_KEY! });
const reranker = createCohereReranker({ apiKey: process.env.COHERE_API_KEY! });

// Tool that searches and reranks
const searchWithReranking = createTool({
  name: 'search_documents',
  description: 'Search documents and return the most relevant results',
  parameters: {
    type: 'object',
    properties: {
      query: { type: 'string', description: 'Search query' },
      count: { type: 'number', description: 'Number of results (default: 5)' }
    },
    required: ['query']
  },
  execute: async ({ query, count = 5 }) => {
    // 1. Broad initial search
    const initialResults = await vectorStore.query({
      indexName: 'documents',
      vector: await getEmbedding(query),
      topK: 20  // Get more than needed
    });

    // 2. Rerank for precision
    const reranked = await reranker.rerank(query, initialResults, { topK: count });

    // 3. Return best results to Agent
    return reranked.map(r => `[Score: ${r.score.toFixed(2)}] ${r.content}`).join('\n\n');
  }
});

const agent = new Agent({
  name: 'Smart Researcher',
  instructions: 'Search for information and provide accurate answers based on the most relevant documents.',
  tools: [searchWithReranking]
});

const response = await agent.chat('Find the most relevant information about pricing tiers');

Multi-Agent RAG with Reranking

typescript
import { Agent, Agents, createCohereReranker, createPineconeStore } from 'praisonai';

const vectorStore = createPineconeStore({ apiKey: process.env.PINECONE_API_KEY! });
const reranker = createCohereReranker({ apiKey: process.env.COHERE_API_KEY! });

// Agent 1: Retrieves documents
const retriever = new Agent({
  name: 'Retriever',
  instructions: 'Search the knowledge base and retrieve relevant documents.',
  knowledge: { vectorStore, indexName: 'docs' }
});

// Agent 2: Reranks and filters
const ranker = new Agent({
  name: 'Ranker',
  instructions: 'Evaluate document relevance and select the best ones.',
  tools: [createTool({
    name: 'rerank_documents',
    description: 'Rerank documents by relevance',
    parameters: {
      type: 'object',
      properties: {
        query: { type: 'string' },
        documents: { type: 'array', items: { type: 'object' } }
      },
      required: ['query', 'documents']
    },
    execute: async ({ query, documents }) => {
      const reranked = await reranker.rerank(query, documents, { topK: 3 });
      return reranked;
    }
  })]
});

// Agent 3: Answers based on reranked results
const answerer = new Agent({
  name: 'Answerer',
  instructions: 'Provide accurate answers based on the provided documents.'
});

const agents = new AgentTeam({
  agents: [retriever, ranker, answerer],
  tasks: [
    { agent: retriever, description: 'Retrieve documents for: {query}' },
    { agent: ranker, description: 'Rerank the retrieved documents' },
    { agent: answerer, description: 'Answer the question using the top documents' }
  ]
});

await agents.start({ query: 'What are the system requirements?' });

LLM-Based Reranking Agent

Use an Agent itself to judge document relevance:

typescript
import { Agent, createLLMReranker } from 'praisonai';

// Scoring Agent
const scorerAgent = new Agent({
  name: 'Relevance Scorer',
  instructions: `Score how relevant a document is to a query.
Return only a number from 0 to 1, where 1 is perfectly relevant.`
});

// Create reranker powered by the Agent
const agentReranker = createLLMReranker({
  generateFn: async (prompt) => await scorerAgent.chat(prompt)
});

// Main Agent with Agent-powered reranking
const mainAgent = new Agent({
  name: 'Research Assistant',
  instructions: 'Answer questions using the knowledge base.',
  knowledge: {
    vectorStore,
    reranker: agentReranker
  }
});

Supported Rerankers

RerankerBest ForAPI Required
CohereProduction AgentsYes
CrossEncoderSelf-hosted AgentsNo
LLMCustom scoring logicYes

Cohere (Recommended for Production)

typescript
import { createCohereReranker } from 'praisonai';

const reranker = createCohereReranker({
  apiKey: process.env.COHERE_API_KEY!,
  model: 'rerank-english-v3.0'
});

Cross-Encoder (No External API)

typescript
import { createCrossEncoderReranker } from 'praisonai';

const reranker = createCrossEncoderReranker({
  embedFn: async (texts) => await getEmbeddings(texts)
});

LLM Reranker (Flexible)

typescript
import { createLLMReranker, Agent } from 'praisonai';

const scorer = new Agent({ name: 'Scorer' });
const reranker = createLLMReranker({
  generateFn: async (prompt) => await scorer.chat(prompt)
});

Why Reranking Matters for Agents

Without RerankingWith Reranking
Agent gets 5 “similar” docsAgent gets 5 “most relevant” docs
May include tangential infoFocused on the actual question
Lower answer accuracyHigher answer accuracy
More hallucination riskGrounded in best sources

Environment Variables

bash
COHERE_API_KEY=your-cohere-key
OPENAI_API_KEY=your-openai-key  # For embeddings

Related