Commit 92ad3c6
authored
Semantic Rerank: Adds Semantic Rerank API (#5445)
# Pull Request Template
## Description
This pull request introduces a new semantic reranking feature to the
Azure Cosmos DB .NET SDK, enabling users to rerank documents using an
inference service that leverages Azure Active Directory (AAD)
authentication. The main changes include the addition of the
`InferenceService` class, new API surface for semantic reranking, and
appropriate integration into the SDK's authorization and client context
infrastructure. Notably, this functionality is only available when using
AAD authentication.
**Semantic Reranking Feature Integration:**
* Added the `InferenceService` class, which handles communication with
the Cosmos DB Inference Service for semantic reranking, including HTTP
client configuration, payload construction, and response handling. This
service enforces AAD authentication and manages its own authorization
and disposal.
* Introduced a new public (under `PREVIEW`) or internal API
`SemanticRerankAsync` to the `Container` class, allowing users to rerank
a list of documents based on a context/query string. This is implemented
in `ContainerInlineCore` and routed through the client context.
[[1]](diffhunk://#diff-e3b7704253edcfb63d18e851d9b0a8de3ea60889d704c363c2da1899a3ac39b7R1682-R1702)
[[2]](diffhunk://#diff-d7119df75f749b2d2ebcadc708f02d419663febceeaab4147a898c4be777e33dR700-R708)
* To use this feature, the environment variable
"AZURE_COSMOS_SEMANTIC_RERANKER_INFERENCE_ENDPOINT", must be set with
the inference endpoint from the service.
* Additionally, the environment variable
"AZURE_COSMOS_SEMANTIC_RERANKER_INFERENCE_SERVICE_MAX_CONNECTION_LIMIT",
can be set to change the inference client's max connection limit.
**Authorization and Token Handling Updates:**
* Extended the `AuthorizationTokenProvider` abstraction and its
implementations to support a new method,
`AddInferenceAuthorizationHeaderAsync`, which is only valid for
AAD-based token providers. Non-AAD providers throw a
`NotImplementedException` for this method.
[[1]](diffhunk://#diff-839842554f21dd8c2180a3c4f5a25abffdc392902a0f10f1130397f064d84db5R55-R60)
[[2]](diffhunk://#diff-93d0d6522a71c823da524b5cdceb024a8aae4497b14b383dcac41a98edf7092aR18-R29)
[[3]](diffhunk://#diff-93d0d6522a71c823da524b5cdceb024a8aae4497b14b383dcac41a98edf7092aR78-R92)
[[4]](diffhunk://#diff-1e0a2b64595baccd2b71f2328bd8f6ca9a81fb8175c5d489e5cdadba1b5c3a05R217-R221)
[[5]](diffhunk://#diff-cca89b75cc8e6136d1f595f20169546258617f88cf0b5e0e345554c073e06e78R95-R99)
[[6]](diffhunk://#diff-3ad037c5217ed55cda63aaa1a0e1897c9b1c6603b6e85b9649cdd8cf457ac6fbR128-R132)
**Client Context and Resource Management:**
* Updated `ClientContextCore` and `CosmosClientContext` to manage the
lifecycle of the `InferenceService`, including creation, caching, and
disposal. Added methods for invoking semantic reranking and for
retrieving or creating the inference service instance.
[[1]](diffhunk://#diff-a41317d6b53931e411d3091f58700758c53e80ca27dccedeafd76226852e58d2R38)
[[2]](diffhunk://#diff-a41317d6b53931e411d3091f58700758c53e80ca27dccedeafd76226852e58d2R472-R497)
[[3]](diffhunk://#diff-a41317d6b53931e411d3091f58700758c53e80ca27dccedeafd76226852e58d2R515)
[[4]](diffhunk://#diff-b0bd965dfed52d866ec53af3bbb3b7afb4f420cdda75ac9071515ac471c0f7baR8)
[[5]](diffhunk://#diff-b0bd965dfed52d866ec53af3bbb3b7afb4f420cdda75ac9071515ac471c0f7baR136-R148)
[[6]](diffhunk://#diff-a41317d6b53931e411d3091f58700758c53e80ca27dccedeafd76226852e58d2R8)
**Dependency Updates:**
* Added a dependency on the `Azure.Identity` package in the test project
to support AAD authentication scenarios.
Please delete options that are not relevant.
**Example**
```csharp
//Sample code to demonstrate Semantic Reranking
// Assume 'container' is an instance of Cosmos.Container
// This example queries items from a fitness store with full-text search and then reranks them semantically.
string search_text = "integrated pull-up bar";
string queryString = $@"
SELECT TOP 15 c.id, c.Name, c.Brand, c.Description
FROM c
WHERE FullTextContains(c.Description, ""{search_text}"")
ORDER BY RANK FullTextScore(c.Description, ""{search_text}"")
";
string reranking_context = "most economical with multiple pulley adjustmnets and ideal for home gyms";
List<string> documents = new List<string>();
FeedIterator<dynamic> resultSetIterator = container.GetItemQueryIterator<dynamic>(
new QueryDefinition(queryString),
requestOptions: new QueryRequestOptions()
{
MaxItemCount = 15,
});
while (resultSetIterator.HasMoreResults)
{
FeedResponse<dynamic> response = await resultSetIterator.ReadNextAsync();
foreach (JsonElement item in response)
{
documents.Add(item.ToString());
}
}
Dictionary<string, dynamic> options = new Dictionary<string, dynamic>
{
{ "return_documents", true },
{ "top_k", 10 },
{ "batch_size", 32 },
{ "sort", true }
};
SemanticRerankResult results = await container.SemanticRerankAsync(
reranking_context,
documents,
options);
// get the best resulting document from the query
results.RerankScores.First().Document;
// or the index of the document in the original list
results.RerankScores.First().Index;
// or the reranking score
results.RerankScores.First().Score;
// get the latency information from the reranking operation
Dictonary<string, object. latencyInfo = results.Latency;
// get the token usage information from the reranking operation
Dictonary<string, object> tokenUseageInfo = results.TokenUseage;
```
- [] New feature (non-breaking change which adds functionality)
## Closing issues
To automatically close an issue: closes #IssueNumber1 parent 4402de8 commit 92ad3c6
File tree
17 files changed
+834
-0
lines changed- Microsoft.Azure.Cosmos.Encryption.Custom/src
- Microsoft.Azure.Cosmos.Encryption/src
- Microsoft.Azure.Cosmos
- src
- Authorization
- Inference
- Resource
- Container
- tests
- Microsoft.Azure.Cosmos.EmulatorTests
- Microsoft.Azure.Cosmos.Tests/Contracts
17 files changed
+834
-0
lines changedLines changed: 15 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1008 | 1008 | | |
1009 | 1009 | | |
1010 | 1010 | | |
| 1011 | + | |
| 1012 | + | |
| 1013 | + | |
| 1014 | + | |
| 1015 | + | |
| 1016 | + | |
| 1017 | + | |
| 1018 | + | |
| 1019 | + | |
| 1020 | + | |
| 1021 | + | |
| 1022 | + | |
| 1023 | + | |
| 1024 | + | |
| 1025 | + | |
1011 | 1026 | | |
1012 | 1027 | | |
1013 | 1028 | | |
| |||
Lines changed: 15 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
732 | 732 | | |
733 | 733 | | |
734 | 734 | | |
| 735 | + | |
| 736 | + | |
| 737 | + | |
| 738 | + | |
| 739 | + | |
| 740 | + | |
| 741 | + | |
| 742 | + | |
| 743 | + | |
| 744 | + | |
| 745 | + | |
| 746 | + | |
| 747 | + | |
| 748 | + | |
| 749 | + | |
735 | 750 | | |
736 | 751 | | |
737 | 752 | | |
| |||
Lines changed: 6 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
52 | 52 | | |
53 | 53 | | |
54 | 54 | | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
55 | 61 | | |
56 | 62 | | |
57 | 63 | | |
| |||
Lines changed: 5 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
214 | 214 | | |
215 | 215 | | |
216 | 216 | | |
| 217 | + | |
| 218 | + | |
| 219 | + | |
| 220 | + | |
| 221 | + | |
217 | 222 | | |
218 | 223 | | |
219 | 224 | | |
| |||
Lines changed: 5 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
92 | 92 | | |
93 | 93 | | |
94 | 94 | | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
95 | 100 | | |
96 | 101 | | |
97 | 102 | | |
| |||
Lines changed: 19 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
15 | 15 | | |
16 | 16 | | |
17 | 17 | | |
| 18 | + | |
18 | 19 | | |
19 | 20 | | |
20 | 21 | | |
| 22 | + | |
| 23 | + | |
21 | 24 | | |
22 | 25 | | |
23 | 26 | | |
24 | 27 | | |
25 | 28 | | |
| 29 | + | |
26 | 30 | | |
27 | 31 | | |
28 | 32 | | |
| |||
71 | 75 | | |
72 | 76 | | |
73 | 77 | | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
74 | 93 | | |
75 | 94 | | |
76 | 95 | | |
| |||
Lines changed: 5 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
125 | 125 | | |
126 | 126 | | |
127 | 127 | | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
128 | 133 | | |
129 | 134 | | |
Lines changed: 209 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
| 178 | + | |
| 179 | + | |
| 180 | + | |
| 181 | + | |
| 182 | + | |
| 183 | + | |
| 184 | + | |
| 185 | + | |
| 186 | + | |
| 187 | + | |
| 188 | + | |
| 189 | + | |
| 190 | + | |
| 191 | + | |
| 192 | + | |
| 193 | + | |
| 194 | + | |
| 195 | + | |
| 196 | + | |
| 197 | + | |
| 198 | + | |
| 199 | + | |
| 200 | + | |
| 201 | + | |
| 202 | + | |
| 203 | + | |
| 204 | + | |
| 205 | + | |
| 206 | + | |
| 207 | + | |
| 208 | + | |
| 209 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
0 commit comments