refine file settings and knowledge service
This commit is contained in:
parent
608dc1b4cf
commit
280b1bcd2a
|
|
@ -2,6 +2,8 @@ namespace BotSharp.Abstraction.Files.Converters;
|
|||
|
||||
public interface IPdf2ImageConverter
|
||||
{
|
||||
public string Name { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Convert pdf pages to images, and return a list of image file paths
|
||||
/// </summary>
|
||||
|
|
|
|||
|
|
@ -0,0 +1,10 @@
|
|||
using BotSharp.Abstraction.Repositories.Enums;
|
||||
|
||||
namespace BotSharp.Abstraction.Files;
|
||||
|
||||
public class FileCoreSettings
|
||||
{
|
||||
public string Storage { get; set; } = FileStorageEnum.LocalFileStorage;
|
||||
public string Pdf2TextConverter { get; set; }
|
||||
public string Pdf2ImageConverter { get; set; }
|
||||
}
|
||||
|
|
@ -1,8 +0,0 @@
|
|||
using BotSharp.Abstraction.Repositories.Enums;
|
||||
|
||||
namespace BotSharp.Abstraction.Files;
|
||||
|
||||
public class FileStorageSettings
|
||||
{
|
||||
public string Default { get; set; } = FileStorageEnum.LocalFileStorage;
|
||||
}
|
||||
|
|
@ -1,5 +1,3 @@
|
|||
using BotSharp.Abstraction.Knowledges.Models;
|
||||
|
||||
namespace BotSharp.Abstraction.Knowledges;
|
||||
|
||||
public interface IKnowledgeHook
|
||||
|
|
|
|||
|
|
@ -4,17 +4,8 @@ namespace BotSharp.Abstraction.Knowledges;
|
|||
|
||||
public interface IKnowledgeService
|
||||
{
|
||||
Task<List<KnowledgeChunk>> CollectChunkedKnowledge();
|
||||
Task EmbedKnowledge(List<KnowledgeChunk> chunks);
|
||||
|
||||
Task Feed(KnowledgeFeedModel knowledge);
|
||||
Task EmbedKnowledge(KnowledgeCreationModel knowledge);
|
||||
Task<string> GetKnowledges(KnowledgeRetrievalModel retrievalModel);
|
||||
Task<List<RetrievedResult>> GetAnswer(KnowledgeRetrievalModel retrievalModel);
|
||||
|
||||
#region List
|
||||
Task<IEnumerable<KnowledgeRetrievalResult>> SearchKnowledge(KnowledgeRetrievalModel model);
|
||||
Task FeedKnowledge(KnowledgeCreationModel model);
|
||||
Task<StringIdPagedItems<KnowledgeCollectionData>> GetKnowledgeCollectionData(string collectionName, KnowledgeFilter filter);
|
||||
Task<IEnumerable<KnowledgeCollectionData>> GetSimilarKnowledgeData(string collectionName, KnowledgeFilter filter);
|
||||
Task<bool> DeleteKnowledgeCollectionData(string collectionName, string id);
|
||||
#endregion
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,12 +1,8 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
|
||||
namespace BotSharp.Abstraction.Knowledges
|
||||
{
|
||||
public interface IPdf2TextConverter
|
||||
{
|
||||
public string Name { get; }
|
||||
Task<string> ConvertPdfToText(string filePath, int? startPageNum, int? endPageNum);
|
||||
}
|
||||
}
|
||||
|
|
@ -1,7 +0,0 @@
|
|||
namespace BotSharp.Abstraction.Knowledges.Models;
|
||||
|
||||
public class KnowledgeCollectionInfo
|
||||
{
|
||||
public ulong DataCount { get; set; }
|
||||
public ulong VectorCount { get; set; }
|
||||
}
|
||||
|
|
@ -1,6 +1,9 @@
|
|||
using BotSharp.Abstraction.Knowledges.Enums;
|
||||
|
||||
namespace BotSharp.Abstraction.Knowledges.Models;
|
||||
|
||||
public class KnowledgeCreationModel
|
||||
{
|
||||
public string Collection { get; set; } = KnowledgeCollectionName.BotSharp;
|
||||
public string Content { get; set; } = string.Empty;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,7 +0,0 @@
|
|||
namespace BotSharp.Abstraction.Knowledges.Models;
|
||||
|
||||
public class KnowledgeFeedModel
|
||||
{
|
||||
public string AgentId { get; set; } = string.Empty;
|
||||
public string Content { get; set; } = string.Empty;
|
||||
}
|
||||
|
|
@ -1,7 +1,13 @@
|
|||
using BotSharp.Abstraction.Knowledges.Enums;
|
||||
|
||||
namespace BotSharp.Abstraction.Knowledges.Models;
|
||||
|
||||
public class KnowledgeRetrievalModel
|
||||
{
|
||||
public string AgentId { get; set; } = string.Empty;
|
||||
public string Question { get; set; } = string.Empty;
|
||||
public string Collection { get; set; } = KnowledgeCollectionName.BotSharp;
|
||||
public string Text { get; set; } = string.Empty;
|
||||
public IEnumerable<string>? Fields { get; set; } = new List<string> { KnowledgePayloadName.Text, KnowledgePayloadName.Answer };
|
||||
public int? Limit { get; set; } = 5;
|
||||
public float? Confidence { get; set; } = 0.5f;
|
||||
public bool WithVector { get; set; }
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,9 +0,0 @@
|
|||
namespace BotSharp.Abstraction.Knowledges.Models;
|
||||
|
||||
public class KnowledgeRetrievalResult
|
||||
{
|
||||
public string Id { get; set; }
|
||||
public string Text { get; set; }
|
||||
public float Score { get; set; }
|
||||
public float[]? Vector { get; set; }
|
||||
}
|
||||
|
|
@ -0,0 +1,12 @@
|
|||
namespace BotSharp.Abstraction.Knowledges.Models;
|
||||
|
||||
public class KnowledgeSearchResult
|
||||
{
|
||||
public IDictionary<string, string> Data { get; set; } = new Dictionary<string, string>();
|
||||
public double Score { get; set; }
|
||||
public float[]? Vector { get; set; }
|
||||
}
|
||||
|
||||
public class KnowledgeRetrievalResult : KnowledgeSearchResult
|
||||
{
|
||||
}
|
||||
|
|
@ -1,12 +0,0 @@
|
|||
namespace BotSharp.Abstraction.Knowledges.Models;
|
||||
|
||||
public class RetrievedResult
|
||||
{
|
||||
public int Paragraph { get; set; }
|
||||
|
||||
[JsonPropertyName("cite_source")]
|
||||
public string CiteSource { get; set; } = "related text";
|
||||
|
||||
[JsonPropertyName("reasoning")]
|
||||
public string Reasoning { get; set; } = "";
|
||||
}
|
||||
|
|
@ -3,7 +3,12 @@ namespace BotSharp.Abstraction.Knowledges.Settings;
|
|||
public class KnowledgeBaseSettings
|
||||
{
|
||||
public string VectorDb { get; set; }
|
||||
public string TextEmbedding { get; set; }
|
||||
public string TextCompletion { get; set; }
|
||||
public KnowledgeModelSetting TextEmbedding { get; set; }
|
||||
public string Pdf2TextConverter { get; set; }
|
||||
}
|
||||
|
||||
public class KnowledgeModelSetting
|
||||
{
|
||||
public string Provider { get; set; }
|
||||
public string Model { get; set; }
|
||||
}
|
||||
|
|
@ -17,4 +17,5 @@ global using BotSharp.Abstraction.Templating;
|
|||
global using BotSharp.Abstraction.Translation.Attributes;
|
||||
global using BotSharp.Abstraction.Messaging.Enums;
|
||||
global using BotSharp.Abstraction.Files.Models;
|
||||
global using BotSharp.Abstraction.Files.Enums;
|
||||
global using BotSharp.Abstraction.Files.Enums;
|
||||
global using BotSharp.Abstraction.Knowledges.Models;
|
||||
|
|
@ -1,13 +1,13 @@
|
|||
using BotSharp.Abstraction.Knowledges.Models;
|
||||
|
||||
namespace BotSharp.Abstraction.VectorStorage;
|
||||
|
||||
public interface IVectorDb
|
||||
{
|
||||
string Name { get; }
|
||||
|
||||
Task<IEnumerable<string>> GetCollections();
|
||||
Task<StringIdPagedItems<KnowledgeCollectionData>> GetCollectionData(string collectionName, KnowledgeFilter filter);
|
||||
Task CreateCollection(string collectionName, int dim);
|
||||
Task<bool> Upsert(string collectionName, string id, float[] vector, string text, Dictionary<string, string>? payload = null);
|
||||
Task<IEnumerable<string>> Search(string collectionName, float[] vector, string returnFieldName, int limit = 5, float confidence = 0.5f);
|
||||
Task<IEnumerable<KnowledgeSearchResult>> Search(string collectionName, float[] vector, IEnumerable<string> fields, int limit = 5, float confidence = 0.5f, bool withVector = false);
|
||||
Task<bool> DeleteCollectionData(string collectionName, string id);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -4,22 +4,22 @@ using Microsoft.Extensions.Configuration;
|
|||
|
||||
namespace BotSharp.Core.Files;
|
||||
|
||||
public class FilePlugin : IBotSharpPlugin
|
||||
public class FileCorePlugin : IBotSharpPlugin
|
||||
{
|
||||
public string Id => "6a8473c0-04eb-4346-be32-24755ce5973d";
|
||||
|
||||
public string Name => "File";
|
||||
public string Name => "File Core";
|
||||
|
||||
public string Description => "Provides file storage and analysis.";
|
||||
|
||||
|
||||
public void RegisterDI(IServiceCollection services, IConfiguration config)
|
||||
{
|
||||
var myFileStorageSettings = new FileStorageSettings();
|
||||
config.Bind("FileStorage", myFileStorageSettings);
|
||||
services.AddSingleton(myFileStorageSettings);
|
||||
var fileCoreSettings = new FileCoreSettings();
|
||||
config.Bind("FileCore", fileCoreSettings);
|
||||
services.AddSingleton(fileCoreSettings);
|
||||
|
||||
if (myFileStorageSettings.Default == FileStorageEnum.LocalFileStorage)
|
||||
if (fileCoreSettings.Storage == FileStorageEnum.LocalFileStorage)
|
||||
{
|
||||
services.AddScoped<IFileStorageService, LocalFileStorageService>();
|
||||
}
|
||||
|
|
@ -97,7 +97,8 @@ public partial class FileInstructService
|
|||
private async Task<IEnumerable<string>> ConvertPdfToImages(IEnumerable<string> files)
|
||||
{
|
||||
var images = new List<string>();
|
||||
var converter = _services.GetServices<IPdf2ImageConverter>().FirstOrDefault();
|
||||
var settings = _services.GetRequiredService<FileCoreSettings>();
|
||||
var converter = _services.GetServices<IPdf2ImageConverter>().FirstOrDefault(x => x.Name == settings.Pdf2ImageConverter);
|
||||
if (converter == null || files.IsNullOrEmpty())
|
||||
{
|
||||
return images;
|
||||
|
|
|
|||
|
|
@ -275,8 +275,9 @@ public partial class LocalFileStorageService
|
|||
|
||||
private IPdf2ImageConverter? GetPdf2ImageConverter()
|
||||
{
|
||||
var converters = _services.GetServices<IPdf2ImageConverter>();
|
||||
return converters.FirstOrDefault();
|
||||
var settings = _services.GetRequiredService<FileCoreSettings>();
|
||||
var converter = _services.GetServices<IPdf2ImageConverter>().FirstOrDefault(x => x.Name == settings.Pdf2ImageConverter);
|
||||
return converter;
|
||||
}
|
||||
|
||||
private async Task<IEnumerable<MessageFileModel>> GetScreenshots(string file, string parentDir, string messageId, string source)
|
||||
|
|
|
|||
|
|
@ -1,3 +1,4 @@
|
|||
using BotSharp.Abstraction.Knowledges.Enums;
|
||||
using BotSharp.Abstraction.Knowledges.Models;
|
||||
using BotSharp.Abstraction.Knowledges.Settings;
|
||||
using BotSharp.OpenAPI.ViewModels.Knowledges;
|
||||
|
|
@ -17,86 +18,28 @@ public class KnowledgeBaseController : ControllerBase
|
|||
_services = services;
|
||||
}
|
||||
|
||||
[HttpGet("/knowledge/{agentId}")]
|
||||
public async Task<List<RetrievedResult>> RetrieveKnowledge([FromRoute] string agentId, [FromQuery(Name = "q")] string question)
|
||||
[HttpPost("/knowledge/search")]
|
||||
public async Task<IEnumerable<KnowledgeRetrivalViewModel>> SearchKnowledge([FromBody] SearchKnowledgeModel model)
|
||||
{
|
||||
return await _knowledgeService.GetAnswer(new KnowledgeRetrievalModel
|
||||
var searchModel = new KnowledgeRetrievalModel
|
||||
{
|
||||
AgentId = agentId,
|
||||
Question = question
|
||||
});
|
||||
Collection = model.Collection,
|
||||
Text = model.Text,
|
||||
Fields = model.Fields,
|
||||
Limit = model.Limit ?? 5,
|
||||
Confidence = model.Confidence ?? 0.5f,
|
||||
WithVector = model.WithVector
|
||||
};
|
||||
|
||||
var results = await _knowledgeService.SearchKnowledge(searchModel);
|
||||
return results.Select(x => KnowledgeRetrivalViewModel.From(x)).ToList();
|
||||
}
|
||||
|
||||
[HttpPost("/knowledge-base/upload")]
|
||||
public async Task<IActionResult> UploadKnowledge(IFormFile file, [FromQuery] int? startPageNum, [FromQuery] int? endPageNum)
|
||||
{
|
||||
var setttings = _services.GetRequiredService<KnowledgeBaseSettings>();
|
||||
var textConverter = _services.GetServices<IPdf2TextConverter>()
|
||||
.First(x => x.GetType().FullName.EndsWith(setttings.Pdf2TextConverter));
|
||||
|
||||
var filePath = Path.GetTempFileName();
|
||||
using (var stream = System.IO.File.Create(filePath))
|
||||
{
|
||||
await file.CopyToAsync(stream);
|
||||
}
|
||||
|
||||
var content = await textConverter.ConvertPdfToText(filePath, startPageNum, endPageNum);
|
||||
|
||||
// Process uploaded files
|
||||
// Don't rely on or trust the FileName property without validation.
|
||||
|
||||
// Add FeedWithMetaData
|
||||
await _knowledgeService.EmbedKnowledge(new KnowledgeCreationModel
|
||||
{
|
||||
Content = content
|
||||
});
|
||||
|
||||
return Ok(new { count = 1, file.Length });
|
||||
}
|
||||
|
||||
[HttpPost("/knowledge/{agentId}")]
|
||||
public async Task<IActionResult> FeedKnowledge([FromRoute] string agentId, List<IFormFile> files, [FromQuery] int? startPageNum, [FromQuery] int? endPageNum, [FromQuery] bool? paddleModel)
|
||||
{
|
||||
var setttings = _services.GetRequiredService<KnowledgeBaseSettings>();
|
||||
var textConverter = _services.GetServices<IPdf2TextConverter>().First(x => x.GetType().FullName.EndsWith(setttings.Pdf2TextConverter));
|
||||
long size = files.Sum(f => f.Length);
|
||||
|
||||
foreach (var formFile in files)
|
||||
{
|
||||
var filePath = Path.GetTempFileName();
|
||||
|
||||
|
||||
using (var stream = new FileStream(filePath, FileMode.Create, FileAccess.Write, FileShare.None))
|
||||
{
|
||||
await formFile.CopyToAsync(stream);
|
||||
await stream.FlushAsync(); // Ensure all data is written to the file
|
||||
}
|
||||
|
||||
var content = await textConverter.ConvertPdfToText(filePath, startPageNum, endPageNum);
|
||||
|
||||
// Process uploaded files
|
||||
// Don't rely on or trust the FileName property without validation.
|
||||
|
||||
// Add FeedWithMetaData
|
||||
await _knowledgeService.Feed(new KnowledgeFeedModel
|
||||
{
|
||||
AgentId = agentId,
|
||||
Content = content
|
||||
});
|
||||
|
||||
// Delete the temp file after processing to clean up
|
||||
System.IO.File.Delete(filePath);
|
||||
}
|
||||
|
||||
return Ok(new { count = files.Count, size });
|
||||
}
|
||||
|
||||
|
||||
[HttpPost("/knowledge/{collection}/data")]
|
||||
public async Task<StringIdPagedItems<KnowledgeCollectionDataViewModel>> GetKnowledgeCollectionData([FromRoute] string collection, [FromBody] KnowledgeFilter filter)
|
||||
{
|
||||
var data = await _knowledgeService.GetKnowledgeCollectionData(collection, filter);
|
||||
var items = data.Items?.Select(x => KnowledgeCollectionDataViewModel.ToViewModel(x))?
|
||||
var items = data.Items?.Select(x => KnowledgeCollectionDataViewModel.From(x))?
|
||||
.ToList() ?? new List<KnowledgeCollectionDataViewModel>();
|
||||
|
||||
return new StringIdPagedItems<KnowledgeCollectionDataViewModel>
|
||||
|
|
@ -107,19 +50,33 @@ public class KnowledgeBaseController : ControllerBase
|
|||
};
|
||||
}
|
||||
|
||||
[HttpPost("/knowledge/{collection}/similar")]
|
||||
public async Task<IEnumerable<KnowledgeCollectionDataViewModel>> GetSimilarKnowledgeData([FromRoute] string collection, [FromBody] KnowledgeFilter filter)
|
||||
{
|
||||
var data = await _knowledgeService.GetKnowledgeCollectionData(collection, filter);
|
||||
var items = data.Items?.Select(x => KnowledgeCollectionDataViewModel.ToViewModel(x))?
|
||||
.ToList() ?? new List<KnowledgeCollectionDataViewModel>();
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
[HttpDelete("/knowledge/{collection}/data/{id}")]
|
||||
public async Task<bool> DeleteKnowledgeCollectionData([FromRoute] string collection, [FromRoute] string id)
|
||||
{
|
||||
return await _knowledgeService.DeleteKnowledgeCollectionData(collection, id);
|
||||
}
|
||||
|
||||
[HttpPost("/knowledge/upload")]
|
||||
public async Task<IActionResult> UploadKnowledge(IFormFile file, [FromQuery] string? collection, [FromQuery] int? startPageNum, [FromQuery] int? endPageNum)
|
||||
{
|
||||
var setttings = _services.GetRequiredService<KnowledgeBaseSettings>();
|
||||
var textConverter = _services.GetServices<IPdf2TextConverter>().FirstOrDefault(x => x.Name == setttings.Pdf2TextConverter);
|
||||
|
||||
var filePath = Path.GetTempFileName();
|
||||
using (var stream = new FileStream(filePath, FileMode.Create, FileAccess.Write, FileShare.None))
|
||||
{
|
||||
await file.CopyToAsync(stream);
|
||||
await stream.FlushAsync();
|
||||
}
|
||||
|
||||
var content = await textConverter.ConvertPdfToText(filePath, startPageNum, endPageNum);
|
||||
await _knowledgeService.FeedKnowledge(new KnowledgeCreationModel
|
||||
{
|
||||
Collection = collection ?? KnowledgeCollectionName.BotSharp,
|
||||
Content = content
|
||||
});
|
||||
|
||||
System.IO.File.Delete(filePath);
|
||||
return Ok(new { count = 1, file.Length });
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -20,7 +20,7 @@ public class KnowledgeCollectionDataViewModel
|
|||
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
|
||||
public float[]? Vector { get; set; }
|
||||
|
||||
public static KnowledgeCollectionDataViewModel ToViewModel(KnowledgeCollectionData data)
|
||||
public static KnowledgeCollectionDataViewModel From(KnowledgeCollectionData data)
|
||||
{
|
||||
return new KnowledgeCollectionDataViewModel
|
||||
{
|
||||
|
|
|
|||
|
|
@ -0,0 +1,27 @@
|
|||
using BotSharp.Abstraction.Knowledges.Models;
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
namespace BotSharp.OpenAPI.ViewModels.Knowledges;
|
||||
|
||||
public class KnowledgeRetrivalViewModel
|
||||
{
|
||||
[JsonPropertyName("data")]
|
||||
public IDictionary<string, string> Data { get; set; }
|
||||
|
||||
[JsonPropertyName("score")]
|
||||
public double Score { get; set; }
|
||||
|
||||
[JsonPropertyName("vector")]
|
||||
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
|
||||
public float[]? Vector { get; set; }
|
||||
|
||||
public static KnowledgeRetrivalViewModel From(KnowledgeRetrievalResult model)
|
||||
{
|
||||
return new KnowledgeRetrivalViewModel
|
||||
{
|
||||
Data = model.Data,
|
||||
Score = model.Score,
|
||||
Vector = model.Vector
|
||||
};
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,25 @@
|
|||
using BotSharp.Abstraction.Knowledges.Enums;
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
namespace BotSharp.OpenAPI.ViewModels.Knowledges;
|
||||
|
||||
public class SearchKnowledgeModel
|
||||
{
|
||||
[JsonPropertyName("collection")]
|
||||
public string Collection { get; set; } = KnowledgeCollectionName.BotSharp;
|
||||
|
||||
[JsonPropertyName("text")]
|
||||
public string Text { get; set; } = string.Empty;
|
||||
|
||||
[JsonPropertyName("fields")]
|
||||
public IEnumerable<string>? Fields { get; set; }
|
||||
|
||||
[JsonPropertyName("limit")]
|
||||
public int? Limit { get; set; } = 5;
|
||||
|
||||
[JsonPropertyName("confidence")]
|
||||
public float? Confidence { get; set; } = 0.5f;
|
||||
|
||||
[JsonPropertyName("with_vector")]
|
||||
public bool WithVector { get; set; }
|
||||
}
|
||||
|
|
@ -17,14 +17,14 @@ public class KnowledgeRetrievalFn : IFunctionCallback
|
|||
{
|
||||
var args = JsonSerializer.Deserialize<ExtractedKnowledge>(message.FunctionArgs ?? "{}");
|
||||
|
||||
var embedding = _services.GetServices<ITextEmbedding>()
|
||||
.FirstOrDefault(x => x.GetType().FullName.EndsWith(_settings.TextEmbedding));
|
||||
var embedding = _services.GetServices<ITextEmbedding>().FirstOrDefault(x => x.Provider == _settings.TextEmbedding.Provider);
|
||||
embedding.SetModelName(_settings.TextEmbedding.Model);
|
||||
|
||||
var vector = await embedding.GetVectorAsync(args.Question);
|
||||
var vectorDb = _services.GetRequiredService<IVectorDb>();
|
||||
var knowledges = await vectorDb.Search(KnowledgeCollectionName.BotSharp, vector, KnowledgePayloadName.Answer);
|
||||
var knowledges = await vectorDb.Search(KnowledgeCollectionName.BotSharp, vector, new List<string> { KnowledgePayloadName.Answer });
|
||||
|
||||
if (knowledges.Count > 0)
|
||||
if (!knowledges.IsNullOrEmpty())
|
||||
{
|
||||
message.Content = string.Join("\r\n\r\n=====\r\n", knowledges);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,5 +1,3 @@
|
|||
using BotSharp.Core.Infrastructures;
|
||||
|
||||
namespace BotSharp.Plugin.KnowledgeBase.Functions;
|
||||
|
||||
public class MemorizeKnowledgeFn : IFunctionCallback
|
||||
|
|
@ -19,8 +17,8 @@ public class MemorizeKnowledgeFn : IFunctionCallback
|
|||
{
|
||||
var args = JsonSerializer.Deserialize<ExtractedKnowledge>(message.FunctionArgs ?? "{}");
|
||||
|
||||
var embedding = _services.GetServices<ITextEmbedding>()
|
||||
.First(x => x.GetType().FullName.EndsWith(_settings.TextEmbedding));
|
||||
var embedding = _services.GetServices<ITextEmbedding>().FirstOrDefault(x => x.Provider == _settings.TextEmbedding.Provider);
|
||||
embedding.SetModelName(_settings.TextEmbedding.Model);
|
||||
|
||||
var vector = await embedding.GetVectorsAsync(new List<string>
|
||||
{
|
||||
|
|
@ -28,10 +26,9 @@ public class MemorizeKnowledgeFn : IFunctionCallback
|
|||
});
|
||||
|
||||
var vectorDb = _services.GetRequiredService<IVectorDb>();
|
||||
|
||||
await vectorDb.CreateCollection(KnowledgeCollectionName.BotSharp, vector[0].Length);
|
||||
|
||||
var id = Utilities.HashTextMd5(args.Question);
|
||||
var id = Guid.NewGuid().ToString();
|
||||
var result = await vectorDb.Upsert(KnowledgeCollectionName.BotSharp, id, vector[0],
|
||||
args.Question,
|
||||
new Dictionary<string, string>
|
||||
|
|
|
|||
|
|
@ -9,6 +9,6 @@ public class MemVecDbPlugin : IBotSharpPlugin
|
|||
public string Description => "Store text embedding, search similar text from memory.";
|
||||
public void RegisterDI(IServiceCollection services, IConfiguration config)
|
||||
{
|
||||
services.AddSingleton<IVectorDb, MemVectorDatabase>();
|
||||
services.AddSingleton<IVectorDb, MemoryVectorDb>();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,139 +0,0 @@
|
|||
using Tensorflow.NumPy;
|
||||
using static Tensorflow.Binding;
|
||||
|
||||
namespace BotSharp.Plugin.KnowledgeBase.MemVecDb;
|
||||
|
||||
public class MemVectorDatabase : IVectorDb
|
||||
{
|
||||
private readonly Dictionary<string, int> _collections = new Dictionary<string, int>();
|
||||
private readonly Dictionary<string, List<VecRecord>> _vectors = new Dictionary<string, List<VecRecord>>();
|
||||
|
||||
public async Task CreateCollection(string collectionName, int dim)
|
||||
{
|
||||
_collections[collectionName] = dim;
|
||||
_vectors[collectionName] = new List<VecRecord>();
|
||||
}
|
||||
|
||||
public async Task<IEnumerable<string>> GetCollections()
|
||||
{
|
||||
return _collections.Select(x => x.Key).ToList();
|
||||
}
|
||||
|
||||
public Task<StringIdPagedItems<KnowledgeCollectionData>> GetCollectionData(string collectionName, KnowledgeFilter filter)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public async Task<IEnumerable<string>> Search(string collectionName, float[] vector, string returnFieldName, int limit = 5, float confidence = 0.5f)
|
||||
{
|
||||
if (!_vectors.ContainsKey(collectionName))
|
||||
{
|
||||
return new List<string>();
|
||||
}
|
||||
|
||||
var similarities = CalCosineSimilarity(vector, _vectors[collectionName]);
|
||||
// var similarities2 = CalEuclideanDistance(vector, _vectors[collectionName]);
|
||||
|
||||
var texts = np.argsort(similarities).ToArray<int>()
|
||||
.Reverse()
|
||||
.Take(limit)
|
||||
.Select(i => _vectors[collectionName][i].Text)
|
||||
.ToList();
|
||||
|
||||
return texts;
|
||||
}
|
||||
|
||||
public async Task<bool> Upsert(string collectionName, string id, float[] vector, string text, Dictionary<string, string>? payload = null)
|
||||
{
|
||||
_vectors[collectionName].Add(new VecRecord
|
||||
{
|
||||
Id = id,
|
||||
Vector = vector,
|
||||
Text = text
|
||||
});
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public Task<bool> DeleteCollectionData(string collectionName, string id)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
#region Private methods
|
||||
private float[] CalEuclideanDistance(float[] vec, List<VecRecord> records)
|
||||
{
|
||||
var a = np.zeros((records.Count, vec.Length), np.float32);
|
||||
var b = np.zeros((records.Count, vec.Length), np.float32);
|
||||
for (var i = 0; i < records.Count; i++)
|
||||
{
|
||||
a[i] = vec;
|
||||
b[i] = records[i].Vector;
|
||||
}
|
||||
|
||||
var c = np.sqrt(np.sum(np.square(a - b), axis: 1));
|
||||
// var c = -np.prod(np.linalg.norm(a, axis: 1) * np.linalg.norm(b, axis: 1), axis: 1);
|
||||
return c.ToArray<float>();
|
||||
}
|
||||
|
||||
private NDArray CalCosineSimilarity(float[] vec, List<VecRecord> records)
|
||||
{
|
||||
var recordsArray = np.zeros((records.Count, records[0].Vector.Length), dtype: np.float32);
|
||||
|
||||
for (int i = 0; i < records.Count; i++)
|
||||
{
|
||||
recordsArray[i] = records[i].Vector;
|
||||
}
|
||||
|
||||
var vecArray = np.expand_dims(np.array(vec, dtype: np.float32), axis: 0); // [1. 300]
|
||||
|
||||
(var normVecArray, var _) = SafeNormalize(vecArray);
|
||||
(var normRecordsArray, var _) = SafeNormalize(recordsArray);
|
||||
|
||||
var simiMatix = tf.matmul(tf.cast(normVecArray, tf.float32), tf.transpose(tf.cast(normRecordsArray, tf.float32))).numpy(); // [1, num_records]
|
||||
|
||||
simiMatix = np.squeeze(simiMatix, axis: 0);
|
||||
|
||||
return simiMatix;
|
||||
}
|
||||
|
||||
public (int, float)[] CalCosineSimilarityTopK(float[] vec, List<VecRecord> records, int topK = 10, float filterProb = 0.75f)
|
||||
{
|
||||
var simiMatix = CalCosineSimilarity(vec, records);
|
||||
|
||||
topK = Math.Min(topK, records.Count);
|
||||
var topIndex = np.argsort(simiMatix)["::-1"][$":{topK}"];
|
||||
|
||||
var resIndex = new List<(int, float)>();
|
||||
|
||||
for (int i = 0; i < topK; i++)
|
||||
{
|
||||
var index = topIndex[i];
|
||||
var value = simiMatix[index];
|
||||
|
||||
if (value > filterProb)
|
||||
{
|
||||
resIndex.Add((topIndex[i], value));
|
||||
}
|
||||
}
|
||||
|
||||
return resIndex.ToArray();
|
||||
}
|
||||
|
||||
private (NDArray, NDArray) SafeNormalize(NDArray x, double eps = 2.223E-15)
|
||||
{
|
||||
var squaredX = np.sum(np.multiply(x, x), axis: 1);
|
||||
var normX = np.sqrt(squaredX);
|
||||
|
||||
var epsTensor = tf.cast(tf.convert_to_tensor(eps), dtype: tf.float32);
|
||||
var normXTensor = tf.cast(normX, tf.float32);
|
||||
var contantMask = (normXTensor < epsTensor);
|
||||
var divideTensor = tf.ones_like(normXTensor, dtype: tf.float32);
|
||||
|
||||
normX = tf.where(contantMask, divideTensor, normXTensor).numpy();
|
||||
normX = np.expand_dims(normX, axis: 1);
|
||||
|
||||
return (x / normX, normX);
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
|
|
@ -0,0 +1,71 @@
|
|||
using BotSharp.Plugin.KnowledgeBase.Utilities;
|
||||
using Tensorflow.NumPy;
|
||||
|
||||
namespace BotSharp.Plugin.KnowledgeBase.MemVecDb;
|
||||
|
||||
public class MemoryVectorDb : IVectorDb
|
||||
{
|
||||
private readonly Dictionary<string, int> _collections = new Dictionary<string, int>();
|
||||
private readonly Dictionary<string, List<VecRecord>> _vectors = new Dictionary<string, List<VecRecord>>();
|
||||
|
||||
|
||||
public string Name => "MemoryVector";
|
||||
|
||||
public async Task CreateCollection(string collectionName, int dim)
|
||||
{
|
||||
_collections[collectionName] = dim;
|
||||
_vectors[collectionName] = new List<VecRecord>();
|
||||
}
|
||||
|
||||
public async Task<IEnumerable<string>> GetCollections()
|
||||
{
|
||||
return _collections.Select(x => x.Key).ToList();
|
||||
}
|
||||
|
||||
public Task<StringIdPagedItems<KnowledgeCollectionData>> GetCollectionData(string collectionName, KnowledgeFilter filter)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public async Task<IEnumerable<KnowledgeSearchResult>> Search(string collectionName, float[] vector,
|
||||
IEnumerable<string> fields, int limit = 5, float confidence = 0.5f, bool withVector = false)
|
||||
{
|
||||
if (!_vectors.ContainsKey(collectionName))
|
||||
{
|
||||
return new List<KnowledgeSearchResult>();
|
||||
}
|
||||
|
||||
var similarities = VectorUtility.CalCosineSimilarity(vector, _vectors[collectionName]);
|
||||
// var similarities = VectorUtility.CalEuclideanDistance(vector, _vectors[collectionName]);
|
||||
|
||||
var results = np.argsort(similarities).ToArray<int>()
|
||||
.Reverse()
|
||||
.Take(limit)
|
||||
.Select(i => new KnowledgeSearchResult
|
||||
{
|
||||
Data = new Dictionary<string, string> { { "text", _vectors[collectionName][i].Text } },
|
||||
Score = similarities[i],
|
||||
Vector = withVector ? _vectors[collectionName][i].Vector : null,
|
||||
})
|
||||
.ToList();
|
||||
|
||||
return await Task.FromResult(results);
|
||||
}
|
||||
|
||||
public async Task<bool> Upsert(string collectionName, string id, float[] vector, string text, Dictionary<string, string>? payload = null)
|
||||
{
|
||||
_vectors[collectionName].Add(new VecRecord
|
||||
{
|
||||
Id = id,
|
||||
Vector = vector,
|
||||
Text = text
|
||||
});
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public Task<bool> DeleteCollectionData(string collectionName, string id)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,28 @@
|
|||
namespace BotSharp.Plugin.KnowledgeBase.Services;
|
||||
|
||||
public partial class KnowledgeService
|
||||
{
|
||||
public async Task FeedKnowledge(KnowledgeCreationModel knowledge)
|
||||
{
|
||||
var index = 0;
|
||||
var lines = _textChopper.Chop(knowledge.Content, new ChunkOption
|
||||
{
|
||||
Size = 1024,
|
||||
Conjunction = 32,
|
||||
SplitByWord = true,
|
||||
});
|
||||
|
||||
var db = GetVectorDb();
|
||||
var textEmbedding = GetTextEmbedding();
|
||||
|
||||
await db.CreateCollection(knowledge.Collection, textEmbedding.Dimension);
|
||||
foreach (var line in lines)
|
||||
{
|
||||
var vec = await textEmbedding.GetVectorAsync(line);
|
||||
var id = Guid.NewGuid().ToString();
|
||||
await db.Upsert(knowledge.Collection, id, vec, line);
|
||||
index++;
|
||||
Console.WriteLine($"Saved vector {index}/{lines.Count}: {line}\n");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -2,20 +2,6 @@ namespace BotSharp.Plugin.KnowledgeBase.Services;
|
|||
|
||||
public partial class KnowledgeService
|
||||
{
|
||||
public async Task<StringIdPagedItems<KnowledgeCollectionData>> GetKnowledgeCollectionData(string collectionName, KnowledgeFilter filter)
|
||||
{
|
||||
try
|
||||
{
|
||||
var db = GetVectorDb();
|
||||
return await db.GetCollectionData(collectionName, filter);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogWarning($"Error when getting knowledge collection data ({collectionName}). {ex.Message}\r\n{ex.InnerException}");
|
||||
return new StringIdPagedItems<KnowledgeCollectionData>();
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<bool> DeleteKnowledgeCollectionData(string collectionName, string id)
|
||||
{
|
||||
try
|
||||
|
|
@ -0,0 +1,38 @@
|
|||
namespace BotSharp.Plugin.KnowledgeBase.Services;
|
||||
|
||||
public partial class KnowledgeService
|
||||
{
|
||||
public async Task<StringIdPagedItems<KnowledgeCollectionData>> GetKnowledgeCollectionData(string collectionName, KnowledgeFilter filter)
|
||||
{
|
||||
try
|
||||
{
|
||||
var db = GetVectorDb();
|
||||
return await db.GetCollectionData(collectionName, filter);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogWarning($"Error when getting knowledge collection data ({collectionName}). {ex.Message}\r\n{ex.InnerException}");
|
||||
return new StringIdPagedItems<KnowledgeCollectionData>();
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<IEnumerable<KnowledgeRetrievalResult>> SearchKnowledge(KnowledgeRetrievalModel model)
|
||||
{
|
||||
var textEmbedding = GetTextEmbedding();
|
||||
var vector = await textEmbedding.GetVectorAsync(model.Text);
|
||||
|
||||
// Vector search
|
||||
var db = GetVectorDb();
|
||||
var collection = !string.IsNullOrWhiteSpace(model.Collection) ? model.Collection : KnowledgeCollectionName.BotSharp;
|
||||
var fields = !model.Fields.IsNullOrEmpty() ? model.Fields : new List<string> { KnowledgePayloadName.Text, KnowledgePayloadName.Answer };
|
||||
var found = await db.Search(collection, vector, fields, limit: model.Limit ?? 5, confidence: model.Confidence ?? 0.5f, withVector: model.WithVector);
|
||||
|
||||
var results = found.Select(x => new KnowledgeRetrievalResult
|
||||
{
|
||||
Data = x.Data,
|
||||
Score = x.Score,
|
||||
Vector = x.Vector
|
||||
}).ToList();
|
||||
return results;
|
||||
}
|
||||
}
|
||||
|
|
@ -7,7 +7,8 @@ public partial class KnowledgeService : IKnowledgeService
|
|||
private readonly ITextChopper _textChopper;
|
||||
private readonly ILogger<KnowledgeService> _logger;
|
||||
|
||||
public KnowledgeService(IServiceProvider services,
|
||||
public KnowledgeService(
|
||||
IServiceProvider services,
|
||||
KnowledgeBaseSettings settings,
|
||||
ITextChopper textChopper,
|
||||
ILogger<KnowledgeService> logger)
|
||||
|
|
@ -18,104 +19,19 @@ public partial class KnowledgeService : IKnowledgeService
|
|||
_logger = logger;
|
||||
}
|
||||
|
||||
public async Task EmbedKnowledge(KnowledgeCreationModel knowledge)
|
||||
private IVectorDb GetVectorDb()
|
||||
{
|
||||
var idStart = 0;
|
||||
var lines = _textChopper.Chop(knowledge.Content, new ChunkOption
|
||||
{
|
||||
Size = 1024,
|
||||
Conjunction = 32,
|
||||
SplitByWord = true,
|
||||
});
|
||||
|
||||
var db = GetVectorDb();
|
||||
var textEmbedding = GetTextEmbedding();
|
||||
|
||||
await db.CreateCollection(KnowledgeCollectionName.BotSharp, textEmbedding.Dimension);
|
||||
foreach (var line in lines)
|
||||
{
|
||||
var vec = await textEmbedding.GetVectorAsync(line);
|
||||
await db.Upsert(KnowledgeCollectionName.BotSharp, idStart.ToString(), vec, line);
|
||||
idStart++;
|
||||
Console.WriteLine($"Saved vector {idStart}/{lines.Count}: {line}\n");
|
||||
}
|
||||
}
|
||||
|
||||
public async Task Feed(KnowledgeFeedModel knowledge)
|
||||
{
|
||||
var idStart = 0;
|
||||
var lines = _textChopper.Chop(knowledge.Content, new ChunkOption
|
||||
{
|
||||
Size = 1024,
|
||||
Conjunction = 32,
|
||||
SplitByWord = true,
|
||||
});
|
||||
|
||||
var db = GetVectorDb();
|
||||
var textEmbedding = GetTextEmbedding();
|
||||
|
||||
await db.CreateCollection(knowledge.AgentId, textEmbedding.Dimension);
|
||||
foreach (var line in lines)
|
||||
{
|
||||
var vec = await textEmbedding.GetVectorAsync(line);
|
||||
await db.Upsert(knowledge.AgentId, idStart.ToString(), vec, line);
|
||||
idStart++;
|
||||
Console.WriteLine($"Saved vector {idStart}/{lines.Count}: {line}\n");
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<string> GetKnowledges(KnowledgeRetrievalModel retrievalModel)
|
||||
{
|
||||
var textEmbedding = GetTextEmbedding();
|
||||
var vector = await textEmbedding.GetVectorAsync(retrievalModel.Question);
|
||||
|
||||
// Vector search
|
||||
var db = GetVectorDb();
|
||||
var result = await db.Search(KnowledgeCollectionName.BotSharp, vector, KnowledgePayloadName.Answer, limit: 10);
|
||||
|
||||
// Restore
|
||||
return string.Join("\n\n", result.Select((x, i) => $"### Paragraph {i + 1} ###\n{x.Trim()}"));
|
||||
}
|
||||
|
||||
public async Task<List<RetrievedResult>> GetAnswer(KnowledgeRetrievalModel retrievalModel)
|
||||
{
|
||||
// Restore
|
||||
var prompt = await GetKnowledges(retrievalModel);
|
||||
|
||||
var sb = new StringBuilder(prompt);
|
||||
sb.AppendLine();
|
||||
sb.AppendLine();
|
||||
sb.AppendLine("------");
|
||||
sb.AppendLine("Answer question based on the given information above. Keep your answers concise. Please response with paragraph number, cite sources and reasoning in JSON format, if multiple paragraphs are found, put them in a JSON array. make sure the paragraph number is real. If you don't know the answer just output empty.");
|
||||
sb.AppendLine("[" + JsonSerializer.Serialize(new RetrievedResult()) + "]");
|
||||
sb.AppendLine("------");
|
||||
sb.AppendLine($"QUESTION: \"{retrievalModel.Question}\"");
|
||||
sb.AppendLine("Which paragraphs are relevant in order to answer the above question?");
|
||||
sb.AppendLine("ANSWER: ");
|
||||
prompt = sb.ToString().Trim();
|
||||
|
||||
var completion = await GetTextCompletion().GetCompletion(prompt, Guid.Empty.ToString(), Guid.Empty.ToString());
|
||||
return JsonSerializer.Deserialize<List<RetrievedResult>>(completion);
|
||||
}
|
||||
|
||||
public IVectorDb GetVectorDb()
|
||||
{
|
||||
var db = _services.GetServices<IVectorDb>()
|
||||
.FirstOrDefault(x => x.GetType().FullName.EndsWith(_settings.VectorDb));
|
||||
var db = _services.GetServices<IVectorDb>().FirstOrDefault(x => x.Name == _settings.VectorDb);
|
||||
return db;
|
||||
}
|
||||
|
||||
public ITextEmbedding GetTextEmbedding()
|
||||
private ITextEmbedding GetTextEmbedding()
|
||||
{
|
||||
var embedding = _services.GetServices<ITextEmbedding>()
|
||||
.FirstOrDefault(x => x.GetType().FullName.EndsWith(_settings.TextEmbedding));
|
||||
var embedding = _services.GetServices<ITextEmbedding>().FirstOrDefault(x => x.Provider == _settings.TextEmbedding.Provider);
|
||||
if (embedding != null)
|
||||
{
|
||||
embedding.SetModelName(_settings.TextEmbedding.Model);
|
||||
}
|
||||
return embedding;
|
||||
}
|
||||
|
||||
public ITextCompletion GetTextCompletion()
|
||||
{
|
||||
var textCompletion = _services.GetServices<ITextCompletion>()
|
||||
.FirstOrDefault(x => x.GetType().FullName.EndsWith(_settings.TextCompletion));
|
||||
return textCompletion;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,14 +0,0 @@
|
|||
namespace BotSharp.Plugin.KnowledgeBase.Services;
|
||||
|
||||
public partial class KnowledgeService
|
||||
{
|
||||
public async Task<List<KnowledgeChunk>> CollectChunkedKnowledge()
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public async Task EmbedKnowledge(List<KnowledgeChunk> chunks)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
}
|
||||
|
|
@ -5,6 +5,8 @@ namespace BotSharp.Plugin.KnowledgeBase.Services;
|
|||
|
||||
public class PigPdf2TextConverter : IPdf2TextConverter
|
||||
{
|
||||
public string Name => "Pig";
|
||||
|
||||
public Task<string> ConvertPdfToText(string filePath, int? startPageNum, int? endPageNum)
|
||||
{
|
||||
// since PdfDocument.Open is not async, we dont need to make this method async
|
||||
|
|
|
|||
|
|
@ -0,0 +1,83 @@
|
|||
using BotSharp.Plugin.KnowledgeBase.MemVecDb;
|
||||
using Tensorflow.NumPy;
|
||||
using static Tensorflow.Binding;
|
||||
|
||||
namespace BotSharp.Plugin.KnowledgeBase.Utilities;
|
||||
|
||||
public static class VectorUtility
|
||||
{
|
||||
public static float[] CalEuclideanDistance(float[] vec, List<VecRecord> records)
|
||||
{
|
||||
var a = np.zeros((records.Count, vec.Length), np.float32);
|
||||
var b = np.zeros((records.Count, vec.Length), np.float32);
|
||||
for (var i = 0; i < records.Count; i++)
|
||||
{
|
||||
a[i] = vec;
|
||||
b[i] = records[i].Vector;
|
||||
}
|
||||
|
||||
var c = np.sqrt(np.sum(np.square(a - b), axis: 1));
|
||||
// var c = -np.prod(np.linalg.norm(a, axis: 1) * np.linalg.norm(b, axis: 1), axis: 1);
|
||||
return c.ToArray<float>();
|
||||
}
|
||||
|
||||
public static NDArray CalCosineSimilarity(float[] vec, List<VecRecord> records)
|
||||
{
|
||||
var recordsArray = np.zeros((records.Count, records[0].Vector.Length), dtype: np.float32);
|
||||
|
||||
for (int i = 0; i < records.Count; i++)
|
||||
{
|
||||
recordsArray[i] = records[i].Vector;
|
||||
}
|
||||
|
||||
var vecArray = np.expand_dims(np.array(vec, dtype: np.float32), axis: 0); // [1. 300]
|
||||
|
||||
(var normVecArray, var _) = SafeNormalize(vecArray);
|
||||
(var normRecordsArray, var _) = SafeNormalize(recordsArray);
|
||||
|
||||
var simiMatix = tf.matmul(tf.cast(normVecArray, tf.float32), tf.transpose(tf.cast(normRecordsArray, tf.float32))).numpy(); // [1, num_records]
|
||||
|
||||
simiMatix = np.squeeze(simiMatix, axis: 0);
|
||||
|
||||
return simiMatix;
|
||||
}
|
||||
|
||||
public static (int, float)[] CalCosineSimilarityTopK(float[] vec, List<VecRecord> records, int topK = 10, float filterProb = 0.75f)
|
||||
{
|
||||
var simiMatix = CalCosineSimilarity(vec, records);
|
||||
|
||||
topK = Math.Min(topK, records.Count);
|
||||
var topIndex = np.argsort(simiMatix)["::-1"][$":{topK}"];
|
||||
|
||||
var resIndex = new List<(int, float)>();
|
||||
|
||||
for (int i = 0; i < topK; i++)
|
||||
{
|
||||
var index = topIndex[i];
|
||||
var value = simiMatix[index];
|
||||
|
||||
if (value > filterProb)
|
||||
{
|
||||
resIndex.Add((topIndex[i], value));
|
||||
}
|
||||
}
|
||||
|
||||
return resIndex.ToArray();
|
||||
}
|
||||
|
||||
private static (NDArray, NDArray) SafeNormalize(NDArray x, double eps = 2.223E-15)
|
||||
{
|
||||
var squaredX = np.sum(np.multiply(x, x), axis: 1);
|
||||
var normX = np.sqrt(squaredX);
|
||||
|
||||
var epsTensor = tf.cast(tf.convert_to_tensor(eps), dtype: tf.float32);
|
||||
var normXTensor = tf.cast(normX, tf.float32);
|
||||
var contantMask = (normXTensor < epsTensor);
|
||||
var divideTensor = tf.ones_like(normXTensor, dtype: tf.float32);
|
||||
|
||||
normX = tf.where(contantMask, divideTensor, normXTensor).numpy();
|
||||
normX = np.expand_dims(normX, axis: 1);
|
||||
|
||||
return (x / normX, normX);
|
||||
}
|
||||
}
|
||||
|
|
@ -9,6 +9,8 @@ namespace BotSharp.Plugin.MetaAI.Providers;
|
|||
|
||||
public class FaissDb : IVectorDb
|
||||
{
|
||||
public string Name => "Faiss";
|
||||
|
||||
public Task CreateCollection(string collectionName, int dim)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
|
|
@ -24,7 +26,8 @@ public class FaissDb : IVectorDb
|
|||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public Task<IEnumerable<string>> Search(string collectionName, float[] vector, string returnFieldName, int limit = 10, float confidence = 0.5f)
|
||||
public Task<IEnumerable<KnowledgeSearchResult>> Search(string collectionName, float[] vector,
|
||||
IEnumerable<string> fields, int limit = 10, float confidence = 0.5f, bool withVector = false)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
|
|
|||
|
|
@ -19,15 +19,18 @@ using BotSharp.Plugin.PaddleSharp.Settings;
|
|||
namespace BotSharp.Plugin.PaddleSharp.Providers;
|
||||
|
||||
public class Pdf2TextConverter : IPdf2TextConverter
|
||||
{
|
||||
{
|
||||
private Dictionary<int, string> _mappings = new Dictionary<int, string>();
|
||||
private FullOcrModel _model;
|
||||
private PaddleSharpSettings _paddleSharpSettings;
|
||||
|
||||
public Pdf2TextConverter(PaddleSharpSettings paddleSharpSettings)
|
||||
{
|
||||
_paddleSharpSettings = paddleSharpSettings;
|
||||
}
|
||||
|
||||
public string Name => "Paddle";
|
||||
|
||||
public async Task<string> ConvertPdfToText(string filePath, int? startPageNum, int? endPageNum)
|
||||
{
|
||||
await ConvertPdfToLocalImagesAsync(filePath, startPageNum, endPageNum);
|
||||
|
|
|
|||
|
|
@ -10,14 +10,16 @@ public class QdrantDb : IVectorDb
|
|||
private readonly QdrantSetting _setting;
|
||||
private readonly IServiceProvider _services;
|
||||
|
||||
public QdrantDb(QdrantSetting setting,
|
||||
public QdrantDb(
|
||||
QdrantSetting setting,
|
||||
IServiceProvider services)
|
||||
{
|
||||
_setting = setting;
|
||||
_services = services;
|
||||
|
||||
}
|
||||
|
||||
public string Name => "Qdrant";
|
||||
|
||||
private QdrantClient GetClient()
|
||||
{
|
||||
if (_client == null)
|
||||
|
|
@ -42,9 +44,8 @@ public class QdrantDb : IVectorDb
|
|||
public async Task<StringIdPagedItems<KnowledgeCollectionData>> GetCollectionData(string collectionName, KnowledgeFilter filter)
|
||||
{
|
||||
var client = GetClient();
|
||||
|
||||
var exists = await client.CollectionExistsAsync(collectionName);
|
||||
if (!exists)
|
||||
var exist = await DoesCollectionExist(client, collectionName);
|
||||
if (!exist)
|
||||
{
|
||||
return new StringIdPagedItems<KnowledgeCollectionData>();
|
||||
}
|
||||
|
|
@ -71,11 +72,12 @@ public class QdrantDb : IVectorDb
|
|||
|
||||
public async Task CreateCollection(string collectionName, int dim)
|
||||
{
|
||||
var collections = await GetCollections();
|
||||
if (!collections.Contains(collectionName))
|
||||
var client = GetClient();
|
||||
var exist = await DoesCollectionExist(client, collectionName);
|
||||
if (!exist)
|
||||
{
|
||||
// Create a new collection
|
||||
await GetClient().CreateCollectionAsync(collectionName, new VectorParams()
|
||||
await client.CreateCollectionAsync(collectionName, new VectorParams()
|
||||
{
|
||||
Size = (ulong)dim,
|
||||
Distance = Distance.Cosine
|
||||
|
|
@ -83,7 +85,7 @@ public class QdrantDb : IVectorDb
|
|||
}
|
||||
|
||||
// Get collection info
|
||||
var collectionInfo = await _client.GetCollectionInfoAsync(collectionName);
|
||||
var collectionInfo = await client.GetCollectionInfoAsync(collectionName);
|
||||
if (collectionInfo == null)
|
||||
{
|
||||
throw new Exception($"Create {collectionName} failed.");
|
||||
|
|
@ -115,7 +117,6 @@ public class QdrantDb : IVectorDb
|
|||
}
|
||||
|
||||
var client = GetClient();
|
||||
|
||||
var result = await client.UpsertAsync(collectionName, points: new List<PointStruct>
|
||||
{
|
||||
point
|
||||
|
|
@ -124,19 +125,54 @@ public class QdrantDb : IVectorDb
|
|||
return result.Status == UpdateStatus.Completed;
|
||||
}
|
||||
|
||||
public async Task<IEnumerable<string>> Search(string collectionName, float[] vector, string returnFieldName, int limit = 5, float confidence = 0.5f)
|
||||
public async Task<IEnumerable<KnowledgeSearchResult>> Search(string collectionName, float[] vector,
|
||||
IEnumerable<string> fields, int limit = 5, float confidence = 0.5f, bool withVector = false)
|
||||
{
|
||||
var client = GetClient();
|
||||
var points = await client.SearchAsync(collectionName, vector, limit: (ulong)limit, scoreThreshold: confidence);
|
||||
|
||||
return points.Select(x => x.Payload[returnFieldName].StringValue).ToList();
|
||||
var results = new List<KnowledgeSearchResult>();
|
||||
foreach (var point in points)
|
||||
{
|
||||
var data = new Dictionary<string, string>();
|
||||
foreach (var field in fields)
|
||||
{
|
||||
if (point.Payload.ContainsKey(field))
|
||||
{
|
||||
data[field] = point.Payload[field].StringValue;
|
||||
}
|
||||
else
|
||||
{
|
||||
data[field] = "";
|
||||
}
|
||||
}
|
||||
|
||||
results.Add(new KnowledgeSearchResult
|
||||
{
|
||||
Data = data,
|
||||
Score = point.Score,
|
||||
Vector = withVector ? point.Vectors?.Vector?.Data?.ToArray() : null
|
||||
});
|
||||
}
|
||||
|
||||
return results;
|
||||
}
|
||||
|
||||
public async Task<bool> DeleteCollectionData(string collectionName, string id)
|
||||
{
|
||||
if (!Guid.TryParse(id, out var guid))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
var client = GetClient();
|
||||
var guid = Guid.Parse(id);
|
||||
var result = await client.DeleteAsync(collectionName, guid);
|
||||
return result.Status == UpdateStatus.Completed;
|
||||
}
|
||||
|
||||
|
||||
private async Task<bool> DoesCollectionExist(QdrantClient client, string collectionName)
|
||||
{
|
||||
return await client.CollectionExistsAsync(collectionName);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -57,8 +57,8 @@ public class IntentClassifier
|
|||
return;
|
||||
}
|
||||
|
||||
var vector = _services.GetServices<ITextEmbedding>()
|
||||
.FirstOrDefault(x => x.GetType().FullName.EndsWith(_knowledgeBaseSettings.TextEmbedding));
|
||||
var vector = _services.GetServices<ITextEmbedding>().FirstOrDefault(x => x.Provider == _knowledgeBaseSettings.TextEmbedding.Provider);
|
||||
vector.SetModelName(_knowledgeBaseSettings.TextEmbedding.Model);
|
||||
|
||||
var layers = new List<ILayer>
|
||||
{
|
||||
|
|
@ -136,8 +136,8 @@ public class IntentClassifier
|
|||
public NDArray GetTextEmbedding(string text)
|
||||
{
|
||||
var knowledgeSettings = _services.GetRequiredService<KnowledgeBaseSettings>();
|
||||
var embedding = _services.GetServices<ITextEmbedding>()
|
||||
.FirstOrDefault(x => x.GetType().FullName.EndsWith(knowledgeSettings.TextEmbedding));
|
||||
var embedding = _services.GetServices<ITextEmbedding>() .FirstOrDefault(x => x.Provider == knowledgeSettings.TextEmbedding.Provider);
|
||||
embedding.SetModelName(knowledgeSettings.TextEmbedding.Model);
|
||||
|
||||
var x = np.zeros((1, embedding.Dimension), dtype: np.float32);
|
||||
x[0] = embedding.GetVectorAsync(text).GetAwaiter().GetResult();
|
||||
|
|
|
|||
|
|
@ -20,6 +20,10 @@ namespace BotSharp.Plugin.SemanticKernel
|
|||
{
|
||||
this._memoryStore = memoryStore;
|
||||
}
|
||||
|
||||
|
||||
public string Name => "SemanticKernel";
|
||||
|
||||
public async Task CreateCollection(string collectionName, int dim)
|
||||
{
|
||||
await _memoryStore.CreateCollectionAsync(collectionName);
|
||||
|
|
@ -40,18 +44,23 @@ namespace BotSharp.Plugin.SemanticKernel
|
|||
return result;
|
||||
}
|
||||
|
||||
public async Task<IEnumerable<string>> Search(string collectionName, float[] vector, string returnFieldName, int limit = 5, float confidence = 0.5f)
|
||||
public async Task<IEnumerable<KnowledgeSearchResult>> Search(string collectionName, float[] vector,
|
||||
IEnumerable<string> fields, int limit = 5, float confidence = 0.5f, bool withVector = false)
|
||||
{
|
||||
var results = _memoryStore.GetNearestMatchesAsync(collectionName, vector, limit);
|
||||
|
||||
var resultTexts = new List<string>();
|
||||
await foreach (var (record, _) in results)
|
||||
var resultTexts = new List<KnowledgeSearchResult>();
|
||||
await foreach (var (record, score) in results)
|
||||
{
|
||||
resultTexts.Add(record.Metadata.Text);
|
||||
resultTexts.Add(new KnowledgeSearchResult
|
||||
{
|
||||
Data = new Dictionary<string, string> { { "text", record.Metadata.Text } },
|
||||
Score = score,
|
||||
Vector = withVector ? record.Embedding.ToArray() : null
|
||||
});
|
||||
}
|
||||
|
||||
return resultTexts;
|
||||
|
||||
}
|
||||
|
||||
public async Task<bool> Upsert(string collectionName, string id, float[] vector, string text, Dictionary<string, string>? payload)
|
||||
|
|
@ -62,9 +71,16 @@ namespace BotSharp.Plugin.SemanticKernel
|
|||
return true;
|
||||
}
|
||||
|
||||
public Task<bool> DeleteCollectionData(string collectionName, string id)
|
||||
public async Task<bool> DeleteCollectionData(string collectionName, string id)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
var exist = await _memoryStore.DoesCollectionExistAsync(collectionName);
|
||||
|
||||
if (exist)
|
||||
{
|
||||
await _memoryStore.RemoveAsync(collectionName, id);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,3 +1,4 @@
|
|||
using BotSharp.Abstraction.Files;
|
||||
using BotSharp.Abstraction.Files.Converters;
|
||||
using BotSharp.Abstraction.Files.Enums;
|
||||
using BotSharp.Abstraction.Files.Utilities;
|
||||
|
|
@ -252,8 +253,9 @@ public partial class TencentCosService
|
|||
|
||||
private IPdf2ImageConverter? GetPdf2ImageConverter()
|
||||
{
|
||||
var converters = _services.GetServices<IPdf2ImageConverter>();
|
||||
return converters.FirstOrDefault();
|
||||
var settings = _services.GetRequiredService<FileCoreSettings>();
|
||||
var converter = _services.GetServices<IPdf2ImageConverter>().FirstOrDefault(x => x.Name == settings.Pdf2ImageConverter);
|
||||
return converter;
|
||||
}
|
||||
|
||||
private string BuilFileUrl(string file)
|
||||
|
|
|
|||
|
|
@ -18,10 +18,10 @@ public class TencentCosPlugin : IBotSharpPlugin
|
|||
|
||||
public void RegisterDI(IServiceCollection services, IConfiguration config)
|
||||
{
|
||||
var myFileStorageSettings = new FileStorageSettings();
|
||||
config.Bind("FileStorage", myFileStorageSettings);
|
||||
var fileCoreSettings = new FileCoreSettings();
|
||||
config.Bind("FileCore", fileCoreSettings);
|
||||
|
||||
if (myFileStorageSettings.Default == FileStorageEnum.TencentCosStorage)
|
||||
if (fileCoreSettings.Storage == FileStorageEnum.TencentCosStorage)
|
||||
{
|
||||
services.AddScoped(provider =>
|
||||
{
|
||||
|
|
|
|||
|
|
@ -230,9 +230,13 @@
|
|||
"FileRepository": "data",
|
||||
"Assemblies": [ "BotSharp.Core" ]
|
||||
},
|
||||
"FileStorage": {
|
||||
"Default": "LocalFileStorage"
|
||||
|
||||
"FileCore": {
|
||||
"Storage": "LocalFileStorage",
|
||||
"Pdf2TextConverter": "",
|
||||
"Pdf2ImageConverter": ""
|
||||
},
|
||||
|
||||
"TencentCos": {
|
||||
"AppId": "",
|
||||
"SecretId": "",
|
||||
|
|
@ -254,10 +258,11 @@
|
|||
},
|
||||
|
||||
"KnowledgeBase": {
|
||||
"VectorDb": "MemVectorDatabase",
|
||||
"TextEmbedding": "fastTextEmbeddingProvider",
|
||||
"TextCompletion": "AzureOpenAI.Providers.TextCompletionProvider",
|
||||
"Pdf2TextConverter": "PigPdf2TextConverter"
|
||||
"VectorDb": "Qdrant",
|
||||
"TextEmbedding": {
|
||||
"Provider": "openai",
|
||||
"Model": "text-embedding-3-small"
|
||||
}
|
||||
},
|
||||
|
||||
"SparkDesk": {
|
||||
|
|
|
|||
Loading…
Reference in a new issue