Optimize StreamingLog.
This commit is contained in:
parent
9d3cd6b6e9
commit
264fe3c22a
|
|
@ -43,6 +43,7 @@ public class HFPlanner : IPlaner
|
|||
{
|
||||
new RoleDialogModel(AgentRole.User, next)
|
||||
{
|
||||
FunctionName = nameof(NaivePlanner),
|
||||
MessageId = messageId
|
||||
}
|
||||
};
|
||||
|
|
|
|||
|
|
@ -49,6 +49,7 @@ public class NaivePlanner : IPlaner
|
|||
{
|
||||
new RoleDialogModel(AgentRole.User, next)
|
||||
{
|
||||
FunctionName = nameof(NaivePlanner),
|
||||
MessageId = messageId
|
||||
}
|
||||
};
|
||||
|
|
|
|||
|
|
@ -48,6 +48,7 @@ public class SequentialPlanner : IPlaner
|
|||
{
|
||||
new RoleDialogModel(AgentRole.User, next)
|
||||
{
|
||||
FunctionName = nameof(NaivePlanner),
|
||||
MessageId = messageId
|
||||
}
|
||||
};
|
||||
|
|
|
|||
|
|
@ -235,7 +235,11 @@ public class ChatCompletionProvider : IChatCompletion
|
|||
}
|
||||
else if (message.Role == ChatRole.User)
|
||||
{
|
||||
chatCompletionsOptions.Messages.Add(new ChatRequestUserMessage(message.Content));
|
||||
chatCompletionsOptions.Messages.Add(new ChatRequestUserMessage(message.Content)
|
||||
{
|
||||
// To display Planner name in log
|
||||
Name = message.FunctionName
|
||||
});
|
||||
}
|
||||
else if (message.Role == ChatRole.Assistant)
|
||||
{
|
||||
|
|
@ -268,6 +272,11 @@ public class ChatCompletionProvider : IChatCompletion
|
|||
.Where(x => x.Role == AgentRole.System)
|
||||
.Select(x => x as ChatRequestSystemMessage).Select(x =>
|
||||
{
|
||||
if (!string.IsNullOrEmpty(x.Name))
|
||||
{
|
||||
// To display Agent name in log
|
||||
return $"[{x.Name}]: {x.Content}";
|
||||
}
|
||||
return $"{x.Role}: {x.Content}";
|
||||
}));
|
||||
prompt += $"{verbose}\r\n";
|
||||
|
|
|
|||
|
|
@ -19,5 +19,6 @@ public class ChatHubPlugin : IBotSharpPlugin
|
|||
// Register hooks
|
||||
services.AddScoped<IConversationHook, ChatHubConversationHook>();
|
||||
services.AddScoped<IContentGeneratingHook, StreamingLogHook>();
|
||||
services.AddScoped<IConversationHook, StreamingLogHook>();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -5,21 +5,27 @@ using Microsoft.AspNetCore.SignalR;
|
|||
|
||||
namespace BotSharp.Plugin.ChatHub.Hooks;
|
||||
|
||||
public class StreamingLogHook : IContentGeneratingHook
|
||||
public class StreamingLogHook : ConversationHookBase, IContentGeneratingHook
|
||||
{
|
||||
private readonly ConversationSetting _convSettings;
|
||||
private readonly JsonSerializerOptions _serializerOptions;
|
||||
private readonly IServiceProvider _services;
|
||||
private readonly IHubContext<SignalRHub> _chatHub;
|
||||
private readonly JsonSerializerOptions _serializerOptions;
|
||||
private readonly IConversationStateService _state;
|
||||
private readonly IUserIdentity _user;
|
||||
|
||||
public StreamingLogHook(
|
||||
ConversationSetting convSettings,
|
||||
IServiceProvider serivces,
|
||||
IHubContext<SignalRHub> chatHub)
|
||||
IHubContext<SignalRHub> chatHub,
|
||||
IConversationStateService state,
|
||||
IUserIdentity user)
|
||||
{
|
||||
_convSettings = convSettings;
|
||||
_services = serivces;
|
||||
_chatHub = chatHub;
|
||||
_state = state;
|
||||
_user = user;
|
||||
_serializerOptions = new JsonSerializerOptions
|
||||
{
|
||||
PropertyNameCaseInsensitive = true,
|
||||
|
|
@ -27,17 +33,27 @@ public class StreamingLogHook : IContentGeneratingHook
|
|||
AllowTrailingCommas = true
|
||||
};
|
||||
}
|
||||
public override async Task OnMessageReceived(RoleDialogModel message)
|
||||
{
|
||||
var conversationId = _state.GetConversationId();
|
||||
var log = $"MessageId: {message.MessageId} ==>\r\n{message.Role}: {message.Content}";
|
||||
await _chatHub.Clients.User(_user.Id).SendAsync("OnContentLogGenerated", BuildLog(conversationId, log));
|
||||
}
|
||||
|
||||
public async Task BeforeGenerating(Agent agent, List<RoleDialogModel> conversations)
|
||||
{
|
||||
if (!_convSettings.ShowVerboseLog) return;
|
||||
|
||||
var user = _services.GetRequiredService<IUserIdentity>();
|
||||
var states = _services.GetRequiredService<IConversationStateService>();
|
||||
var conversationId = states.GetConversationId();
|
||||
/*var _state = _services.GetRequiredService<IConversationStateService>();
|
||||
var conversationId = _state.GetConversationId();
|
||||
var dialog = conversations.Last();
|
||||
var log = $"{dialog.Role}: {dialog.Content} [msg_id: {dialog.MessageId}] ==>";
|
||||
await _chatHub.Clients.User(user.Id).SendAsync("OnContentLogGenerated", BuildLog(conversationId, log));
|
||||
await _chatHub.Clients.User(_user.Id).SendAsync("OnContentLogGenerated", BuildLog(conversationId, log));*/
|
||||
}
|
||||
|
||||
public override async Task OnFunctionExecuted(RoleDialogModel message)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public async Task AfterGenerated(RoleDialogModel message, TokenStatsModel tokenStats)
|
||||
|
|
@ -45,17 +61,16 @@ public class StreamingLogHook : IContentGeneratingHook
|
|||
if (!_convSettings.ShowVerboseLog) return;
|
||||
|
||||
var agentService = _services.GetRequiredService<IAgentService>();
|
||||
var states = _services.GetRequiredService<IConversationStateService>();
|
||||
var conversationId = states.GetConversationId();
|
||||
var conversationId = _state.GetConversationId();
|
||||
var agent = await agentService.LoadAgent(message.CurrentAgentId);
|
||||
|
||||
await _chatHub.Clients.User(_user.Id).SendAsync("OnContentLogGenerated", BuildLog(conversationId, tokenStats.Prompt));
|
||||
|
||||
var log = message.Role == AgentRole.Function ?
|
||||
$"[{agent?.Name}]: {message.FunctionName}({message.FunctionArgs})" :
|
||||
$"[{agent?.Name}]: {message.Content}" + $" <== [msg_id: {message.MessageId}]";
|
||||
|
||||
var user = _services.GetRequiredService<IUserIdentity>();
|
||||
await _chatHub.Clients.User(user.Id).SendAsync("OnContentLogGenerated", BuildLog(conversationId, tokenStats.Prompt));
|
||||
await _chatHub.Clients.User(user.Id).SendAsync("OnContentLogGenerated", BuildLog(conversationId, log));
|
||||
$"[{agent?.Name}]: {message.Content}";
|
||||
log += $"\r\n<== MessageId: {message.MessageId}";
|
||||
await _chatHub.Clients.User(_user.Id).SendAsync("OnContentLogGenerated", BuildLog(conversationId, log));
|
||||
}
|
||||
|
||||
private string BuildLog(string conversationId, string content)
|
||||
|
|
|
|||
Loading…
Reference in a new issue