Implemented search cache
This commit is contained in:
19
src/Server/Models/SearchdomainModels.cs
Normal file
19
src/Server/Models/SearchdomainModels.cs
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
using MySqlX.XDevAPI.Common;
|
||||||
|
|
||||||
|
namespace Server.Models;
|
||||||
|
public readonly struct ResultItem(float score, string name)
|
||||||
|
{
|
||||||
|
public readonly float Score = score;
|
||||||
|
public readonly string Name = name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public struct DateTimedSearchResult
|
||||||
|
{
|
||||||
|
public List<DateTime> accessTimes;
|
||||||
|
public SearchResult Results;
|
||||||
|
}
|
||||||
|
|
||||||
|
public readonly struct SearchResult(List<ResultItem> results)
|
||||||
|
{
|
||||||
|
public readonly List<ResultItem> Results = results;
|
||||||
|
}
|
||||||
@@ -3,6 +3,7 @@ using System.Data.Common;
|
|||||||
using ElmahCore.Mvc.Logger;
|
using ElmahCore.Mvc.Logger;
|
||||||
using MySql.Data.MySqlClient;
|
using MySql.Data.MySqlClient;
|
||||||
using Server.Helper;
|
using Server.Helper;
|
||||||
|
using Server.Models;
|
||||||
|
|
||||||
namespace Server;
|
namespace Server;
|
||||||
|
|
||||||
@@ -13,7 +14,7 @@ public class Searchdomain
|
|||||||
public AIProvider aIProvider;
|
public AIProvider aIProvider;
|
||||||
public string searchdomain;
|
public string searchdomain;
|
||||||
public int id;
|
public int id;
|
||||||
public Dictionary<string, List<(DateTime, List<(float, string)>)>> searchCache; // Yeah look at this abomination. searchCache[x][0] = last accessed time, searchCache[x][1] = results for x
|
public Dictionary<string, DateTimedSearchResult> searchCache; // Key: query, Value: Search results for that query (with timestamp)
|
||||||
public List<Entity> entityCache;
|
public List<Entity> entityCache;
|
||||||
public List<string> modelsInUse;
|
public List<string> modelsInUse;
|
||||||
public Dictionary<string, Dictionary<string, float[]>> embeddingCache;
|
public Dictionary<string, Dictionary<string, float[]>> embeddingCache;
|
||||||
@@ -22,8 +23,6 @@ public class Searchdomain
|
|||||||
public SQLHelper helper;
|
public SQLHelper helper;
|
||||||
private readonly ILogger _logger;
|
private readonly ILogger _logger;
|
||||||
|
|
||||||
// TODO Add settings and update cli/program.cs, as well as DatabaseInsertSearchdomain()
|
|
||||||
|
|
||||||
public Searchdomain(string searchdomain, string connectionString, AIProvider aIProvider, Dictionary<string, Dictionary<string, float[]>> embeddingCache, ILogger logger, string provider = "sqlserver", bool runEmpty = false)
|
public Searchdomain(string searchdomain, string connectionString, AIProvider aIProvider, Dictionary<string, Dictionary<string, float[]>> embeddingCache, ILogger logger, string provider = "sqlserver", bool runEmpty = false)
|
||||||
{
|
{
|
||||||
_connectionString = connectionString;
|
_connectionString = connectionString;
|
||||||
@@ -47,6 +46,7 @@ public class Searchdomain
|
|||||||
|
|
||||||
public void UpdateEntityCache()
|
public void UpdateEntityCache()
|
||||||
{
|
{
|
||||||
|
InvalidateSearchCache();
|
||||||
Dictionary<string, dynamic> parametersIDSearchdomain = new()
|
Dictionary<string, dynamic> parametersIDSearchdomain = new()
|
||||||
{
|
{
|
||||||
["id"] = this.id
|
["id"] = this.id
|
||||||
@@ -151,8 +151,14 @@ public class Searchdomain
|
|||||||
embeddingCache = []; // TODO remove this and implement proper remediation to improve performance
|
embeddingCache = []; // TODO remove this and implement proper remediation to improve performance
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<(float, string)> Search(string query, bool sort=true)
|
public List<(float, string)> Search(string query)
|
||||||
{
|
{
|
||||||
|
if (searchCache.TryGetValue(query, out DateTimedSearchResult cachedResult))
|
||||||
|
{
|
||||||
|
cachedResult.accessTimes.Add(DateTime.Now);
|
||||||
|
return [.. cachedResult.Results.Results.Select(r => (r.Score, r.Name))];
|
||||||
|
}
|
||||||
|
|
||||||
if (!embeddingCache.TryGetValue(query, out Dictionary<string, float[]>? queryEmbeddings))
|
if (!embeddingCache.TryGetValue(query, out Dictionary<string, float[]>? queryEmbeddings))
|
||||||
{
|
{
|
||||||
queryEmbeddings = Datapoint.GenerateEmbeddings(query, modelsInUse, aIProvider);
|
queryEmbeddings = Datapoint.GenerateEmbeddings(query, modelsInUse, aIProvider);
|
||||||
@@ -181,8 +187,17 @@ public class Searchdomain
|
|||||||
}
|
}
|
||||||
result.Add((entity.probMethod(datapointProbs), entity.name));
|
result.Add((entity.probMethod(datapointProbs), entity.name));
|
||||||
}
|
}
|
||||||
|
List<(float, string)> results = [.. result.OrderByDescending(s => s.Item1)];
|
||||||
return [.. result.OrderByDescending(s => s.Item1)]; // [.. element] = element.ToList()
|
SearchResult searchResult = new(
|
||||||
|
[.. results.Select(r =>
|
||||||
|
new ResultItem(r.Item1, r.Item2 ))]
|
||||||
|
);
|
||||||
|
searchCache[query] = new DateTimedSearchResult
|
||||||
|
{
|
||||||
|
accessTimes = [DateTime.Now],
|
||||||
|
Results = searchResult
|
||||||
|
};
|
||||||
|
return results;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static List<string> GetModels(List<Entity> entities)
|
public static List<string> GetModels(List<Entity> entities)
|
||||||
@@ -217,4 +232,9 @@ public class Searchdomain
|
|||||||
reader.Close();
|
reader.Close();
|
||||||
return this.id;
|
return this.id;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void InvalidateSearchCache()
|
||||||
|
{
|
||||||
|
searchCache = [];
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -64,7 +64,9 @@ public class SearchdomainManager
|
|||||||
|
|
||||||
public void InvalidateSearchdomainCache(string searchdomainName)
|
public void InvalidateSearchdomainCache(string searchdomainName)
|
||||||
{
|
{
|
||||||
GetSearchdomain(searchdomainName).UpdateEntityCache();
|
var searchdomain = GetSearchdomain(searchdomainName);
|
||||||
|
searchdomain.UpdateEntityCache();
|
||||||
|
searchdomain.InvalidateSearchCache(); // TODO implement cache remediation (Suggestion: searchdomain-wide setting for cache remediation / invalidation - )
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<string> ListSearchdomains()
|
public List<string> ListSearchdomains()
|
||||||
|
|||||||
Reference in New Issue
Block a user