Refactor FixMalformedResponse.
This commit is contained in:
parent
2f3e71ef09
commit
5ced9f6da4
|
|
@ -10,7 +10,7 @@ public interface IAgentService
|
||||||
Task<List<Agent>> GetAgents();
|
Task<List<Agent>> GetAgents();
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Load agent configurations and trigghe hooks
|
/// Load agent configurations and trigger hooks
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="id"></param>
|
/// <param name="id"></param>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
|
|
|
||||||
|
|
@ -1,11 +1,7 @@
|
||||||
using BotSharp.Abstraction.Functions.Models;
|
using BotSharp.Abstraction.Functions.Models;
|
||||||
using BotSharp.Abstraction.Repositories;
|
|
||||||
using BotSharp.Abstraction.Routing.Models;
|
|
||||||
using BotSharp.Abstraction.Templating;
|
using BotSharp.Abstraction.Templating;
|
||||||
using System.Drawing;
|
using System.Drawing;
|
||||||
using System.IO;
|
|
||||||
using System.Text.RegularExpressions;
|
using System.Text.RegularExpressions;
|
||||||
|
|
||||||
namespace BotSharp.Core.Routing;
|
namespace BotSharp.Core.Routing;
|
||||||
|
|
||||||
public partial class RoutingService
|
public partial class RoutingService
|
||||||
|
|
@ -96,29 +92,10 @@ public partial class RoutingService
|
||||||
_logger.LogInformation(response.Content);
|
_logger.LogInformation(response.Content);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
// Sometimes it populate malformed Function in Agent name
|
// Fix LLM malformed response
|
||||||
if (!string.IsNullOrEmpty(args.Function) && args.Function == args.AgentName)
|
FixMalformedResponse(args);
|
||||||
{
|
|
||||||
args.Function = "route_to_agent";
|
|
||||||
_logger.LogWarning($"Captured LLM malformed response");
|
|
||||||
}
|
|
||||||
|
|
||||||
// Another case of malformed response
|
SaveStateByArgs(args.Arguments);
|
||||||
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();
|
|
||||||
|
|
||||||
#if DEBUG
|
#if DEBUG
|
||||||
Console.WriteLine($"*** Next Instruction *** {args}", Color.Green);
|
Console.WriteLine($"*** Next Instruction *** {args}", Color.Green);
|
||||||
|
|
@ -145,4 +122,54 @@ public partial class RoutingService
|
||||||
{ "enabled_reasoning", _settings.EnableReasoning }
|
{ "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");
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -3,5 +3,6 @@
|
||||||
"description": "Pizza restaurant AI Bot",
|
"description": "Pizza restaurant AI Bot",
|
||||||
"createdDateTime": "2023-08-18T14:39:32.2349685Z",
|
"createdDateTime": "2023-08-18T14:39:32.2349685Z",
|
||||||
"updatedDateTime": "2023-08-18T14:39:32.2349686Z",
|
"updatedDateTime": "2023-08-18T14:39:32.2349686Z",
|
||||||
"id": "01fcc3e5-9af7-49e6-ad7a-a760bd12dc4a"
|
"id": "01fcc3e5-9af7-49e6-ad7a-a760bd12dc4a",
|
||||||
|
"isPublic": true
|
||||||
}
|
}
|
||||||
Loading…
Reference in a new issue