From 25723cb7a46961515b79acf57786995e48ca9564 Mon Sep 17 00:00:00 2001 From: LD-Reborn Date: Thu, 25 Dec 2025 14:19:25 +0100 Subject: [PATCH] Fixed embeddingCache not yet global --- src/Server/Searchdomain.cs | 24 +++++++++++++++++++----- src/Server/Views/Home/Index.cshtml | 1 + 2 files changed, 20 insertions(+), 5 deletions(-) diff --git a/src/Server/Searchdomain.cs b/src/Server/Searchdomain.cs index b48fcf0..2435914 100644 --- a/src/Server/Searchdomain.cs +++ b/src/Server/Searchdomain.cs @@ -159,7 +159,6 @@ public class Searchdomain } entityReader.Close(); modelsInUse = GetModels(entityCache); - embeddingCache = []; // TODO remove this and implement proper remediation to improve performance } public List<(float, string)> Search(string query, int? topN = null) @@ -170,14 +169,29 @@ public class Searchdomain return [.. cachedResult.Results.Select(r => (r.Score, r.Name))]; } - if (!embeddingCache.TryGetValue(query, out Dictionary? queryEmbeddings)) + bool hasQuery = embeddingCache.TryGetValue(query, out Dictionary? queryEmbeddings); + bool allModelsInQuery = queryEmbeddings is not null && modelsInUse.All(model => queryEmbeddings.ContainsKey(model)); + if (!(hasQuery && allModelsInQuery)) { - queryEmbeddings = Datapoint.GenerateEmbeddings(query, modelsInUse, aIProvider); + queryEmbeddings = Datapoint.GenerateEmbeddings(query, modelsInUse, aIProvider, embeddingCache); if (embeddingCache.Count < embeddingCacheMaxSize) // TODO add better way of managing cache limit hits { // Idea: Add access count to each entry. On limit hit, sort the entries by access count and remove the bottom 10% of entries - embeddingCache.Add(query, queryEmbeddings); + if (!embeddingCache.ContainsKey(query)) + { + embeddingCache.Add(query, queryEmbeddings); + } + else + { + foreach (KeyValuePair kvp in queryEmbeddings) + { + if (!embeddingCache.ContainsKey(kvp.Key)) + { + embeddingCache[query][kvp.Key] = kvp.Value; + } + } + } } - } // TODO implement proper cache remediation for embeddingCache here + } List<(float, string)> result = []; diff --git a/src/Server/Views/Home/Index.cshtml b/src/Server/Views/Home/Index.cshtml index 668d510..b167e0b 100644 --- a/src/Server/Views/Home/Index.cshtml +++ b/src/Server/Views/Home/Index.cshtml @@ -1,5 +1,6 @@ @using Server.Models @using System.Web +@using Shared.Models @using Server.Services @using Server