-
Notifications
You must be signed in to change notification settings - Fork 591
Add test for chat service && pull out document search into an individual service #270
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
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
c6ba06b
commit
LittleLittleCloud f2c2add
revert some change
LittleLittleCloud d2bba38
update
LittleLittleCloud ce29264
update
LittleLittleCloud 33e28a3
use enum
LittleLittleCloud a6aadcf
Update app/shared/Shared/Services/AzureComputerVisionService.cs
LittleLittleCloud 332b25c
Update app/shared/Shared/Services/AzureComputerVisionService.cs
LittleLittleCloud b3432af
Update app/shared/Shared/Services/AzureComputerVisionService.cs
LittleLittleCloud af741e3
pull out retrivalmode into another file
LittleLittleCloud 27696b5
Merge branch 'u/xiaoyun/addTestToChatService' of https://github.com/A…
LittleLittleCloud 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
// Copyright (c) Microsoft. All rights reserved. | ||
|
||
namespace Shared.Models; | ||
|
||
/// <summary> | ||
/// retrieval mode for azure search service | ||
/// </summary> | ||
public enum RetrievalMode | ||
{ | ||
/// <summary> | ||
/// Text-only model, where only query will be used to retrieve the results | ||
/// </summary> | ||
Text = 0, | ||
|
||
/// <summary> | ||
/// Vector-only model, where only embeddings will be used to retrieve the results | ||
/// </summary> | ||
Vector, | ||
|
||
/// <summary> | ||
/// Text + Vector model, where both query and embeddings will be used to retrieve the results | ||
/// </summary> | ||
Hybrid, | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
using Azure; | ||
using Azure.Search.Documents; | ||
using Azure.Search.Documents.Models; | ||
using Shared.Models; | ||
|
||
public interface IDocumentService | ||
{ | ||
Task<SupportingContentRecord[]> QueryDocumentsAsync( | ||
string? query = null, | ||
float[]? embedding = null, | ||
RequestOverrides? overrides = null, | ||
CancellationToken cancellationToken = default); | ||
} | ||
|
||
public class AzureDocumentService(SearchClient searchClient) : IDocumentService | ||
{ | ||
public async Task<SupportingContentRecord[]> QueryDocumentsAsync( | ||
string? query = null, | ||
float[]? embedding = null, | ||
RequestOverrides? overrides = null, | ||
CancellationToken cancellationToken = default) | ||
{ | ||
if (query is null && embedding is null) | ||
IEvangelist marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
throw new ArgumentException("Either query or embedding must be provided"); | ||
} | ||
|
||
var documentContents = string.Empty; | ||
var top = overrides?.Top ?? 3; | ||
var exclude_category = overrides?.ExcludeCategory; | ||
var filter = exclude_category == null ? string.Empty : $"category ne '{exclude_category}'"; | ||
var useSemanticRanker = overrides?.SemanticRanker ?? false; | ||
var useSemanticCaptions = overrides?.SemanticCaptions ?? false; | ||
|
||
SearchOptions searchOptions = useSemanticRanker | ||
? new SearchOptions | ||
{ | ||
Filter = filter, | ||
QueryType = SearchQueryType.Semantic, | ||
SemanticSearch = new() | ||
{ | ||
SemanticConfigurationName = "default", | ||
QueryCaption = new(useSemanticCaptions | ||
? QueryCaptionType.Extractive | ||
: QueryCaptionType.None), | ||
}, | ||
// TODO: Find if these options are assignable | ||
//QueryLanguage = "en-us", | ||
//QuerySpeller = "lexicon", | ||
Size = top, | ||
} | ||
: new SearchOptions | ||
{ | ||
Filter = filter, | ||
Size = top, | ||
}; | ||
|
||
if (embedding != null && overrides?.RetrievalMode != RetrievalMode.Text) | ||
{ | ||
var k = useSemanticRanker ? 50 : top; | ||
var vectorQuery = new VectorizedQuery(embedding) | ||
{ | ||
// if semantic ranker is enabled, we need to set the rank to a large number to get more | ||
// candidates for semantic reranking | ||
KNearestNeighborsCount = useSemanticRanker ? 50 : top, | ||
}; | ||
vectorQuery.Fields.Add("embedding"); | ||
searchOptions.VectorSearch = new(); | ||
searchOptions.VectorSearch.Queries.Add(vectorQuery); | ||
} | ||
|
||
var searchResultResponse = await searchClient.SearchAsync<SearchDocument>( | ||
query, searchOptions, cancellationToken); | ||
if (searchResultResponse.Value is null) | ||
{ | ||
throw new InvalidOperationException("fail to get search result"); | ||
} | ||
|
||
SearchResults<SearchDocument> searchResult = searchResultResponse.Value; | ||
|
||
// Assemble sources here. | ||
// Example output for each SearchDocument: | ||
// { | ||
// "@search.score": 11.65396, | ||
// "id": "Northwind_Standard_Benefits_Details_pdf-60", | ||
// "content": "x-ray, lab, or imaging service, you will likely be responsible for paying a copayment or coinsurance. The exact amount you will be required to pay will depend on the type of service you receive. You can use the Northwind app or website to look up the cost of a particular service before you receive it.\nIn some cases, the Northwind Standard plan may exclude certain diagnostic x-ray, lab, and imaging services. For example, the plan does not cover any services related to cosmetic treatments or procedures. Additionally, the plan does not cover any services for which no diagnosis is provided.\nIt’s important to note that the Northwind Standard plan does not cover any services related to emergency care. This includes diagnostic x-ray, lab, and imaging services that are needed to diagnose an emergency condition. If you have an emergency condition, you will need to seek care at an emergency room or urgent care facility.\nFinally, if you receive diagnostic x-ray, lab, or imaging services from an out-of-network provider, you may be required to pay the full cost of the service. To ensure that you are receiving services from an in-network provider, you can use the Northwind provider search ", | ||
// "category": null, | ||
// "sourcepage": "Northwind_Standard_Benefits_Details-24.pdf", | ||
// "sourcefile": "Northwind_Standard_Benefits_Details.pdf" | ||
// } | ||
var sb = new List<SupportingContentRecord>(); | ||
foreach (var doc in searchResult.GetResults()) | ||
{ | ||
doc.Document.TryGetValue("sourcepage", out var sourcePageValue); | ||
string? contentValue; | ||
try | ||
{ | ||
if (useSemanticCaptions) | ||
{ | ||
var docs = doc.SemanticSearch.Captions.Select(c => c.Text); | ||
contentValue = string.Join(" . ", docs); | ||
} | ||
else | ||
{ | ||
doc.Document.TryGetValue("content", out var value); | ||
contentValue = (string)value; | ||
} | ||
} | ||
catch (ArgumentNullException) | ||
{ | ||
contentValue = null; | ||
} | ||
|
||
if (sourcePageValue is string sourcePage && contentValue is string content) | ||
{ | ||
content = content.Replace('\r', ' ').Replace('\n', ' '); | ||
sb.Add(new SupportingContentRecord(sourcePage, content)); | ||
} | ||
} | ||
|
||
return [.. sb]; | ||
} | ||
} |
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.
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.
Uh oh!
There was an error while loading. Please reload this page.