AI Search draft.

This commit is contained in:
Haiping Chen 2024-07-02 09:52:16 -05:00
parent 3218b36c53
commit 2f197ccb80
7 changed files with 54 additions and 26 deletions

View file

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

View file

@ -4,6 +4,6 @@ public interface IVectorDb
{
Task<List<string>> 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<string, string>? payload = null);
Task<List<string>> Search(string collectionName, float[] vector, int limit = 5);
}

View file

@ -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<string, string>? payload = null)
{
_vectors[collectionName].Add(new VecRecord
{

View file

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

View file

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

View file

@ -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<List<string>> 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<IAgentService>();
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<string, string>? payload = null)
{
// Insert vectors
await GetClient().UpsertAsync(collectionName, points: new List<PointStruct>
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<IAgentService>();
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<PointStruct>
{
point
});
}
public async Task<List<string>> Search(string collectionName, float[] vector, int limit = 5)

View file

@ -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)
{