Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

resolve conflict #584

Merged
merged 1 commit into from
Aug 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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);
}
Original file line number Diff line number Diff line change
@@ -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;
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@

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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -18,20 +18,19 @@ public KnowledgeBaseController(IKnowledgeService knowledgeService, IServiceProvi
_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,
Confidence = model.Confidence ?? 0.5f,
WithVector = model.WithVector
};

var results = await _knowledgeService.SearchKnowledge(searchModel);
var results = await _knowledgeService.SearchKnowledge(collection, options);
return results.Select(x => KnowledgeRetrivalViewModel.From(x)).ToList();
}

Expand All @@ -56,8 +55,8 @@ public async Task<bool> DeleteKnowledgeCollectionData([FromRoute] string collect
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);
Expand All @@ -70,9 +69,8 @@ public async Task<IActionResult> UploadKnowledge(IFormFile file, [FromQuery] str
}

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
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ public async Task<bool> Execute(RoleDialogModel message)
});

message.Content = result ? "Saved to my brain" : "I forgot it";

return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -15,12 +15,12 @@ public async Task FeedKnowledge(KnowledgeCreationModel knowledge)
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");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,15 @@ public async Task<StringIdPagedItems<KnowledgeCollectionData>> GetKnowledgeColle
}
}

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
{
Expand Down
Original file line number Diff line number Diff line change
@@ -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();
}
}