Merge pull request #269 from iceljc/features/add-conv-states-log

add conv state log
This commit is contained in:
Haiping 2024-01-26 15:51:03 -06:00 committed by GitHub
commit 29a2ffe706
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 32 additions and 4 deletions

View file

@ -0,0 +1,11 @@
namespace BotSharp.Abstraction.Conversations.Models;
public class ConversationStateLogModel
{
[JsonPropertyName("conversation_id")]
public string ConvsersationId { get; set; }
[JsonPropertyName("states")]
public string States { get; set; }
[JsonPropertyName("created_at")]
public DateTime CreateTime { get; set; }
}

View file

@ -4,6 +4,8 @@ public class StreamingLogModel
{
[JsonPropertyName("conversation_id")]
public string ConversationId { get; set; }
[JsonPropertyName("name")]
public string? Name { get; set; }
[JsonPropertyName("content")]
public string Content { get; set; }

View file

@ -100,6 +100,7 @@ public class ChatHubConversationHook : ConversationHookBase
public override async Task OnResponseGenerated(RoleDialogModel message)
{
var conv = _services.GetRequiredService<IConversationService>();
var state = _services.GetRequiredService<IConversationStateService>();
var json = JsonSerializer.Serialize(new ChatResponseModel()
{
@ -115,7 +116,20 @@ public class ChatHubConversationHook : ConversationHookBase
}
}, _serializerOptions);
await _chatHub.Clients.User(_user.Id).SendAsync("OnMessageReceivedFromAssistant", json);
await _chatHub.Clients.User(_user.Id).SendAsync("OnConversateStatesGenerated", BuildConversationStates(conv.ConversationId, state.GetStates()));
await base.OnResponseGenerated(message);
}
private string BuildConversationStates(string conversationId, Dictionary<string, string> states)
{
var model = new ConversationStateLogModel
{
ConvsersationId = conversationId,
States = JsonSerializer.Serialize(states, _serializerOptions),
CreateTime = DateTime.UtcNow
};
return JsonSerializer.Serialize(model, _serializerOptions);
}
}

View file

@ -37,7 +37,7 @@ public class StreamingLogHook : ConversationHookBase, IContentGeneratingHook
{
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));
await _chatHub.Clients.User(_user.Id).SendAsync("OnContentLogGenerated", BuildLog(conversationId, _user.UserName, log));
}
public async Task BeforeGenerating(Agent agent, List<RoleDialogModel> conversations)
@ -64,20 +64,21 @@ public class StreamingLogHook : ConversationHookBase, IContentGeneratingHook
var conversationId = _state.GetConversationId();
var agent = await agentService.LoadAgent(message.CurrentAgentId);
await _chatHub.Clients.User(_user.Id).SendAsync("OnContentLogGenerated", BuildLog(conversationId, tokenStats.Prompt));
await _chatHub.Clients.User(_user.Id).SendAsync("OnContentLogGenerated", BuildLog(conversationId, agent?.Name, tokenStats.Prompt));
var log = message.Role == AgentRole.Function ?
$"[{agent?.Name}]: {message.FunctionName}({message.FunctionArgs})" :
$"[{agent?.Name}]: {message.Content}";
log += $"\r\n<== MessageId: {message.MessageId}";
await _chatHub.Clients.User(_user.Id).SendAsync("OnContentLogGenerated", BuildLog(conversationId, log));
await _chatHub.Clients.User(_user.Id).SendAsync("OnContentLogGenerated", BuildLog(conversationId, agent?.Name, log));
}
private string BuildLog(string conversationId, string content)
private string BuildLog(string conversationId, string? name, string content)
{
var log = new StreamingLogModel
{
ConversationId = conversationId,
Name = name,
Content = content,
CreateTime = DateTime.UtcNow
};