Skip to main content
Cohere

Search documentation

Type to search this documentation.

On this pageOverview

Reranking - quickstart

Cohere's reranking models are available via the Rerank endpoint. This endpoint provides a powerful semantic boost to the search quality of any keyword or vector search system.

This quickstart guide shows you how to perform reranking with the Rerank endpoint.

  1. Setup

    First, install the Cohere Python SDK with the following command.

    Bash
    pip install -U cohere

    Next, import the library and create a client.

    PYTHON
    import cohere
    
    co = cohere.ClientV2(
        "COHERE_API_KEY"
    )  # Get your free API key here: https://dashboard.cohere.com/api-keys
    PYTHON
    import cohere
    
    co = cohere.ClientV2(
        api_key="",  # Leave this blank
        base_url="<YOUR_DEPLOYMENT_URL>",
    )
    PYTHON
    import cohere
    
    co = cohere.BedrockClientV2(
        aws_region="AWS_REGION",
        aws_access_key="AWS_ACCESS_KEY_ID",
        aws_secret_key="AWS_SECRET_ACCESS_KEY",
        aws_session_token="AWS_SESSION_TOKEN",
    )
    
    # Get the model name: https://docs.aws.amazon.com/bedrock/latest/userguide/models-supported.html
    PYTHON
    import cohere
    
    co = cohere.SagemakerClientV2(
        aws_region="AWS_REGION",
        aws_access_key="AWS_ACCESS_KEY_ID",
        aws_secret_key="AWS_SECRET_ACCESS_KEY",
        aws_session_token="AWS_SESSION_TOKEN",
    )
    PYTHON
    import cohere
    
    co = cohere.ClientV2(
        api_key="AZURE_API_KEY",
        base_url="AZURE_ENDPOINT",  # example: "https://cohere-command-r-plus-08-2024-xyz.eastus.models.ai.azure.com/"
    )
  2. Retrieved Documents

    First, define the list of documents to be reranked.

    PYTHON
    documents = [
        "Reimbursing Travel Expenses: Easily manage your travel expenses by submitting them through our finance tool. Approvals are prompt and straightforward.",
        "Working from Abroad: Working remotely from another country is possible. Simply coordinate with your manager and ensure your availability during core hours.",
        "Health and Wellness Benefits: We care about your well-being and offer gym memberships, on-site yoga classes, and comprehensive health insurance.",
        "Performance Reviews Frequency: We conduct informal check-ins every quarter and formal performance reviews twice a year.",
    ]
  3. Reranking

    Then, perform reranking by passing the documents and the user query to the Rerank endpoint.

    PYTHON
    # Add the user query
    query = "Are there fitness-related perks?"
    
    # Rerank the documents
    
    results = co.rerank(
        model="rerank-v4.0-pro", query=query, documents=documents, top_n=2
    )
    
    for result in results.results:
        print(result)
    PYTHON
    # Add the user query
    query = "Are there fitness-related perks?"
    
    # Rerank the documents
    results = co.rerank(
        model="rerank-v4.0-pro", query=query, documents=documents, top_n=2
    )
    
    for result in results.results:
        print(result)
    PYTHON
    # Add the user query
    query = "Are there fitness-related perks?"
    
    # Rerank the documents
    
    results = co.rerank(
        model="YOUR_MODEL_NAME", query=query, documents=documents, top_n=2
    )
    
    for result in results.results:
        print(result)
    PYTHON
    # Add the user query
    query = "Are there fitness-related perks?"
    
    # Rerank the documents
    results = co.rerank(
        model="YOUR_ENDPOINT_NAME",
        query=query,
        documents=documents,
        top_n=2,
    )
    
    for result in results.results:
        print(result)
    PYTHON
    # Add the user query
    query = "Are there fitness-related perks?"
    
    # Rerank the documents
    
    results = co.rerank(
        model="model",  # Pass a dummy string
        query=query,
        documents=documents,
        top_n=2,
    )
    
    for result in results.results:
        print(result)
    wordWrap
    document=None index=2 relevance_score=0.115670934
    document=None index=1 relevance_score=0.01729751
Suggest an edit

Propose a replacement for this page. The site team reviews it before applying any changes.

Export
Documentation menu