Skip to content

[Cosmos]: Adding Semantic Reranker API#42991

Merged
aayush3011 merged 26 commits intoAzure:mainfrom
aayush3011:users/akataria/semanticReranking
Oct 2, 2025
Merged

[Cosmos]: Adding Semantic Reranker API#42991
aayush3011 merged 26 commits intoAzure:mainfrom
aayush3011:users/akataria/semanticReranking

Conversation

@aayush3011
Copy link
Copy Markdown
Member

@aayush3011 aayush3011 commented Sep 12, 2025

Description

This PR adds semantic reranking functionality to the Azure Cosmos DB Python SDK, enabling developers to leverage document reranking capabilities directly from their Cosmos DB containers.

Core Implementation

  • New semantic_rerank() method on ContainerProxy class for performing semantic reranking operations
  • Dedicated inference pipeline to communicate with Azure's inference service
  • Custom authentication policy InferenceServiceBearerTokenPolicy that preserves standard Bearer token format needed for Inference service.

API Design

  • Public Method: ContainerProxy.semantic_rerank() - Developer-facing API
  • Internal Method: CosmosClientConnection.SemanticRerank() - Handles pipeline communication
  • Infrastructure: _SemanticReranker class manages endpoint construction and pipeline setup

Example usage

results = container.semantic_rerank(
    reranking_context="What is the capital of France?",
    documents=[
          "Berlin is the capital of Germany.",
          "Paris is the capital of France.",
          "Madrid is the capital of Spain."
    ],
    semantic_reranking_options={
        "return_documents": True,
        "top_k": 10,
        "batch_size": 32,
        "sort": True
    }
)

Result

{
  "Scores": [
    {
      "index": 1,
      "document": "Paris is the capital of France.",
      "score": 0.9921875
    },
    {
      "index": 2,
      "document": "Madrid is the capital of Spain.",
      "score": 0.0024719238
    },
    {
      "index": 0,
      "document": "Berlin is the capital of Germany.",
      "score": 0.001411438
    }
  ],
  "latency": {
    "data_preprocess_time": 1.9073486e-06,
    "inference_time": 0.021552563,
    "postprocess_time": 1.1920929e-06
  },
  "token_usage": {
    "total_tokens": 405
  }
}

All SDK Contribution checklist:

  • The pull request does not introduce [breaking changes]
  • CHANGELOG is updated for new features, bug fixes or other significant changes.
  • I have read the contribution guidelines.

General Guidelines and Best Practices

  • Title of the pull request is clear and informative.
  • There are a small number of commits, each of which have an informative message. This means that previously merged commits do not appear in the history of the PR. For more information on cleaning up the commits in your PR, see this page.

Testing Guidelines

  • Pull request includes test coverage for the included changes.

@aayush3011 aayush3011 requested a review from a team as a code owner September 12, 2025 21:01
Copilot AI review requested due to automatic review settings September 12, 2025 21:01
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR adds semantic reranking functionality to the Azure Cosmos DB Python SDK, enabling developers to leverage document reranking capabilities directly from their Cosmos DB containers by integrating with Azure's inference service.

  • Implements a new semantic_rerank() method on the ContainerProxy class for performing semantic reranking operations
  • Creates a dedicated inference pipeline with custom authentication to communicate with Azure's inference service
  • Adds comprehensive test coverage for the new semantic reranking functionality

Reviewed Changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
azure/cosmos/container.py Adds the public semantic_rerank() method to ContainerProxy class
azure/cosmos/_semantic_reranker.py Core implementation of semantic reranking client with inference pipeline
azure/cosmos/_inference_auth_policy.py Custom Bearer token authentication policy for inference service
azure/cosmos/_cosmos_client_connection.py Integrates semantic reranker into client connection infrastructure
tests/test_semantic_reranker.py Test suite for semantic reranking functionality

Comment thread sdk/cosmos/azure-cosmos/azure/cosmos/container.py Outdated
Comment thread sdk/cosmos/azure-cosmos/azure/cosmos/_semantic_reranker.py Outdated
Comment thread sdk/cosmos/azure-cosmos/azure/cosmos/_semantic_reranker.py Outdated
aayush3011 and others added 3 commits September 12, 2025 14:03
@github-actions
Copy link
Copy Markdown

github-actions Bot commented Sep 12, 2025

API Change Check

APIView identified API level changes in this PR and created the following API reviews

azure-cosmos

Copy link
Copy Markdown
Member

@Pilchie Pilchie left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks fine to me so far.

Comment thread sdk/cosmos/azure-cosmos/azure/cosmos/container.py Outdated
Comment thread sdk/cosmos/azure-cosmos/azure/cosmos/_semantic_reranker.py Outdated
Comment thread sdk/cosmos/azure-cosmos/azure/cosmos/_semantic_reranker.py Outdated
@aayush3011
Copy link
Copy Markdown
Member Author

/azp run python - cosmos - tests

@azure-pipelines
Copy link
Copy Markdown

Azure Pipelines successfully started running 1 pipeline(s).

Comment thread sdk/cosmos/azure-cosmos/azure/cosmos/_semantic_reranker.py Outdated
Comment thread sdk/cosmos/azure-cosmos/azure/cosmos/_semantic_reranker.py Outdated
@aayush3011 aayush3011 changed the title Adding Semantic Reranker API [Cosmos]: Adding Semantic Reranker API Sep 16, 2025
Comment thread sdk/cosmos/azure-cosmos/azure/cosmos/aio/_container.py Outdated
Comment thread sdk/cosmos/azure-cosmos/CHANGELOG.md Outdated
Comment thread sdk/cosmos/azure-cosmos/azure/cosmos/_inference_service.py
Comment thread sdk/cosmos/azure-cosmos/azure/cosmos/_inference_service.py
Copy link
Copy Markdown
Member

@simorenoh simorenoh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just some small questions and changes, thanks Aayush!

Comment thread sdk/cosmos/azure-cosmos/azure/cosmos/_inference_service.py
Comment thread sdk/cosmos/azure-cosmos/azure/cosmos/_inference_service.py Outdated
Comment thread sdk/cosmos/azure-cosmos/azure/cosmos/aio/_container.py Outdated
Comment thread sdk/cosmos/azure-cosmos/azure/cosmos/_inference_service.py
Comment thread sdk/cosmos/azure-cosmos/azure/cosmos/_inference_service.py Outdated
Comment thread sdk/cosmos/azure-cosmos/azure/cosmos/aio/_inference_service_async.py Outdated
Comment thread sdk/cosmos/azure-cosmos/azure/cosmos/_inference_service.py Outdated
Copy link
Copy Markdown

@raj-shah14 raj-shah14 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me

@aayush3011
Copy link
Copy Markdown
Member Author

/azp run python - cosmos - ci

@azure-pipelines
Copy link
Copy Markdown

Azure Pipelines successfully started running 1 pipeline(s).

@aayush3011 aayush3011 merged commit fa629aa into Azure:main Oct 2, 2025
21 checks passed
@aayush3011 aayush3011 deleted the users/akataria/semanticReranking branch March 2, 2026 18:11
@xinlian12
Copy link
Copy Markdown
Member

@sdkReviewAgent-2

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

9 participants