-
Notifications
You must be signed in to change notification settings - Fork 8
fix: AI, lifecycle improvements, etc #990
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 5 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
9f9b3db
wip improvements
BenjaminMichaelis 4dfac7f
wip
BenjaminMichaelis c7945f3
fix
BenjaminMichaelis fdc9525
update
BenjaminMichaelis bfc1140
update
BenjaminMichaelis 69b342c
cleanup
BenjaminMichaelis 91aa472
Fix PR review comments: health endpoint test, AppInsights config, Fil…
BenjaminMichaelis c9acf63
refactor(tests): DRY up test infrastructure
BenjaminMichaelis a0a2e19
fix: standardize AppInsights on APPLICATIONINSIGHTS_CONNECTION_STRING
BenjaminMichaelis 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,27 +1,54 @@ | ||
| using EssentialCSharp.Chat.Common.Models; | ||
| using System.Diagnostics; | ||
| using EssentialCSharp.Chat.Common.Models; | ||
| using Microsoft.Extensions.Logging; | ||
| using Microsoft.Extensions.VectorData; | ||
| using Npgsql; | ||
|
|
||
| namespace EssentialCSharp.Chat.Common.Services; | ||
|
|
||
| public class AISearchService(VectorStore vectorStore, EmbeddingService embeddingService) | ||
| public class AISearchService( | ||
| VectorStore vectorStore, | ||
| EmbeddingService embeddingService, | ||
| ILogger<AISearchService> logger) | ||
| { | ||
| // TODO: Implement Hybrid Search functionality, may need to switch db providers to support full text search? | ||
|
|
||
| public async Task<IAsyncEnumerable<VectorSearchResult<BookContentChunk>>> ExecuteVectorSearch(string query, string? collectionName = null) | ||
| public async Task<IReadOnlyList<VectorSearchResult<BookContentChunk>>> ExecuteVectorSearch( | ||
| string query, string? collectionName = null, CancellationToken cancellationToken = default) | ||
| { | ||
| collectionName ??= EmbeddingService.CollectionName; | ||
|
|
||
| VectorStoreCollection<string, BookContentChunk> collection = vectorStore.GetCollection<string, BookContentChunk>(collectionName); | ||
|
|
||
| ReadOnlyMemory<float> searchVector = await embeddingService.GenerateEmbeddingAsync(query); | ||
| ReadOnlyMemory<float> searchVector = await embeddingService.GenerateEmbeddingAsync(query, cancellationToken); | ||
|
|
||
| var vectorSearchOptions = new VectorSearchOptions<BookContentChunk> | ||
| { | ||
| VectorProperty = x => x.TextEmbedding, | ||
| }; | ||
|
|
||
| var searchResults = collection.SearchAsync(searchVector, options: vectorSearchOptions, top: 3); | ||
|
|
||
| return searchResults; | ||
| for (int attempt = 0; attempt <= 1; attempt++) | ||
| { | ||
| try | ||
| { | ||
| var results = new List<VectorSearchResult<BookContentChunk>>(); | ||
| await foreach (var result in collection.SearchAsync(searchVector, options: vectorSearchOptions, top: 3, cancellationToken: cancellationToken)) | ||
| { | ||
| results.Add(result); | ||
| } | ||
| return results; | ||
| } | ||
| catch (PostgresException ex) when (ex.SqlState == "28000" && attempt == 0) | ||
| { | ||
| // The pooled connection held an expired Entra ID token. Npgsql automatically | ||
| // removes the broken connection from the pool on error — no manual pool clearing | ||
| // needed (clearing would evict all healthy connections, hurting concurrent users). | ||
| // The retry opens a fresh physical connection, which calls UsePasswordProvider | ||
| // and gets a new token from DefaultAzureCredential. | ||
| logger.LogWarning(ex, "Entra ID token expired on pooled connection (SqlState 28000); retrying once."); | ||
| } | ||
| } | ||
|
|
||
| throw new UnreachableException("Retry loop exited without returning or throwing."); | ||
| } | ||
| } | ||
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.
Uh oh!
There was an error while loading. Please reload this page.