Skip to content
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

EnumHelper.cs #535

Merged
merged 9 commits into from
Jul 10, 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
Expand Up @@ -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);

Expand Down
18 changes: 18 additions & 0 deletions src/Infrastructure/BotSharp.Abstraction/Utilities/EnumHelper.cs
Original file line number Diff line number Diff line change
@@ -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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,10 @@ await hook.OnNewTaskDetected(message, inst.NextActionReason)
}

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

Expand All @@ -82,7 +85,7 @@ await hook.OnNewTaskDetected(message, inst.NextActionReason)
}
else
{
ret = await routing.InvokeAgent(agentId, _dialogs, onFunctionExecuting);
var ret = await routing.InvokeAgent(agentId, _dialogs, onFunctionExecuting);
}

var response = _dialogs.Last();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public async Task<List<string>> Search(string collectionName, float[] vector, in
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
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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; }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public async Task EmbedKnowledge(KnowledgeCreationModel knowledge)
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");
}
Expand All @@ -55,7 +55,7 @@ public async Task Feed(KnowledgeFeedModel knowledge)
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");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down
41 changes: 20 additions & 21 deletions src/Plugins/BotSharp.Plugin.Qdrant/QdrantDb.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ private QdrantClient GetClient()
_client = new QdrantClient
(
host: _setting.Url,
https: true,
apiKey: _setting.ApiKey
);
}
Expand All @@ -41,7 +42,7 @@ private QdrantClient GetClient()
public async Task<List<string>> GetCollections()
{
// List all the collections
var collections = await _client.ListCollectionsAsync();
var collections = await GetClient().ListCollectionsAsync();
return collections.ToList();
}

Expand All @@ -56,11 +57,6 @@ public async Task CreateCollection(string collectionName, int dim)
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
Expand All @@ -71,26 +67,29 @@ public async Task CreateCollection(string collectionName, int dim)
}
}

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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,9 +111,20 @@ public async Task<IPage> NewPage(string ctxId, DataFetched? fetched)
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()));
}
}
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public async Task<BrowserActionResult> GoToPage(MessageInfo message, PageActionA
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
Expand Down