Skip to content

Commit 9cc9839

Browse files
committed
Merge branch 'master' into v0.20-json-schema
2 parents 56f99c2 + 5bb1765 commit 9cc9839

File tree

8 files changed

+55
-29
lines changed

8 files changed

+55
-29
lines changed

docs/architecture/logging.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# Logging
2+
3+
## Setting
4+
To initialize the logging feature, set up the following flags in `Conversation`. Each flag can display or record specific content during conversation.
5+
6+
* `ShowVerboseLog`: print conversation details or prompt in console.
7+
* `EnableLlmCompletionLog`: log LLM completion results, e.g., real-time prompt sent to LLM and response generated from LLm.
8+
* `EnableExecutionLog`: log details after events, e.g., receiving message, executing function, generating response, etc.
9+
10+
11+
```json
12+
"Conversation": {
13+
"ShowVerboseLog": false,
14+
"EnableLlmCompletionLog": false,
15+
"EnableExecutionLog": true
16+
}
17+
```
18+
19+
### Usage
20+
To enable the logging functionality, add the following line of code in `Program.cs`.
21+
22+
```csharp
23+
builder.Services.AddBotSharpLogger(builder.Configuration);
24+
```

docs/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ The main documentation for the site is organized into the following sections:
102102
architecture/plugin
103103
architecture/hooks
104104
architecture/routing
105+
architecture/logging
105106
architecture/data-persistence
106107

107108
If you feel that this project is helpful to you, please Star us on the project, we will be very grateful.

src/Infrastructure/BotSharp.Abstraction/Loggers/IVerboseLogHook.cs

Lines changed: 0 additions & 6 deletions
This file was deleted.

src/Infrastructure/BotSharp.Logger/BotSharpLoggerExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ public static IServiceCollection AddBotSharpLogger(this IServiceCollection servi
66
{
77
services.AddScoped<IContentGeneratingHook, CommonContentGeneratingHook>();
88
services.AddScoped<IContentGeneratingHook, TokenStatsConversationHook>();
9-
services.AddScoped<IVerboseLogHook, VerboseLogHook>();
9+
services.AddScoped<IContentGeneratingHook, VerboseLogHook>();
1010
return services;
1111
}
1212
}

src/Infrastructure/BotSharp.Logger/Hooks/CommonContentGeneratingHook.cs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,6 @@ public CommonContentGeneratingHook(IServiceProvider services)
77
_services = services;
88
}
99

10-
/// <summary>
11-
/// After content generated.
12-
/// </summary>
13-
/// <returns></returns>
1410
public async Task AfterGenerated(RoleDialogModel message, TokenStatsModel tokenStats)
1511
{
1612
SaveLlmCompletionLog(message, tokenStats);
Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,45 @@
1+
using BotSharp.Abstraction.Agents;
2+
using BotSharp.Abstraction.Agents.Enums;
3+
14
namespace BotSharp.Logger.Hooks;
25

3-
public class VerboseLogHook : IVerboseLogHook
6+
public class VerboseLogHook : IContentGeneratingHook
47
{
58
private readonly ConversationSetting _convSettings;
69
private readonly ILogger<VerboseLogHook> _logger;
10+
private readonly IServiceProvider _services;
711

8-
public VerboseLogHook(ConversationSetting convSettings, ILogger<VerboseLogHook> logger)
12+
public VerboseLogHook(
13+
ConversationSetting convSettings,
14+
IServiceProvider serivces,
15+
ILogger<VerboseLogHook> logger)
916
{
1017
_convSettings = convSettings;
18+
_services = serivces;
1119
_logger = logger;
1220
}
1321

14-
public void GenerateLog(string text)
22+
public async Task BeforeGenerating(Agent agent, List<RoleDialogModel> conversations)
23+
{
24+
if (!_convSettings.ShowVerboseLog) return;
25+
26+
var dialog = conversations.Last();
27+
var log = $"{dialog.Role}: {dialog.Content}";
28+
_logger.LogInformation(log);
29+
}
30+
31+
public async Task AfterGenerated(RoleDialogModel message, TokenStatsModel tokenStats)
1532
{
1633
if (!_convSettings.ShowVerboseLog) return;
1734

18-
_logger.LogInformation(text);
35+
var agentService = _services.GetRequiredService<IAgentService>();
36+
var agent = await agentService.LoadAgent(message.CurrentAgentId);
37+
38+
var log = message.Role == AgentRole.Function ?
39+
$"[{agent.Name}]: {message.FunctionName}({message.FunctionArgs})" :
40+
$"[{agent.Name}]: {message.Content}";
41+
42+
_logger.LogInformation(tokenStats.Prompt);
43+
_logger.LogInformation(log);
1944
}
2045
}

src/Plugins/BotSharp.Plugin.AzureOpenAI/Providers/ChatCompletionProvider.cs

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
using BotSharp.Abstraction.Agents.Models;
55
using BotSharp.Abstraction.Conversations;
66
using BotSharp.Abstraction.Conversations.Models;
7-
using BotSharp.Abstraction.Conversations.Settings;
87
using BotSharp.Abstraction.Loggers;
98
using BotSharp.Abstraction.MLTasks;
109
using BotSharp.Plugin.AzureOpenAI.Settings;
@@ -39,7 +38,6 @@ public ChatCompletionProvider(AzureOpenAiSettings settings,
3938
public RoleDialogModel GetChatCompletions(Agent agent, List<RoleDialogModel> conversations)
4039
{
4140
var contentHooks = _services.GetServices<IContentGeneratingHook>().ToList();
42-
var logHook = _services.GetService<IVerboseLogHook>();
4341

4442
// Before chat completion hook
4543
Task.WaitAll(contentHooks.Select(hook =>
@@ -75,11 +73,6 @@ public RoleDialogModel GetChatCompletions(Agent agent, List<RoleDialogModel> con
7573
}
7674
}
7775

78-
var log = responseMessage.Role == AgentRole.Function ?
79-
$"[{agent.Name}]: {responseMessage.FunctionName}({responseMessage.FunctionArgs})" :
80-
$"[{agent.Name}]: {responseMessage.Content}";
81-
logHook?.GenerateLog(log);
82-
8376
// After chat completion hook
8477
Task.WaitAll(contentHooks.Select(hook =>
8578
hook.AfterGenerated(responseMessage, new TokenStatsModel
@@ -192,7 +185,6 @@ public async Task<bool> GetChatCompletionsStreamingAsync(Agent agent, List<RoleD
192185
protected (string, ChatCompletionsOptions) PrepareOptions(Agent agent, List<RoleDialogModel> conversations)
193186
{
194187
var agentService = _services.GetRequiredService<IAgentService>();
195-
var logHook = _services.GetService<IVerboseLogHook>();
196188

197189
var chatCompletionsOptions = new ChatCompletionsOptions();
198190

@@ -248,7 +240,6 @@ public async Task<bool> GetChatCompletionsStreamingAsync(Agent agent, List<RoleD
248240
// chatCompletionsOptions.PresencePenalty = 0;
249241

250242
var prompt = GetPrompt(chatCompletionsOptions);
251-
logHook?.GenerateLog(prompt);
252243

253244
return (prompt, chatCompletionsOptions);
254245
}

src/Plugins/BotSharp.Plugin.AzureOpenAI/Providers/TextCompletionProvider.cs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
using System;
44
using System.Threading.Tasks;
55
using BotSharp.Plugin.AzureOpenAI.Settings;
6-
using Microsoft.Extensions.Logging;
76
using BotSharp.Abstraction.Conversations;
87
using Microsoft.Extensions.DependencyInjection;
98
using BotSharp.Abstraction.Conversations.Models;
@@ -32,7 +31,6 @@ public TextCompletionProvider(IServiceProvider services,
3231
public async Task<string> GetCompletion(string text, string agentId, string messageId)
3332
{
3433
var contentHooks = _services.GetServices<IContentGeneratingHook>().ToList();
35-
var logHook = _services.GetService<IVerboseLogHook>();
3634

3735
// Before chat completion hook
3836
var agent = new Agent()
@@ -63,7 +61,6 @@ public async Task<string> GetCompletion(string text, string agentId, string mess
6361
MaxTokens = 256,
6462
};
6563
completionsOptions.StopSequences.Add($"{AgentRole.Assistant}:");
66-
logHook?.GenerateLog(text);
6764

6865
var state = _services.GetRequiredService<IConversationStateService>();
6966
var temperature = float.Parse(state.GetState("temperature", "0.5"));
@@ -80,8 +77,6 @@ public async Task<string> GetCompletion(string text, string agentId, string mess
8077
completion += t.Text;
8178
};
8279

83-
logHook?.GenerateLog(completion);
84-
8580
// After chat completion hook
8681
var responseMessage = new RoleDialogModel(AgentRole.Assistant, completion)
8782
{

0 commit comments

Comments
 (0)