Skip to content

Commit 4d25681

Browse files
authored
Merge pull request #141 from hchen2020/master
Fix bug when there are multiple missing fileds.
2 parents 65e8554 + e939707 commit 4d25681

File tree

6 files changed

+53
-24
lines changed

6 files changed

+53
-24
lines changed

src/Infrastructure/BotSharp.Abstraction/Agents/IAgentRouting.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,6 @@ public interface IAgentRouting
77
string AgentId { get; }
88
Task<Agent> LoadRouter();
99
RoutingItem[] GetRoutingRecords();
10+
RoutingItem GetRecordByAgentId(string id);
1011
RoutingItem GetRecordByName(string name);
1112
}

src/Infrastructure/BotSharp.Abstraction/Conversations/Models/IncomingMessageModel.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
using System.Text.Json.Serialization;
2+
13
namespace BotSharp.Abstraction.Conversations.Models;
24

35
public class IncomingMessageModel
@@ -9,6 +11,7 @@ public class IncomingMessageModel
911
/// <summary>
1012
/// Model name
1113
/// </summary>
14+
[JsonPropertyName("model")]
1215
public virtual string ModelName { get; set; } = "gpt-3.5-turbo";
1316

1417
/// <summary>

src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationService.GetChatCompletionsAsyncRecursively.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,10 @@ private async Task<bool> GetChatCompletionsAsyncRecursively(Agent agent,
7878
return;
7979
}
8080

81-
fn.Content = fn.FunctionArgs.Replace("\r", " ").Replace("\n", " ").Trim() + " => " + fn.ExecutionResult;
81+
var content = fn.FunctionArgs.Replace("\r", " ").Replace("\n", " ").Trim() + " => " + fn.ExecutionResult;
82+
_logger.LogInformation(content);
83+
84+
fn.Content = content;
8285

8386
// Agent has been transferred
8487
if (fn.CurrentAgentId != preAgentId)

src/Infrastructure/BotSharp.Core/Routing/RouteToAgentFn.cs

Lines changed: 40 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
using BotSharp.Abstraction.Functions;
2-
using BotSharp.Abstraction.MLTasks;
32
using BotSharp.Abstraction.Routing.Models;
43

54
namespace BotSharp.Core.Routing;
@@ -39,6 +38,8 @@ public async Task<bool> Execute(RoleDialogModel message)
3938
}
4039
}
4140

41+
// Set default execution data
42+
message.ExecutionData = JsonSerializer.Deserialize<JsonElement>(message.FunctionArgs);
4243
return true;
4344
}
4445

@@ -60,45 +61,65 @@ private bool HasMissingRequiredField(RoleDialogModel message, out string agentId
6061
}
6162

6263
agentId = routingRule.AgentId;
64+
// Add routed agent
65+
message.FunctionArgs = AppendPropertyToArgs(message.FunctionArgs, "route_to", agentId);
6366

6467
// Check required fields
6568
var root = JsonSerializer.Deserialize<JsonElement>(message.FunctionArgs);
66-
bool hasMissingField = false;
67-
string missingFieldName = "";
69+
var missingFields = new List<string>();
6870
foreach (var field in routingRule.RequiredFields)
6971
{
7072
if (!root.EnumerateObject().Any(x => x.Name == field))
7173
{
72-
message.ExecutionResult = $"missing {field}.";
73-
hasMissingField = true;
74-
missingFieldName = field;
75-
break;
74+
missingFields.Add(field);
7675
}
7776
else if (root.EnumerateObject().Any(x => x.Name == field) &&
7877
string.IsNullOrEmpty(root.EnumerateObject().FirstOrDefault(x => x.Name == field).Value.ToString()))
7978
{
80-
message.ExecutionResult = $"missing {field}.";
81-
hasMissingField = true;
82-
missingFieldName = field;
83-
break;
79+
missingFields.Add(field);
8480
}
8581
}
8682

8783
// Check if states contains the field according conversation context.
8884
var states = _services.GetRequiredService<IConversationStateService>();
89-
if (!string.IsNullOrEmpty(states.GetState(missingFieldName)))
85+
foreach (var field in missingFields.ToList())
9086
{
91-
var value = states.GetState(missingFieldName);
92-
message.FunctionArgs = message.FunctionArgs.Substring(0, message.FunctionArgs.Length - 1) + $", \"{missingFieldName}\": \"{value}\"" + "}";
93-
hasMissingField = false;
94-
missingFieldName = "";
87+
if (!string.IsNullOrEmpty(states.GetState(field)))
88+
{
89+
var value = states.GetState(field);
90+
message.FunctionArgs = AppendPropertyToArgs(message.FunctionArgs, field, value);
91+
missingFields.Remove(field);
92+
}
9593
}
9694

97-
if (hasMissingField && !string.IsNullOrEmpty(routingRule.RedirectTo))
95+
if (missingFields.Any())
9896
{
99-
agentId = routingRule.RedirectTo;
97+
// Add field to args
98+
message.FunctionArgs = AppendPropertyToArgs(message.FunctionArgs, "missing_fields", missingFields);
99+
message.ExecutionResult = $"missing some information";
100+
101+
// Handle redirect
102+
if (!string.IsNullOrEmpty(routingRule.RedirectTo))
103+
{
104+
agentId = routingRule.RedirectTo;
105+
var agent = router.GetRecordByAgentId(agentId);
106+
107+
// Add redirected agent
108+
message.FunctionArgs = AppendPropertyToArgs(message.FunctionArgs, "redirect_to", agent.Name);
109+
}
100110
}
101111

102-
return hasMissingField;
112+
return missingFields.Any();
113+
}
114+
115+
private string AppendPropertyToArgs(string args, string key, string value)
116+
{
117+
return args.Substring(0, args.Length - 1) + $", \"{key}\": \"{value}\"" + "}";
118+
}
119+
120+
private string AppendPropertyToArgs(string args, string key, IEnumerable<string> values)
121+
{
122+
string fields = string.Join(",", values.Select(x => $"\"{x}\""));
123+
return args.Substring(0, args.Length - 1) + $", \"{key}\": [{fields}]" + "}";
103124
}
104125
}

src/Infrastructure/BotSharp.Core/Routing/Router.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
using BotSharp.Abstraction.Repositories;
44
using BotSharp.Abstraction.Routing.Models;
55
using BotSharp.Abstraction.Routing.Settings;
6-
using System.IO;
76

87
namespace BotSharp.Core.Routing;
98

@@ -56,4 +55,9 @@ public RoutingItem GetRecordByName(string name)
5655
{
5756
return GetRoutingRecords().First(x => x.Name.ToLower() == name.ToLower());
5857
}
58+
59+
public RoutingItem GetRecordByAgentId(string id)
60+
{
61+
return GetRoutingRecords().First(x => x.AgentId == id);
62+
}
5963
}

src/Plugins/BotSharp.Plugin.ChatbotUI/ViewModels/OpenAiMessageInput.cs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,6 @@ public class OpenAiMessageInput : IncomingMessageModel
1010
public string AgentId { get; set; } = string.Empty;
1111
public string ConversationId { get; set; } = string.Empty;
1212

13-
[JsonPropertyName("model")]
14-
public override string ModelName { get; set; } = string.Empty;
15-
1613
public List<OpenAiMessageBody> Messages { get; set; } = new List<OpenAiMessageBody>();
1714

1815
[JsonPropertyName("max_tokens")]

0 commit comments

Comments
 (0)