Skip to content
Merged
Show file tree
Hide file tree
Changes from 19 commits
Commits
Show all changes
42 commits
Select commit Hold shift + click to select a range
e9996a7
feat(cosmos): add semantic rerank API for Cosmos DB Inference Service
Apr 3, 2026
6205899
fix: add rerank to CSpell dictionary for cosmos API review
Apr 6, 2026
3372c72
fix: add named snippet for semanticRerank example
Apr 6, 2026
c364a51
Add semantic rerank integration test
Apr 6, 2026
b543c0b
fix: correct RerankScore.document type and update integration tests
Apr 8, 2026
d3d8848
fix: update integration tests with self-contained FTS+rerank e2e test
Apr 8, 2026
6cef892
fix: disable fakeTimers in integration config and simplify test 3
Apr 9, 2026
66f912a
Revert global fakeTimers override, use vi.useRealTimers() locally; st…
Apr 10, 2026
d0f5c75
Skip e2e integration test that requires pre-existing DB and container
Apr 10, 2026
d0bd299
Fix TSDoc escape errors in integration test JSDoc
Apr 10, 2026
8e4a686
Apply prettier formatting to integration tests
Apr 10, 2026
fa52444
Skip all semantic rerank integration tests
Apr 10, 2026
c9362d8
fix: address PR review comments for semantic rerank feature
Copilot Apr 10, 2026
0b39a3d
fix: use cached httpClient, ErrorResponse, remove unnecessary timer h…
Apr 12, 2026
4a9ca54
fix: update JSDoc snippet to use proper guard for rerankScores
Apr 13, 2026
327f057
refactor: remove inferenceEndpoint from CosmosClientOptions
Apr 13, 2026
26fc878
chore: remove accidentally committed .tshy build artifacts
Apr 13, 2026
783699b
fix: resolve no-shadow lint error in semanticRerank.spec.ts
Apr 13, 2026
3a833d7
feat: add documentType/targetPaths options and structured error parsing
Apr 14, 2026
ad6a614
refactor: rename rerankContext to context, convert SemanticRerankOpti…
Apr 17, 2026
ce691bb
refactor(cosmos): move inference constants, use StatusCodes, extract …
Apr 22, 2026
36c5605
refactor(cosmos): add MultipleChoices (300) to StatusCodes, use in In…
Apr 22, 2026
fed6fed
chore(cosmos): regenerate API review after constants and statusCodes …
Apr 22, 2026
45acf97
feat(cosmos): add inferenceEndpoint to CosmosClientOptions
Apr 23, 2026
ebe2235
feat(cosmos): add enablePreviewFeatures to CosmosClientOptions
Jun 23, 2026
f2178d0
refactor(cosmos): address semantic rerank PR review comments
Jun 24, 2026
ed78516
chore(cosmos): remove rerank entries from cspell dictionary
Jun 24, 2026
6a24a83
refactor(cosmos): simplify InferenceService.parseResponse error handling
Jun 29, 2026
4e770da
refactor(cosmos): finalize semantic rerank review follow-ups
Jul 3, 2026
3ce69e2
refactor(cosmos): structured error for semantic rerank failures
Jul 6, 2026
4e2e86b
feat(cosmos): add diagnostics to semantic rerank
Jul 6, 2026
ec48e18
feat(cosmos): nest semanticRerank preview config under enablePreviewF…
Jul 6, 2026
c4c335e
feat(cosmos): add configurable semantic rerank inference request timeout
Jul 6, 2026
4f55fb7
Merge remote-tracking branch 'origin/main' into feature/cosmos-semant…
Jul 12, 2026
caf0e7e
feat(cosmos): mark semantic rerank public APIs as @beta
Jul 12, 2026
869e40c
feat(cosmos): use nullish coalescing assignment for inference service…
Jul 15, 2026
40911f5
docs(cosmos): mark ClientContext.semanticRerank as @hidden
Jul 15, 2026
102b7f1
chore(cosmos): whitelist rerank/Rerank in cosmos cspell override
Jul 15, 2026
94b60dc
refactor(cosmos): rename SemanticRerankPreviewConfig to SemanticReran…
Jul 15, 2026
f3a2dc6
style(cosmos): apply prettier formatting to InferenceService.ts
Jul 15, 2026
013f307
docs(cosmos): add Preview features section to README
Jul 15, 2026
ebcb598
test(cosmos): add diagnostics coverage for semantic rerank
Jul 16, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .vscode/cspell.json
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,8 @@
"Parition",
"colls",
"pkranges",
"rerank",
"Rerank",
"sproc",
"sprocs",
"udfs",
Expand Down
29 changes: 29 additions & 0 deletions sdk/cosmosdb/cosmos/review/cosmos-node.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,7 @@ export class ClientContext {
diagnosticNode: DiagnosticNodeInternal;
partitionKeyRangeId?: string;
}): Promise<Response_2<T & Resource>>;
semanticRerank(rerankContext: string, documents: string[], options?: SemanticRerankOptions): Promise<SemanticRerankResult>;
Comment thread
aditishree1 marked this conversation as resolved.
Outdated
Comment thread
aditishree1 marked this conversation as resolved.
Outdated
// (undocumented)
upsert<T, U = T>(input: {
body: T;
Expand Down Expand Up @@ -731,6 +732,7 @@ export class Container {
readPartitionKeyRanges(feedOptions?: FeedOptions): QueryIterator<PartitionKeyRange>;
replace(body: ContainerDefinition, options?: RequestOptions): Promise<ContainerResponse>;
get scripts(): Scripts;
semanticRerank(rerankContext: string, documents: string[], options?: SemanticRerankOptions): Promise<SemanticRerankResult>;
Comment thread
aditishree1 marked this conversation as resolved.
get url(): string;
}

Expand Down Expand Up @@ -2133,6 +2135,13 @@ export interface RequestOptions extends SharedOptions {
urlConnection?: string;
}

// @public
export interface RerankScore {
document: string | null;
Comment thread
aditishree1 marked this conversation as resolved.
Outdated
index: number;
score: number;
}

// @public (undocumented)
export interface Resource {
_etag: string;
Expand Down Expand Up @@ -2377,6 +2386,26 @@ export class Scripts {
get userDefinedFunctions(): UserDefinedFunctions;
}

// @public
export interface SemanticRerankOptions {
Comment thread
aditishree1 marked this conversation as resolved.
Outdated
abortSignal?: AbortSignal;
additionalOptions?: Record<string, unknown>;
batchSize?: number;
documentType?: string;
Comment thread
aayush3011 marked this conversation as resolved.
Outdated
returnDocuments?: boolean;
sort?: boolean;
targetPaths?: string;
topK?: number;
Comment thread
aditishree1 marked this conversation as resolved.
Outdated
}

// @public
export interface SemanticRerankResult {
headers: Record<string, string>;
latency: Record<string, unknown> | undefined;
rerankScores: RerankScore[];
tokenUsage: Record<string, unknown> | undefined;
}

// @public
export function setAuthorizationTokenHeaderUsingMasterKey(verb: HTTPMethod, resourceId: string, resourceType: ResourceType, headers: CosmosHeaders, masterKey: string): Promise<void>;

Expand Down
45 changes: 45 additions & 0 deletions sdk/cosmosdb/cosmos/src/ClientContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ import {
AAD_AUTH_PREFIX,
AAD_RESOURCE_NOT_FOUND_ERROR,
} from "./common/constants.js";
import { InferenceService } from "./inference/InferenceService.js";
import type { SemanticRerankOptions } from "./inference/SemanticRerankOptions.js";
import type { SemanticRerankResult } from "./inference/SemanticRerankResult.js";

const logger: AzureLogger = createClientLogger("ClientContext");

Expand All @@ -70,6 +73,7 @@ export class ClientContext {
public partitionKeyRangeCache: PartitionKeyRangeCache;
/** boolean flag to support operations with client-side encryption */
public enableEncryption: boolean = false;
private inferenceService: InferenceService | null = null;

public constructor(
private cosmosClientOptions: CosmosClientOptions,
Expand Down Expand Up @@ -1108,4 +1112,45 @@ export class ClientContext {
this.globalEndpointManager.lastKnownPPCBEnabled
);
}

/**
* Rerank a list of documents using semantic reranking via the Cosmos DB Inference Service.
* This method uses a semantic reranker to score and reorder the provided documents
* based on their relevance to the given reranking context.
*
* The semantic reranking requests use a separate HTTP pipeline and do not use
* the default SDK retry policies.
*
* @param rerankContext - The context (e.g. query string) to use for reranking.
* @param documents - The documents to be reranked.
* @param options - Optional settings for the reranking request.
* @returns The reranking results including scores, latency, and token usage.
*/
public async semanticRerank(
Comment thread
aditishree1 marked this conversation as resolved.
rerankContext: string,
documents: string[],
options?: SemanticRerankOptions,
): Promise<SemanticRerankResult> {
const service = this.getOrCreateInferenceService();
return service.semanticRerank(rerankContext, documents, options);
}

/**
* Gets or lazily creates the InferenceService instance.
* @internal
*/
private getOrCreateInferenceService(): InferenceService {
if (!this.inferenceService) {
this.inferenceService = new InferenceService(this.cosmosClientOptions);
}
return this.inferenceService;
Comment thread
aditishree1 marked this conversation as resolved.
}

/**
* Disposes the InferenceService if it was created.
* @internal
*/
public disposeInferenceService(): void {
Comment thread
aditishree1 marked this conversation as resolved.
Outdated
this.inferenceService = null;
}
}
1 change: 1 addition & 0 deletions sdk/cosmosdb/cosmos/src/CosmosClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,7 @@ export class CosmosClient {
if (this.globalPartitionEndpointManager) {
this.globalPartitionEndpointManager.dispose();
}
this.clientContext.disposeInferenceService();
}

private async backgroundRefreshEndpointList(
Expand Down
60 changes: 60 additions & 0 deletions sdk/cosmosdb/cosmos/src/client/Container/Container.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ import { MetadataLookUpType } from "../../CosmosDiagnostics.js";
import type { EncryptionSettingForProperty } from "../../encryption/index.js";
import { EncryptionProcessor } from "../../encryption/index.js";
import type { EncryptionManager } from "../../encryption/EncryptionManager.js";
import type { SemanticRerankOptions } from "../../inference/SemanticRerankOptions.js";
import type { SemanticRerankResult } from "../../inference/SemanticRerankResult.js";

/**
* Operations for reading, replacing, or deleting a specific, existing container by id.
Expand Down Expand Up @@ -691,6 +693,64 @@ export class Container {
}
}

/**
* Rerank a list of documents using semantic reranking via the Cosmos DB Inference Service.
* This method uses a semantic reranker to score and reorder the provided documents
* based on their relevance to the given reranking context.
*
* The semantic reranking requests use a separate HTTP pipeline from the main Cosmos DB client
* and do not use the default SDK retry policies.
*
* To use this feature, you must:
* 1. Configure AAD authentication via `aadCredentials` in `CosmosClientOptions`
* 2. Set the `AZURE_COSMOS_SEMANTIC_RERANKER_INFERENCE_ENDPOINT` environment variable
*
* @param rerankContext - The context (e.g. query string) to use for reranking the documents.
* @param documents - A list of documents (as JSON strings) to be reranked.
* @param options - Optional settings for the reranking request.
* @returns The reranking results including scored documents, latency, and token usage.
*
* @example Semantic reranking of query results
* ```ts snippet:ContainerSemanticRerank
* import { DefaultAzureCredential } from "@azure/identity";
* import { CosmosClient } from "@azure/cosmos";
*
* const endpoint = "https://your-account.documents.azure.com";
* const aadCredentials = new DefaultAzureCredential();
* const client = new CosmosClient({
* endpoint,
* aadCredentials,
* });
*
* const { database } = await client.databases.createIfNotExists({ id: "Test Database" });
* const { container } = await database.containers.createIfNotExists({ id: "Test Container" });
*
* const queryResults = ["doc1 JSON", "doc2 JSON", "doc3 JSON"];
* const result = await container.semanticRerank(
* "most economical with multiple adjustments",
* queryResults,
* { returnDocuments: true, topK: 10, sort: true },
* );
* // Access the top-ranked document
* if (result.rerankScores.length > 0) {
* const topResult = result.rerankScores[0];
* const topScore = topResult.score;
* const topDocument = topResult.document;
* if (topDocument !== null) {
* console.log("Top-ranked document:", topDocument);
* }
* console.log("Top score:", topScore);
* }
* ```
*/
public async semanticRerank(
Comment thread
aditishree1 marked this conversation as resolved.
rerankContext: string,
documents: string[],
options?: SemanticRerankOptions,
): Promise<SemanticRerankResult> {
return this.clientContext.semanticRerank(rerankContext, documents, options);
}

/**
* @internal
*/
Expand Down
6 changes: 6 additions & 0 deletions sdk/cosmosdb/cosmos/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,3 +164,9 @@ export {
type CosmosEncryptedNumber,
CosmosEncryptedNumberType,
} from "./encryption/index.js";

export type {
RerankScore,
SemanticRerankResult,
SemanticRerankOptions,
} from "./inference/index.js";
Loading
Loading