Skip to content

Commit

Permalink
Rename UseSessionId to UseStickySessions + refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
dluc committed Nov 12, 2024
1 parent 475e584 commit 32d3793
Show file tree
Hide file tree
Showing 10 changed files with 36 additions and 27 deletions.
2 changes: 1 addition & 1 deletion applications/tests/Evaluation.Tests/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@
// the requests across replicas and adversely affect the performance of the search service.
//
// See https://learn.microsoft.com/rest/api/searchservice/documents/search-post?view=rest-searchservice-2024-07-01&tabs=HTTP#request-body
"UseSessionId": false
"UseStickySessions": false
},
"OpenAI": {
// Name of the model used to generate text (text completion or chat completion)
Expand Down
2 changes: 1 addition & 1 deletion examples/002-dotnet-Serverless/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@
// the requests across replicas and adversely affect the performance of the search service.
//
// See https://learn.microsoft.com/rest/api/searchservice/documents/search-post?view=rest-searchservice-2024-07-01&tabs=HTTP#request-body
"UseSessionId": false
"UseStickySessions": false
},
"OpenAI": {
// Name of the model used to generate text (text completion or chat completion)
Expand Down
2 changes: 1 addition & 1 deletion examples/210-KM-without-builder/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@
// the requests across replicas and adversely affect the performance of the search service.
//
// See https://learn.microsoft.com/rest/api/searchservice/documents/search-post?view=rest-searchservice-2024-07-01&tabs=HTTP#request-body
"UseSessionId": false
"UseStickySessions": false
},
"AzureAIDocIntel": {
// "APIKey" or "AzureIdentity".
Expand Down
2 changes: 1 addition & 1 deletion examples/401-evaluation/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@
// the requests across replicas and adversely affect the performance of the search service.
//
// See https://learn.microsoft.com/rest/api/searchservice/documents/search-post?view=rest-searchservice-2024-07-01&tabs=HTTP#request-body
"UseSessionId": false
"UseStickySessions": false
},
"OpenAI": {
// Name of the model used to generate text (text completion or chat completion)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
// using the env vars AZURE_TENANT_ID, AZURE_CLIENT_ID, AZURE_CLIENT_SECRET.
"Auth": "AzureIdentity",
"Endpoint": "https://<...>",
"APIKey": ""
"APIKey": "",
"UseStickySessions": true
},
"OpenAI": {
// Name of the model used to generate text (text completion or chat completion)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public enum AuthTypes
///
/// See https://learn.microsoft.com/rest/api/searchservice/documents/search-post?view=rest-searchservice-2024-07-01&tabs=HTTP#request-body
/// </summary>
public bool UseSessionId { get; set; } = false;
public bool UseStickySessions { get; set; } = false;

public void SetCredential(TokenCredential credential)
{
Expand Down
41 changes: 24 additions & 17 deletions extensions/AzureAISearch/AzureAISearch/AzureAISearchMemory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public class AzureAISearchMemory : IMemoryDb, IMemoryDbUpsertBatch
private readonly ITextEmbeddingGenerator _embeddingGenerator;
private readonly ILogger<AzureAISearchMemory> _log;
private readonly bool _useHybridSearch;
private readonly bool _useSessionId;
private readonly bool _useStickySessions;

/// <summary>
/// Create a new instance
Expand All @@ -50,7 +50,7 @@ public AzureAISearchMemory(
this._embeddingGenerator = embeddingGenerator;
this._log = (loggerFactory ?? DefaultLogger.Factory).CreateLogger<AzureAISearchMemory>();
this._useHybridSearch = config.UseHybridSearch;
this._useSessionId = config.UseSessionId;
this._useStickySessions = config.UseStickySessions;

if (string.IsNullOrEmpty(config.Endpoint))
{
Expand Down Expand Up @@ -192,13 +192,12 @@ await client.IndexDocumentsAsync(
FilterMode = VectorFilterMode.PreFilter
}
};
this.ApplyCommonSearchOptions(options, withEmbeddings, filters);
options = this.PrepareSearchOptions(options, withEmbeddings, filters, limit);

if (limit > 0)
{
vectorQuery.KNearestNeighborsCount = limit;
options.Size = limit;
this._log.LogDebug("KNearestNeighborsCount and max results: {0}", limit);
this._log.LogDebug("KNearestNeighborsCount: {0}", limit);
}

Response<SearchResults<AzureAISearchMemoryRecord>>? searchResult = null;
Expand Down Expand Up @@ -246,14 +245,7 @@ public async IAsyncEnumerable<MemoryRecord> GetListAsync(
{
var client = this.GetSearchClient(index);

SearchOptions options = new();
this.ApplyCommonSearchOptions(options, withEmbeddings, filters);

if (limit > 0)
{
options.Size = limit;
this._log.LogDebug("Max results: {0}", limit);
}
SearchOptions options = this.PrepareSearchOptions(null, withEmbeddings, filters, limit);

Response<SearchResults<AzureAISearchMemoryRecord>>? searchResult = null;
try
Expand Down Expand Up @@ -601,14 +593,20 @@ at Azure.Search.Documents.SearchClient.SearchInternal[T](SearchOptions options,
return indexSchema;
}

private void ApplyCommonSearchOptions(
SearchOptions options,
private SearchOptions PrepareSearchOptions(
SearchOptions? options,
bool withEmbeddings,
ICollection<MemoryFilter>? filters = null)
ICollection<MemoryFilter>? filters = null,
int limit = 1)
{
options ??= new SearchOptions();

// Define which fields to fetch
options.Select.Add(AzureAISearchMemoryRecord.IdField);
options.Select.Add(AzureAISearchMemoryRecord.TagsField);
options.Select.Add(AzureAISearchMemoryRecord.PayloadField);

// Embeddings are fetched only when needed, to reduce latency and cost
if (withEmbeddings)
{
options.Select.Add(AzureAISearchMemoryRecord.VectorField);
Expand All @@ -633,10 +631,19 @@ private void ApplyCommonSearchOptions(
// Size = limit
// };

if (this._useSessionId)
if (limit > 0)
{
options.Size = limit;
this._log.LogDebug("Max results: {0}", limit);
}

// Decide whether to use a sticky session for the current request
if (this._useStickySessions)
{
options.SessionId = Guid.NewGuid().ToString("N");
}

return options;
}

private static double ScoreToCosineSimilarity(double score)
Expand Down
2 changes: 1 addition & 1 deletion service/Service/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@
// the requests across replicas and adversely affect the performance of the search service.
//
// See https://learn.microsoft.com/rest/api/searchservice/documents/search-post?view=rest-searchservice-2024-07-01&tabs=HTTP#request-body
"UseSessionId": false
"UseStickySessions": false
},
"AzureAIDocIntel": {
// "APIKey" or "AzureIdentity".
Expand Down
3 changes: 2 additions & 1 deletion service/tests/Core.FunctionalTests/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
"Auth": "AzureIdentity",
"Endpoint": "https://<...>",
"APIKey": "",
"UseHybridSearch": false
"UseHybridSearch": false,
"UseStickySessions": true
},
"LlamaSharp": {
"TextModel": {
Expand Down
4 changes: 2 additions & 2 deletions tools/InteractiveSetup/Services/AzureAISearch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public static void Setup(Context ctx, bool force = false)
{ "Auth", "ApiKey" },
{ "APIKey", "" },
{ "UseHybridSearch", false },
{ "UseSessionId", false }
{ "UseStickySessions", false }
};
}

Expand All @@ -46,6 +46,6 @@ public static void Setup(Context ctx, bool force = false)

AppSettings.Change(x => x.Services[ServiceName]["Endpoint"] = SetupUI.AskOpenQuestion("Azure AI Search <endpoint>", config["Endpoint"].ToString()));
AppSettings.Change(x => x.Services[ServiceName]["UseHybridSearch"] = SetupUI.AskBoolean("Use hybrid search (yes/no)?", (bool)config["UseHybridSearch"]));
AppSettings.Change(x => x.Services[ServiceName]["UseSessionId"] = SetupUI.AskBoolean("Use session ID (yes/no)?", (bool)config["UseSessionId"]));
AppSettings.Change(x => x.Services[ServiceName]["UseStickySessions"] = SetupUI.AskBoolean("Use sticky sessions (yes/no)?", (bool)config["UseStickySessions"]));
}
}

0 comments on commit 32d3793

Please sign in to comment.