Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion BotSharp.sln
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,9 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BotSharp.Plugin.WebDriver",
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BotSharp.Plugin.ChatHub", "src\Plugins\BotSharp.Plugin.ChatHub\BotSharp.Plugin.ChatHub.csproj", "{EDCD9C20-2D9D-4098-A16E-03F97B306CB8}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BotSharp.Plugin.TelegramBots", "src\Plugins\BotSharp.Plugin.TelegramBots\BotSharp.Plugin.TelegramBots.csproj", "{DCA18996-4D3A-4E98-BCD0-1FB77C59253E}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BotSharp.Plugin.TelegramBots", "src\Plugins\BotSharp.Plugin.TelegramBots\BotSharp.Plugin.TelegramBots.csproj", "{DCA18996-4D3A-4E98-BCD0-1FB77C59253E}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BotSharp.Logger", "src\Infrastructure\BotSharp.Logger\BotSharp.Logger.csproj", "{5CA3335E-E6AD-46FD-B277-29BBC3A16500}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand Down Expand Up @@ -283,6 +285,14 @@ Global
{DCA18996-4D3A-4E98-BCD0-1FB77C59253E}.Release|Any CPU.Build.0 = Release|Any CPU
{DCA18996-4D3A-4E98-BCD0-1FB77C59253E}.Release|x64.ActiveCfg = Release|Any CPU
{DCA18996-4D3A-4E98-BCD0-1FB77C59253E}.Release|x64.Build.0 = Release|Any CPU
{5CA3335E-E6AD-46FD-B277-29BBC3A16500}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{5CA3335E-E6AD-46FD-B277-29BBC3A16500}.Debug|Any CPU.Build.0 = Debug|Any CPU
{5CA3335E-E6AD-46FD-B277-29BBC3A16500}.Debug|x64.ActiveCfg = Debug|Any CPU
{5CA3335E-E6AD-46FD-B277-29BBC3A16500}.Debug|x64.Build.0 = Debug|Any CPU
{5CA3335E-E6AD-46FD-B277-29BBC3A16500}.Release|Any CPU.ActiveCfg = Release|Any CPU
{5CA3335E-E6AD-46FD-B277-29BBC3A16500}.Release|Any CPU.Build.0 = Release|Any CPU
{5CA3335E-E6AD-46FD-B277-29BBC3A16500}.Release|x64.ActiveCfg = Release|Any CPU
{5CA3335E-E6AD-46FD-B277-29BBC3A16500}.Release|x64.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down Expand Up @@ -320,6 +330,7 @@ Global
{F06B22CB-B143-4680-8FFF-35B9E50E6C47} = {51AFE054-AE99-497D-A593-69BAEFB5106F}
{EDCD9C20-2D9D-4098-A16E-03F97B306CB8} = {64264688-0F5C-4AB0-8F2B-B59B717CCE00}
{DCA18996-4D3A-4E98-BCD0-1FB77C59253E} = {64264688-0F5C-4AB0-8F2B-B59B717CCE00}
{5CA3335E-E6AD-46FD-B277-29BBC3A16500} = {E29DC6C4-5E57-48C5-BCB0-6B8F84782749}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {A9969D89-C98B-40A5-A12B-FC87E55B3A19}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,6 @@ public class ConversationSetting
public bool EnableKnowledgeBase { get; set; }
public bool ShowVerboseLog { get; set; }
public int MaxRecursiveDepth { get; set; } = 3;
public bool EnableLlmCompletionLog { get; set; }
public bool EnableExecutionLog { get; set; }
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace BotSharp.Abstraction.MLTasks;
namespace BotSharp.Abstraction.Loggers;

/// <summary>
/// Model content generating hook, it can be used for logging, metrics and tracing.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace BotSharp.Abstraction.Loggers;

public interface IVerboseLogHook
{
void GenerateLog(string text);
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,11 @@ public interface IBotSharpRepository
List<Conversation> GetConversations(ConversationFilter filter);
void UpdateConversationTitle(string conversationId, string title);
List<Conversation> GetLastConversations();
void AddExectionLogs(string conversationId, List<string> logs);
List<string> GetExectionLogs(string conversationId);
#endregion

#region Execution Log
void AddExecutionLogs(string conversationId, List<string> logs);
List<string> GetExecutionLogs(string conversationId);
#endregion

#region LLM Completion Log
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@

namespace BotSharp.Core;

public static class BotSharpServiceCollectionExtensions
public static class BotSharpCoreExtensions
{
public static IServiceCollection AddBotSharp(this IServiceCollection services, IConfiguration config)
public static IServiceCollection AddBotSharpCore(this IServiceCollection services, IConfiguration config)
{
services.AddScoped<IUserService, UserService>();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,6 @@ public void Append(string conversationId, string content)
content = content.Replace("\r\n", " ").Replace("\n", " ");
content = Regex.Replace(content, @"\s+", " ");
var db = _services.GetRequiredService<IBotSharpRepository>();
db.AddExectionLogs(conversationId, new List<string> { content });
db.AddExecutionLogs(conversationId, new List<string> { content });
}
}
19 changes: 11 additions & 8 deletions src/Infrastructure/BotSharp.Core/Repository/BotSharpDbContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -168,31 +168,34 @@ public void UpdateConversationStatus(string conversationId, string status)
{
throw new NotImplementedException();
}
#endregion


public void AddExectionLogs(string conversationId, List<string> logs)
#region User
public User? GetUserByEmail(string email)
{
throw new NotImplementedException();
}

public List<string> GetExectionLogs(string conversationId)
public User? GetUserById(string id)
{
throw new NotImplementedException();
}
#endregion


#region User
public User? GetUserByEmail(string email)
public void CreateUser(User user)
{
throw new NotImplementedException();
}
#endregion

public User? GetUserById(string id)

#region Execution Log
public void AddExecutionLogs(string conversationId, List<string> logs)
{
throw new NotImplementedException();
}

public void CreateUser(User user)
public List<string> GetExecutionLogs(string conversationId)
{
throw new NotImplementedException();
}
Expand Down
59 changes: 30 additions & 29 deletions src/Infrastructure/BotSharp.Core/Repository/FileRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
using MongoDB.Driver;
using BotSharp.Abstraction.Routing.Models;
using BotSharp.Abstraction.Repositories.Filters;
using BotSharp.Abstraction.Utilities;
using BotSharp.Abstraction.Conversations.Models;

namespace BotSharp.Core.Repository;

Expand Down Expand Up @@ -789,33 +787,6 @@ public List<Conversation> GetLastConversations()
.Select(g => g.OrderByDescending(x => x.CreatedTime).First())
.ToList();
}

public void AddExectionLogs(string conversationId, List<string> logs)
{
if (string.IsNullOrEmpty(conversationId) || logs.IsNullOrEmpty()) return;

var dir = Path.Combine(_dbSettings.FileRepository, "conversations", conversationId);
if (!Directory.Exists(dir))
{
Directory.CreateDirectory(dir);
}

var file = Path.Combine(dir, "execution.log");
File.AppendAllLines(file, logs);
}

public List<string> GetExectionLogs(string conversationId)
{
var logs = new List<string>();
if (string.IsNullOrEmpty(conversationId)) return logs;

var dir = Path.Combine(_dbSettings.FileRepository, "conversations", conversationId);
if (!Directory.Exists(dir)) return logs;

var file = Path.Combine(dir, "execution.log");
logs = File.ReadAllLines(file)?.ToList() ?? new List<string>();
return logs;
}
#endregion

#region User
Expand Down Expand Up @@ -843,6 +814,35 @@ public void CreateUser(User user)
}
#endregion

#region Execution Log
public void AddExecutionLogs(string conversationId, List<string> logs)
{
if (string.IsNullOrEmpty(conversationId) || logs.IsNullOrEmpty()) return;

var dir = Path.Combine(_dbSettings.FileRepository, "conversations", conversationId);
if (!Directory.Exists(dir))
{
Directory.CreateDirectory(dir);
}

var file = Path.Combine(dir, "execution.log");
File.AppendAllLines(file, logs);
}

public List<string> GetExecutionLogs(string conversationId)
{
var logs = new List<string>();
if (string.IsNullOrEmpty(conversationId)) return logs;

var dir = Path.Combine(_dbSettings.FileRepository, "conversations", conversationId);
if (!Directory.Exists(dir)) return logs;

var file = Path.Combine(dir, "execution.log");
logs = File.ReadAllLines(file)?.ToList() ?? new List<string>();
return logs;
}
#endregion

#region LLM Completion Log
public void SaveLlmCompletionLog(LlmCompletionLog log)
{
Expand All @@ -855,6 +855,7 @@ public void SaveLlmCompletionLog(LlmCompletionLog log)
Directory.CreateDirectory(logDir);
}

log.Id = Guid.NewGuid().ToString();
var index = GetLlmCompletionLogIndex(logDir, log.MessageId);
var file = Path.Combine(logDir, $"{log.MessageId}.{index}.log");
File.WriteAllText(file, JsonSerializer.Serialize(log, _options));
Expand Down
58 changes: 58 additions & 0 deletions src/Infrastructure/BotSharp.Logger/BotSharp.Logger.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netstandard2.1</TargetFramework>
<LangVersion>$(LangVersion)</LangVersion>
<VersionPrefix>$(BotSharpVersion)</VersionPrefix>
<GeneratePackageOnBuild>$(GeneratePackageOnBuild)</GeneratePackageOnBuild>
</PropertyGroup>

<PropertyGroup>
<Authors>Haiping Chen</Authors>
<Company>SciSharp STACK</Company>
<Product>LL Application Framework</Product>
<Description>
Open source LLM application framework to build scalable, flexible and robust AI system.
</Description>
<RepositoryType>git</RepositoryType>
<RepositoryUrl>https://github.com/SciSharp/BotSharp</RepositoryUrl>
<PackageTags>Chatbot, Bot, LLM, AI, ChatGPT, OpenAI</PackageTags>
<PackageReleaseNotes>Support dialogue status tracking.</PackageReleaseNotes>
<Copyright>Since 2018 Haiping Chen</Copyright>
<PackageProjectUrl>https://github.com/SciSharp/BotSharp</PackageProjectUrl>
<PackageIconUrl>https://raw.githubusercontent.com/SciSharp/BotSharp/master/docs/static/logos/BotSharp.png</PackageIconUrl>
<PackageLicenseUrl>https://raw.githubusercontent.com/SciSharp/BotSharp/master/LICENSE</PackageLicenseUrl>
<PackageIcon>Icon.png</PackageIcon>
<Nullable>enable</Nullable>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
<DefineConstants>TRACE;DEBUG</DefineConstants>
<NoWarn>1701;1702</NoWarn>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
<DefineConstants>TRACE;</DefineConstants>
<NoWarn>1701;1702</NoWarn>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<NoWarn>1701;1702</NoWarn>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<NoWarn>1701;1702</NoWarn>
</PropertyGroup>

<ItemGroup>
<None Include="..\..\..\arts\Icon.png">
<Pack>True</Pack>
<PackagePath>\</PackagePath>
</None>
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\BotSharp.Abstraction\BotSharp.Abstraction.csproj" />
</ItemGroup>

</Project>
12 changes: 12 additions & 0 deletions src/Infrastructure/BotSharp.Logger/BotSharpLoggerExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
namespace BotSharp.Logger;

public static class BotSharpLoggerExtensions
{
public static IServiceCollection AddBotSharpLogger(this IServiceCollection services, IConfiguration config)
{
services.AddScoped<IContentGeneratingHook, CommonContentGeneratingHook>();
services.AddScoped<IContentGeneratingHook, TokenStatsConversationHook>();
services.AddScoped<IVerboseLogHook, VerboseLogHook>();
return services;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
public class CommonContentGeneratingHook : IContentGeneratingHook
{
private readonly IServiceProvider _services;

public CommonContentGeneratingHook(IServiceProvider services)
{
_services = services;
}

/// <summary>
/// After content generated.
/// </summary>
/// <returns></returns>
public async Task AfterGenerated(RoleDialogModel message, TokenStatsModel tokenStats)
{
SaveLlmCompletionLog(message, tokenStats);
await Task.CompletedTask;
}

private void SaveLlmCompletionLog(RoleDialogModel message, TokenStatsModel tokenStats)
{
var convSettings = _services.GetRequiredService<ConversationSetting>();
if (!convSettings.EnableLlmCompletionLog) return;

var db = _services.GetRequiredService<IBotSharpRepository>();
var state = _services.GetRequiredService<IConversationStateService>();

var completionLog = new LlmCompletionLog
{
ConversationId = state.GetConversationId(),
MessageId = message.MessageId,
AgentId = message.CurrentAgentId,
Prompt = tokenStats.Prompt,
Response = message.Content
};

db.SaveLlmCompletionLog(completionLog);
}
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,5 @@
using BotSharp.Abstraction.Agents.Models;
using BotSharp.Abstraction.Conversations;
using BotSharp.Abstraction.Conversations.Models;
using BotSharp.Abstraction.MLTasks;
using System.Collections.Generic;
using System.Threading.Tasks;
namespace BotSharp.Logger.Hooks;

namespace BotSharp.Plugin.AzureOpenAI.Hooks;

/// <summary>
/// Token statistics for Azure OpenAI
/// </summary>
public class TokenStatsConversationHook : IContentGeneratingHook
{
private readonly ITokenStatistics _tokenStatistics;
Expand All @@ -22,14 +12,15 @@ public TokenStatsConversationHook(ITokenStatistics tokenStatistics)
public async Task BeforeGenerating(Agent agent, List<RoleDialogModel> conversations)
{
_tokenStatistics.StartTimer();
await Task.CompletedTask;
}

public async Task AfterGenerated(RoleDialogModel message, TokenStatsModel tokenStats)
{
_tokenStatistics.StopTimer();

tokenStats.PromptCost = 0.0015f;
tokenStats.CompletionCost = 0.002f;
_tokenStatistics.AddToken(tokenStats);
await Task.CompletedTask;
}
}
20 changes: 20 additions & 0 deletions src/Infrastructure/BotSharp.Logger/Hooks/VerboseLogHook.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
namespace BotSharp.Logger.Hooks;

public class VerboseLogHook : IVerboseLogHook
{
private readonly ConversationSetting _convSettings;
private readonly ILogger<VerboseLogHook> _logger;

public VerboseLogHook(ConversationSetting convSettings, ILogger<VerboseLogHook> logger)
{
_convSettings = convSettings;
_logger = logger;
}

public void GenerateLog(string text)
{
if (!_convSettings.ShowVerboseLog) return;

_logger.LogInformation(text);
}
}
Loading