Skip to content

Fix bug when there are multiple missing fileds. #141

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
Sep 13, 2023
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 @@ -7,5 +7,6 @@ public interface IAgentRouting
string AgentId { get; }
Task<Agent> LoadRouter();
RoutingItem[] GetRoutingRecords();
RoutingItem GetRecordByAgentId(string id);
RoutingItem GetRecordByName(string name);
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using System.Text.Json.Serialization;

namespace BotSharp.Abstraction.Conversations.Models;

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

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,10 @@ private async Task<bool> GetChatCompletionsAsyncRecursively(Agent agent,
return;
}

fn.Content = fn.FunctionArgs.Replace("\r", " ").Replace("\n", " ").Trim() + " => " + fn.ExecutionResult;
var content = fn.FunctionArgs.Replace("\r", " ").Replace("\n", " ").Trim() + " => " + fn.ExecutionResult;
_logger.LogInformation(content);

fn.Content = content;

// Agent has been transferred
if (fn.CurrentAgentId != preAgentId)
Expand Down
59 changes: 40 additions & 19 deletions src/Infrastructure/BotSharp.Core/Routing/RouteToAgentFn.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using BotSharp.Abstraction.Functions;
using BotSharp.Abstraction.MLTasks;
using BotSharp.Abstraction.Routing.Models;

namespace BotSharp.Core.Routing;
Expand Down Expand Up @@ -39,6 +38,8 @@ public async Task<bool> Execute(RoleDialogModel message)
}
}

// Set default execution data
message.ExecutionData = JsonSerializer.Deserialize<JsonElement>(message.FunctionArgs);
return true;
}

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

agentId = routingRule.AgentId;
// Add routed agent
message.FunctionArgs = AppendPropertyToArgs(message.FunctionArgs, "route_to", agentId);

// Check required fields
var root = JsonSerializer.Deserialize<JsonElement>(message.FunctionArgs);
bool hasMissingField = false;
string missingFieldName = "";
var missingFields = new List<string>();
foreach (var field in routingRule.RequiredFields)
{
if (!root.EnumerateObject().Any(x => x.Name == field))
{
message.ExecutionResult = $"missing {field}.";
hasMissingField = true;
missingFieldName = field;
break;
missingFields.Add(field);
}
else if (root.EnumerateObject().Any(x => x.Name == field) &&
string.IsNullOrEmpty(root.EnumerateObject().FirstOrDefault(x => x.Name == field).Value.ToString()))
{
message.ExecutionResult = $"missing {field}.";
hasMissingField = true;
missingFieldName = field;
break;
missingFields.Add(field);
}
}

// Check if states contains the field according conversation context.
var states = _services.GetRequiredService<IConversationStateService>();
if (!string.IsNullOrEmpty(states.GetState(missingFieldName)))
foreach (var field in missingFields.ToList())
{
var value = states.GetState(missingFieldName);
message.FunctionArgs = message.FunctionArgs.Substring(0, message.FunctionArgs.Length - 1) + $", \"{missingFieldName}\": \"{value}\"" + "}";
hasMissingField = false;
missingFieldName = "";
if (!string.IsNullOrEmpty(states.GetState(field)))
{
var value = states.GetState(field);
message.FunctionArgs = AppendPropertyToArgs(message.FunctionArgs, field, value);
missingFields.Remove(field);
}
}

if (hasMissingField && !string.IsNullOrEmpty(routingRule.RedirectTo))
if (missingFields.Any())
{
agentId = routingRule.RedirectTo;
// Add field to args
message.FunctionArgs = AppendPropertyToArgs(message.FunctionArgs, "missing_fields", missingFields);
message.ExecutionResult = $"missing some information";

// Handle redirect
if (!string.IsNullOrEmpty(routingRule.RedirectTo))
{
agentId = routingRule.RedirectTo;
var agent = router.GetRecordByAgentId(agentId);

// Add redirected agent
message.FunctionArgs = AppendPropertyToArgs(message.FunctionArgs, "redirect_to", agent.Name);
}
}

return hasMissingField;
return missingFields.Any();
}

private string AppendPropertyToArgs(string args, string key, string value)
{
return args.Substring(0, args.Length - 1) + $", \"{key}\": \"{value}\"" + "}";
}

private string AppendPropertyToArgs(string args, string key, IEnumerable<string> values)
{
string fields = string.Join(",", values.Select(x => $"\"{x}\""));
return args.Substring(0, args.Length - 1) + $", \"{key}\": [{fields}]" + "}";
}
}
6 changes: 5 additions & 1 deletion src/Infrastructure/BotSharp.Core/Routing/Router.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
using BotSharp.Abstraction.Repositories;
using BotSharp.Abstraction.Routing.Models;
using BotSharp.Abstraction.Routing.Settings;
using System.IO;

namespace BotSharp.Core.Routing;

Expand Down Expand Up @@ -56,4 +55,9 @@ public RoutingItem GetRecordByName(string name)
{
return GetRoutingRecords().First(x => x.Name.ToLower() == name.ToLower());
}

public RoutingItem GetRecordByAgentId(string id)
{
return GetRoutingRecords().First(x => x.AgentId == id);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,6 @@ public class OpenAiMessageInput : IncomingMessageModel
public string AgentId { get; set; } = string.Empty;
public string ConversationId { get; set; } = string.Empty;

[JsonPropertyName("model")]
public override string ModelName { get; set; } = string.Empty;

public List<OpenAiMessageBody> Messages { get; set; } = new List<OpenAiMessageBody>();

[JsonPropertyName("max_tokens")]
Expand Down