Merge pull request #535 from Qtoss-AI/master

EnumHelper.cs
This commit is contained in:
C. Oceania 2024-07-10 11:10:46 -05:00 committed by GitHub
commit 0420d7feba
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
13 changed files with 79 additions and 36 deletions

View file

@ -21,7 +21,7 @@ public interface IBotSharpPlugin
/// <summary>
/// Has build-in agent profile with this plugin
/// </summary>
string[] AgentIds => new string[0];
string[] AgentIds => [];
void RegisterDI(IServiceCollection services, IConfiguration config);

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

@ -57,7 +57,10 @@ public class RouteToAgentRoutingHandler : RoutingHandlerBase, IRoutingHandler
}
message.FunctionArgs = JsonSerializer.Serialize(inst);
var ret = await routing.InvokeFunction(message.FunctionName, message);
if (message.FunctionName != null)
{
var ret = await routing.InvokeFunction(message.FunctionName, message);
}
var agentId = routing.Context.GetCurrentAgentId();
@ -82,7 +85,7 @@ public class RouteToAgentRoutingHandler : RoutingHandlerBase, IRoutingHandler
}
else
{
ret = await routing.InvokeAgent(agentId, _dialogs, onFunctionExecuting);
var ret = await routing.InvokeAgent(agentId, _dialogs, onFunctionExecuting);
}
var response = _dialogs.Last();

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

@ -11,8 +11,7 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Aspire.Qdrant.Client" Version="8.0.1" />
<PackageReference Include="Qdrant.Client" Version="1.9.0" />
<PackageReference Include="Qdrant.Client" Version="1.10.0" />
</ItemGroup>
<ItemGroup>

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,29 @@ 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,
// 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 });
Payload = { }
};
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

@ -17,8 +17,8 @@
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Playwright" Version="1.44.0" />
<PackageReference Include="Selenium.WebDriver" Version="4.21.0" />
<PackageReference Include="Microsoft.Playwright" Version="1.45.0" />
<PackageReference Include="Selenium.WebDriver" Version="4.22.0" />
<PackageReference Include="HtmlAgilityPack" Version="1.11.61" />
</ItemGroup>

View file

@ -111,9 +111,20 @@ public class PlaywrightInstance : IDisposable
e.Headers["content-type"].Contains("application/json") &&
e.Request.ResourceType == "fetch")
{
Serilog.Log.Information($"Response: {e.Url}");
var json = await e.JsonAsync();
fetched(e.Url.ToLower(), JsonSerializer.Serialize(json));
Serilog.Log.Information($"fetched: {e.Url}");
JsonElement? json = null;
try
{
json = await e.JsonAsync();
}
catch(Exception ex)
{
Serilog.Log.Error(ex.ToString());
}
finally
{
fetched(e.Url.ToLower(), JsonSerializer.Serialize(json ?? new JsonElement()));
}
}
};
}

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

View file

@ -29,6 +29,7 @@ public partial class PlaywrightWebDriver
var page = args.OpenNewTab ? await _instance.NewPage(message.ContextId, fetched: args.OnDataFetched) :
_instance.GetPage(message.ContextId);
Serilog.Log.Information($"goto page: {args.Url}");
var response = await page.GotoAsync(args.Url, new PageGotoOptions
{
Timeout = args.Timeout