resolve conflict.
This commit is contained in:
commit
5f0c3106bb
|
|
@ -11,6 +11,9 @@ public abstract class ConversationHookBase : IConversationHook
|
|||
protected List<RoleDialogModel> _dialogs;
|
||||
public List<RoleDialogModel> Dialogs => _dialogs;
|
||||
|
||||
protected int _priority = 0;
|
||||
public int Priority => _priority;
|
||||
|
||||
public IConversationHook SetAgent(Agent agent)
|
||||
{
|
||||
_agent = agent;
|
||||
|
|
|
|||
|
|
@ -2,6 +2,7 @@ namespace BotSharp.Abstraction.Conversations;
|
|||
|
||||
public interface IConversationHook
|
||||
{
|
||||
int Priority { get; }
|
||||
Agent Agent { get; }
|
||||
IConversationHook SetAgent(Agent agent);
|
||||
|
||||
|
|
|
|||
|
|
@ -30,6 +30,11 @@ public class RoleDialogModel
|
|||
/// </summary>
|
||||
public object ExecutionData { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Intent name
|
||||
/// </summary>
|
||||
public string IntentName { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Stop conversation completion
|
||||
/// </summary>
|
||||
|
|
|
|||
|
|
@ -2,5 +2,7 @@ namespace BotSharp.Abstraction.Templating;
|
|||
|
||||
public interface IResponseTemplateService
|
||||
{
|
||||
Task<string> RenderFunctionResponse(string agentId, RoleDialogModel fn);
|
||||
Task<string> RenderFunctionResponse(string agentId, RoleDialogModel message);
|
||||
|
||||
Task<string> RenderIntentResponse(string agentId, RoleDialogModel message);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -7,7 +7,8 @@ public partial class ConversationService
|
|||
{
|
||||
private async Task CallFunctions(RoleDialogModel msg)
|
||||
{
|
||||
var hooks = _services.GetServices<IConversationHook>().ToList();
|
||||
var hooks = _services.GetServices<IConversationHook>()
|
||||
.OrderBy(x => x.Priority).ToList();
|
||||
|
||||
// Invoke functions
|
||||
var functions = _services.GetServices<IFunctionCallback>()
|
||||
|
|
|
|||
|
|
@ -12,13 +12,13 @@ public class ResponseTemplateService : IResponseTemplateService
|
|||
_services = services;
|
||||
}
|
||||
|
||||
public async Task<string> RenderFunctionResponse(string agentId, RoleDialogModel fn)
|
||||
public async Task<string> RenderFunctionResponse(string agentId, RoleDialogModel message)
|
||||
{
|
||||
// Find response template
|
||||
var agentService = _services.GetRequiredService<IAgentService>();
|
||||
var dir = Path.Combine(agentService.GetAgentDataDir(agentId), "responses");
|
||||
var responses = Directory.GetFiles(dir)
|
||||
.Where(f => f.Split(Path.DirectorySeparatorChar).Last().Split('.')[1] == fn.FunctionName)
|
||||
.Where(f => f.Split(Path.DirectorySeparatorChar).Last().Split('.')[1] == message.FunctionName)
|
||||
.ToList();
|
||||
|
||||
if (responses.Count == 0)
|
||||
|
|
@ -33,8 +33,37 @@ public class ResponseTemplateService : IResponseTemplateService
|
|||
|
||||
// Convert args and execute data to dictionary
|
||||
var dict = new Dictionary<string, object>();
|
||||
ExtractArgs(JsonSerializer.Deserialize<JsonDocument>(fn.FunctionArgs), dict);
|
||||
ExtractExecuteData(fn.ExecutionData, dict);
|
||||
ExtractArgs(JsonSerializer.Deserialize<JsonDocument>(message.FunctionArgs), dict);
|
||||
ExtractExecuteData(message.ExecutionData, dict);
|
||||
|
||||
var text = render.Render(template, dict);
|
||||
|
||||
return text;
|
||||
}
|
||||
|
||||
public async Task<string> RenderIntentResponse(string agentId, RoleDialogModel message)
|
||||
{
|
||||
// Find response template
|
||||
var agentService = _services.GetRequiredService<IAgentService>();
|
||||
var dir = Path.Combine(agentService.GetAgentDataDir(agentId), "responses");
|
||||
var responses = Directory.GetFiles(dir)
|
||||
.Where(f => f.Split(Path.DirectorySeparatorChar).Last().Split('.')[1] == message.IntentName)
|
||||
.ToList();
|
||||
|
||||
if (responses.Count == 0)
|
||||
{
|
||||
return string.Empty;
|
||||
}
|
||||
|
||||
var randomIndex = new Random().Next(0, responses.Count);
|
||||
var template = File.ReadAllText(responses[randomIndex]);
|
||||
|
||||
var render = _services.GetRequiredService<ITemplateRender>();
|
||||
|
||||
// Convert args and execute data to dictionary
|
||||
var dict = new Dictionary<string, object>();
|
||||
ExtractArgs(JsonSerializer.Deserialize<JsonDocument>(message.FunctionArgs), dict);
|
||||
ExtractExecuteData(message.ExecutionData, dict);
|
||||
|
||||
var text = render.Render(template, dict);
|
||||
|
||||
|
|
|
|||
|
|
@ -1,3 +1,5 @@
|
|||
using BotSharp.Abstraction.Agents.Enums;
|
||||
using BotSharp.Abstraction.Agents.Models;
|
||||
using BotSharp.Abstraction.Conversations;
|
||||
using BotSharp.Abstraction.Conversations.Models;
|
||||
using BotSharp.Abstraction.MLTasks;
|
||||
|
|
@ -25,7 +27,16 @@ public class RoutingConversationHook: ConversationHookBase
|
|||
.FirstOrDefault(x => x.GetType().FullName.EndsWith(_settings.TextEmbedding));
|
||||
|
||||
// Utilize local discriminative model to predict intent
|
||||
message.Content = "response content";
|
||||
message.StopCompletion = true;
|
||||
message.IntentName = "greeting";
|
||||
|
||||
// Render by template
|
||||
var templateService = _services.GetRequiredService<IResponseTemplateService>();
|
||||
var response = await templateService.RenderIntentResponse(_agent.Id, message);
|
||||
|
||||
if (!string.IsNullOrEmpty(response))
|
||||
{
|
||||
message.Content = response;
|
||||
message.StopCompletion = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue