diff --git a/src/Infrastructure/BotSharp.Abstraction/Utilities/EnumHelper.cs b/src/Infrastructure/BotSharp.Abstraction/Utilities/EnumHelper.cs new file mode 100644 index 00000000..1ac23ed7 --- /dev/null +++ b/src/Infrastructure/BotSharp.Abstraction/Utilities/EnumHelper.cs @@ -0,0 +1,18 @@ +using System.ComponentModel; +using System.Reflection; + +namespace BotSharp.Abstraction.Utilities; + +public static class EnumHelper +{ + public static string GetDescription(Enum value) + { + FieldInfo fi = value.GetType().GetField(value.ToString()); + DescriptionAttribute[] attributes = (DescriptionAttribute[])fi.GetCustomAttributes(typeof(DescriptionAttribute), false); + + if (attributes != null && attributes.Length > 0) + return attributes[0].Description; + else + return value.ToString(); + } +} diff --git a/src/Infrastructure/BotSharp.Abstraction/VectorStorage/IVectorDb.cs b/src/Infrastructure/BotSharp.Abstraction/VectorStorage/IVectorDb.cs index defe7647..c385153a 100644 --- a/src/Infrastructure/BotSharp.Abstraction/VectorStorage/IVectorDb.cs +++ b/src/Infrastructure/BotSharp.Abstraction/VectorStorage/IVectorDb.cs @@ -4,6 +4,6 @@ public interface IVectorDb { Task> GetCollections(); Task CreateCollection(string collectionName, int dim); - Task Upsert(string collectionName, int id, float[] vector, string text); + Task Upsert(string collectionName, string id, float[] vector, string text, Dictionary? payload = null); Task> Search(string collectionName, float[] vector, int limit = 5); } diff --git a/src/Plugins/BotSharp.Plugin.KnowledgeBase/MemVecDb/MemVectorDatabase.cs b/src/Plugins/BotSharp.Plugin.KnowledgeBase/MemVecDb/MemVectorDatabase.cs index cbe3c257..a595c5da 100644 --- a/src/Plugins/BotSharp.Plugin.KnowledgeBase/MemVecDb/MemVectorDatabase.cs +++ b/src/Plugins/BotSharp.Plugin.KnowledgeBase/MemVecDb/MemVectorDatabase.cs @@ -37,7 +37,7 @@ public class MemVectorDatabase : IVectorDb return texts; } - public async Task Upsert(string collectionName, int id, float[] vector, string text) + public async Task Upsert(string collectionName, string id, float[] vector, string text, Dictionary? payload = null) { _vectors[collectionName].Add(new VecRecord { diff --git a/src/Plugins/BotSharp.Plugin.KnowledgeBase/MemVecDb/VecRecord.cs b/src/Plugins/BotSharp.Plugin.KnowledgeBase/MemVecDb/VecRecord.cs index 79775698..3c4d024b 100644 --- a/src/Plugins/BotSharp.Plugin.KnowledgeBase/MemVecDb/VecRecord.cs +++ b/src/Plugins/BotSharp.Plugin.KnowledgeBase/MemVecDb/VecRecord.cs @@ -2,7 +2,7 @@ namespace BotSharp.Plugin.KnowledgeBase.MemVecDb; public class VecRecord { - public int Id { get; set; } + public string Id { get; set; } public float[] Vector { get; set; } public string Text { get; set; } diff --git a/src/Plugins/BotSharp.Plugin.KnowledgeBase/Services/KnowledgeService.cs b/src/Plugins/BotSharp.Plugin.KnowledgeBase/Services/KnowledgeService.cs index 5f545078..bd4cf6f4 100644 --- a/src/Plugins/BotSharp.Plugin.KnowledgeBase/Services/KnowledgeService.cs +++ b/src/Plugins/BotSharp.Plugin.KnowledgeBase/Services/KnowledgeService.cs @@ -32,7 +32,7 @@ public partial class KnowledgeService : IKnowledgeService foreach (var line in lines) { var vec = await textEmbedding.GetVectorAsync(line); - await db.Upsert("shared", idStart, vec, line); + await db.Upsert("shared", idStart.ToString(), vec, line); idStart++; Console.WriteLine($"Saved vector {idStart}/{lines.Count}: {line}\n"); } @@ -55,7 +55,7 @@ public partial class KnowledgeService : IKnowledgeService foreach (var line in lines) { var vec = await textEmbedding.GetVectorAsync(line); - await db.Upsert(knowledge.AgentId, idStart, vec, line); + await db.Upsert(knowledge.AgentId, idStart.ToString(), vec, line); idStart++; Console.WriteLine($"Saved vector {idStart}/{lines.Count}: {line}\n"); } diff --git a/src/Plugins/BotSharp.Plugin.Qdrant/QdrantDb.cs b/src/Plugins/BotSharp.Plugin.Qdrant/QdrantDb.cs index 3748b45f..f0b750d6 100644 --- a/src/Plugins/BotSharp.Plugin.Qdrant/QdrantDb.cs +++ b/src/Plugins/BotSharp.Plugin.Qdrant/QdrantDb.cs @@ -32,6 +32,7 @@ public class QdrantDb : IVectorDb _client = new QdrantClient ( host: _setting.Url, + https: true, apiKey: _setting.ApiKey ); } @@ -41,7 +42,7 @@ public class QdrantDb : IVectorDb public async Task> GetCollections() { // List all the collections - var collections = await _client.ListCollectionsAsync(); + var collections = await GetClient().ListCollectionsAsync(); return collections.ToList(); } @@ -56,11 +57,6 @@ public class QdrantDb : IVectorDb Size = (ulong)dim, Distance = Distance.Cosine }); - - var agentService = _services.GetRequiredService(); - var agentDataDir = agentService.GetAgentDataDir(collectionName); - var knowledgePath = Path.Combine(agentDataDir, "knowledge.txt"); - File.WriteAllLines(knowledgePath, new string[0]); } // Get collection info @@ -71,26 +67,28 @@ public class QdrantDb : IVectorDb } } - public async Task Upsert(string collectionName, int id, float[] vector, string text) + public async Task Upsert(string collectionName, string id, float[] vector, string text, Dictionary? payload = null) { // Insert vectors - await GetClient().UpsertAsync(collectionName, points: new List + var point = new PointStruct() { - new PointStruct() + Id = new PointId() { - Id = new PointId() - { - Num = (ulong)id, - }, - Vectors = vector - } - }); + Uuid = id + }, + Vectors = vector, + Payload = { } + }; - // Store chunks in local file system - var agentService = _services.GetRequiredService(); - var agentDataDir = agentService.GetAgentDataDir(collectionName); - var knowledgePath = Path.Combine(agentDataDir, "knowledge.txt"); - File.AppendAllLines(knowledgePath, new[] { text }); + foreach (var item in payload) + { + point.Payload.Add(item.Key, item.Value); + } + + var result = await GetClient().UpsertAsync(collectionName, points: new List + { + point + }); } public async Task> Search(string collectionName, float[] vector, int limit = 5) diff --git a/src/Plugins/BotSharp.Plugin.WebDriver/Drivers/PlaywrightDriver/PlaywrightWebDriver.DoAction.cs b/src/Plugins/BotSharp.Plugin.WebDriver/Drivers/PlaywrightDriver/PlaywrightWebDriver.DoAction.cs index 6d8006f6..1ca20d28 100644 --- a/src/Plugins/BotSharp.Plugin.WebDriver/Drivers/PlaywrightDriver/PlaywrightWebDriver.DoAction.cs +++ b/src/Plugins/BotSharp.Plugin.WebDriver/Drivers/PlaywrightDriver/PlaywrightWebDriver.DoAction.cs @@ -5,7 +5,19 @@ public partial class PlaywrightWebDriver public async Task DoAction(MessageInfo message, ElementActionArgs action, BrowserActionResult result) { var page = _instance.GetPage(message.ContextId); + if (string.IsNullOrEmpty(result.Selector)) + { + Serilog.Log.Error($"Selector is not set."); + return; + } + ILocator locator = page.Locator(result.Selector); + var count = await locator.CountAsync(); + if (count == 0) + { + Serilog.Log.Error($"Element not found: {result.Selector}"); + return; + } if (action.Action == BroswerActionEnum.Click) {