From 4307d7711d2b0e5e2e63c9ae1727b2777e902c87 Mon Sep 17 00:00:00 2001
From: Haiping Chen <101423@smsassist.com>
Date: Mon, 22 Apr 2024 15:46:47 -0500
Subject: [PATCH 1/4] Refactor human intervention needed.
---
.../Routing/IRoutingContext.cs | 4 +-
.../BotSharp.Core/Agents/AgentPlugin.cs | 7 +++
.../BotSharp.Core/BotSharp.Core.csproj | 12 ++++++
.../Functions/HumanInterventionNeededFn.cs | 29 +++++++++++++
.../Routing/Functions/RouteToAgentFn.cs | 2 +-
.../HumanInterventionNeededHandler.cs | 43 -------------------
.../BotSharp.Core/Routing/RoutingContext.cs | 19 +++++++-
.../agent.json | 11 +++++
.../functions.json | 20 +++++++++
.../instruction.liquid | 2 +
.../templates/planner_prompt.naive.liquid | 2 -
11 files changed, 103 insertions(+), 48 deletions(-)
create mode 100644 src/Infrastructure/BotSharp.Core/Routing/Functions/HumanInterventionNeededFn.cs
delete mode 100644 src/Infrastructure/BotSharp.Core/Routing/Handlers/HumanInterventionNeededHandler.cs
create mode 100644 src/Infrastructure/BotSharp.Core/data/agents/01dcc3e5-0af7-49e6-ad7a-a760bd12dc4b/agent.json
create mode 100644 src/Infrastructure/BotSharp.Core/data/agents/01dcc3e5-0af7-49e6-ad7a-a760bd12dc4b/functions.json
create mode 100644 src/Infrastructure/BotSharp.Core/data/agents/01dcc3e5-0af7-49e6-ad7a-a760bd12dc4b/instruction.liquid
diff --git a/src/Infrastructure/BotSharp.Abstraction/Routing/IRoutingContext.cs b/src/Infrastructure/BotSharp.Abstraction/Routing/IRoutingContext.cs
index 2c0aa15e..3c93976b 100644
--- a/src/Infrastructure/BotSharp.Abstraction/Routing/IRoutingContext.cs
+++ b/src/Infrastructure/BotSharp.Abstraction/Routing/IRoutingContext.cs
@@ -3,7 +3,8 @@ namespace BotSharp.Abstraction.Routing;
public interface IRoutingContext
{
string GetCurrentAgentId();
- string PreviousAgentId();
+ string FirstGoalAgentId();
+ bool ContainsAgentId(string agentId);
string OriginAgentId { get; }
string ConversationId { get; }
string MessageId { get; }
@@ -13,6 +14,7 @@ public interface IRoutingContext
int AgentCount { get; }
void Push(string agentId, string? reason = null);
void Pop(string? reason = null);
+ void PopTo(string agentId, string reason);
void Replace(string agentId, string? reason = null);
void Empty(string? reason = null);
}
diff --git a/src/Infrastructure/BotSharp.Core/Agents/AgentPlugin.cs b/src/Infrastructure/BotSharp.Core/Agents/AgentPlugin.cs
index 7df5fa02..eb21fb31 100644
--- a/src/Infrastructure/BotSharp.Core/Agents/AgentPlugin.cs
+++ b/src/Infrastructure/BotSharp.Core/Agents/AgentPlugin.cs
@@ -14,6 +14,13 @@ public class AgentPlugin : IBotSharpPlugin
public SettingsMeta Settings =>
new SettingsMeta("Agent");
+ public string[] AgentIds => new string[]
+ {
+ "01fcc3e5-9af7-49e6-ad7a-a760bd12dc4a",
+ "01e2fc5c-2c89-4ec7-8470-7688608b496c",
+ "01dcc3e5-0af7-49e6-ad7a-a760bd12dc4b"
+ };
+
public object GetNewSettingsInstance() =>
new AgentSettings();
diff --git a/src/Infrastructure/BotSharp.Core/BotSharp.Core.csproj b/src/Infrastructure/BotSharp.Core/BotSharp.Core.csproj
index ece7daa7..873fa93a 100644
--- a/src/Infrastructure/BotSharp.Core/BotSharp.Core.csproj
+++ b/src/Infrastructure/BotSharp.Core/BotSharp.Core.csproj
@@ -46,6 +46,9 @@
+
+
+
@@ -67,6 +70,15 @@
+
+ PreserveNewest
+
+
+ PreserveNewest
+
+
+ PreserveNewest
+
PreserveNewest
diff --git a/src/Infrastructure/BotSharp.Core/Routing/Functions/HumanInterventionNeededFn.cs b/src/Infrastructure/BotSharp.Core/Routing/Functions/HumanInterventionNeededFn.cs
new file mode 100644
index 00000000..d0c26be0
--- /dev/null
+++ b/src/Infrastructure/BotSharp.Core/Routing/Functions/HumanInterventionNeededFn.cs
@@ -0,0 +1,29 @@
+using BotSharp.Abstraction.Functions;
+
+namespace BotSharp.Core.Routing.Functions;
+
+public class HumanInterventionNeededFn : IFunctionCallback
+{
+ public string Name => "human_intervention_needed";
+
+ private readonly IServiceProvider _services;
+
+ public HumanInterventionNeededFn(IServiceProvider services)
+ {
+ _services = services;
+ }
+
+ public async Task Execute(RoleDialogModel message)
+ {
+ var hooks = _services.GetServices()
+ .OrderBy(x => x.Priority)
+ .ToList();
+
+ foreach (var hook in hooks)
+ {
+ await hook.OnHumanInterventionNeeded(message);
+ }
+
+ return true;
+ }
+}
diff --git a/src/Infrastructure/BotSharp.Core/Routing/Functions/RouteToAgentFn.cs b/src/Infrastructure/BotSharp.Core/Routing/Functions/RouteToAgentFn.cs
index 8221cfb5..2450a6fc 100644
--- a/src/Infrastructure/BotSharp.Core/Routing/Functions/RouteToAgentFn.cs
+++ b/src/Infrastructure/BotSharp.Core/Routing/Functions/RouteToAgentFn.cs
@@ -51,7 +51,7 @@ public partial class RouteToAgentFn : IFunctionCallback
var originalAgent = db.GetAgents(filter).FirstOrDefault();
if (originalAgent != null)
{
- _context.Push(originalAgent.Id, $"user goal agent{(correctToOriginalAgent ? " & is corrected" : "")}");
+ _context.Push(originalAgent.Id, $"user goal agent{(correctToOriginalAgent ? " " + originalAgent.Name + " & is corrected" : "")}");
}
}
diff --git a/src/Infrastructure/BotSharp.Core/Routing/Handlers/HumanInterventionNeededHandler.cs b/src/Infrastructure/BotSharp.Core/Routing/Handlers/HumanInterventionNeededHandler.cs
deleted file mode 100644
index 4aca2c3a..00000000
--- a/src/Infrastructure/BotSharp.Core/Routing/Handlers/HumanInterventionNeededHandler.cs
+++ /dev/null
@@ -1,43 +0,0 @@
-using BotSharp.Abstraction.Routing.Settings;
-
-namespace BotSharp.Core.Routing.Handlers;
-
-public class HumanInterventionNeededHandler : RoutingHandlerBase, IRoutingHandler
-{
- public string Name => "human_intervention_needed";
-
- public string Description => "Reach out to human customer service.";
-
- public List Parameters => new List
- {
- new ParameterPropertyDef("reason", "why need customer service"),
- new ParameterPropertyDef("summary", "the whole conversation summary with important information"),
- new ParameterPropertyDef("response", "asking user whether to connect with customer service representative")
- };
-
- public HumanInterventionNeededHandler(IServiceProvider services, ILogger logger, RoutingSettings settings)
- : base(services, logger, settings)
- {
-
- }
-
- public async Task Handle(IRoutingService routing, FunctionCallFromLlm inst, RoleDialogModel message)
- {
- var response = RoleDialogModel.From(message,
- role: AgentRole.Assistant,
- content: inst.Response);
-
- _dialogs.Add(response);
-
- var hooks = _services.GetServices()
- .OrderBy(x => x.Priority)
- .ToList();
-
- foreach (var hook in hooks)
- {
- await hook.OnHumanInterventionNeeded(response);
- }
-
- return true;
- }
-}
diff --git a/src/Infrastructure/BotSharp.Core/Routing/RoutingContext.cs b/src/Infrastructure/BotSharp.Core/Routing/RoutingContext.cs
index 87e55840..4d8eba43 100644
--- a/src/Infrastructure/BotSharp.Core/Routing/RoutingContext.cs
+++ b/src/Infrastructure/BotSharp.Core/Routing/RoutingContext.cs
@@ -1,4 +1,5 @@
using BotSharp.Abstraction.Routing.Settings;
+using BotSharp.Abstraction.Utilities;
namespace BotSharp.Core.Routing;
@@ -137,7 +138,18 @@ public class RoutingContext : IRoutingContext
}
}
- public string PreviousAgentId()
+ public void PopTo(string agentId, string reason)
+ {
+ var currentAgentId = GetCurrentAgentId();
+ while (!string.IsNullOrEmpty(currentAgentId) &&
+ currentAgentId != agentId)
+ {
+ Pop(reason);
+ currentAgentId = GetCurrentAgentId();
+ }
+ }
+
+ public string FirstGoalAgentId()
{
if (_stack.Count == 1)
{
@@ -151,6 +163,11 @@ public class RoutingContext : IRoutingContext
return string.Empty;
}
+ public bool ContainsAgentId(string agentId)
+ {
+ return _stack.ToArray().Contains(agentId);
+ }
+
public void Replace(string agentId, string? reason = null)
{
var fromAgent = agentId;
diff --git a/src/Infrastructure/BotSharp.Core/data/agents/01dcc3e5-0af7-49e6-ad7a-a760bd12dc4b/agent.json b/src/Infrastructure/BotSharp.Core/data/agents/01dcc3e5-0af7-49e6-ad7a-a760bd12dc4b/agent.json
new file mode 100644
index 00000000..d32e4680
--- /dev/null
+++ b/src/Infrastructure/BotSharp.Core/data/agents/01dcc3e5-0af7-49e6-ad7a-a760bd12dc4b/agent.json
@@ -0,0 +1,11 @@
+{
+ "id": "01dcc3e5-0af7-49e6-ad7a-a760bd12dc4b",
+ "name": "Human Support",
+ "description": "Reach out to human customer service representative.",
+ "type": "task",
+ "createdDateTime": "2024-04-22T10:00:00Z",
+ "updatedDateTime": "2024-04-22T10:00:00Z",
+ "disabled": false,
+ "isPublic": true,
+ "profiles": [ "human" ]
+}
\ No newline at end of file
diff --git a/src/Infrastructure/BotSharp.Core/data/agents/01dcc3e5-0af7-49e6-ad7a-a760bd12dc4b/functions.json b/src/Infrastructure/BotSharp.Core/data/agents/01dcc3e5-0af7-49e6-ad7a-a760bd12dc4b/functions.json
new file mode 100644
index 00000000..240f5b7c
--- /dev/null
+++ b/src/Infrastructure/BotSharp.Core/data/agents/01dcc3e5-0af7-49e6-ad7a-a760bd12dc4b/functions.json
@@ -0,0 +1,20 @@
+[
+ {
+ "name": "human_intervention_needed",
+ "description": "If user wants to speak to human customer service.",
+ "parameters": {
+ "type": "object",
+ "properties": {
+ "reason": {
+ "type": "string",
+ "description": "why customer needs customer service."
+ },
+ "summary": {
+ "type": "string",
+ "description": "the whole conversation summary with important information"
+ }
+ },
+ "required": [ "reason", "summary" ]
+ }
+ }
+]
diff --git a/src/Infrastructure/BotSharp.Core/data/agents/01dcc3e5-0af7-49e6-ad7a-a760bd12dc4b/instruction.liquid b/src/Infrastructure/BotSharp.Core/data/agents/01dcc3e5-0af7-49e6-ad7a-a760bd12dc4b/instruction.liquid
new file mode 100644
index 00000000..3b1aab42
--- /dev/null
+++ b/src/Infrastructure/BotSharp.Core/data/agents/01dcc3e5-0af7-49e6-ad7a-a760bd12dc4b/instruction.liquid
@@ -0,0 +1,2 @@
+You are a human customer service connection program.
+When other AI customer service cannot solve user problems, you know how to call the API to transfer users to human customer service for answers.
\ No newline at end of file
diff --git a/src/Infrastructure/BotSharp.Core/data/agents/01fcc3e5-9af7-49e6-ad7a-a760bd12dc4a/templates/planner_prompt.naive.liquid b/src/Infrastructure/BotSharp.Core/data/agents/01fcc3e5-9af7-49e6-ad7a-a760bd12dc4a/templates/planner_prompt.naive.liquid
index 3a332350..f7388be6 100644
--- a/src/Infrastructure/BotSharp.Core/data/agents/01fcc3e5-9af7-49e6-ad7a-a760bd12dc4a/templates/planner_prompt.naive.liquid
+++ b/src/Infrastructure/BotSharp.Core/data/agents/01fcc3e5-9af7-49e6-ad7a-a760bd12dc4a/templates/planner_prompt.naive.liquid
@@ -10,5 +10,3 @@ Expected user goal agent is {{ expected_user_goal_agent }}.
{%- else -%}
User goal agent is inferred based on user initial request.
{%- endif %}
-If user wants to speak to human customer service, use function human_intervention_needed.
-If user wants to or is processing with a specific task that can be handled by agents, respond in appropriate output format defined to let proper agent to handle the task.
From d54eb271b3cb06e2438d513146ed045f95682588 Mon Sep 17 00:00:00 2001
From: sylviachency <33144082+sylviachency@users.noreply.github.com>
Date: Tue, 23 Apr 2024 00:16:43 -0500
Subject: [PATCH 2/4] Update instruction.liquid
---
.../01dcc3e5-0af7-49e6-ad7a-a760bd12dc4b/instruction.liquid | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/src/Infrastructure/BotSharp.Core/data/agents/01dcc3e5-0af7-49e6-ad7a-a760bd12dc4b/instruction.liquid b/src/Infrastructure/BotSharp.Core/data/agents/01dcc3e5-0af7-49e6-ad7a-a760bd12dc4b/instruction.liquid
index 3b1aab42..58795499 100644
--- a/src/Infrastructure/BotSharp.Core/data/agents/01dcc3e5-0af7-49e6-ad7a-a760bd12dc4b/instruction.liquid
+++ b/src/Infrastructure/BotSharp.Core/data/agents/01dcc3e5-0af7-49e6-ad7a-a760bd12dc4b/instruction.liquid
@@ -1,2 +1,2 @@
You are a human customer service connection program.
-When other AI customer service cannot solve user problems, you know how to call the API to transfer users to human customer service for answers.
\ No newline at end of file
+When other AI customer service agents cannot solve user problems, you know how to call the API to transfer users to human customer service for answers.
From b8baf14d63b41da5a1c0b7e08dda5de356dca4a2 Mon Sep 17 00:00:00 2001
From: sylviachency <33144082+sylviachency@users.noreply.github.com>
Date: Tue, 23 Apr 2024 00:17:06 -0500
Subject: [PATCH 3/4] Update instruction.liquid
---
.../dfd9b46d-d00c-40af-8a75-3fbdc2b89869/instruction.liquid | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/src/Infrastructure/BotSharp.Core/data/agents/dfd9b46d-d00c-40af-8a75-3fbdc2b89869/instruction.liquid b/src/Infrastructure/BotSharp.Core/data/agents/dfd9b46d-d00c-40af-8a75-3fbdc2b89869/instruction.liquid
index d0bed0db..bc80b14c 100644
--- a/src/Infrastructure/BotSharp.Core/data/agents/dfd9b46d-d00c-40af-8a75-3fbdc2b89869/instruction.liquid
+++ b/src/Infrastructure/BotSharp.Core/data/agents/dfd9b46d-d00c-40af-8a75-3fbdc2b89869/instruction.liquid
@@ -1,7 +1,7 @@
-This is a model evaluation program, which interactive with model to complete a certain task based on the background information given to you.
+This is a model evaluation program, which interacts with model to complete a certain task based on the background information given to you.
{{ task_prompt }}
user: Hi!
assistant: Hello, How can I help you?
-user:
\ No newline at end of file
+user:
From bac6888934ee91f7b9fe572ddc129f00e533cf70 Mon Sep 17 00:00:00 2001
From: sylviachency <33144082+sylviachency@users.noreply.github.com>
Date: Tue, 23 Apr 2024 00:19:34 -0500
Subject: [PATCH 4/4] Update agent.json
---
.../agents/01dcc3e5-0af7-49e6-ad7a-a760bd12dc4b/agent.json | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/src/Infrastructure/BotSharp.Core/data/agents/01dcc3e5-0af7-49e6-ad7a-a760bd12dc4b/agent.json b/src/Infrastructure/BotSharp.Core/data/agents/01dcc3e5-0af7-49e6-ad7a-a760bd12dc4b/agent.json
index d32e4680..24508fcc 100644
--- a/src/Infrastructure/BotSharp.Core/data/agents/01dcc3e5-0af7-49e6-ad7a-a760bd12dc4b/agent.json
+++ b/src/Infrastructure/BotSharp.Core/data/agents/01dcc3e5-0af7-49e6-ad7a-a760bd12dc4b/agent.json
@@ -1,11 +1,11 @@
{
"id": "01dcc3e5-0af7-49e6-ad7a-a760bd12dc4b",
"name": "Human Support",
- "description": "Reach out to human customer service representative.",
+ "description": "Reach out to human customer service.",
"type": "task",
"createdDateTime": "2024-04-22T10:00:00Z",
"updatedDateTime": "2024-04-22T10:00:00Z",
"disabled": false,
"isPublic": true,
"profiles": [ "human" ]
-}
\ No newline at end of file
+}