22using HybridRrf . Data ;
33using Microsoft . EntityFrameworkCore ;
44using ParadeDB . EntityFrameworkCore . Extensions ;
5- using Pgvector ;
6- using Pgvector . EntityFrameworkCore ;
75using Shared ;
86
97var options = new DbContextOptionsBuilder < AppDbContext > ( )
10- . UseNpgsql (
11- ExampleSetup . ConnectionString ,
12- o =>
13- {
14- o . UseParadeDb ( ) ;
15- o . UseVector ( ) ;
16- }
17- )
8+ . UseNpgsql ( ExampleSetup . ConnectionString , o => o . UseParadeDb ( ) )
189 . UseSnakeCaseNamingConvention ( )
1910 . Options ;
2011
2718Console . WriteLine ( "RRF formula: score = sum(1 / (k + rank)) across all rankings" ) ;
2819
2920await ExampleSetup . SetupHybridAsync ( dbContext ) ;
30- await LoadEmbeddingsAsync ( dbContext ) ;
21+ await EmbeddingLoader . LoadAsync ( dbContext ) ;
3122
3223await Demo ( dbContext , "running shoes" , QueryEmbeddings . Values ) ;
3324await Demo ( dbContext , "footwear for exercise" , QueryEmbeddings . Values ) ;
4031
4132static async Task Demo ( AppDbContext db , string query , Dictionary < string , float [ ] > queryEmbeddings )
4233{
43- var results = await HybridSearch ( db , query , new Vector ( queryEmbeddings [ query ] ) ) ;
34+ var results = await HybridSearch ( db , query , queryEmbeddings [ query ] ) ;
4435 DisplayResults ( query , results ) ;
4536}
4637
4738static async Task < List < ( string Description , double RrfScore ) > > HybridSearch (
4839 AppDbContext db ,
4940 string query ,
50- Vector queryEmbedding ,
41+ float [ ] queryEmbedding ,
5142 int topK = 20 ,
5243 int rrfK = 60 ,
5344 int limit = 5
@@ -71,7 +62,7 @@ static async Task Demo(AppDbContext db, string query, Dictionary<string, float[]
7162 {
7263 x . Id ,
7364 x . Description ,
74- Distance = x . Embedding ! . CosineDistance ( queryEmbedding ) ,
65+ Distance = EF . Functions . CosineDistance ( x . Embedding , queryEmbedding ) ,
7566 } )
7667 . OrderBy ( x => x . Distance )
7768 . Take ( topK )
@@ -101,31 +92,3 @@ static void DisplayResults(string query, List<(string Description, double RrfSco
10192 Console . WriteLine ( $ " { i + 1 } . { desc , - 60 } (RRF: { results [ i ] . RrfScore : F4} )") ;
10293 }
10394}
104-
105- static async Task LoadEmbeddingsAsync ( AppDbContext db )
106- {
107- var csvPath = Path . Combine ( AppContext . BaseDirectory , "HybridRrf" , "mock_items_embeddings.csv" ) ;
108- var lines = await File . ReadAllLinesAsync ( csvPath ) ;
109-
110- var embeddings = new Dictionary < int , float [ ] > ( ) ;
111-
112- for ( var i = 1 ; i < lines . Length ; i ++ )
113- {
114- var parts = lines [ i ] . Split ( ',' , 3 ) ;
115- embeddings [ int . Parse ( parts [ 0 ] ) ] = parts [ 2 ]
116- . Trim ( '"' , '[' , ']' )
117- . Split ( ',' , StringSplitOptions . TrimEntries )
118- . Select ( float . Parse )
119- . ToArray ( ) ;
120- }
121-
122- var ids = embeddings . Keys . ToArray ( ) ;
123- var items = await db . MockItems . Where ( x => ids . Contains ( x . Id ) ) . ToListAsync ( ) ;
124- foreach ( var item in items )
125- {
126- item . Embedding = new Vector ( embeddings [ item . Id ] ) ;
127- }
128-
129- await db . SaveChangesAsync ( ) ;
130- Console . WriteLine ( $ "Loaded { items . Count } embeddings") ;
131- }
0 commit comments