Skip to content

SQL Driver #309

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
Feb 20, 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 @@ -16,7 +16,7 @@ public class FunctionCallFromLlm : RoutingArgs
public bool ExecutingDirectly { get; set; }

[JsonIgnore(Condition = JsonIgnoreCondition.Always)]
public bool HideDialogContext { get; set; }
public bool HandleDialogsByPlanner { get; set; }

/// <summary>
/// Router routed to a wrong agent.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
using BotSharp.Abstraction.Knowledges.Models;

namespace BotSharp.Abstraction.Knowledges;

public interface IKnowledgeHook
{
Task<List<KnowledgeChunk>> CollectChunkedKnowledge();
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ namespace BotSharp.Abstraction.Knowledges;

public interface IKnowledgeService
{
Task<List<KnowledgeChunk>> CollectChunkedKnowledge();
Task EmbedKnowledge(List<KnowledgeChunk> chunks);

Task Feed(KnowledgeFeedModel knowledge);
Task EmbedKnowledge(KnowledgeCreationModel knowledge);
Task<string> GetKnowledges(KnowledgeRetrievalModel retrievalModel);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace BotSharp.Abstraction.Knowledges.Models;

public class KnowledgeChunk
{
public string Id { get; set; }
public string Name { get; set; }
public string Content { get; set; }
public string SourceAgentId { get; set; }
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using BotSharp.Abstraction.Functions.Models;
using BotSharp.Abstraction.Routing.Models;

namespace BotSharp.Abstraction.Routing.Planning;

Expand All @@ -10,7 +9,11 @@ namespace BotSharp.Abstraction.Routing.Planning;
public interface IPlaner
{
Task<FunctionCallFromLlm> GetNextInstruction(Agent router, string messageId, List<RoleDialogModel> dialogs);
Task<bool> AgentExecuting(Agent router, FunctionCallFromLlm inst, RoleDialogModel message);
Task<bool> AgentExecuted(Agent router, FunctionCallFromLlm inst, RoleDialogModel message);
Task<bool> AgentExecuting(Agent router, FunctionCallFromLlm inst, RoleDialogModel message, List<RoleDialogModel> dialogs);
Task<bool> AgentExecuted(Agent router, FunctionCallFromLlm inst, RoleDialogModel message, List<RoleDialogModel> dialogs);
List<RoleDialogModel> BeforeHandleContext(FunctionCallFromLlm inst, RoleDialogModel message, List<RoleDialogModel> dialogs)
=> dialogs;
bool AfterHandleContext(List<RoleDialogModel> dialogs, List<RoleDialogModel> taskAgentDialogs)
=> true;
int MaxLoopCount => 5;
}
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public async Task<FunctionCallFromLlm> GetNextInstruction(Agent router, string m
return inst;
}

public async Task<bool> AgentExecuting(Agent router, FunctionCallFromLlm inst, RoleDialogModel message)
public async Task<bool> AgentExecuting(Agent router, FunctionCallFromLlm inst, RoleDialogModel message, List<RoleDialogModel> dialogs)
{
if (!string.IsNullOrEmpty(inst.AgentName))
{
Expand All @@ -82,7 +82,7 @@ public async Task<bool> AgentExecuting(Agent router, FunctionCallFromLlm inst, R
return true;
}

public async Task<bool> AgentExecuted(Agent router, FunctionCallFromLlm inst, RoleDialogModel message)
public async Task<bool> AgentExecuted(Agent router, FunctionCallFromLlm inst, RoleDialogModel message, List<RoleDialogModel> dialogs)
{
var context = _services.GetRequiredService<RoutingContext>();
context.Empty();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public async Task<FunctionCallFromLlm> GetNextInstruction(Agent router, string m
return inst;
}

public async Task<bool> AgentExecuting(Agent router, FunctionCallFromLlm inst, RoleDialogModel message)
public async Task<bool> AgentExecuting(Agent router, FunctionCallFromLlm inst, RoleDialogModel message, List<RoleDialogModel> dialogs)
{
// Set user content as Planner's question
message.FunctionName = inst.Function;
Expand All @@ -85,7 +85,7 @@ public async Task<bool> AgentExecuting(Agent router, FunctionCallFromLlm inst, R
return true;
}

public async Task<bool> AgentExecuted(Agent router, FunctionCallFromLlm inst, RoleDialogModel message)
public async Task<bool> AgentExecuted(Agent router, FunctionCallFromLlm inst, RoleDialogModel message, List<RoleDialogModel> dialogs)
{
var context = _services.GetRequiredService<RoutingContext>();
if (inst.UnmatchedAgent)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using Amazon.SecurityToken.Model.Internal.MarshallTransformations;
using BotSharp.Abstraction.Agents.Models;
using BotSharp.Abstraction.Functions.Models;
using BotSharp.Abstraction.MLTasks;
Expand Down Expand Up @@ -102,14 +103,33 @@ public async Task<FunctionCallFromLlm> GetNextInstruction(Agent router, string m
{
inst.Response = decomposation.Description;
inst.Reason = $"{decomposation.TotalRemainingSteps} steps left.";
inst.HideDialogContext = true;
inst.HandleDialogsByPlanner = true;
}

_lastInst = inst;
return inst;
}

public async Task<bool> AgentExecuting(Agent router, FunctionCallFromLlm inst, RoleDialogModel message)
public List<RoleDialogModel> BeforeHandleContext(FunctionCallFromLlm inst, RoleDialogModel message, List<RoleDialogModel> dialogs)
{
var taskAgentDialogs = new List<RoleDialogModel>
{
new RoleDialogModel(AgentRole.User, inst.Response)
{
MessageId = message.MessageId,
}
};

return taskAgentDialogs;
}

public bool AfterHandleContext(List<RoleDialogModel> dialogs, List<RoleDialogModel> taskAgentDialogs)
{
dialogs.AddRange(taskAgentDialogs.Skip(1));
return true;
}

public async Task<bool> AgentExecuting(Agent router, FunctionCallFromLlm inst, RoleDialogModel message, List<RoleDialogModel> dialogs)
{
// Set user content as Planner's question
message.FunctionName = inst.Function;
Expand All @@ -118,7 +138,7 @@ public async Task<bool> AgentExecuting(Agent router, FunctionCallFromLlm inst, R
return true;
}

public async Task<bool> AgentExecuted(Agent router, FunctionCallFromLlm inst, RoleDialogModel message)
public async Task<bool> AgentExecuted(Agent router, FunctionCallFromLlm inst, RoleDialogModel message, List<RoleDialogModel> dialogs)
{
var context = _services.GetRequiredService<RoutingContext>();

Expand Down
7 changes: 4 additions & 3 deletions src/Infrastructure/BotSharp.Core/Routing/RoutingPlugin.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using BotSharp.Abstraction.Routing;
using BotSharp.Abstraction.Routing.Models;
using BotSharp.Abstraction.Routing.Planning;
using BotSharp.Abstraction.Routing.Settings;
using BotSharp.Abstraction.Settings;
using BotSharp.Core.Routing.Hooks;
Expand Down Expand Up @@ -34,8 +35,8 @@ public void RegisterDI(IServiceCollection services, IConfiguration config)
services.AddScoped<IRoutingService, RoutingService>();
services.AddScoped<IAgentHook, RoutingAgentHook>();

services.AddScoped<NaivePlanner>();
services.AddScoped<HFPlanner>();
services.AddScoped<SequentialPlanner>();
services.AddScoped<IPlaner, NaivePlanner>();
services.AddScoped<IPlaner, HFPlanner>();
services.AddScoped<IPlaner, SequentialPlanner>();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,17 @@ public partial class RoutingService
{
public IPlaner GetPlanner(Agent router)
{
var planner = router.RoutingRules.FirstOrDefault(x => x.Type == RuleType.Planner);
var rule = router.RoutingRules.FirstOrDefault(x => x.Type == RuleType.Planner);

if (planner?.Field == nameof(HFPlanner))
return _services.GetRequiredService<HFPlanner>();
else if (planner?.Field == nameof(SequentialPlanner))
return _services.GetRequiredService<SequentialPlanner>();
else
var planner = _services.GetServices<IPlaner>().
FirstOrDefault(x => x.GetType().Name.EndsWith(rule.Field));

if (planner == null)
{
_logger.LogError($"Can't find specific planner named {rule.Field}");
return _services.GetRequiredService<NaivePlanner>();
}

return planner;
}
}
13 changes: 5 additions & 8 deletions src/Infrastructure/BotSharp.Core/Routing/RoutingService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -106,24 +106,21 @@ public async Task<RoleDialogModel> InstructLoop(RoleDialogModel message)
#else
_logger.LogInformation($"*** Next Instruction *** {inst}");
#endif
await planner.AgentExecuting(_router, inst, message);
await planner.AgentExecuting(_router, inst, message, dialogs);

// Handover to Task Agent
if (inst.HideDialogContext)
if (inst.HandleDialogsByPlanner)
{
var dialogWithoutContext = new List<RoleDialogModel>
{
new RoleDialogModel(AgentRole.User, inst.Response)
};
var dialogWithoutContext = planner.BeforeHandleContext(inst, message, dialogs);
response = await executor.Execute(this, inst, message, dialogWithoutContext);
dialogs.AddRange(dialogWithoutContext.Skip(1));
planner.AfterHandleContext(dialogs, dialogWithoutContext);
}
else
{
response = await executor.Execute(this, inst, message, dialogs);
}

await planner.AgentExecuted(_router, inst, response);
await planner.AgentExecuted(_router, inst, response, dialogs);
}

return response;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using BotSharp.Abstraction.Knowledges.Models;
using BotSharp.Abstraction.Knowledges.Settings;
using Microsoft.AspNetCore.Http;

namespace BotSharp.OpenAPI.Controllers;

Expand All @@ -17,6 +16,13 @@ public KnowledgeBaseController(IKnowledgeService knowledgeService, IServiceProvi
_services = services;
}

[HttpPost("/knowledge-base/embed")]
public async Task EmbedKnowledge()
{
var chunks = await _knowledgeService.CollectChunkedKnowledge();
await _knowledgeService.EmbedKnowledge(chunks);
}

[HttpGet("/knowledge/{agentId}")]
public async Task<List<RetrievedResult>> RetrieveKnowledge([FromRoute] string agentId, [FromQuery(Name = "q")] string question)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,25 +17,7 @@
</ItemGroup>

<ItemGroup>
<None Remove="data\agents\f5679799-ba89-4fef-936a-bcc311e5f14d\agent.json" />
<None Remove="data\agents\f5679799-ba89-4fef-936a-bcc311e5f14d\functions.json" />
<None Remove="data\agents\f5679799-ba89-4fef-936a-bcc311e5f14d\instruction.liquid" />
</ItemGroup>

<ItemGroup>
<Content Include="data\agents\f5679799-ba89-4fef-936a-bcc311e5f14d\agent.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="data\agents\f5679799-ba89-4fef-936a-bcc311e5f14d\functions.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="data\agents\f5679799-ba89-4fef-936a-bcc311e5f14d\instruction.liquid">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
</ItemGroup>

<ItemGroup>
<PackageReference Include="PdfPig" Version="0.1.9-alpha-20240121-04fc8" />
<PackageReference Include="PdfPig" Version="0.1.9-alpha-20240208-19734" />
<PackageReference Include="TensorFlow.Keras" Version="0.15.0" />
</ItemGroup>

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ public class KnowledgeBasePlugin : IBotSharpPlugin
public string Name => "Knowledge Base";
public string Description => "Embedding private data and feed them into LLM in the conversation.";
public string IconUrl => "https://cdn-icons-png.flaticon.com/512/9592/9592995.png";
public string[] AgentIds => new[] { "f5679799-ba89-4fef-936a-bcc311e5f14d" };

public void RegisterDI(IServiceCollection services, IConfiguration config)
{
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace BotSharp.Plugin.KnowledgeBase.Services;

public class KnowledgeService : IKnowledgeService
public partial class KnowledgeService : IKnowledgeService
{
private readonly IServiceProvider _services;
private readonly KnowledgeBaseSettings _settings;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
namespace BotSharp.Plugin.KnowledgeBase.Services;

public partial class KnowledgeService
{
public async Task<List<KnowledgeChunk>> CollectChunkedKnowledge()
{
throw new NotImplementedException();
}

public async Task EmbedKnowledge(List<KnowledgeChunk> chunks)
{
throw new NotImplementedException();
}
}

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@

<ItemGroup>
<ProjectReference Include="..\..\Infrastructure\BotSharp.Abstraction\BotSharp.Abstraction.csproj" />
<ProjectReference Include="..\..\Infrastructure\BotSharp.Core\BotSharp.Core.csproj" />
</ItemGroup>

<ItemGroup>
<Folder Include="data\agents\beda4c12-e1ec-4b4b-b328-3df4a6687c4f\templates\" />
</ItemGroup>

</Project>
Loading