Cohere Reranker
The Cohere Reranker is a postprocessor that uses the Cohere API to rerank the results of a search query.
Install the required packages:
npm i @vectorstores/core @vectorstores/cohere openaipnpm add @vectorstores/core @vectorstores/cohere openaiyarn add @vectorstores/core @vectorstores/cohere openaibun add @vectorstores/core @vectorstores/cohere openaiYou’ll need:
OPENAI_API_KEYfor generating embeddings (used to build the index)COHERE_API_KEYfor reranking
import { CohereRerank } from "@vectorstores/cohere";import { Document, MetadataMode, Settings, VectorStoreIndex } from "@vectorstores/core";import { OpenAI } from "openai";Load and index documents, then rerank retrieval results
Section titled “Load and index documents, then rerank retrieval results”This example follows the same flow as examples/retrieval/rerankers/CohereReranker.ts: retrieve nodes normally, then rerank them with Cohere.
async function main() { // ---- embeddings (required to build VectorStoreIndex) const openai = new OpenAI({ apiKey: process.env.OPENAI_API_KEY }); Settings.embedFunc = async (input: string[]): Promise<number[][]> => { const { data } = await openai.embeddings.create({ model: "text-embedding-3-small", input, }); return data.map((d) => d.embedding); };
// ---- load and index const essay = "Paul Graham is the cofounder of Y Combinator. He grew up in Pittsburgh..."; const document = new Document({ text: essay, id_: "essay" }); const index = await VectorStoreIndex.fromDocuments([document]);
// ---- cohere reranker const cohereRerank = new CohereRerank({ apiKey: process.env.COHERE_API_KEY, topN: 5, model: "rerank-v3.5", });
// ---- retrieve more than the default (2) const retriever = index.asRetriever({ similarityTopK: 5, });
const query = "What did the author do growing up?";
// Retrieve nodes const nodes = await retriever.retrieve({ query });
// Apply Cohere reranking const rerankedNodes = await cohereRerank.postprocessNodes(nodes, query);
console.log("With Cohere reranking:"); for (const r of rerankedNodes) { console.log("Score:", r.score ?? "-"); console.log(r.node.getContent(MetadataMode.NONE)); console.log("---"); }
console.log("Without Cohere reranking:"); for (const r of nodes) { console.log("Score:", r.score ?? "-"); console.log(r.node.getContent(MetadataMode.NONE)); console.log("---"); }}
main().catch(console.error);