Skip to content
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 @@ -3,6 +3,6 @@ namespace BotSharp.Abstraction.Conversations.Settings;
public class RateLimitSetting
{
public int MaxConversationPerDay { get; set; } = 100;
public int MaxInputLengthPerRequest { get; set; } = 512;
public int MaxInputLengthPerRequest { get; set; } = 1024;
public int MinTimeSecondsBetweenMessages { get; set; } = 2;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace BotSharp.Abstraction.Planning;

/// <summary>
/// Planning process for Task Agent
/// </summary>
public class ITaskPlanner
{

}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace BotSharp.Abstraction.Routing.Planning;
/// Task breakdown and execution plan
/// https://www.promptingguide.ai/techniques/cot
/// </summary>
public interface IPlaner
public interface IRoutingPlaner
{
Task<FunctionCallFromLlm> GetNextInstruction(Agent router, string messageId, List<RoleDialogModel> dialogs);
Task<bool> AgentExecuting(Agent router, FunctionCallFromLlm inst, RoleDialogModel message, List<RoleDialogModel> dialogs);
Expand Down
1 change: 1 addition & 0 deletions src/Infrastructure/BotSharp.Core/BotSharp.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,7 @@
</ItemGroup>

<ItemGroup>
<Folder Include="Planning\" />
<Folder Include="Translation\Models\" />
</ItemGroup>

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using BotSharp.Abstraction.Messaging;
using BotSharp.Abstraction.Messaging.Models.RichContent;
using BotSharp.Abstraction.Routing.Settings;
using System.Drawing;
using BotSharp.Core.Routing.Planning;

namespace BotSharp.Core.Conversations.Services;

Expand Down Expand Up @@ -76,9 +76,14 @@ public async Task<bool> SendMessage(string agentId,
// Routing with reasoning
var settings = _services.GetRequiredService<RoutingSettings>();

response = agent.Type == AgentType.Routing ?
await routing.InstructLoop(message, dialogs, onFunctionExecuting) :
await routing.InstructDirect(agent, message);
if (agent.Type == AgentType.Routing)
{
response = await routing.InstructLoop(message, dialogs, onFunctionExecuting);
}
else
{
response = await routing.InstructDirect(agent, message);
}

routing.ResetRecursiveCounter();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace BotSharp.Core.Routing.Planning;
/// <summary>
/// Human feedback based planner
/// </summary>
public class HFPlanner : IPlaner
public class HFPlanner : IRoutingPlaner
{
private readonly IServiceProvider _services;
private readonly ILogger _logger;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

namespace BotSharp.Core.Routing.Planning;

public class NaivePlanner : IPlaner
public class NaivePlanner : IRoutingPlaner
{
private readonly IServiceProvider _services;
private readonly ILogger _logger;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

namespace BotSharp.Core.Routing.Planning;

public class SequentialPlanner : IPlaner
public class SequentialPlanner : IRoutingPlaner
{
private readonly IServiceProvider _services;
private readonly ILogger _logger;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

namespace BotSharp.Core.Routing.Planning;

public partial class TwoStagePlanner : IPlaner
public partial class TwoStagePlanner : IRoutingPlaner
{
private readonly IServiceProvider _services;
private readonly ILogger _logger;
Expand Down
8 changes: 4 additions & 4 deletions src/Infrastructure/BotSharp.Core/Routing/RoutingPlugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ public void RegisterDI(IServiceCollection services, IConfiguration config)
services.AddScoped<IRoutingService, RoutingService>();
services.AddScoped<IAgentHook, RoutingAgentHook>();

services.AddScoped<IPlaner, NaivePlanner>();
services.AddScoped<IPlaner, HFPlanner>();
services.AddScoped<IPlaner, SequentialPlanner>();
services.AddScoped<IPlaner, TwoStagePlanner>();
services.AddScoped<IRoutingPlaner, NaivePlanner>();
services.AddScoped<IRoutingPlaner, HFPlanner>();
services.AddScoped<IRoutingPlaner, SequentialPlanner>();
services.AddScoped<IRoutingPlaner, TwoStagePlanner>();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ namespace BotSharp.Core.Routing;

public partial class RoutingService
{
public IPlaner GetPlanner(Agent router)
public IRoutingPlaner GetPlanner(Agent router)
{
var rule = router.RoutingRules.FirstOrDefault(x => x.Type == RuleType.Planner);

var planner = _services.GetServices<IPlaner>().
var planner = _services.GetServices<IRoutingPlaner>().
FirstOrDefault(x => x.GetType().Name.EndsWith(rule.Field));

if (planner == null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,7 @@ Task Solutions:
{{ k }}
{% endfor %}
{%- endif %}

=====
Task description:
{{ task_description }}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

Original file line number Diff line number Diff line change
Expand Up @@ -242,8 +242,8 @@ public async Task<ChatResponseModel> SendMessage([FromRoute] string agentId,
var routing = _services.GetRequiredService<IRoutingService>();
routing.Context.SetMessageId(conversationId, inputMsg.MessageId);

conv.SetConversationId(conversationId, input.States);
SetStates(conv, input);
conv.SetConversationId(conversationId, input.States);

var response = new ChatResponseModel();

Expand Down
2 changes: 1 addition & 1 deletion src/Plugins/BotSharp.Plugin.AnthropicAI/AnthropicPlugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ public class AnthropicPlugin : IBotSharpPlugin
{
public string Id => "012119da-8367-4be8-9a75-ab6ae55071e6";
public string Name => "Anthropic AI";
public string Description => "Anthropic is an AI safety and research company";
public string Description => "Anthropic is an AI company that's working to build reliable and steerable AI systems.";
public string? IconUrl => "https://www.anthropic.com/images/icons/safari-pinned-tab.svg";

public void RegisterDI(IServiceCollection services, IConfiguration config)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

<ItemGroup>
<None Remove="data\agents\01acc3e5-0af7-49e6-ad7a-a760bd12dc40\agent.json" />
<None Remove="data\agents\01acc3e5-0af7-49e6-ad7a-a760bd12dc40\functions\confirm_knowledge_persistence.json" />
<None Remove="data\agents\01acc3e5-0af7-49e6-ad7a-a760bd12dc40\functions\memorize_knowledge.json" />
<None Remove="data\agents\01acc3e5-0af7-49e6-ad7a-a760bd12dc40\instruction.liquid" />
<None Remove="data\agents\6745151e-6d46-4a02-8de4-1c4f21c7da95\templates\knowledge_retrieval.fn.liquid" />
Expand All @@ -27,6 +28,9 @@
<Content Include="data\agents\01acc3e5-0af7-49e6-ad7a-a760bd12dc40\agent.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="data\agents\01acc3e5-0af7-49e6-ad7a-a760bd12dc40\functions\confirm_knowledge_persistence.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="data\agents\01acc3e5-0af7-49e6-ad7a-a760bd12dc40\functions\memorize_knowledge.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
using BotSharp.Abstraction.Functions;
using BotSharp.Abstraction.Messaging.Enums;
using BotSharp.Abstraction.Messaging.Models.RichContent.Template;
using BotSharp.Abstraction.Messaging.Models.RichContent;
using BotSharp.Abstraction.Messaging;

namespace BotSharp.Plugin.KnowledgeBase.Functions;

public class ConfirmKnowledgePersistenceFn : IFunctionCallback
{
public string Name => "confirm_knowledge_persistence";

private readonly IServiceProvider _services;
private readonly KnowledgeBaseSettings _settings;

public ConfirmKnowledgePersistenceFn(IServiceProvider services, KnowledgeBaseSettings settings)
{
_services = services;
_settings = settings;
}

public async Task<bool> Execute(RoleDialogModel message)
{
var args = JsonSerializer.Deserialize<ExtractedKnowledge>(message.FunctionArgs ?? "{}");

message.Content = $"This knowledge is new to me, should I save it to my memory?";
message.RichContent = BuildRichContent(message.Content);
message.StopCompletion = true;

return true;
}

private RichContent<IRichMessage> BuildRichContent(string text)
{
var states = _services.GetRequiredService<IConversationStateService>();
var conversationId = states.GetConversationId();
var recipient = new Recipient { Id = conversationId };
var res = new RichContent<IRichMessage>
{
Recipient = recipient,
FillPostback = true,
Editor = EditorTypeEnum.None,
Message = new ButtonTemplateMessage
{
Text = text,
Buttons =
[
new ElementButton
{
Type = "text",
Title = "Sure, memorize it",
Payload = "Yes, save it to your memory",
IsPrimary = true
},
new ElementButton
{
Type = "text",
Title = "No, skip the useless information",
Payload = "No, skip it"
}
]
}
};

return res;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,15 @@ public async Task<bool> Execute(RoleDialogModel message)
var id = Utilities.HashTextMd5(args.Question);
var knowledges = await vectorDb.Search("lessen", vector[0], "answer");

message.Content = string.Join("\r\n\r\n=====\r\n", knowledges);
if (knowledges.Count > 0)
{
message.Content = string.Join("\r\n\r\n=====\r\n", knowledges);
}
else
{
message.Content = $"I didn't find any useful knowledge related to [{args.Question}]. \r\nCan you tell me the instruction and I'll memorize it.";
message.StopCompletion = true;
}

return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public async Task<bool> Execute(RoleDialogModel message)
{ "answer", args.Answer }
});

message.Content = $"Save result: {(result ? "success" : "failed")}";
message.Content = result ? "Saved to my brain" : "I forgot it";

return true;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"name": "confirm_knowledge_persistence",
"description": "Confirm with user whether need to save this knowledge to memory",
"parameters": {
"type": "object",
"properties": {
"question": {
"type": "string",
"description": "knowledge topic"
}
},
"required": [ "question" ]
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "memorize_knowledge",
"description": "Retrieve related domain knowledge to handle user request",
"description": "save the knowledge to memory",
"parameters": {
"type": "object",
"properties": {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
You an avid learner who is eager to learn and acquire knowledge from the conversation.
You're good at learning useful domain knowledge and experience by interacting with user.
You are an avid learner who is eager to learn and acquire knowledge from the conversation.
You're good at learning useful domain knowledge and experience by interacting with user.
Summarize the answer or solution in a concise steps.
Confirm with user if you're going to save the knowledge to memory.
If user confirmed, use tool of memorize_knowledge to save it.
39 changes: 39 additions & 0 deletions src/Plugins/BotSharp.Plugin.Planner/BotSharp.Plugin.Planner.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>$(TargetFramework)</TargetFramework>
<Nullable>enable</Nullable>
<LangVersion>$(LangVersion)</LangVersion>
<VersionPrefix>$(BotSharpVersion)</VersionPrefix>
<GeneratePackageOnBuild>$(GeneratePackageOnBuild)</GeneratePackageOnBuild>
<GenerateDocumentationFile>$(GenerateDocumentationFile)</GenerateDocumentationFile>
<OutputPath>$(SolutionDir)packages</OutputPath>
</PropertyGroup>

<ItemGroup>
<None Remove="data\agents\6745151e-6d46-4a02-8de4-1c4f21c7da95\functions\plan_primary_stage.json" />
<None Remove="data\agents\6745151e-6d46-4a02-8de4-1c4f21c7da95\functions\plan_secondary_stage.json" />
<None Remove="data\agents\6745151e-6d46-4a02-8de4-1c4f21c7da95\templates\plan_primary_stage.fn.liquid" />
<None Remove="data\agents\6745151e-6d46-4a02-8de4-1c4f21c7da95\templates\plan_secondary_stage.fn.liquid" />
</ItemGroup>

<ItemGroup>
<Content Include="data\agents\6745151e-6d46-4a02-8de4-1c4f21c7da95\functions\plan_secondary_stage.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="data\agents\6745151e-6d46-4a02-8de4-1c4f21c7da95\functions\plan_primary_stage.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="data\agents\6745151e-6d46-4a02-8de4-1c4f21c7da95\templates\plan_secondary_stage.fn.liquid">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="data\agents\6745151e-6d46-4a02-8de4-1c4f21c7da95\templates\plan_primary_stage.fn.liquid">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
</ItemGroup>

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

</Project>
6 changes: 6 additions & 0 deletions src/Plugins/BotSharp.Plugin.Planner/Enums/UtilityName.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace BotSharp.Plugin.Planner.Enums;

public class UtilityName
{
public const string TwoStagePlanner = "two-stage-planner";
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
using Amazon.Runtime.Internal.Transform;
using BotSharp.Abstraction.Conversations.Models;
using BotSharp.Abstraction.Functions;
using BotSharp.Abstraction.Templating;
using BotSharp.Plugin.Planner.TwoStaging.Models;
using System.Threading.Tasks;

namespace BotSharp.Plugin.Planner.Functions;

public class PrimaryStagePlanFn : IFunctionCallback
{
public string Name => "plan_primary_stage";
private readonly IServiceProvider _services;

public PrimaryStagePlanFn(IServiceProvider services)
{
_services = services;
}

public async Task<bool> Execute(RoleDialogModel message)
{
var task = JsonSerializer.Deserialize<PrimaryRequirementRequest>(message.FunctionArgs);

if (!task.HasKnowledgeReference)
{
message.Content = "Search knowledge base for the solution instructions";
return false;
}

var agentService = _services.GetRequiredService<IAgentService>();
var aiAssistant = await agentService.GetAgent(BuiltInAgentId.AIAssistant);
var template = aiAssistant.Templates.First(x => x.Name == "planner_prompt.two_stage.1st.plan").Content;
var render = _services.GetRequiredService<ITemplateRender>();
render.Render(template, new Dictionary<string, object>
{
{ "relevant_knowledges", message.Content }
});
//message.Content = task.Requirements;
//message.Content += "\r\n\r\n=====\r\nGet the first primary step, plan the secondary steps.";
return true;
}
}
Loading