Merge pull request #300 from iceljc/features/add-role-in-log
Features/add role in log
This commit is contained in:
commit
d411382cef
|
|
@ -6,6 +6,8 @@ public class StreamingLogModel
|
||||||
public string ConversationId { get; set; }
|
public string ConversationId { get; set; }
|
||||||
[JsonPropertyName("name")]
|
[JsonPropertyName("name")]
|
||||||
public string? Name { get; set; }
|
public string? Name { get; set; }
|
||||||
|
[JsonPropertyName("role")]
|
||||||
|
public string Role { get; set; }
|
||||||
|
|
||||||
[JsonPropertyName("content")]
|
[JsonPropertyName("content")]
|
||||||
public string Content { get; set; }
|
public string Content { get; set; }
|
||||||
|
|
|
||||||
|
|
@ -206,8 +206,13 @@ namespace BotSharp.Core.Repository
|
||||||
var records = new List<Conversation>();
|
var records = new List<Conversation>();
|
||||||
var dir = Path.Combine(_dbSettings.FileRepository, _conversationSettings.DataDir);
|
var dir = Path.Combine(_dbSettings.FileRepository, _conversationSettings.DataDir);
|
||||||
var pager = filter?.Pager ?? new Pagination();
|
var pager = filter?.Pager ?? new Pagination();
|
||||||
var totalDirs = Directory.GetDirectories(dir);
|
|
||||||
|
|
||||||
|
if (!Directory.Exists(dir))
|
||||||
|
{
|
||||||
|
Directory.CreateDirectory(dir);
|
||||||
|
}
|
||||||
|
|
||||||
|
var totalDirs = Directory.GetDirectories(dir);
|
||||||
foreach (var d in totalDirs)
|
foreach (var d in totalDirs)
|
||||||
{
|
{
|
||||||
var path = Path.Combine(d, CONVERSATION_FILE);
|
var path = Path.Combine(d, CONVERSATION_FILE);
|
||||||
|
|
|
||||||
|
|
@ -32,7 +32,7 @@ public static class BotSharpOpenApiExtensions
|
||||||
|
|
||||||
// Add bearer authentication
|
// Add bearer authentication
|
||||||
var schema = "MIXED_SCHEME";
|
var schema = "MIXED_SCHEME";
|
||||||
services.AddAuthentication(options =>
|
var builder = services.AddAuthentication(options =>
|
||||||
{
|
{
|
||||||
// custom scheme defined in .AddPolicyScheme() below
|
// custom scheme defined in .AddPolicyScheme() below
|
||||||
// inspired from https://weblog.west-wind.com/posts/2022/Mar/29/Combining-Bearer-Token-and-Cookie-Auth-in-ASPNET
|
// inspired from https://weblog.west-wind.com/posts/2022/Mar/29/Combining-Bearer-Token-and-Cookie-Auth-in-ASPNET
|
||||||
|
|
@ -59,11 +59,6 @@ public static class BotSharpOpenApiExtensions
|
||||||
}
|
}
|
||||||
}).AddCookie(options =>
|
}).AddCookie(options =>
|
||||||
{
|
{
|
||||||
}).AddGitHub(options =>
|
|
||||||
{
|
|
||||||
options.ClientId = config["OAuth:GitHub:ClientId"];
|
|
||||||
options.ClientSecret = config["OAuth:GitHub:ClientSecret"];
|
|
||||||
options.Scope.Add("user:email");
|
|
||||||
}).AddPolicyScheme(schema, "Mixed authentication", options =>
|
}).AddPolicyScheme(schema, "Mixed authentication", options =>
|
||||||
{
|
{
|
||||||
// runs on each request
|
// runs on each request
|
||||||
|
|
@ -85,6 +80,16 @@ public static class BotSharpOpenApiExtensions
|
||||||
};
|
};
|
||||||
});
|
});
|
||||||
|
|
||||||
|
if (!string.IsNullOrWhiteSpace(config["OAuth:GitHub:ClientId"]) && !string.IsNullOrWhiteSpace(config["OAuth:GitHub:ClientSecret"]))
|
||||||
|
{
|
||||||
|
builder = builder.AddGitHub(options =>
|
||||||
|
{
|
||||||
|
options.ClientId = config["OAuth:GitHub:ClientId"];
|
||||||
|
options.ClientSecret = config["OAuth:GitHub:ClientSecret"];
|
||||||
|
options.Scope.Add("user:email");
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
// Add services to the container.
|
// Add services to the container.
|
||||||
services.AddControllers()
|
services.AddControllers()
|
||||||
.AddJsonOptions(options =>
|
.AddJsonOptions(options =>
|
||||||
|
|
|
||||||
|
|
@ -37,7 +37,7 @@ public class StreamingLogHook : ConversationHookBase, IContentGeneratingHook
|
||||||
{
|
{
|
||||||
var conversationId = _state.GetConversationId();
|
var conversationId = _state.GetConversationId();
|
||||||
var log = $"MessageId: {message.MessageId} ==>\r\n{message.Role}: {message.Content}";
|
var log = $"MessageId: {message.MessageId} ==>\r\n{message.Role}: {message.Content}";
|
||||||
await _chatHub.Clients.User(_user.Id).SendAsync("OnContentLogGenerated", BuildLog(conversationId, _user.UserName, log));
|
await _chatHub.Clients.User(_user.Id).SendAsync("OnContentLogGenerated", BuildLog(conversationId, _user.UserName, message.Role, log));
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task BeforeGenerating(Agent agent, List<RoleDialogModel> conversations)
|
public async Task BeforeGenerating(Agent agent, List<RoleDialogModel> conversations)
|
||||||
|
|
@ -64,21 +64,22 @@ public class StreamingLogHook : ConversationHookBase, IContentGeneratingHook
|
||||||
var conversationId = _state.GetConversationId();
|
var conversationId = _state.GetConversationId();
|
||||||
var agent = await agentService.LoadAgent(message.CurrentAgentId);
|
var agent = await agentService.LoadAgent(message.CurrentAgentId);
|
||||||
|
|
||||||
await _chatHub.Clients.User(_user.Id).SendAsync("OnContentLogGenerated", BuildLog(conversationId, agent?.Name, tokenStats.Prompt));
|
await _chatHub.Clients.User(_user.Id).SendAsync("OnContentLogGenerated", BuildLog(conversationId, agent?.Name, message.Role, tokenStats.Prompt));
|
||||||
|
|
||||||
var log = message.Role == AgentRole.Function ?
|
var log = message.Role == AgentRole.Function ?
|
||||||
$"[{agent?.Name}]: {message.FunctionName}({message.FunctionArgs})" :
|
$"[{agent?.Name}]: {message.FunctionName}({message.FunctionArgs})" :
|
||||||
$"[{agent?.Name}]: {message.Content}";
|
$"[{agent?.Name}]: {message.Content}";
|
||||||
log += $"\r\n<== MessageId: {message.MessageId}";
|
log += $"\r\n<== MessageId: {message.MessageId}";
|
||||||
await _chatHub.Clients.User(_user.Id).SendAsync("OnContentLogGenerated", BuildLog(conversationId, agent?.Name, log));
|
await _chatHub.Clients.User(_user.Id).SendAsync("OnContentLogGenerated", BuildLog(conversationId, agent?.Name, message.Role, log));
|
||||||
}
|
}
|
||||||
|
|
||||||
private string BuildLog(string conversationId, string? name, string content)
|
private string BuildLog(string conversationId, string? name, string role, string content)
|
||||||
{
|
{
|
||||||
var log = new StreamingLogModel
|
var log = new StreamingLogModel
|
||||||
{
|
{
|
||||||
ConversationId = conversationId,
|
ConversationId = conversationId,
|
||||||
Name = name,
|
Name = name,
|
||||||
|
Role = role,
|
||||||
Content = content,
|
Content = content,
|
||||||
CreateTime = DateTime.UtcNow
|
CreateTime = DateTime.UtcNow
|
||||||
};
|
};
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue