Skip to content

Commit f27899a

Browse files
Semantic Reranking: Refactors RerankResult.Document to return string type (#5528)
# Pull Request Template ## Description This pull request updates the handling of the `Document` property in the `RerankScore` class and its deserialization logic to consistently use a `string` type instead of a generic `object`. This change simplifies the type system and ensures that document content or identifiers are always represented as strings which is returned by the service. Type consistency and deserialization improvements: * Changed the `Document` property in the `RerankScore` class from `object` to `string`, and updated the constructor accordingly to require a `string` document. [[1]](diffhunk://#diff-8278775c5227f8e0f3aa61d4b0a96434387143cde3ea6a4e1d4445362a559530L21-R21) [[2]](diffhunk://#diff-8278775c5227f8e0f3aa61d4b0a96434387143cde3ea6a4e1d4445362a559530L39-R39) * Updated the deserialization logic in `DeserializeSemanticRerankResult` to always store the document as a string, either by using the raw JSON text for objects or the string value directly, ensuring type consistency throughout the codebase. ## Type of change Please delete options that are not relevant. - [] Bug fix (non-breaking change which fixes an issue) ## Closing issues To automatically close an issue: closes #IssueNumber
1 parent 17cfb96 commit f27899a

File tree

3 files changed

+14
-12
lines changed

3 files changed

+14
-12
lines changed

Microsoft.Azure.Cosmos/src/Inference/RerankScore.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ class RerankScore
1818
/// <summary>
1919
/// Gets the document content or identifier that was reranked.
2020
/// </summary>
21-
public object Document { get; }
21+
public string Document { get; }
2222

2323
/// <summary>
2424
/// Gets the score assigned to the document after reranking.
@@ -36,7 +36,7 @@ class RerankScore
3636
/// <param name="document">The document content or identifier.</param>
3737
/// <param name="score">The reranked score for the document.</param>
3838
/// <param name="index">The original index of the document.</param>
39-
public RerankScore(object document, double score, int index)
39+
public RerankScore(string document, double score, int index)
4040
{
4141
this.Document = document;
4242
this.Score = score;

Microsoft.Azure.Cosmos/src/Inference/SemanticRerankResult.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -83,17 +83,19 @@ internal static async Task<SemanticRerankResult> DeserializeSemanticRerankResult
8383
{
8484
foreach (JsonElement item in scoresElement.EnumerateArray())
8585
{
86-
object document = null;
86+
string document = string.Empty;
8787
if (item.TryGetProperty("document", out JsonElement docElement))
8888
{
8989
// Try to deserialize as an object
9090
switch (docElement.ValueKind)
9191
{
9292
case JsonValueKind.Object:
93-
document = JsonSerializer.Deserialize<Dictionary<string, object>>(docElement.GetRawText());
93+
document = docElement.GetRawText();
9494
break;
95-
case JsonValueKind.Null:
96-
document = null;
95+
case JsonValueKind.String:
96+
document = docElement.GetString();
97+
break;
98+
default:
9799
break;
98100
}
99101
}

Microsoft.Azure.Cosmos/tests/Microsoft.Azure.Cosmos.Tests/Contracts/DotNetPreviewSDKAPI.net6.json

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1452,22 +1452,22 @@
14521452
"Attributes": [],
14531453
"MethodInfo": "Int32 Index;CanRead:True;CanWrite:False;Int32 get_Index();IsAbstract:False;IsStatic:False;IsVirtual:False;IsGenericMethod:False;IsConstructor:False;IsFinal:False;"
14541454
},
1455-
"System.Object Document": {
1455+
"System.String Document": {
14561456
"Type": "Property",
14571457
"Attributes": [],
1458-
"MethodInfo": "System.Object Document;CanRead:True;CanWrite:False;System.Object get_Document();IsAbstract:False;IsStatic:False;IsVirtual:False;IsGenericMethod:False;IsConstructor:False;IsFinal:False;"
1458+
"MethodInfo": "System.String Document;CanRead:True;CanWrite:False;System.String get_Document();IsAbstract:False;IsStatic:False;IsVirtual:False;IsGenericMethod:False;IsConstructor:False;IsFinal:False;"
14591459
},
1460-
"System.Object get_Document()[System.Runtime.CompilerServices.CompilerGeneratedAttribute()]": {
1460+
"System.String get_Document()[System.Runtime.CompilerServices.CompilerGeneratedAttribute()]": {
14611461
"Type": "Method",
14621462
"Attributes": [
14631463
"CompilerGeneratedAttribute"
14641464
],
1465-
"MethodInfo": "System.Object get_Document();IsAbstract:False;IsStatic:False;IsVirtual:False;IsGenericMethod:False;IsConstructor:False;IsFinal:False;"
1465+
"MethodInfo": "System.String get_Document();IsAbstract:False;IsStatic:False;IsVirtual:False;IsGenericMethod:False;IsConstructor:False;IsFinal:False;"
14661466
},
1467-
"Void .ctor(System.Object, Double, Int32)": {
1467+
"Void .ctor(System.String, Double, Int32)": {
14681468
"Type": "Constructor",
14691469
"Attributes": [],
1470-
"MethodInfo": "Void .ctor(System.Object, Double, Int32)"
1470+
"MethodInfo": "Void .ctor(System.String, Double, Int32)"
14711471
}
14721472
},
14731473
"NestedTypes": {}

0 commit comments

Comments
 (0)