Skip to content
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
16 changes: 15 additions & 1 deletion BotSharp.sln
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,11 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BotSharp.Plugin.TencentCos"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Interpreters", "Interpreters", "{C4C59872-3C8A-450D-83D5-2BE402D610D5}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BotSharp.Plugin.PythonInterpreter", "src\Plugins\BotSharp.Plugin.PythonInterpreter\BotSharp.Plugin.PythonInterpreter.csproj", "{05E6E405-5021-406E-8A5E-0A7CEC881F6D}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BotSharp.Plugin.PythonInterpreter", "src\Plugins\BotSharp.Plugin.PythonInterpreter\BotSharp.Plugin.PythonInterpreter.csproj", "{05E6E405-5021-406E-8A5E-0A7CEC881F6D}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Graph", "Graph", "{97A0B191-64D7-4F8A-BFE8-1BFCC5E247E1}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BotSharp.Plugin.Graph", "src\Plugins\BotSharp.Plugin.Graph\BotSharp.Plugin.Graph.csproj", "{EBFE97DA-D0BA-48BA-8B5D-083B60348D1D}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand Down Expand Up @@ -437,6 +441,14 @@ Global
{05E6E405-5021-406E-8A5E-0A7CEC881F6D}.Release|Any CPU.Build.0 = Release|Any CPU
{05E6E405-5021-406E-8A5E-0A7CEC881F6D}.Release|x64.ActiveCfg = Release|Any CPU
{05E6E405-5021-406E-8A5E-0A7CEC881F6D}.Release|x64.Build.0 = Release|Any CPU
{EBFE97DA-D0BA-48BA-8B5D-083B60348D1D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{EBFE97DA-D0BA-48BA-8B5D-083B60348D1D}.Debug|Any CPU.Build.0 = Debug|Any CPU
{EBFE97DA-D0BA-48BA-8B5D-083B60348D1D}.Debug|x64.ActiveCfg = Debug|Any CPU
{EBFE97DA-D0BA-48BA-8B5D-083B60348D1D}.Debug|x64.Build.0 = Debug|Any CPU
{EBFE97DA-D0BA-48BA-8B5D-083B60348D1D}.Release|Any CPU.ActiveCfg = Release|Any CPU
{EBFE97DA-D0BA-48BA-8B5D-083B60348D1D}.Release|Any CPU.Build.0 = Release|Any CPU
{EBFE97DA-D0BA-48BA-8B5D-083B60348D1D}.Release|x64.ActiveCfg = Release|Any CPU
{EBFE97DA-D0BA-48BA-8B5D-083B60348D1D}.Release|x64.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down Expand Up @@ -489,6 +501,8 @@ Global
{BF029B0A-768B-43A1-8D91-E70B95505716} = {38B37C0D-1930-4D47-BCBF-E358EC1096B1}
{C4C59872-3C8A-450D-83D5-2BE402D610D5} = {2635EC9B-2E5F-4313-AC21-0B847F31F36C}
{05E6E405-5021-406E-8A5E-0A7CEC881F6D} = {C4C59872-3C8A-450D-83D5-2BE402D610D5}
{97A0B191-64D7-4F8A-BFE8-1BFCC5E247E1} = {2635EC9B-2E5F-4313-AC21-0B847F31F36C}
{EBFE97DA-D0BA-48BA-8B5D-083B60348D1D} = {97A0B191-64D7-4F8A-BFE8-1BFCC5E247E1}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {A9969D89-C98B-40A5-A12B-FC87E55B3A19}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>$(TargetFramework)</TargetFramework>
Expand Down
10 changes: 10 additions & 0 deletions src/Infrastructure/BotSharp.Abstraction/Graph/IGraphDb.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using BotSharp.Abstraction.Graph.Models;

namespace BotSharp.Abstraction.Graph;

public interface IGraphDb
{
public string Name { get; }

Task<GraphSearchData> Search(string query, GraphSearchOptions options);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace BotSharp.Abstraction.Graph.Models;

public class GraphSearchData
{
public string Result { get; set; }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace BotSharp.Abstraction.Graph.Models;

public class GraphSearchOptions
{
public string Method { get; set; }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace BotSharp.Abstraction.Graph.Models;

public class GraphSearchResult
{
public string Result { get; set; }
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
using BotSharp.Abstraction.Graph.Models;
using BotSharp.Abstraction.VectorStorage.Models;

namespace BotSharp.Abstraction.Knowledges;

public interface IKnowledgeService
{
Task<IEnumerable<string>> GetKnowledgeCollections();
Task<IEnumerable<KnowledgeSearchResult>> SearchKnowledge(string collectionName, KnowledgeSearchOptions options);
Task FeedKnowledge(string collectionName, KnowledgeCreationModel model);
Task<StringIdPagedItems<KnowledgeSearchResult>> GetKnowledgeCollectionData(string collectionName, KnowledgeFilter filter);
Task<bool> DeleteKnowledgeCollectionData(string collectionName, string id);
Task<IEnumerable<string>> GetVectorCollections();
Task<IEnumerable<VectorSearchResult>> SearchVectorKnowledge(string query, string collectionName, VectorSearchOptions options);
Task FeedVectorKnowledge(string collectionName, KnowledgeCreationModel model);
Task<StringIdPagedItems<VectorSearchResult>> GetVectorCollectionData(string collectionName, VectorFilter filter);
Task<bool> DeleteVectorCollectionData(string collectionName, string id);
Task<GraphSearchResult> SearchGraphKnowledge(string query, GraphSearchOptions options);
Task<KnowledgeSearchResult> SearchKnowledge(string query, string collectionName, VectorSearchOptions vectorOptions, GraphSearchOptions graphOptions);
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,20 +1,10 @@
using BotSharp.Abstraction.Graph.Models;
using BotSharp.Abstraction.VectorStorage.Models;

namespace BotSharp.Abstraction.Knowledges.Models;

public class KnowledgeSearchResult : KnowledgeCollectionData
public class KnowledgeSearchResult
{
public KnowledgeSearchResult()
{

}

public static KnowledgeSearchResult CopyFrom(KnowledgeCollectionData data)
{
return new KnowledgeSearchResult
{
Id = data.Id,
Data = data.Data,
Score = data.Score,
Vector = data.Vector
};
}
}
public IEnumerable<VectorSearchResult> VectorResult { get; set; }
public GraphSearchResult GraphResult { get; set; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ namespace BotSharp.Abstraction.Knowledges.Settings;

public class KnowledgeBaseSettings
{
public string VectorDb { get; set; }
public string DefaultCollection { get; set; } = KnowledgeCollectionName.BotSharp;
public string VectorDb { get; set; }
public string GraphDb { get; set; }
public KnowledgeModelSetting TextEmbedding { get; set; }
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
using BotSharp.Abstraction.VectorStorage.Models;

namespace BotSharp.Abstraction.VectorStorage;

public interface IVectorDb
{
string Name { get; }

Task<IEnumerable<string>> GetCollections();
Task<StringIdPagedItems<KnowledgeCollectionData>> GetCollectionData(string collectionName, KnowledgeFilter filter);
Task<StringIdPagedItems<VectorCollectionData>> GetCollectionData(string collectionName, VectorFilter 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<KnowledgeCollectionData>> Search(string collectionName, float[] vector, IEnumerable<string>? fields, int limit = 5, float confidence = 0.5f, bool withVector = false);
Task<IEnumerable<VectorCollectionData>> 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);
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace BotSharp.Abstraction.Knowledges.Models;
namespace BotSharp.Abstraction.VectorStorage.Models;

public class KnowledgeCollectionData
public class VectorCollectionData
{
public string Id { get; set; }
public Dictionary<string, string> Data { get; set; } = new();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace BotSharp.Abstraction.VectorStorage.Models;

public class VectorFilter : StringIdPagination
{
[JsonPropertyName("with_vector")]
public bool WithVector { get; set; }
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
using BotSharp.Abstraction.Knowledges.Enums;

namespace BotSharp.Abstraction.Knowledges.Models;
namespace BotSharp.Abstraction.VectorStorage.Models;

public class KnowledgeSearchOptions
public class VectorSearchOptions
{
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;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
namespace BotSharp.Abstraction.VectorStorage.Models;

public class VectorSearchResult : VectorCollectionData
{
public VectorSearchResult()
{

}

public static VectorSearchResult CopyFrom(VectorCollectionData data)
{
return new VectorSearchResult
{
Id = data.Id,
Data = data.Data,
Score = data.Score,
Vector = data.Vector
};
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using BotSharp.Abstraction.Graph.Models;
using BotSharp.Abstraction.Knowledges.Models;
using BotSharp.Abstraction.VectorStorage.Models;
using BotSharp.OpenAPI.ViewModels.Knowledges;

namespace BotSharp.OpenAPI.Controllers;
Expand All @@ -16,51 +18,50 @@ public KnowledgeBaseController(IKnowledgeService knowledgeService, IServiceProvi
_services = services;
}

[HttpGet("knowledge/collections")]
public async Task<IEnumerable<string>> GetKnowledgeCollections()
[HttpGet("knowledge/vector/collections")]
public async Task<IEnumerable<string>> GetVectorCollections()
{
return await _knowledgeService.GetKnowledgeCollections();
return await _knowledgeService.GetVectorCollections();
}

[HttpPost("/knowledge/{collection}/search")]
public async Task<IEnumerable<KnowledgeSearchResultViewModel>> SearchKnowledge([FromRoute] string collection, [FromBody] SearchKnowledgeRequest request)
[HttpPost("/knowledge/vector/{collection}/search")]
public async Task<IEnumerable<VectorKnowledgeViewModel>> SearchVectorKnowledge([FromRoute] string collection, [FromBody] SearchVectorKnowledgeRequest request)
{
var options = new KnowledgeSearchOptions
var options = new VectorSearchOptions
{
Text = request.Text,
Fields = request.Fields,
Limit = request.Limit ?? 5,
Confidence = request.Confidence ?? 0.5f,
WithVector = request.WithVector
};

var results = await _knowledgeService.SearchKnowledge(collection, options);
return results.Select(x => KnowledgeSearchResultViewModel.From(x)).ToList();
var results = await _knowledgeService.SearchVectorKnowledge(request.Text, collection, options);
return results.Select(x => VectorKnowledgeViewModel.From(x)).ToList();
}

[HttpPost("/knowledge/{collection}/data")]
public async Task<StringIdPagedItems<KnowledgeSearchResultViewModel>> GetKnowledgeCollectionData([FromRoute] string collection, [FromBody] KnowledgeFilter filter)
[HttpPost("/knowledge/vector/{collection}/data")]
public async Task<StringIdPagedItems<VectorKnowledgeViewModel>> GetVectorCollectionData([FromRoute] string collection, [FromBody] VectorFilter filter)
{
var data = await _knowledgeService.GetKnowledgeCollectionData(collection, filter);
var items = data.Items?.Select(x => KnowledgeSearchResultViewModel.From(x))?
.ToList() ?? new List<KnowledgeSearchResultViewModel>();
var data = await _knowledgeService.GetVectorCollectionData(collection, filter);
var items = data.Items?.Select(x => VectorKnowledgeViewModel.From(x))?
.ToList() ?? new List<VectorKnowledgeViewModel>();

return new StringIdPagedItems<KnowledgeSearchResultViewModel>
return new StringIdPagedItems<VectorKnowledgeViewModel>
{
Count = data.Count,
NextId = data.NextId,
Items = items
};
}

[HttpDelete("/knowledge/{collection}/data/{id}")]
public async Task<bool> DeleteKnowledgeCollectionData([FromRoute] string collection, [FromRoute] string id)
[HttpDelete("/knowledge/vector/{collection}/data/{id}")]
public async Task<bool> DeleteVectorCollectionData([FromRoute] string collection, [FromRoute] string id)
{
return await _knowledgeService.DeleteKnowledgeCollectionData(collection, id);
return await _knowledgeService.DeleteVectorCollectionData(collection, id);
}

[HttpPost("/knowledge/{collection}/upload")]
public async Task<IActionResult> UploadKnowledge([FromRoute] string collection, IFormFile file, [FromForm] int? startPageNum, [FromForm] int? endPageNum)
[HttpPost("/knowledge/vector/{collection}/upload")]
public async Task<IActionResult> UploadVectorKnowledge([FromRoute] string collection, 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 @@ -73,12 +74,51 @@ public async Task<IActionResult> UploadKnowledge([FromRoute] string collection,
}

var content = await textConverter.ConvertPdfToText(filePath, startPageNum, endPageNum);
await _knowledgeService.FeedKnowledge(collection, new KnowledgeCreationModel
await _knowledgeService.FeedVectorKnowledge(collection, new KnowledgeCreationModel
{
Content = content
});

System.IO.File.Delete(filePath);
return Ok(new { count = 1, file.Length });
}

[HttpPost("/knowledge/graph/search")]
public async Task<GraphKnowledgeViewModel> SearchGraphKnowledge([FromBody] SearchGraphKnowledgeRequest request)
{
var options = new GraphSearchOptions
{
Method = request.Method
};

var result = await _knowledgeService.SearchGraphKnowledge(request.Query, options);
return new GraphKnowledgeViewModel
{
Result = result.Result
};
}

[HttpPost("/knowledge/search")]
public async Task<KnowledgeSearchViewModel> SearchKnowledge([FromBody] SearchKnowledgeRequest request)
{
var vectorOptions = new VectorSearchOptions
{
Fields = request.VectorParams.Fields,
Limit = request.VectorParams.Limit ?? 5,
Confidence = request.VectorParams.Confidence ?? 0.5f,
WithVector = request.VectorParams.WithVector
};

var graphOptions = new GraphSearchOptions
{
Method = request.GraphParams.Method
};

var result = await _knowledgeService.SearchKnowledge(request.Text, request.VectorParams.Collection, vectorOptions, graphOptions);
return new KnowledgeSearchViewModel
{
VectorResult = result?.VectorResult?.Select(x => VectorKnowledgeViewModel.From(x)),
GraphResult = result?.GraphResult != null ? new GraphKnowledgeViewModel { Result = result.GraphResult.Result } : null
};
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using System.Text.Json.Serialization;

namespace BotSharp.OpenAPI.ViewModels.Knowledges;

public class GraphKnowledgeViewModel
{
[JsonPropertyName("result")]
public string Result { get; set; }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System.Text.Json.Serialization;

namespace BotSharp.OpenAPI.ViewModels.Knowledges;

public class KnowledgeSearchViewModel
{
[JsonPropertyName("vector_result")]
public IEnumerable<VectorKnowledgeViewModel>? VectorResult { get; set; }

[JsonPropertyName("graph_result")]
public GraphKnowledgeViewModel? GraphResult { get; set; }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System.Text.Json.Serialization;

namespace BotSharp.OpenAPI.ViewModels.Knowledges;

public class SearchGraphKnowledgeRequest
{
[JsonPropertyName("query")]
public string Query { get; set; } = string.Empty;

[JsonPropertyName("method")]
public string Method { get; set; } = string.Empty;
}
Loading