Merge pull request #584 from iceljc/bugfix/resolve-conflict

resolve conflict
This commit is contained in:
iceljc 2024-08-09 17:40:14 -05:00 committed by GitHub
commit e3d9d38820
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
9 changed files with 32 additions and 28 deletions

View file

@ -1,11 +1,9 @@
using BotSharp.Abstraction.Knowledges.Models;
namespace BotSharp.Abstraction.Knowledges;
public interface IKnowledgeService
{
Task<IEnumerable<KnowledgeRetrievalResult>> SearchKnowledge(KnowledgeRetrievalModel model);
Task FeedKnowledge(KnowledgeCreationModel model);
Task<IEnumerable<KnowledgeRetrievalResult>> SearchKnowledge(string collectionName, KnowledgeRetrievalOptions options);
Task FeedKnowledge(string collectionName, KnowledgeCreationModel model);
Task<StringIdPagedItems<KnowledgeCollectionData>> GetKnowledgeCollectionData(string collectionName, KnowledgeFilter filter);
Task<bool> DeleteKnowledgeCollectionData(string collectionName, string id);
}

View file

@ -1,9 +1,6 @@
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;
}

View file

@ -2,9 +2,8 @@ using BotSharp.Abstraction.Knowledges.Enums;
namespace BotSharp.Abstraction.Knowledges.Models;
public class KnowledgeRetrievalModel
public class KnowledgeRetrievalOptions
{
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;

View file

@ -1,7 +1,7 @@
using BotSharp.Abstraction.Knowledges.Enums;
using BotSharp.Abstraction.Knowledges.Models;
using BotSharp.Abstraction.Knowledges.Settings;
using BotSharp.OpenAPI.ViewModels.Knowledges;
using Microsoft.Extensions.Options;
namespace BotSharp.OpenAPI.Controllers;
@ -18,12 +18,11 @@ public class KnowledgeBaseController : ControllerBase
_services = services;
}
[HttpPost("/knowledge/search")]
public async Task<IEnumerable<KnowledgeRetrivalViewModel>> SearchKnowledge([FromBody] SearchKnowledgeModel model)
[HttpPost("/knowledge/{collection}/search")]
public async Task<IEnumerable<KnowledgeRetrivalViewModel>> SearchKnowledge([FromQuery] string collection, [FromBody] SearchKnowledgeModel model)
{
var searchModel = new KnowledgeRetrievalModel
var options = new KnowledgeRetrievalOptions
{
Collection = model.Collection,
Text = model.Text,
Fields = model.Fields,
Limit = model.Limit ?? 5,
@ -31,7 +30,7 @@ public class KnowledgeBaseController : ControllerBase
WithVector = model.WithVector
};
var results = await _knowledgeService.SearchKnowledge(searchModel);
var results = await _knowledgeService.SearchKnowledge(collection, options);
return results.Select(x => KnowledgeRetrivalViewModel.From(x)).ToList();
}
@ -56,8 +55,8 @@ public class KnowledgeBaseController : ControllerBase
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)
[HttpPost("/knowledge/{collection}/upload")]
public async Task<IActionResult> UploadKnowledge([FromRoute] string collection, [FromForm] IFormFile file, [FromForm] int? startPageNum, [FromForm] int? endPageNum)
{
var setttings = _services.GetRequiredService<FileCoreSettings>();
var textConverter = _services.GetServices<IPdf2TextConverter>().FirstOrDefault(x => x.Name == setttings.Pdf2TextConverter);
@ -70,9 +69,8 @@ public class KnowledgeBaseController : ControllerBase
}
var content = await textConverter.ConvertPdfToText(filePath, startPageNum, endPageNum);
await _knowledgeService.FeedKnowledge(new KnowledgeCreationModel
await _knowledgeService.FeedKnowledge(collection, new KnowledgeCreationModel
{
Collection = collection ?? KnowledgeCollectionName.BotSharp,
Content = content
});

View file

@ -37,7 +37,6 @@ public class MemorizeKnowledgeFn : IFunctionCallback
});
message.Content = result ? "Saved to my brain" : "I forgot it";
return true;
}
}

View file

@ -2,7 +2,7 @@ namespace BotSharp.Plugin.KnowledgeBase.Services;
public partial class KnowledgeService
{
public async Task FeedKnowledge(KnowledgeCreationModel knowledge)
public async Task FeedKnowledge(string collectionName, KnowledgeCreationModel knowledge)
{
var index = 0;
var lines = _textChopper.Chop(knowledge.Content, new ChunkOption
@ -15,12 +15,12 @@ public partial class KnowledgeService
var db = GetVectorDb();
var textEmbedding = GetTextEmbedding();
await db.CreateCollection(knowledge.Collection, textEmbedding.Dimension);
await db.CreateCollection(collectionName, 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);
await db.Upsert(collectionName, id, vec, line);
index++;
Console.WriteLine($"Saved vector {index}/{lines.Count}: {line}\n");
}

View file

@ -16,16 +16,15 @@ public partial class KnowledgeService
}
}
public async Task<IEnumerable<KnowledgeRetrievalResult>> SearchKnowledge(KnowledgeRetrievalModel model)
public async Task<IEnumerable<KnowledgeRetrievalResult>> SearchKnowledge(string collectionName, KnowledgeRetrievalOptions options)
{
var textEmbedding = GetTextEmbedding();
var vector = await textEmbedding.GetVectorAsync(model.Text);
var vector = await textEmbedding.GetVectorAsync(options.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 fields = !options.Fields.IsNullOrEmpty() ? options.Fields : new List<string> { KnowledgePayloadName.Text, KnowledgePayloadName.Answer };
var found = await db.Search(collectionName, vector, fields, limit: options.Limit ?? 5, confidence: options.Confidence ?? 0.5f, withVector: options.WithVector);
var results = found.Select(x => new KnowledgeRetrievalResult
{

View file

@ -0,0 +1,14 @@
namespace BotSharp.Plugin.TencentCos.Services;
public partial class TencentCosService
{
public Task SaveSpeechFileAsync(string conversationId, string fileName, BinaryData data)
{
throw new NotImplementedException();
}
public Task<BinaryData> RetrieveSpeechFileAsync(string conversationId, string fileName)
{
throw new NotImplementedException();
}
}