Jump to content

Connect SuperML | Leeroopedia MCP: Equip your AI agents with best practices, code verification, and debugging knowledge. Powered by Leeroo — building Organizational Superintelligence. Contact us at founders@leeroo.com.

Implementation:Cohere ai Cohere python AwsRerank

From Leeroopedia
Knowledge Sources
Domains SDK, AWS, Reranking
Last Updated 2026-02-15 14:00 GMT

Overview

Implements data structures for document reranking results returned from Cohere rerank models deployed on AWS.

Description

The AwsRerank module provides the RerankDocument named tuple, RerankResult class, and Reranking container class for handling reranking outputs from Cohere models running on AWS SageMaker or Amazon Bedrock. RerankResult holds a relevance score, the original document index, and optionally the document content. The Reranking class parses raw API response dictionaries into a list of RerankResult objects and supports iteration, indexing, and standard string representations.

Usage

Use these classes when processing document reranking results from Cohere rerank models deployed on AWS. They are typically instantiated internally by the AWS client after calling a rerank endpoint. The results are ordered by relevance score, allowing callers to retrieve the most relevant documents for a given query.

Code Reference

Source Location

  • Repository: Cohere Python SDK
  • File: src/cohere/manually_maintained/cohere_aws/rerank.py

Signature

RerankDocument = NamedTuple("Document", [("text", str)])

class RerankResult(CohereObject):
    def __init__(
        self,
        document: Dict[str, Any] = None,
        index: int = None,
        relevance_score: float = None,
        *args, **kwargs,
    ) -> None: ...
    def __repr__(self) -> str: ...

class Reranking(CohereObject):
    def __init__(
        self,
        response: Optional[Dict[str, Any]] = None,
        **kwargs,
    ) -> None: ...
    def _results(self, response: Dict[str, Any]) -> List[RerankResult]: ...
    def __str__(self) -> str: ...
    def __repr__(self) -> str: ...
    def __iter__(self) -> Iterator: ...
    def __getitem__(self, index) -> RerankResult: ...

Import

from cohere.manually_maintained.cohere_aws.rerank import RerankDocument, RerankResult, Reranking

I/O Contract

RerankDocument (NamedTuple)

Field Type Description
text str The text content of the document.

RerankResult

Parameter Type Default Description
document Dict[str, Any] None A dictionary containing the document data (typically has a "text" key). May be None if document return was not requested.
index int None The zero-based index of this document in the original input list.
relevance_score float None The relevance score assigned by the rerank model (higher is more relevant).
Attribute Type Description
document Dict[str, Any] or None The document data dictionary.
index int The original index of this document.
relevance_score float The relevance score for this document.

Reranking

Parameter Type Description
response Dict[str, Any] The raw API response dictionary containing a "results" key. Each result must have "index" and "relevance_score" keys, and optionally a "document" key.
Method Return Type Description
__iter__() Iterator[RerankResult] Iterates over the contained RerankResult objects.
__getitem__(index) RerankResult Returns the RerankResult at the specified index.
__str__() str Returns a string representation of the results list.
__repr__() str Returns the repr of the results list.
Attribute Type Description
results List[RerankResult] The parsed list of RerankResult objects extracted from the response.

Usage Examples

from cohere.manually_maintained.cohere_aws.rerank import Reranking, RerankResult

# Parse a reranking response from an AWS endpoint
response = {
    "results": [
        {
            "document": {"text": "Paris is the capital of France."},
            "index": 0,
            "relevance_score": 0.98,
        },
        {
            "document": {"text": "Berlin is the capital of Germany."},
            "index": 2,
            "relevance_score": 0.45,
        },
        {
            "index": 1,
            "relevance_score": 0.12,
        },
    ]
}

reranking = Reranking(response=response)

# Iterate over results
for result in reranking:
    print(f"Index: {result.index}, Score: {result.relevance_score}")

# Access by index
top_result = reranking[0]
print(top_result.document)          # {"text": "Paris is the capital of France."}
print(top_result.relevance_score)   # 0.98

# Result without document data
no_doc_result = reranking[2]
print(no_doc_result.document)       # None
print(repr(no_doc_result))          # RerankResult<index: 1, relevance_score: 0.12>

Related Pages

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment