Re-format dialog history.

This commit is contained in:
hchen 2023-08-07 15:17:54 -05:00
parent b6c0d47b02
commit 3f49d11eff
4 changed files with 29 additions and 7 deletions

View file

@ -6,6 +6,7 @@ public class RoleDialogModel
/// user, system, assistant, function /// user, system, assistant, function
/// </summary> /// </summary>
public string Role { get; set; } public string Role { get; set; }
public DateTime CreatedAt { get; set; } = DateTime.UtcNow;
public string Content { get; set; } public string Content { get; set; }
/// <summary> /// <summary>

View file

@ -19,5 +19,6 @@ public class FunctionExecutionValidationResult
public string ValidationStatus { get; set; } public string ValidationStatus { get; set; }
[JsonPropertyName("validation_message")] [JsonPropertyName("validation_message")]
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
public string ValidationMessage { get; set; } public string ValidationMessage { get; set; }
} }

View file

@ -1,5 +1,6 @@
using BotSharp.Abstraction.Conversations.Models; using BotSharp.Abstraction.Conversations.Models;
using System.IO; using System.IO;
using Tensorflow;
namespace BotSharp.Core.Conversations.Services; namespace BotSharp.Core.Conversations.Services;
@ -14,20 +15,32 @@ public class ConversationStorage : IConversationStorage
public void Append(string agentId, string conversationId, RoleDialogModel dialog) public void Append(string agentId, string conversationId, RoleDialogModel dialog)
{ {
var conversationFile = GetStorageFile(agentId, conversationId); var conversationFile = GetStorageFile(agentId, conversationId);
File.AppendAllText(conversationFile, $"{dialog.Role}: {dialog.Content}\n"); var sb = new StringBuilder();
sb.AppendLine($"{dialog.Role}|{dialog.CreatedAt}");
sb.AppendLine($" - {dialog.Content}");
var conversation = sb.ToString();
File.AppendAllText(conversationFile, conversation);
} }
public List<RoleDialogModel> GetDialogs(string agentId, string conversationId) public List<RoleDialogModel> GetDialogs(string agentId, string conversationId)
{ {
var conversationFile = GetStorageFile(agentId, conversationId); var conversationFile = GetStorageFile(agentId, conversationId);
var dialogs = File.ReadAllLines(conversationFile); var dialogs = File.ReadAllLines(conversationFile);
return dialogs.Select(x =>
var results = new List<RoleDialogModel>();
for (int i = 0; i < dialogs.Length; i += 2)
{ {
var pos = x.IndexOf(':'); var meta = dialogs[i];
var role = x.Substring(0, pos); var dialog = dialogs[i + 1];
var text = x.Substring(pos + 1); var role = meta.Split('|')[0];
return new RoleDialogModel(role, text); var createdAt = DateTime.Parse(meta.Split('|')[1]);
}).ToList(); var text = dialog.Substring(4);
results.Add(new RoleDialogModel(role, text)
{
CreatedAt = createdAt
});
}
return results;
} }
public void InitStorage(string agentId, string conversationId) public void InitStorage(string agentId, string conversationId)

View file

@ -105,6 +105,13 @@ public class WebhookController : ControllerBase
Recipient = JsonSerializer.Serialize(new { Id = sessionId }, jsonOpt), Recipient = JsonSerializer.Serialize(new { Id = sessionId }, jsonOpt),
Message = JsonSerializer.Serialize(new { Text = "I'm pulling the relevent information, please wait a second ..." }, jsonOpt) Message = JsonSerializer.Serialize(new { Text = "I'm pulling the relevent information, please wait a second ..." }, jsonOpt)
}); });
await messenger.SendMessage(setting.ApiVersion, setting.PageId, new SendingMessageRequest
{
AccessToken = setting.PageAccessToken,
Recipient = JsonSerializer.Serialize(new { Id = sessionId }, jsonOpt),
SenderAction = SenderActionEnum.TypingOn
});
}); });
// Response to user // Response to user