Skip to content

Refactor FixMalformedResponse. #177

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
Oct 18, 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 @@ -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>
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -96,29 +92,10 @@ public async Task<FunctionCallFromLlm> GetNextInstruction()
_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");
}

// 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);
}
// Fix LLM malformed response
FixMalformedResponse(args);

args.Function = args.Function.Split('.').Last();
SaveStateByArgs(args.Arguments);

#if DEBUG
Console.WriteLine($"*** Next Instruction *** {args}", Color.Green);
Expand All @@ -145,4 +122,54 @@ private string GetNextStepPrompt()
{ "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");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
}