Skip to content

Commit c667f1b

Browse files
author
Jicheng Lu
committed
add logger
1 parent b99a158 commit c667f1b

File tree

27 files changed

+214
-69
lines changed

27 files changed

+214
-69
lines changed

BotSharp.sln

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,9 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BotSharp.Plugin.WebDriver",
7373
EndProject
7474
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BotSharp.Plugin.ChatHub", "src\Plugins\BotSharp.Plugin.ChatHub\BotSharp.Plugin.ChatHub.csproj", "{EDCD9C20-2D9D-4098-A16E-03F97B306CB8}"
7575
EndProject
76-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BotSharp.Plugin.TelegramBots", "src\Plugins\BotSharp.Plugin.TelegramBots\BotSharp.Plugin.TelegramBots.csproj", "{DCA18996-4D3A-4E98-BCD0-1FB77C59253E}"
76+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BotSharp.Plugin.TelegramBots", "src\Plugins\BotSharp.Plugin.TelegramBots\BotSharp.Plugin.TelegramBots.csproj", "{DCA18996-4D3A-4E98-BCD0-1FB77C59253E}"
77+
EndProject
78+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BotSharp.Logger", "src\Infrastructure\BotSharp.Logger\BotSharp.Logger.csproj", "{5CA3335E-E6AD-46FD-B277-29BBC3A16500}"
7779
EndProject
7880
Global
7981
GlobalSection(SolutionConfigurationPlatforms) = preSolution
@@ -283,6 +285,14 @@ Global
283285
{DCA18996-4D3A-4E98-BCD0-1FB77C59253E}.Release|Any CPU.Build.0 = Release|Any CPU
284286
{DCA18996-4D3A-4E98-BCD0-1FB77C59253E}.Release|x64.ActiveCfg = Release|Any CPU
285287
{DCA18996-4D3A-4E98-BCD0-1FB77C59253E}.Release|x64.Build.0 = Release|Any CPU
288+
{5CA3335E-E6AD-46FD-B277-29BBC3A16500}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
289+
{5CA3335E-E6AD-46FD-B277-29BBC3A16500}.Debug|Any CPU.Build.0 = Debug|Any CPU
290+
{5CA3335E-E6AD-46FD-B277-29BBC3A16500}.Debug|x64.ActiveCfg = Debug|Any CPU
291+
{5CA3335E-E6AD-46FD-B277-29BBC3A16500}.Debug|x64.Build.0 = Debug|Any CPU
292+
{5CA3335E-E6AD-46FD-B277-29BBC3A16500}.Release|Any CPU.ActiveCfg = Release|Any CPU
293+
{5CA3335E-E6AD-46FD-B277-29BBC3A16500}.Release|Any CPU.Build.0 = Release|Any CPU
294+
{5CA3335E-E6AD-46FD-B277-29BBC3A16500}.Release|x64.ActiveCfg = Release|Any CPU
295+
{5CA3335E-E6AD-46FD-B277-29BBC3A16500}.Release|x64.Build.0 = Release|Any CPU
286296
EndGlobalSection
287297
GlobalSection(SolutionProperties) = preSolution
288298
HideSolutionNode = FALSE
@@ -320,6 +330,7 @@ Global
320330
{F06B22CB-B143-4680-8FFF-35B9E50E6C47} = {51AFE054-AE99-497D-A593-69BAEFB5106F}
321331
{EDCD9C20-2D9D-4098-A16E-03F97B306CB8} = {64264688-0F5C-4AB0-8F2B-B59B717CCE00}
322332
{DCA18996-4D3A-4E98-BCD0-1FB77C59253E} = {64264688-0F5C-4AB0-8F2B-B59B717CCE00}
333+
{5CA3335E-E6AD-46FD-B277-29BBC3A16500} = {E29DC6C4-5E57-48C5-BCB0-6B8F84782749}
323334
EndGlobalSection
324335
GlobalSection(ExtensibilityGlobals) = postSolution
325336
SolutionGuid = {A9969D89-C98B-40A5-A12B-FC87E55B3A19}

src/Infrastructure/BotSharp.Abstraction/MLTasks/IContentGeneratingHook.cs renamed to src/Infrastructure/BotSharp.Abstraction/Loggers/IContentGeneratingHook.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
namespace BotSharp.Abstraction.MLTasks;
1+
namespace BotSharp.Abstraction.Loggers;
22

33
/// <summary>
44
/// Model content generating hook, it can be used for logging, metrics and tracing.
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
namespace BotSharp.Abstraction.Loggers;
2+
3+
public interface IVerboseLogHook
4+
{
5+
void GenerateLog(string text);
6+
}

src/Infrastructure/BotSharp.Abstraction/Repositories/IBotSharpRepository.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,9 @@ public interface IBotSharpRepository
3838
List<Conversation> GetConversations(ConversationFilter filter);
3939
void UpdateConversationTitle(string conversationId, string title);
4040
List<Conversation> GetLastConversations();
41+
#endregion
42+
43+
#region Execution Log
4144
void AddExecutionLogs(string conversationId, List<string> logs);
4245
List<string> GetExecutionLogs(string conversationId);
4346
#endregion

src/Infrastructure/BotSharp.Core/BotSharpServiceCollectionExtensions.cs renamed to src/Infrastructure/BotSharp.Core/BotSharpCoreExtensions.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@
2222

2323
namespace BotSharp.Core;
2424

25-
public static class BotSharpServiceCollectionExtensions
25+
public static class BotSharpCoreExtensions
2626
{
27-
public static IServiceCollection AddBotSharp(this IServiceCollection services, IConfiguration config)
27+
public static IServiceCollection AddBotSharpCore(this IServiceCollection services, IConfiguration config)
2828
{
2929
services.AddScoped<IUserService, UserService>();
3030

src/Infrastructure/BotSharp.Core/Repository/FileRepository.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -855,6 +855,7 @@ public void SaveLlmCompletionLog(LlmCompletionLog log)
855855
Directory.CreateDirectory(logDir);
856856
}
857857

858+
log.Id = Guid.NewGuid().ToString();
858859
var index = GetLlmCompletionLogIndex(logDir, log.MessageId);
859860
var file = Path.Combine(logDir, $"{log.MessageId}.{index}.log");
860861
File.WriteAllText(file, JsonSerializer.Serialize(log, _options));
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>netstandard2.1</TargetFramework>
5+
<LangVersion>$(LangVersion)</LangVersion>
6+
<VersionPrefix>$(BotSharpVersion)</VersionPrefix>
7+
<GeneratePackageOnBuild>$(GeneratePackageOnBuild)</GeneratePackageOnBuild>
8+
</PropertyGroup>
9+
10+
<PropertyGroup>
11+
<Authors>Haiping Chen</Authors>
12+
<Company>SciSharp STACK</Company>
13+
<Product>LL Application Framework</Product>
14+
<Description>
15+
Open source LLM application framework to build scalable, flexible and robust AI system.
16+
</Description>
17+
<RepositoryType>git</RepositoryType>
18+
<RepositoryUrl>https://github.com/SciSharp/BotSharp</RepositoryUrl>
19+
<PackageTags>Chatbot, Bot, LLM, AI, ChatGPT, OpenAI</PackageTags>
20+
<PackageReleaseNotes>Support dialogue status tracking.</PackageReleaseNotes>
21+
<Copyright>Since 2018 Haiping Chen</Copyright>
22+
<PackageProjectUrl>https://github.com/SciSharp/BotSharp</PackageProjectUrl>
23+
<PackageIconUrl>https://raw.githubusercontent.com/SciSharp/BotSharp/master/docs/static/logos/BotSharp.png</PackageIconUrl>
24+
<PackageLicenseUrl>https://raw.githubusercontent.com/SciSharp/BotSharp/master/LICENSE</PackageLicenseUrl>
25+
<PackageIcon>Icon.png</PackageIcon>
26+
<Nullable>enable</Nullable>
27+
</PropertyGroup>
28+
29+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
30+
<DefineConstants>TRACE;DEBUG</DefineConstants>
31+
<NoWarn>1701;1702</NoWarn>
32+
</PropertyGroup>
33+
34+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
35+
<DefineConstants>TRACE;</DefineConstants>
36+
<NoWarn>1701;1702</NoWarn>
37+
</PropertyGroup>
38+
39+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
40+
<NoWarn>1701;1702</NoWarn>
41+
</PropertyGroup>
42+
43+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
44+
<NoWarn>1701;1702</NoWarn>
45+
</PropertyGroup>
46+
47+
<ItemGroup>
48+
<None Include="..\..\..\arts\Icon.png">
49+
<Pack>True</Pack>
50+
<PackagePath>\</PackagePath>
51+
</None>
52+
</ItemGroup>
53+
54+
<ItemGroup>
55+
<ProjectReference Include="..\BotSharp.Abstraction\BotSharp.Abstraction.csproj" />
56+
</ItemGroup>
57+
58+
</Project>
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
namespace BotSharp.Logger;
2+
3+
public static class BotSharpLoggerExtensions
4+
{
5+
public static IServiceCollection AddBotSharpLogger(this IServiceCollection services, IConfiguration config)
6+
{
7+
services.AddScoped<IContentGeneratingHook, CommonContentGeneratingHook>();
8+
services.AddScoped<IContentGeneratingHook, TokenStatsConversationHook>();
9+
services.AddScoped<IVerboseLogHook, VerboseLogHook>();
10+
return services;
11+
}
12+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
public class CommonContentGeneratingHook : IContentGeneratingHook
2+
{
3+
private readonly IServiceProvider _services;
4+
5+
public CommonContentGeneratingHook(IServiceProvider services)
6+
{
7+
_services = services;
8+
}
9+
10+
/// <summary>
11+
/// After content generated.
12+
/// </summary>
13+
/// <returns></returns>
14+
public async Task AfterGenerated(RoleDialogModel message, TokenStatsModel tokenStats)
15+
{
16+
SaveLlmCompletionLog(message, tokenStats);
17+
await Task.CompletedTask;
18+
}
19+
20+
private void SaveLlmCompletionLog(RoleDialogModel message, TokenStatsModel tokenStats)
21+
{
22+
var convSettings = _services.GetRequiredService<ConversationSetting>();
23+
if (!convSettings.EnableLlmCompletionLog) return;
24+
25+
var db = _services.GetRequiredService<IBotSharpRepository>();
26+
var state = _services.GetRequiredService<IConversationStateService>();
27+
28+
var completionLog = new LlmCompletionLog
29+
{
30+
ConversationId = state.GetConversationId(),
31+
MessageId = message.MessageId,
32+
AgentId = message.CurrentAgentId,
33+
Prompt = tokenStats.Prompt,
34+
Response = message.Content
35+
};
36+
37+
db.SaveLlmCompletionLog(completionLog);
38+
}
39+
}
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,5 @@
1-
using BotSharp.Abstraction.Agents.Models;
2-
using BotSharp.Abstraction.Conversations;
3-
using BotSharp.Abstraction.Conversations.Models;
4-
using BotSharp.Abstraction.MLTasks;
5-
using System.Collections.Generic;
6-
using System.Threading.Tasks;
1+
namespace BotSharp.Logger.Hooks;
72

8-
namespace BotSharp.Plugin.AzureOpenAI.Hooks;
9-
10-
/// <summary>
11-
/// Token statistics for Azure OpenAI
12-
/// </summary>
133
public class TokenStatsConversationHook : IContentGeneratingHook
144
{
155
private readonly ITokenStatistics _tokenStatistics;
@@ -22,14 +12,15 @@ public TokenStatsConversationHook(ITokenStatistics tokenStatistics)
2212
public async Task BeforeGenerating(Agent agent, List<RoleDialogModel> conversations)
2313
{
2414
_tokenStatistics.StartTimer();
15+
await Task.CompletedTask;
2516
}
2617

2718
public async Task AfterGenerated(RoleDialogModel message, TokenStatsModel tokenStats)
2819
{
2920
_tokenStatistics.StopTimer();
30-
3121
tokenStats.PromptCost = 0.0015f;
3222
tokenStats.CompletionCost = 0.002f;
3323
_tokenStatistics.AddToken(tokenStats);
24+
await Task.CompletedTask;
3425
}
3526
}

0 commit comments

Comments
 (0)