-
Notifications
You must be signed in to change notification settings - Fork 524
Semantic Rerank: Adds Semantic Rerank API #5445
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
NaluTripician
wants to merge
17
commits into
master
Choose a base branch
from
users/nalutripician/semanticRerank
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 11 commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
7f732e5
inital commit
NaluTripician 11c81aa
updates
NaluTripician bad23ca
tests
NaluTripician f396716
test changes
NaluTripician 697d9eb
Merge branch 'master' into users/nalutripician/semanticRerank
NaluTripician 02fc277
auth changes
NaluTripician e16e988
test changes
NaluTripician 3ad012b
Update Microsoft.Azure.Cosmos.sln
NaluTripician f2e0e5b
fixed auth issues
NaluTripician db15b93
Merge branch 'master' into users/nalutripician/semanticRerank
NaluTripician e42274e
Merge branch 'master' into users/nalutripician/semanticRerank
NaluTripician 8923888
addresses PR comments
NaluTripician 5437698
Merge branch 'master' into users/nalutripician/semanticRerank
NaluTripician 4a5afaf
Merge branch 'master' into users/nalutripician/semanticRerank
NaluTripician c05501e
test Fix
NaluTripician 76f9ce1
Adds Semantic rerank result
NaluTripician 8fba1a1
fixed typo
NaluTripician File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
162 changes: 162 additions & 0 deletions
162
Microsoft.Azure.Cosmos/src/Inference/InferenceService.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,162 @@ | ||
| //------------------------------------------------------------ | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. | ||
| //------------------------------------------------------------ | ||
|
|
||
| namespace Microsoft.Azure.Cosmos | ||
| { | ||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Linq; | ||
| using System.Net.Http; | ||
| using System.Net.Http.Headers; | ||
| using System.Text; | ||
| using System.Threading; | ||
| using System.Threading.Tasks; | ||
| using global::Azure.Core; | ||
| using Microsoft.Azure.Documents; | ||
| using Microsoft.Azure.Documents.Collections; | ||
|
|
||
| internal class InferenceService : IDisposable | ||
| { | ||
| private const string basePath = "dbinference.azure.com/inference/semanticReranking"; | ||
| private const string inferenceUserAgent = "cosmos-inference-dotnet"; | ||
| private const string inferenceServiceDefaultScope = "https://dbinference.azure.com/.default"; | ||
|
|
||
| private readonly Uri inferenceEndpoint; | ||
| private readonly HttpClient httpClient; | ||
| private readonly AuthorizationTokenProvider cosmosAuthorization; | ||
|
|
||
| private bool disposedValue; | ||
|
|
||
| public InferenceService(CosmosClient client, AccountProperties accountProperties) | ||
| { | ||
| //Create HttpClient | ||
| HttpMessageHandler httpMessageHandler = CosmosHttpClientCore.CreateHttpClientHandler( | ||
| gatewayModeMaxConnectionLimit: client.DocumentClient.ConnectionPolicy.MaxConnectionLimit, | ||
| webProxy: null, | ||
| serverCertificateCustomValidationCallback: client.DocumentClient.ConnectionPolicy.ServerCertificateCustomValidationCallback); | ||
|
|
||
| this.httpClient = new HttpClient(httpMessageHandler); | ||
|
|
||
| this.CreateClientHelper(this.httpClient); | ||
|
|
||
| //Set endpoints | ||
| this.inferenceEndpoint = new Uri($"https://{accountProperties.Id}.{basePath}"); | ||
|
|
||
| //set authorization | ||
| if (client.DocumentClient.cosmosAuthorization.GetType() != typeof(AuthorizationTokenProviderTokenCredential)) | ||
| { | ||
| throw new InvalidOperationException("InferenceService only supports AAD authentication."); | ||
| } | ||
|
|
||
| AuthorizationTokenProviderTokenCredential defaultOperationTokenProvider = client.DocumentClient.cosmosAuthorization as AuthorizationTokenProviderTokenCredential; | ||
| TokenCredential tokenCredential = defaultOperationTokenProvider.tokenCredential; | ||
|
|
||
| this.cosmosAuthorization = new AuthorizationTokenProviderTokenCredential( | ||
| tokenCredential: tokenCredential, | ||
| accountEndpoint: new Uri(inferenceServiceDefaultScope), | ||
| backgroundTokenCredentialRefreshInterval: client.ClientOptions?.TokenCredentialBackgroundRefreshInterval); | ||
| } | ||
|
|
||
| public async Task<IReadOnlyDictionary<string, dynamic>> SemanticRerankAsync( | ||
| string renrankContext, | ||
| IEnumerable<string> documents, | ||
| SemanticRerankRequestOptions options = null, | ||
NaluTripician marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| CancellationToken cancellationToken = default) | ||
| { | ||
| HttpRequestMessage message = new HttpRequestMessage(HttpMethod.Post, this.inferenceEndpoint); | ||
| INameValueCollection additionalHeaders = new RequestNameValueCollection(); | ||
| await this.cosmosAuthorization.AddInferenceAuthorizationHeaderAsync( | ||
| headersCollection: additionalHeaders, | ||
| this.inferenceEndpoint, | ||
| HttpConstants.HttpMethods.Post, | ||
| AuthorizationTokenType.AadToken); | ||
| additionalHeaders.Add(HttpConstants.HttpHeaders.UserAgent, inferenceUserAgent); | ||
|
|
||
| foreach (string key in additionalHeaders.AllKeys()) | ||
| { | ||
| message.Headers.Add(key, additionalHeaders[key]); | ||
| } | ||
|
|
||
| Dictionary<string, dynamic> body = this.AddSemanticRerankPayload(renrankContext, documents, options); | ||
|
|
||
| message.Content = new StringContent( | ||
| Newtonsoft.Json.JsonConvert.SerializeObject(body), | ||
| Encoding.UTF8, | ||
| RuntimeConstants.MediaTypes.Json); | ||
|
|
||
| HttpResponseMessage responseMessage = await this.httpClient.SendAsync(message, cancellationToken); | ||
| responseMessage.EnsureSuccessStatusCode(); | ||
|
|
||
| // return the content of the responsemessage as a dictonary | ||
| string content = await responseMessage.Content.ReadAsStringAsync(); | ||
| return Newtonsoft.Json.JsonConvert.DeserializeObject<Dictionary<string, dynamic>>(content); | ||
| } | ||
|
|
||
| private void CreateClientHelper(HttpClient httpClient) | ||
| { | ||
| httpClient.Timeout = TimeSpan.FromSeconds(120); | ||
| httpClient.DefaultRequestHeaders.CacheControl = new CacheControlHeaderValue { NoCache = true }; | ||
|
|
||
| // Set requested API version header that can be used for | ||
| // version enforcement. | ||
| httpClient.DefaultRequestHeaders.Add(HttpConstants.HttpHeaders.Version, | ||
| HttpConstants.Versions.CurrentVersion); | ||
|
|
||
| httpClient.DefaultRequestHeaders.Add(HttpConstants.HttpHeaders.Accept, RuntimeConstants.MediaTypes.Json); | ||
| } | ||
|
|
||
| private Dictionary<string, dynamic> AddSemanticRerankPayload(string rerankContext, IEnumerable<string> documents, SemanticRerankRequestOptions options) | ||
| { | ||
| Dictionary<string, dynamic> payload = new Dictionary<string, dynamic> | ||
| { | ||
| { "query", rerankContext }, | ||
| { "documents", documents.ToArray() } | ||
| }; | ||
|
|
||
| if (options == null) | ||
| { | ||
| return payload; | ||
| } | ||
|
|
||
| payload["return_documents"] = options.ReturnDocuments; | ||
| if (options.TopK > -1) | ||
| { | ||
| payload["top_k"] = options.TopK; | ||
| } | ||
| if (options.BatchSize > -1) | ||
| { | ||
| payload["batch_size"] = options.BatchSize; | ||
| } | ||
| payload["sort"] = options.Sort; | ||
| if (!string.IsNullOrEmpty(options.DocumentType)) | ||
| { | ||
| payload["document_type"] = options.DocumentType; | ||
| } | ||
| if (!string.IsNullOrEmpty(options.TargetPaths)) | ||
| { | ||
| payload["target_paths"] = options.TargetPaths; | ||
| } | ||
|
|
||
| return payload; | ||
| } | ||
|
|
||
| protected void Dispose(bool disposing) | ||
| { | ||
| if (!this.disposedValue) | ||
| { | ||
| if (disposing) | ||
| { | ||
| this.httpClient.Dispose(); | ||
| } | ||
|
|
||
| this.disposedValue = true; | ||
| } | ||
| } | ||
|
|
||
| public void Dispose() | ||
| { | ||
| this.Dispose(true); | ||
| } | ||
| } | ||
| } | ||
43 changes: 43 additions & 0 deletions
43
Microsoft.Azure.Cosmos/src/RequestOptions/SemanticRerankRequestOptions.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| //------------------------------------------------------------ | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. | ||
| //------------------------------------------------------------ | ||
|
|
||
| namespace Microsoft.Azure.Cosmos | ||
| { | ||
| /// <summary> | ||
| /// Request options for semantic rerank operations in Azure Cosmos DB. | ||
| /// </summary> | ||
| public class SemanticRerankRequestOptions : RequestOptions | ||
NaluTripician marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| { | ||
| /// <summary> | ||
| /// Gets or sets a value indicating whether to return the documents text in the response. Default is true. | ||
| /// </summary> | ||
| public bool ReturnDocuments { get; set; } = true; | ||
|
|
||
| /// <summary> | ||
| /// Gets or sets the number of top documents to return. Default all documents are returned. | ||
| /// </summary> | ||
| public int TopK { get; set; } = -1; | ||
|
|
||
| /// <summary> | ||
| /// Batch size for internal scoring operations | ||
| /// </summary> | ||
| public int BatchSize { get; set; } = -1; | ||
|
|
||
| /// <summary> | ||
| /// Whether to sort the results by relevance score in descending order. | ||
| /// </summary> | ||
| public bool Sort { get; set; } = true; | ||
|
|
||
| /// <summary> | ||
| /// Type of document being processed. Supported values are "string" and "json". | ||
| /// </summary> | ||
| public string DocumentType { get; set; } | ||
|
|
||
| /// <summary> | ||
| /// If document type is "json", the list of JSON paths to extract text from for reranking. Comma separated string. | ||
| /// </summary> | ||
| public string TargetPaths { get; set; } | ||
|
|
||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As discussed offline, please add an environment variable where the inference endpoint can be set up.
AZURE_COSMOS_SEMANTIC_RERANKER_INFERENCE_ENDPOINT