-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathProgram.cs
More file actions
131 lines (114 loc) · 4.01 KB
/
Copy pathProgram.cs
File metadata and controls
131 lines (114 loc) · 4.01 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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
using HybridRrf;
using HybridRrf.Data;
using Microsoft.EntityFrameworkCore;
using ParadeDB.EntityFrameworkCore.Extensions;
using Pgvector;
using Pgvector.EntityFrameworkCore;
using Shared;
var options = new DbContextOptionsBuilder<AppDbContext>()
.UseNpgsql(
ExampleSetup.ConnectionString,
o =>
{
o.UseParadeDb();
o.UseVector();
}
)
.UseSnakeCaseNamingConvention()
.Options;
await using var dbContext = new AppDbContext(options);
Console.WriteLine(new string('=', 70));
Console.WriteLine("Hybrid Search with Reciprocal Rank Fusion (RRF)");
Console.WriteLine(new string('=', 70));
Console.WriteLine("\nBM25 (keyword) + Vector (semantic)");
Console.WriteLine("RRF formula: score = sum(1 / (k + rank)) across all rankings");
await ExampleSetup.SetupHybridAsync(dbContext);
await LoadEmbeddingsAsync(dbContext);
await Demo(dbContext, "running shoes", QueryEmbeddings.Values);
await Demo(dbContext, "footwear for exercise", QueryEmbeddings.Values);
await Demo(dbContext, "wireless earbuds", QueryEmbeddings.Values);
Console.WriteLine("\n" + new string('=', 70));
Console.WriteLine("BM25 results use the ParadeDB EF query builder.");
Console.WriteLine(new string('=', 70));
return;
static async Task Demo(AppDbContext db, string query, Dictionary<string, float[]> queryEmbeddings)
{
var results = await HybridSearch(db, query, new Vector(queryEmbeddings[query]));
DisplayResults(query, results);
}
static async Task<List<(string Description, double RrfScore)>> HybridSearch(
AppDbContext db,
string query,
Vector queryEmbedding,
int topK = 20,
int rrfK = 60,
int limit = 5
)
{
var fulltext = await db
.MockItems.Where(x => EF.Functions.MatchAll(x.Description, query))
.Select(x => new
{
x.Id,
x.Description,
Score = EF.Functions.Score(x.Id),
})
.OrderByDescending(x => x.Score)
.Take(topK)
.ToListAsync();
var semantic = await db
.MockItems.Where(x => x.Embedding != null)
.Select(x => new
{
x.Id,
x.Description,
Distance = x.Embedding!.CosineDistance(queryEmbedding),
})
.OrderBy(x => x.Distance)
.Take(topK)
.ToListAsync();
return fulltext
.Select((x, index) => (x.Id, x.Description, Score: 1.0 / (rrfK + index + 1)))
.Concat(
semantic.Select((x, index) => (x.Id, x.Description, Score: 1.0 / (rrfK + index + 1)))
)
.GroupBy(x => x.Id)
.Select(x => (x.First().Description, RrfScore: x.Sum(y => y.Score)))
.OrderByDescending(x => x.RrfScore)
.Take(limit)
.ToList();
}
static void DisplayResults(string query, List<(string Description, double RrfScore)> results)
{
Console.WriteLine($"\n{new string('=', 70)}");
Console.WriteLine($"Query: '{query}'");
Console.WriteLine(new string('=', 70));
for (var i = 0; i < results.Count; i++)
{
var desc = results[i].Description[..Math.Min(60, results[i].Description.Length)];
Console.WriteLine($" {i + 1}. {desc, -60} (RRF: {results[i].RrfScore:F4})");
}
}
static async Task LoadEmbeddingsAsync(AppDbContext db)
{
var csvPath = Path.Combine(AppContext.BaseDirectory, "HybridRrf", "mock_items_embeddings.csv");
var lines = await File.ReadAllLinesAsync(csvPath);
var embeddings = new Dictionary<int, float[]>();
for (var i = 1; i < lines.Length; i++)
{
var parts = lines[i].Split(',', 3);
embeddings[int.Parse(parts[0])] = parts[2]
.Trim('"', '[', ']')
.Split(',', StringSplitOptions.TrimEntries)
.Select(float.Parse)
.ToArray();
}
var ids = embeddings.Keys.ToArray();
var items = await db.MockItems.Where(x => ids.Contains(x.Id)).ToListAsync();
foreach (var item in items)
{
item.Embedding = new Vector(embeddings[item.Id]);
}
await db.SaveChangesAsync();
Console.WriteLine($"Loaded {items.Count} embeddings");
}