Merge pull request #244 from iceljc/features/refine-dialog-meta-data

structure dialog meta data
This commit is contained in:
Haiping 2023-12-28 13:32:40 -06:00 committed by GitHub
commit ccca262c6d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 94 additions and 17 deletions

View file

@ -25,7 +25,7 @@ public class Conversation
public class DialogElement public class DialogElement
{ {
public string MetaData { get; set; } public DialogMeta MetaData { get; set; }
public string Content { get; set; } public string Content { get; set; }
public DialogElement() public DialogElement()
@ -33,9 +33,19 @@ public class DialogElement
} }
public DialogElement(string meta, string content) public DialogElement(DialogMeta meta, string content)
{ {
MetaData = meta; MetaData = meta;
Content = content; Content = content;
} }
} }
public class DialogMeta
{
public string Role { get; set; }
public string AgentId { get; set; }
public string MessageId { get; set; }
public string? FunctionName { get; set; }
public string? SenderId { get; set; }
public DateTime CreateTime { get; set; }
}

View file

@ -24,8 +24,15 @@ public class ConversationStorage : IConversationStorage
if (dialog.Role == AgentRole.Function) if (dialog.Role == AgentRole.Function)
{ {
// var args = dialog.FunctionArgs.RemoveNewLine(); var meta = new DialogMeta
var meta = $"{dialog.CreatedAt}|{dialog.Role}|{agentId}|{dialog.MessageId}|{dialog.FunctionName}"; {
Role = dialog.Role,
AgentId = agentId,
MessageId = dialog.MessageId,
FunctionName = dialog.FunctionName,
CreateTime = dialog.CreatedAt
};
var content = dialog.Content.RemoveNewLine(); var content = dialog.Content.RemoveNewLine();
if (string.IsNullOrEmpty(content)) if (string.IsNullOrEmpty(content))
{ {
@ -35,7 +42,15 @@ public class ConversationStorage : IConversationStorage
} }
else else
{ {
var meta = $"{dialog.CreatedAt}|{dialog.Role}|{agentId}|{dialog.MessageId}|{dialog.SenderId}"; var meta = new DialogMeta
{
Role = dialog.Role,
AgentId = agentId,
MessageId = dialog.MessageId,
SenderId = dialog.SenderId,
CreateTime = dialog.CreatedAt
};
var content = dialog.Content.RemoveNewLine(); var content = dialog.Content.RemoveNewLine();
if (string.IsNullOrEmpty(content)) if (string.IsNullOrEmpty(content))
{ {
@ -58,13 +73,12 @@ public class ConversationStorage : IConversationStorage
{ {
var meta = dialog.MetaData; var meta = dialog.MetaData;
var content = dialog.Content; var content = dialog.Content;
var blocks = meta.Split('|'); var role = meta.Role;
var createdAt = DateTime.Parse(blocks[0]); var currentAgentId = meta.AgentId;
var role = blocks[1]; var messageId = meta.MessageId;
var currentAgentId = blocks[2]; var function = role == AgentRole.Function ? meta.FunctionName : null;
var messageId = blocks[3]; var senderId = role == AgentRole.Function ? currentAgentId : meta.SenderId;
var senderId = role == AgentRole.Function ? currentAgentId : blocks[4]; var createdAt = meta.CreateTime;
var function = role == AgentRole.Function ? blocks[4] : null;
results.Add(new RoleDialogModel(role, content) results.Add(new RoleDialogModel(role, content)
{ {

View file

@ -1037,9 +1037,18 @@ public class FileRepository : IBotSharpRepository
{ {
for (int i = 0; i < rawDialogs.Count(); i += 2) for (int i = 0; i < rawDialogs.Count(); i += 2)
{ {
var meta = rawDialogs[i]; var blocks = rawDialogs[i].Split("|");
var content = rawDialogs[i + 1]; var content = rawDialogs[i + 1];
var trimmed = content.Substring(4); var trimmed = content.Substring(4);
var meta = new DialogMeta
{
Role = blocks[1],
AgentId = blocks[2],
MessageId = blocks[3],
FunctionName = blocks[1] == AgentRole.Function ? blocks[4] : null,
SenderId = blocks[1] == AgentRole.Function ? null : blocks[4],
CreateTime = DateTime.Parse(blocks[0])
};
dialogs.Add(new DialogElement(meta, trimmed)); dialogs.Add(new DialogElement(meta, trimmed));
} }
} }
@ -1053,7 +1062,10 @@ public class FileRepository : IBotSharpRepository
foreach (var element in dialogs) foreach (var element in dialogs)
{ {
dialogTexts.Add(element.MetaData); var meta = element.MetaData;
var source = meta.FunctionName ?? meta.SenderId;
var metaStr = $"{meta.CreateTime}|{meta.Role}|{meta.AgentId}|{meta.MessageId}|{source}";
dialogTexts.Add(metaStr);
var content = $" - {element.Content}"; var content = $" - {element.Content}";
dialogTexts.Add(content); dialogTexts.Add(content);
} }

View file

@ -4,7 +4,7 @@ namespace BotSharp.Plugin.MongoStorage.Models;
public class DialogMongoElement public class DialogMongoElement
{ {
public string MetaData { get; set; } public DialogMetaMongoElement MetaData { get; set; }
public string Content { get; set; } public string Content { get; set; }
public DialogMongoElement() public DialogMongoElement()
@ -16,7 +16,7 @@ public class DialogMongoElement
{ {
return new DialogMongoElement return new DialogMongoElement
{ {
MetaData = dialog.MetaData, MetaData = DialogMetaMongoElement.ToMongoElement(dialog.MetaData),
Content = dialog.Content Content = dialog.Content
}; };
} }
@ -25,8 +25,49 @@ public class DialogMongoElement
{ {
return new DialogElement return new DialogElement
{ {
MetaData = dialog.MetaData, MetaData = DialogMetaMongoElement.ToDomainElement(dialog.MetaData),
Content = dialog.Content Content = dialog.Content
}; };
} }
} }
public class DialogMetaMongoElement
{
public string Role { get; set; }
public string AgentId { get; set; }
public string MessageId { get; set; }
public string? FunctionName { get; set; }
public string? SenderId { get; set; }
public DateTime CreateTime { get; set; }
public DialogMetaMongoElement()
{
}
public static DialogMeta ToDomainElement(DialogMetaMongoElement meta)
{
return new DialogMeta
{
Role = meta.Role,
AgentId = meta.AgentId,
MessageId = meta.MessageId,
FunctionName = meta.FunctionName,
SenderId = meta.SenderId,
CreateTime = meta.CreateTime,
};
}
public static DialogMetaMongoElement ToMongoElement(DialogMeta meta)
{
return new DialogMetaMongoElement
{
Role = meta.Role,
AgentId = meta.AgentId,
MessageId = meta.MessageId,
FunctionName = meta.FunctionName,
SenderId = meta.SenderId,
CreateTime = meta.CreateTime,
};
}
}