Merge pull request #177 from hchen2020/master

Refactor FixMalformedResponse.
This commit is contained in:
Haiping 2023-10-18 10:55:41 -05:00 committed by GitHub
commit 67a445a4e0
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 56 additions and 28 deletions

View file

@ -10,7 +10,7 @@ public interface IAgentService
Task<List<Agent>> GetAgents();
/// <summary>
/// Load agent configurations and trigghe hooks
/// Load agent configurations and trigger hooks
/// </summary>
/// <param name="id"></param>
/// <returns></returns>

View file

@ -1,11 +1,7 @@
using BotSharp.Abstraction.Functions.Models;
using BotSharp.Abstraction.Repositories;
using BotSharp.Abstraction.Routing.Models;
using BotSharp.Abstraction.Templating;
using System.Drawing;
using System.IO;
using System.Text.RegularExpressions;
namespace BotSharp.Core.Routing;
public partial class RoutingService
@ -96,29 +92,10 @@ public partial class RoutingService
_logger.LogInformation(response.Content);
#endif
// Sometimes it populate malformed Function in Agent name
if (!string.IsNullOrEmpty(args.Function) && args.Function == args.AgentName)
{
args.Function = "route_to_agent";
_logger.LogWarning($"Captured LLM malformed response");
}
// Fix LLM malformed response
FixMalformedResponse(args);
// Another case of malformed response
var agentService = _services.GetRequiredService<IAgentService>();
var agents = await agentService.GetAgents();
if (string.IsNullOrEmpty(args.AgentName) && agents.Select(x => x.Name).Contains(args.Function))
{
args.AgentName = args.Function;
args.Function = "route_to_agent";
_logger.LogWarning($"Captured LLM malformed response");
}
if (args.Arguments != null)
{
SaveStateByArgs(args.Arguments);
}
args.Function = args.Function.Split('.').Last();
SaveStateByArgs(args.Arguments);
#if DEBUG
Console.WriteLine($"*** Next Instruction *** {args}", Color.Green);
@ -145,4 +122,54 @@ public partial class RoutingService
{ "enabled_reasoning", _settings.EnableReasoning }
});
}
/// <summary>
/// Sometimes LLM hallucinates and fails to set function names correctly.
/// </summary>
/// <param name="args"></param>
private void FixMalformedResponse(FunctionCallFromLlm args)
{
var agentService = _services.GetRequiredService<IAgentService>();
var agents = agentService.GetAgents().Result;
var malformed = false;
// Sometimes it populate malformed Function in Agent name
if (!string.IsNullOrEmpty(args.Function) &&
args.Function == args.AgentName)
{
args.Function = "route_to_agent";
malformed = true;
}
// Another case of malformed response
if (string.IsNullOrEmpty(args.AgentName) &&
agents.Select(x => x.Name).Contains(args.Function))
{
args.AgentName = args.Function;
args.Function = "route_to_agent";
malformed = true;
}
// It should be Route to agent, but it is used as Response to user.
if (string.IsNullOrEmpty(args.AgentName) &&
agents.Select(x => x.Name).Contains(args.AgentName) &&
args.Function != "route_to_agent")
{
args.Function = "route_to_agent";
malformed = true;
}
// Function name shouldn't contain dot symbol
if (!string.IsNullOrEmpty(args.Function) &&
args.Function.Contains('.'))
{
args.Function = args.Function.Split('.').Last();
malformed = true;
}
if (malformed)
{
_logger.LogWarning($"Captured LLM malformed response");
}
}
}

View file

@ -3,5 +3,6 @@
"description": "Pizza restaurant AI Bot",
"createdDateTime": "2023-08-18T14:39:32.2349685Z",
"updatedDateTime": "2023-08-18T14:39:32.2349686Z",
"id": "01fcc3e5-9af7-49e6-ad7a-a760bd12dc4a"
"id": "01fcc3e5-9af7-49e6-ad7a-a760bd12dc4a",
"isPublic": true
}