Add ConversationEnding to IConversationHook

This commit is contained in:
Haiping Chen 2023-10-15 19:08:24 -05:00
parent 54255b4b13
commit 8ea6b0f814
6 changed files with 25 additions and 10 deletions

View file

@ -21,6 +21,9 @@ Task BeforeCompletion();
Task OnFunctionExecuting(RoleDialogModel message); Task OnFunctionExecuting(RoleDialogModel message);
Task OnFunctionExecuted(RoleDialogModel message); Task OnFunctionExecuted(RoleDialogModel message);
Task AfterCompletion(RoleDialogModel message); Task AfterCompletion(RoleDialogModel message);
// LLM detected the user's intention to end the conversation
Task ConversationEnding(RoleDialogModel conversation);
``` ```
More information about conversation hook please go to [Conversation Hook](../conversation/hook.md). More information about conversation hook please go to [Conversation Hook](../conversation/hook.md).

View file

@ -61,4 +61,9 @@ public abstract class ConversationHookBase : IConversationHook
_dialogs = dialogs; _dialogs = dialogs;
return Task.CompletedTask; return Task.CompletedTask;
} }
public virtual Task ConversationEnding(RoleDialogModel message)
{
return Task.CompletedTask;
}
} }

View file

@ -24,4 +24,6 @@ public interface IConversationHook
Task OnFunctionExecuting(RoleDialogModel message); Task OnFunctionExecuting(RoleDialogModel message);
Task OnFunctionExecuted(RoleDialogModel message); Task OnFunctionExecuted(RoleDialogModel message);
Task AfterCompletion(RoleDialogModel message); Task AfterCompletion(RoleDialogModel message);
Task ConversationEnding(RoleDialogModel conversation);
} }

View file

@ -6,10 +6,6 @@ public partial class ConversationService
{ {
public async Task CallFunctions(RoleDialogModel msg) public async Task CallFunctions(RoleDialogModel msg)
{ {
var hooks = _services.GetServices<IConversationHook>()
.OrderBy(x => x.Priority)
.ToList();
// Invoke functions // Invoke functions
var functions = _services.GetServices<IFunctionCallback>() var functions = _services.GetServices<IFunctionCallback>()
.Where(x => x.Name == msg.FunctionName) .Where(x => x.Name == msg.FunctionName)
@ -22,6 +18,10 @@ public partial class ConversationService
return; return;
} }
var hooks = _services.GetServices<IConversationHook>()
.OrderBy(x => x.Priority)
.ToList();
foreach (var fn in functions) foreach (var fn in functions)
{ {
// Before executing functions // Before executing functions

View file

@ -25,6 +25,16 @@ public class ConversationEndRoutingHandler : RoutingHandlerBase, IRoutingHandler
FunctionName = inst.Function, FunctionName = inst.Function,
ExecutionData = inst ExecutionData = inst
}; };
var hooks = _services.GetServices<IConversationHook>()
.OrderBy(x => x.Priority)
.ToList();
foreach (var hook in hooks)
{
await hook.OnFunctionExecuting(result);
}
return result; return result;
} }
} }

View file

@ -29,12 +29,7 @@ public class AgentController : ControllerBase, IApiAdapter
public async Task<List<AgentViewModel>> GetAgents() public async Task<List<AgentViewModel>> GetAgents()
{ {
var agents = await _agentService.GetAgents(); var agents = await _agentService.GetAgents();
return agents.Select(x => AgentViewModel.FromAgent(x)).ToList();
// Add the router as agent
var routing = _services.GetRequiredService<IRouterInstance>();
agents.Insert(0, routing.Load().Router);
return agents.Select(x => AgentViewModel.FromAgent(x)).ToList();
} }
[HttpPost("/agent")] [HttpPost("/agent")]