Merge pull request #141 from hchen2020/master

Fix bug when there are multiple missing fileds.
This commit is contained in:
Haiping 2023-09-13 05:11:07 -05:00 committed by GitHub
commit 4d25681491
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 53 additions and 24 deletions

View file

@ -7,5 +7,6 @@ public interface IAgentRouting
string AgentId { get; }
Task<Agent> LoadRouter();
RoutingItem[] GetRoutingRecords();
RoutingItem GetRecordByAgentId(string id);
RoutingItem GetRecordByName(string name);
}

View file

@ -1,3 +1,5 @@
using System.Text.Json.Serialization;
namespace BotSharp.Abstraction.Conversations.Models;
public class IncomingMessageModel
@ -9,6 +11,7 @@ public class IncomingMessageModel
/// <summary>
/// Model name
/// </summary>
[JsonPropertyName("model")]
public virtual string ModelName { get; set; } = "gpt-3.5-turbo";
/// <summary>

View file

@ -78,7 +78,10 @@ public partial class ConversationService
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)

View file

@ -1,5 +1,4 @@
using BotSharp.Abstraction.Functions;
using BotSharp.Abstraction.MLTasks;
using BotSharp.Abstraction.Routing.Models;
namespace BotSharp.Core.Routing;
@ -39,6 +38,8 @@ public class RouteToAgentFn : IFunctionCallback
}
}
// Set default execution data
message.ExecutionData = JsonSerializer.Deserialize<JsonElement>(message.FunctionArgs);
return true;
}
@ -60,45 +61,65 @@ public class RouteToAgentFn : IFunctionCallback
}
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}]" + "}";
}
}

View file

@ -3,7 +3,6 @@ using BotSharp.Abstraction.Agents.Models;
using BotSharp.Abstraction.Repositories;
using BotSharp.Abstraction.Routing.Models;
using BotSharp.Abstraction.Routing.Settings;
using System.IO;
namespace BotSharp.Core.Routing;
@ -56,4 +55,9 @@ public class Router : IAgentRouting
{
return GetRoutingRecords().First(x => x.Name.ToLower() == name.ToLower());
}
public RoutingItem GetRecordByAgentId(string id)
{
return GetRoutingRecords().First(x => x.AgentId == id);
}
}

View file

@ -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")]