Skip to content

add vector data source #637

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

Merged
merged 1 commit into from
Sep 12, 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
@@ -0,0 +1,7 @@
namespace BotSharp.Abstraction.Knowledges.Enums;

public static class KnowledgeDocSource
{
public const string Api = "api";
public const string User = "user";
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace BotSharp.Abstraction.Knowledges.Enums;

public class KnowledgeDocType
{
public const string File = "file";
public const string Http = "http";
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ public static class KnowledgePayloadName
public static string Answer = "answer";
public static string Request = "request";
public static string Response = "response";
public static string DataSource = "dataSource";
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace BotSharp.Abstraction.VectorStorage.Enums;

public static class VectorDataSource
{
public const string Api = "api";
public const string User = "user";
public const string File = "file";
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
using BotSharp.Abstraction.VectorStorage.Enums;

namespace BotSharp.Abstraction.VectorStorage.Models;

public class VectorCreateModel
{
public string Text { get; set; }
public string DataSource { get; set; } = VectorDataSource.Api;
public Dictionary<string, string>? Payload { get; set; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,11 @@ public class VectorFilter : StringIdPagination
/// </summary>
[JsonPropertyName("search_pairs")]
public IEnumerable<KeyValue>? SearchPairs { get; set; }


/// <summary>
/// Included payload keys
/// </summary>
[JsonPropertyName("included_payloads")]
public IEnumerable<string>? IncludedPayloads { get; set; }
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
using Azure.Core;
using BotSharp.Abstraction.Files.Constants;
using BotSharp.Abstraction.Files.Utilities;
using BotSharp.Abstraction.Graph.Models;
using BotSharp.Abstraction.Knowledges.Models;
using BotSharp.Abstraction.VectorStorage.Models;
using BotSharp.OpenAPI.ViewModels.Knowledges;
using System.IO;

namespace BotSharp.OpenAPI.Controllers;

Expand Down Expand Up @@ -77,6 +74,7 @@ public async Task<bool> CreateVectorKnowledge([FromRoute] string collection, [Fr
var create = new VectorCreateModel
{
Text = request.Text,
DataSource = request.DataSource,
Payload = request.Payload
};

Expand All @@ -91,6 +89,7 @@ public async Task<bool> UpdateVectorKnowledge([FromRoute] string collection, [Fr
{
Id = request.Id,
Text = request.Text,
DataSource = request.DataSource,
Payload = request.Payload
};

Expand All @@ -106,24 +105,6 @@ public async Task<bool> DeleteVectorCollectionData([FromRoute] string collection
#endregion


#region Graph
[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
};
}
#endregion


#region Document
[HttpPost("/knowledge/document/{collection}/upload")]
public async Task<UploadKnowledgeResponse> UploadKnowledgeDocuments([FromRoute] string collection, [FromBody] VectorKnowledgeUploadRequest request)
Expand Down Expand Up @@ -181,6 +162,25 @@ public async Task<IActionResult> GetKnowledgeDocument([FromRoute] string collect
#endregion



#region Graph
[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
};
}
#endregion


#region Common
[HttpPost("/knowledge/vector/refresh-configs")]
public async Task<string> RefreshVectorCollectionConfigs([FromBody] VectorCollectionConfigsModel request)
Expand Down
3 changes: 2 additions & 1 deletion src/Infrastructure/BotSharp.OpenAPI/Using.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@
global using BotSharp.Abstraction.Repositories.Filters;
global using BotSharp.Abstraction.Files.Models;
global using BotSharp.Abstraction.Files;
global using BotSharp.Abstraction.VectorStorage.Enums;
global using BotSharp.OpenAPI.ViewModels.Conversations;
global using BotSharp.OpenAPI.ViewModels.Users;
global using BotSharp.OpenAPI.ViewModels.Agents;
global using BotSharp.OpenAPI.ViewModels.Files;
global using BotSharp.OpenAPI.ViewModels.Files;
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ public class VectorKnowledgeCreateRequest
[JsonPropertyName("text")]
public string Text { get; set; }

[JsonPropertyName("data_source")]
public string DataSource { get; set; } = VectorDataSource.Api;

[JsonPropertyName("payload")]
public Dictionary<string, string>? Payload { get; set; }
}
3 changes: 2 additions & 1 deletion src/Plugins/BotSharp.Plugin.ChatHub/WebSocketsMiddleware.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ private bool VerifyGetRequest(HttpRequest request)
var regexes = new List<Regex>
{
new Regex(@"/conversation/(.*?)/message/(.*?)/(.*?)/file/(.*?)/(.*?)", RegexOptions.IgnoreCase),
new Regex(@"/user/avatar", RegexOptions.IgnoreCase)
new Regex(@"/user/avatar", RegexOptions.IgnoreCase),
new Regex(@"/knowledge/document/(.*?)/file/(.*?)", RegexOptions.IgnoreCase)
};

return request.Method.IsEqualTo("GET") && regexes.Any(x => x.IsMatch(request.Path.Value ?? string.Empty));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using BotSharp.Abstraction.Files;
using BotSharp.Abstraction.VectorStorage.Enums;

namespace BotSharp.Plugin.KnowledgeBase.Services;

Expand Down Expand Up @@ -119,7 +120,10 @@ public async Task<bool> CreateVectorCollectionData(string collectionName, Vector

var db = GetVectorDb();
var guid = Guid.NewGuid();
return await db.Upsert(collectionName, guid, vector, create.Text, create.Payload);
var payload = create.Payload ?? new();
payload[KnowledgePayloadName.DataSource] = !string.IsNullOrWhiteSpace(create.DataSource) ? create.DataSource : VectorDataSource.Api;

return await db.Upsert(collectionName, guid, vector, create.Text, payload);
}
catch (Exception ex)
{
Expand All @@ -146,7 +150,10 @@ public async Task<bool> UpdateVectorCollectionData(string collectionName, Vector

var textEmbedding = GetTextEmbedding(collectionName);
var vector = await textEmbedding.GetVectorAsync(update.Text);
return await db.Upsert(collectionName, guid, vector, update.Text, update.Payload);
var payload = update.Payload ?? new();
payload[KnowledgePayloadName.DataSource] = !string.IsNullOrWhiteSpace(update.DataSource) ? update.DataSource : VectorDataSource.Api;

return await db.Upsert(collectionName, guid, vector, update.Text, payload);
}
catch (Exception ex)
{
Expand Down
16 changes: 16 additions & 0 deletions src/Plugins/BotSharp.Plugin.Qdrant/QdrantDb.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using BotSharp.Abstraction.Utilities;
using BotSharp.Abstraction.VectorStorage.Models;
using Google.Protobuf.WellKnownTypes;
using Microsoft.Extensions.Logging;
using Qdrant.Client;
using Qdrant.Client.Grpc;
Expand Down Expand Up @@ -112,10 +113,25 @@ public async Task<StringIdPagedItems<VectorCollectionData>> GetPagedCollectionDa
};
}

// Build payload selector
WithPayloadSelector? payloadSelector = null;
if (!filter.IncludedPayloads.IsNullOrEmpty())
{
payloadSelector = new WithPayloadSelector
{
Enable = true,
Include = new PayloadIncludeSelector
{
Fields = { filter.IncludedPayloads.ToArray() }
}
};
}

var totalPointCount = await client.CountAsync(collectionName, filter: queryFilter);
var response = await client.ScrollAsync(collectionName, limit: (uint)filter.Size,
offset: !string.IsNullOrWhiteSpace(filter.StartId) ? new PointId { Uuid = filter.StartId } : null,
filter: queryFilter,
payloadSelector: payloadSelector,
vectorsSelector: filter.WithVector);

var points = response?.Result?.Select(x => new VectorCollectionData
Expand Down