Fix StopCompletion issue for hook.OnMessageReceived.

This commit is contained in:
Haiping Chen 2023-12-12 08:16:34 -06:00
parent 69b209c8d3
commit 61c1f9623b
3 changed files with 24 additions and 16 deletions

View file

@ -29,7 +29,7 @@ public interface IConversationService
Func<RoleDialogModel, Task> onFunctionExecuting,
Func<RoleDialogModel, Task> onFunctionExecuted);
List<RoleDialogModel> GetDialogHistory(int lastCount = 20);
List<RoleDialogModel> GetDialogHistory(int lastCount = 50);
Task CleanHistory(string agentId);
Task CallFunctions(RoleDialogModel msg);

View file

@ -32,8 +32,12 @@ public partial class ConversationService
_storage.Append(_conversationId, message);
var statistics = _services.GetRequiredService<ITokenStatistics>();
var hooks = _services.GetServices<IConversationHook>().ToList();
RoleDialogModel response = message;
bool stopCompletion = false;
// Before chat completion hook
foreach (var hook in hooks)
{
@ -45,27 +49,26 @@ public partial class ConversationService
// Interrupted by hook
if (message.StopCompletion)
{
await onMessageReceived(message);
_storage.Append(_conversationId, message);
return true;
stopCompletion = true;
}
}
// Routing with reasoning
var routing = _services.GetRequiredService<IRoutingService>();
var settings = _services.GetRequiredService<RoutingSettings>();
if (!stopCompletion)
{
// Routing with reasoning
var routing = _services.GetRequiredService<IRoutingService>();
var settings = _services.GetRequiredService<RoutingSettings>();
var response = agentId == settings.RouterId ?
await routing.InstructLoop(message) :
await routing.ExecuteDirectly(agent, message);
response = agentId == settings.RouterId ?
await routing.InstructLoop(message) :
await routing.ExecuteDirectly(agent, message);
routing.ResetRecursiveCounter();
}
await HandleAssistantMessage(response, onMessageReceived);
var statistics = _services.GetRequiredService<ITokenStatistics>();
statistics.PrintStatistics();
routing.ResetRecursiveCounter();
return true;
}
@ -131,7 +134,11 @@ public partial class ConversationService
// Add to dialog history
_storage.Append(_conversationId, response);
var conversation = _services.GetRequiredService<IConversationService>();
var updatedConversation = await conversation.UpdateConversationTitle(_conversationId, response.Instruction.Reason);
if (response.Instruction != null)
{
var conversation = _services.GetRequiredService<IConversationService>();
var updatedConversation = await conversation.UpdateConversationTitle(_conversationId, response.Instruction.Reason);
}
}
}

View file

@ -10,6 +10,7 @@ public partial class RoutingService
if (function == null) return false;
message.FunctionName = name;
message.Role = AgentRole.Function;
return await function.Execute(message);
}
}