rename model

This commit is contained in:
Jicheng Lu 2024-02-29 20:50:39 -06:00
parent 1d3bb4702d
commit 174aa61fe9
12 changed files with 58 additions and 50 deletions

View file

@ -15,7 +15,7 @@ public interface IConversationService
Task<List<Conversation>> GetLastConversations(); Task<List<Conversation>> GetLastConversations();
Task<bool> DeleteConversation(string id); Task<bool> DeleteConversation(string id);
Task<bool> TruncateConversation(string conversationId, string messageId); Task<bool> TruncateConversation(string conversationId, string messageId);
Task<List<ConversationContentLogModel>> GetConversationContentLogs(string conversationId); Task<List<ContentLogOutputModel>> GetConversationContentLogs(string conversationId);
Task<List<ConversationStateLogModel>> GetConversationStateLogs(string conversationId); Task<List<ConversationStateLogModel>> GetConversationStateLogs(string conversationId);
/// <summary> /// <summary>

View file

@ -0,0 +1,22 @@
namespace BotSharp.Abstraction.Loggers.Models;
public class ContentLogInputModel
{
public string ConversationId { get; set; }
public string? Name { get; set; }
public string? AgentId { get; set; }
public string Log { get; set; }
public string Source { get; set; }
public RoleDialogModel Message { get; set; }
public ContentLogInputModel()
{
}
public ContentLogInputModel(string conversationId, RoleDialogModel message)
{
ConversationId = conversationId;
Message = message;
}
}

View file

@ -1,6 +1,6 @@
namespace BotSharp.Abstraction.Loggers.Models; namespace BotSharp.Abstraction.Loggers.Models;
public class ConversationContentLogModel public class ContentLogOutputModel
{ {
[JsonPropertyName("conversation_id")] [JsonPropertyName("conversation_id")]
public string ConversationId { get; set; } public string ConversationId { get; set; }
@ -11,6 +11,9 @@ public class ConversationContentLogModel
[JsonPropertyName("name")] [JsonPropertyName("name")]
public string? Name { get; set; } public string? Name { get; set; }
[JsonPropertyName("agent_id")]
public string? AgentId { get; set; }
[JsonPropertyName("role")] [JsonPropertyName("role")]
public string Role { get; set; } public string Role { get; set; }

View file

@ -72,8 +72,8 @@ public interface IBotSharpRepository
#endregion #endregion
#region Conversation Content Log #region Conversation Content Log
void SaveConversationContentLog(ConversationContentLogModel log); void SaveConversationContentLog(ContentLogOutputModel log);
List<ConversationContentLogModel> GetConversationContentLogs(string conversationId); List<ContentLogOutputModel> GetConversationContentLogs(string conversationId);
#endregion #endregion
#region Conversation State Log #region Conversation State Log

View file

@ -5,7 +5,7 @@ namespace BotSharp.Core.Conversations.Services;
public partial class ConversationService public partial class ConversationService
{ {
public async Task<List<ConversationContentLogModel>> GetConversationContentLogs(string conversationId) public async Task<List<ContentLogOutputModel>> GetConversationContentLogs(string conversationId)
{ {
var db = _services.GetRequiredService<IBotSharpRepository>(); var db = _services.GetRequiredService<IBotSharpRepository>();
var logs = db.GetConversationContentLogs(conversationId); var logs = db.GetConversationContentLogs(conversationId);

View file

@ -257,12 +257,12 @@ public class BotSharpDbContext : Database, IBotSharpRepository
#endregion #endregion
#region Conversation Content Log #region Conversation Content Log
public void SaveConversationContentLog(ConversationContentLogModel log) public void SaveConversationContentLog(ContentLogOutputModel log)
{ {
throw new NotImplementedException(); throw new NotImplementedException();
} }
public List<ConversationContentLogModel> GetConversationContentLogs(string conversationId) public List<ContentLogOutputModel> GetConversationContentLogs(string conversationId)
{ {
throw new NotImplementedException(); throw new NotImplementedException();
} }

View file

@ -409,7 +409,7 @@ namespace BotSharp.Core.Repository
foreach (var file in Directory.GetFiles(contentLogDir)) foreach (var file in Directory.GetFiles(contentLogDir))
{ {
var text = File.ReadAllText(file); var text = File.ReadAllText(file);
var log = JsonSerializer.Deserialize<ConversationContentLogModel>(text); var log = JsonSerializer.Deserialize<ContentLogOutputModel>(text);
if (log == null) continue; if (log == null) continue;
if (log.CreateTime >= refTime) if (log.CreateTime >= refTime)

View file

@ -63,7 +63,7 @@ namespace BotSharp.Core.Repository
#endregion #endregion
#region Conversation Content Log #region Conversation Content Log
public void SaveConversationContentLog(ConversationContentLogModel log) public void SaveConversationContentLog(ContentLogOutputModel log)
{ {
if (log == null) return; if (log == null) return;
@ -88,9 +88,9 @@ namespace BotSharp.Core.Repository
File.WriteAllText(file, JsonSerializer.Serialize(log, _options)); File.WriteAllText(file, JsonSerializer.Serialize(log, _options));
} }
public List<ConversationContentLogModel> GetConversationContentLogs(string conversationId) public List<ContentLogOutputModel> GetConversationContentLogs(string conversationId)
{ {
var logs = new List<ConversationContentLogModel>(); var logs = new List<ContentLogOutputModel>();
if (string.IsNullOrEmpty(conversationId)) return logs; if (string.IsNullOrEmpty(conversationId)) return logs;
var convDir = FindConversationDirectory(conversationId); var convDir = FindConversationDirectory(conversationId);
@ -102,7 +102,7 @@ namespace BotSharp.Core.Repository
foreach (var file in Directory.GetFiles(logDir)) foreach (var file in Directory.GetFiles(logDir))
{ {
var text = File.ReadAllText(file); var text = File.ReadAllText(file);
var log = JsonSerializer.Deserialize<ConversationContentLogModel>(text); var log = JsonSerializer.Deserialize<ContentLogOutputModel>(text);
if (log == null) continue; if (log == null) continue;
logs.Add(log); logs.Add(log);

View file

@ -37,7 +37,7 @@ public class LoggerController : ControllerBase
} }
[HttpGet("/logger/conversation/{conversationId}/content-log")] [HttpGet("/logger/conversation/{conversationId}/content-log")]
public async Task<List<ConversationContentLogModel>> GetConversationContentLogs([FromRoute] string conversationId) public async Task<List<ContentLogOutputModel>> GetConversationContentLogs([FromRoute] string conversationId)
{ {
var conversationService = _services.GetRequiredService<IConversationService>(); var conversationService = _services.GetRequiredService<IConversationService>();
return await conversationService.GetConversationContentLogs(conversationId); return await conversationService.GetConversationContentLogs(conversationId);

View file

@ -50,7 +50,7 @@ public class StreamingLogHook : ConversationHookBase, IContentGeneratingHook, IR
var conversationId = _state.GetConversationId(); var conversationId = _state.GetConversationId();
var log = $"{message.Content}"; var log = $"{message.Content}";
var input = new ContentLogInput(conversationId, message) var input = new ContentLogInputModel(conversationId, message)
{ {
Name = _user.UserName, Name = _user.UserName,
Source = ContentLogSource.UserInput, Source = ContentLogSource.UserInput,
@ -70,7 +70,7 @@ public class StreamingLogHook : ConversationHookBase, IContentGeneratingHook, IR
var agent = await _agentService.LoadAgent(message.CurrentAgentId); var agent = await _agentService.LoadAgent(message.CurrentAgentId);
var log = $"{message.FunctionName}({message.FunctionArgs})\r\n => {message.Content}"; var log = $"{message.FunctionName}({message.FunctionArgs})\r\n => {message.Content}";
var input = new ContentLogInput(conversationId, message) var input = new ContentLogInputModel(conversationId, message)
{ {
Name = agent?.Name, Name = agent?.Name,
AgentId = agent?.Id, AgentId = agent?.Id,
@ -95,7 +95,7 @@ public class StreamingLogHook : ConversationHookBase, IContentGeneratingHook, IR
var log = tokenStats.Prompt; var log = tokenStats.Prompt;
var input = new ContentLogInput(conversationId, message) var input = new ContentLogInputModel(conversationId, message)
{ {
Name = agent?.Name, Name = agent?.Name,
AgentId = agent?.Id, AgentId = agent?.Id,
@ -126,7 +126,7 @@ public class StreamingLogHook : ConversationHookBase, IContentGeneratingHook, IR
log += $"\r\n{richContent}"; log += $"\r\n{richContent}";
} }
var input = new ContentLogInput(conv.ConversationId, message) var input = new ContentLogInputModel(conv.ConversationId, message)
{ {
Name = agent?.Name, Name = agent?.Name,
AgentId = agent?.Id, AgentId = agent?.Id,
@ -150,7 +150,7 @@ public class StreamingLogHook : ConversationHookBase, IContentGeneratingHook, IR
MessageId = _routingCtx.MessageId MessageId = _routingCtx.MessageId
}; };
var input = new ContentLogInput(conversationId, message) var input = new ContentLogInputModel(conversationId, message)
{ {
Name = "Router", Name = "Router",
Source = ContentLogSource.HardRule, Source = ContentLogSource.HardRule,
@ -171,7 +171,7 @@ public class StreamingLogHook : ConversationHookBase, IContentGeneratingHook, IR
MessageId = _routingCtx.MessageId MessageId = _routingCtx.MessageId
}; };
var input = new ContentLogInput(conversationId, message) var input = new ContentLogInputModel(conversationId, message)
{ {
Name = "Router", Name = "Router",
Source = ContentLogSource.HardRule, Source = ContentLogSource.HardRule,
@ -192,7 +192,7 @@ public class StreamingLogHook : ConversationHookBase, IContentGeneratingHook, IR
MessageId = _routingCtx.MessageId MessageId = _routingCtx.MessageId
}; };
var input = new ContentLogInput(conversationId, message) var input = new ContentLogInputModel(conversationId, message)
{ {
Name = "Router", Name = "Router",
Source = ContentLogSource.HardRule, Source = ContentLogSource.HardRule,
@ -212,7 +212,7 @@ public class StreamingLogHook : ConversationHookBase, IContentGeneratingHook, IR
MessageId = _routingCtx.MessageId MessageId = _routingCtx.MessageId
}; };
var input = new ContentLogInput(conversationId, message) var input = new ContentLogInputModel(conversationId, message)
{ {
Name = "Router", Name = "Router",
Source = ContentLogSource.HardRule, Source = ContentLogSource.HardRule,
@ -227,7 +227,7 @@ public class StreamingLogHook : ConversationHookBase, IContentGeneratingHook, IR
var agent = await _agentService.LoadAgent(message.CurrentAgentId); var agent = await _agentService.LoadAgent(message.CurrentAgentId);
var log = JsonSerializer.Serialize(instruct, _serializerOptions); var log = JsonSerializer.Serialize(instruct, _serializerOptions);
var input = new ContentLogInput(conversationId, message) var input = new ContentLogInputModel(conversationId, message)
{ {
Name = agent?.Name, Name = agent?.Name,
AgentId = agent?.Id, AgentId = agent?.Id,
@ -239,26 +239,27 @@ public class StreamingLogHook : ConversationHookBase, IContentGeneratingHook, IR
#endregion #endregion
private string BuildContentLog(ContentLogInput input) private string BuildContentLog(ContentLogInputModel input)
{ {
var log = new ConversationContentLogModel var output = new ContentLogOutputModel
{ {
ConversationId = input.ConversationId, ConversationId = input.ConversationId,
MessageId = input.Message.MessageId, MessageId = input.Message.MessageId,
Name = input.Name, Name = input.Name,
AgentId = input.AgentId,
Role = input.Message.Role, Role = input.Message.Role,
Content = input.Log, Content = input.Log,
Source = input.Source, Source = input.Source,
CreateTime = DateTime.UtcNow CreateTime = DateTime.UtcNow
}; };
var json = JsonSerializer.Serialize(log, _serializerOptions); var json = JsonSerializer.Serialize(output, _serializerOptions);
var convSettings = _services.GetRequiredService<ConversationSetting>(); var convSettings = _services.GetRequiredService<ConversationSetting>();
if (convSettings.EnableContentLog) if (convSettings.EnableContentLog)
{ {
var db = _services.GetRequiredService<IBotSharpRepository>(); var db = _services.GetRequiredService<IBotSharpRepository>();
db.SaveConversationContentLog(log); db.SaveConversationContentLog(output);
} }
return json; return json;
@ -283,25 +284,4 @@ public class StreamingLogHook : ConversationHookBase, IContentGeneratingHook, IR
return JsonSerializer.Serialize(log, _serializerOptions); return JsonSerializer.Serialize(log, _serializerOptions);
} }
}
internal class ContentLogInput
{
public string ConversationId { get; set; }
public string? Name { get; set; }
public string? AgentId { get; set; }
public string Log { get; set; }
public string Source { get; set; }
public RoleDialogModel Message { get; set; }
public ContentLogInput()
{
}
public ContentLogInput(string conversationId, RoleDialogModel message)
{
ConversationId = conversationId;
Message = message;
}
} }

View file

@ -5,6 +5,7 @@ public class ConversationContentLogDocument : MongoBase
public string ConversationId { get; set; } public string ConversationId { get; set; }
public string MessageId { get; set; } public string MessageId { get; set; }
public string? Name { get; set; } public string? Name { get; set; }
public string? AgentId { get; set; }
public string Role { get; set; } public string Role { get; set; }
public string Source { get; set; } public string Source { get; set; }
public string Content { get; set; } public string Content { get; set; }

View file

@ -60,7 +60,7 @@ public partial class MongoRepository
#endregion #endregion
#region Conversation Content Log #region Conversation Content Log
public void SaveConversationContentLog(ConversationContentLogModel log) public void SaveConversationContentLog(ContentLogOutputModel log)
{ {
if (log == null) return; if (log == null) return;
@ -72,6 +72,7 @@ public partial class MongoRepository
ConversationId = conversationId, ConversationId = conversationId,
MessageId = messageId, MessageId = messageId,
Name = log.Name, Name = log.Name,
AgentId = log.AgentId,
Role = log.Role, Role = log.Role,
Source = log.Source, Source = log.Source,
Content = log.Content, Content = log.Content,
@ -81,16 +82,17 @@ public partial class MongoRepository
_dc.ContentLogs.InsertOne(logDoc); _dc.ContentLogs.InsertOne(logDoc);
} }
public List<ConversationContentLogModel> GetConversationContentLogs(string conversationId) public List<ContentLogOutputModel> GetConversationContentLogs(string conversationId)
{ {
var logs = _dc.ContentLogs var logs = _dc.ContentLogs
.AsQueryable() .AsQueryable()
.Where(x => x.ConversationId == conversationId) .Where(x => x.ConversationId == conversationId)
.Select(x => new ConversationContentLogModel .Select(x => new ContentLogOutputModel
{ {
ConversationId = x.ConversationId, ConversationId = x.ConversationId,
MessageId = x.MessageId, MessageId = x.MessageId,
Name = x.Name, Name = x.Name,
AgentId = x.AgentId,
Role = x.Role, Role = x.Role,
Source = x.Source, Source = x.Source,
Content = x.Content, Content = x.Content,