Package com.redis.vl.utils.rerank
Class CohereReranker
java.lang.Object
com.redis.vl.utils.rerank.BaseReranker
com.redis.vl.utils.rerank.CohereReranker
Reranker that uses Cohere's Rerank API to rerank documents based on query relevance.
This reranker interacts with Cohere's /rerank API, requiring an API key for authentication.
The API key can be provided directly in the apiConfig
Map or through the
COHERE_API_KEY
environment variable.
Users must obtain an API key from https://dashboard.cohere.com/. Additionally, the
com.cohere:cohere-java
library must be available on the classpath.
Example usage:
// Initialize with API key
Map<String, String> apiConfig = Map.of("api_key", "your-api-key");
CohereReranker reranker = CohereReranker.builder()
.apiConfig(apiConfig)
.limit(3)
.build();
// Rerank string documents
List<String> docs = Arrays.asList("doc1", "doc2", "doc3");
RerankResult result = reranker.rank("query", docs);
// Rerank dict documents with rankBy fields
List<Map<String, Object>> dictDocs = Arrays.asList(
Map.of("content", "doc1", "source", "wiki"),
Map.of("content", "doc2", "source", "textbook")
);
CohereReranker reranker2 = CohereReranker.builder()
.apiConfig(apiConfig)
.rankBy(Arrays.asList("content", "source"))
.build();
RerankResult result2 = reranker2.rank("query", dictDocs);
- See Also:
-
Field Summary
Fields inherited from class com.redis.vl.utils.rerank.BaseReranker
limit, model, rankBy, returnScore
-
Method Summary
Modifier and TypeMethodDescriptionRerank documents based on query relevance using Cohere's Rerank API.Rerank documents based on query relevance using Cohere's Rerank API with runtime parameter overrides.Methods inherited from class com.redis.vl.utils.rerank.BaseReranker
getLimit, getModel, getRankBy, isReturnScore, validateDocs, validateQuery
-
Method Details
-
rank
Rerank documents based on query relevance using Cohere's Rerank API.- Specified by:
rank
in classBaseReranker
- Parameters:
query
- The search querydocs
- List of documents (either List<String> or List<Map<String, Object>>)- Returns:
- RerankResult with reranked documents and relevance scores
- Throws:
IllegalArgumentException
- if query or docs are invalid
-
rank
Rerank documents based on query relevance using Cohere's Rerank API with runtime parameter overrides.- Parameters:
query
- The search querydocs
- List of documents (either List<String> or List<Map<String, Object>>)kwargs
- Optional parameters to override defaults (limit, return_score, rank_by, max_chunks_per_doc)- Returns:
- RerankResult with reranked documents and relevance scores
- Throws:
IllegalArgumentException
- if query or docs are invalid
-