-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathAISearchService.cs
More file actions
54 lines (46 loc) · 2.34 KB
/
AISearchService.cs
File metadata and controls
54 lines (46 loc) · 2.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
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,
ILogger<AISearchService> logger)
{
// TODO: Implement Hybrid Search functionality, may need to switch db providers to support full text search?
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, cancellationToken);
var vectorSearchOptions = new VectorSearchOptions<BookContentChunk>
{
VectorProperty = x => x.TextEmbedding,
};
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.");
}
}