JsonContent<T>

This commit is contained in:
hchen 2023-10-26 13:52:44 -05:00
parent 012158470f
commit 2cd3dfcd29
3 changed files with 15 additions and 4 deletions

View file

@ -1,3 +1,4 @@
using System.Text.Json;
using System.Text.RegularExpressions;
namespace BotSharp.Abstraction.Utilities;
@ -27,4 +28,16 @@ public static class StringExtensions
{
return str1.Equals(str2, option);
}
public static string JsonContent(this string text)
{
var m = Regex.Match(text, @"\{(?:[^{}]|(?<open>\{)|(?<-open>\}))+(?(open)(?!))\}");
return m.Success ? m.Value : "{}";
}
public static T? JsonContent<T>(this string text)
{
text = JsonContent(text);
return JsonSerializer.Deserialize<T>(text);
}
}

View file

@ -82,9 +82,7 @@ public partial class RoutingService
new RoleDialogModel(AgentRole.User, content)
});
var pattern = @"\{(?:[^{}]|(?<open>\{)|(?<-open>\}))+(?(open)(?!))\}";
response.Content = Regex.Match(response.Content, pattern).Value;
args = JsonSerializer.Deserialize<FunctionCallFromLlm>(response.Content);
args = response.Content.JsonContent<FunctionCallFromLlm>();
break;
}
catch (Exception ex)

View file

@ -1 +1 @@
What is the next step based on the CONVERSATION? Response must be in appropriate JSON format. Route to the latest agent as much as possible.
What is the next step based on the CONVERSATION? Response must be in appropriate JSON format.