structure content log
This commit is contained in:
parent
6e8afbab31
commit
b6049fd521
|
|
@ -0,0 +1,13 @@
|
|||
namespace BotSharp.Abstraction.Loggers.Models;
|
||||
|
||||
public class StreamingLogModel
|
||||
{
|
||||
[JsonPropertyName("conversation_id")]
|
||||
public string ConversationId { get; set; }
|
||||
|
||||
[JsonPropertyName("content")]
|
||||
public string Content { get; set; }
|
||||
|
||||
[JsonPropertyName("created_at")]
|
||||
public DateTime CreateTime { get; set; }
|
||||
}
|
||||
|
|
@ -1,6 +1,6 @@
|
|||
using BotSharp.Abstraction.Agents.Models;
|
||||
using BotSharp.Abstraction.Loggers;
|
||||
using BotSharp.Abstraction.Users.Models;
|
||||
using BotSharp.Abstraction.Loggers.Models;
|
||||
using Microsoft.AspNetCore.SignalR;
|
||||
|
||||
namespace BotSharp.Plugin.ChatHub.Hooks;
|
||||
|
|
@ -10,6 +10,7 @@ public class StreamingLogHook : IContentGeneratingHook
|
|||
private readonly ConversationSetting _convSettings;
|
||||
private readonly IServiceProvider _services;
|
||||
private readonly IHubContext<SignalRHub> _chatHub;
|
||||
private readonly JsonSerializerOptions _serializerOptions;
|
||||
|
||||
public StreamingLogHook(
|
||||
ConversationSetting convSettings,
|
||||
|
|
@ -19,6 +20,12 @@ public class StreamingLogHook : IContentGeneratingHook
|
|||
_convSettings = convSettings;
|
||||
_services = serivces;
|
||||
_chatHub = chatHub;
|
||||
_serializerOptions = new JsonSerializerOptions
|
||||
{
|
||||
PropertyNameCaseInsensitive = true,
|
||||
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
|
||||
AllowTrailingCommas = true
|
||||
};
|
||||
}
|
||||
|
||||
public async Task BeforeGenerating(Agent agent, List<RoleDialogModel> conversations)
|
||||
|
|
@ -26,9 +33,11 @@ public class StreamingLogHook : IContentGeneratingHook
|
|||
if (!_convSettings.ShowVerboseLog) return;
|
||||
|
||||
var user = _services.GetRequiredService<IUserIdentity>();
|
||||
var states = _services.GetRequiredService<IConversationStateService>();
|
||||
var conversationId = states.GetConversationId();
|
||||
var dialog = conversations.Last();
|
||||
var log = $"{dialog.Role}: {dialog.Content} [msg_id: {dialog.MessageId}] ==>";
|
||||
await _chatHub.Clients.User(user.Id).SendAsync("OnContentLogGenerated", log);
|
||||
await _chatHub.Clients.User(user.Id).SendAsync("OnContentLogGenerated", BuildLog(conversationId, log));
|
||||
}
|
||||
|
||||
public async Task AfterGenerated(RoleDialogModel message, TokenStatsModel tokenStats)
|
||||
|
|
@ -36,6 +45,8 @@ public class StreamingLogHook : IContentGeneratingHook
|
|||
if (!_convSettings.ShowVerboseLog) return;
|
||||
|
||||
var agentService = _services.GetRequiredService<IAgentService>();
|
||||
var states = _services.GetRequiredService<IConversationStateService>();
|
||||
var conversationId = states.GetConversationId();
|
||||
var agent = await agentService.LoadAgent(message.CurrentAgentId);
|
||||
|
||||
var log = message.Role == AgentRole.Function ?
|
||||
|
|
@ -43,7 +54,18 @@ public class StreamingLogHook : IContentGeneratingHook
|
|||
$"[{agent?.Name}]: {message.Content}" + $" <== [msg_id: {message.MessageId}]";
|
||||
|
||||
var user = _services.GetRequiredService<IUserIdentity>();
|
||||
await _chatHub.Clients.User(user.Id).SendAsync("OnContentLogGenerated", tokenStats.Prompt);
|
||||
await _chatHub.Clients.User(user.Id).SendAsync("OnContentLogGenerated", log);
|
||||
await _chatHub.Clients.User(user.Id).SendAsync("OnContentLogGenerated", BuildLog(conversationId, tokenStats.Prompt));
|
||||
await _chatHub.Clients.User(user.Id).SendAsync("OnContentLogGenerated", BuildLog(conversationId, log));
|
||||
}
|
||||
|
||||
private string BuildLog(string conversationId, string content)
|
||||
{
|
||||
var log = new StreamingLogModel
|
||||
{
|
||||
ConversationId = conversationId,
|
||||
Content = content,
|
||||
CreateTime = DateTime.UtcNow
|
||||
};
|
||||
return JsonSerializer.Serialize(log, _serializerOptions);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue