-
Notifications
You must be signed in to change notification settings - Fork 177
Expand file tree
/
Copy pathLoggingChatClient.cs
More file actions
36 lines (32 loc) · 1.42 KB
/
LoggingChatClient.cs
File metadata and controls
36 lines (32 loc) · 1.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
using System.Runtime.CompilerServices;
using Microsoft.Extensions.AI;
using Microsoft.Extensions.Logging;
public class LoggingChatClient : DelegatingChatClient
{
private readonly ILogger _logger;
private readonly IChatClient _innerClient;
public LoggingChatClient(IChatClient innerClient, ILogger? logger = null) : base(innerClient)
{
_innerClient = innerClient;
_logger = logger ?? LoggerFactory.Create(builder => builder.AddConsole()).CreateLogger<LoggingChatClient>();
}
public override async Task<ChatResponse> GetResponseAsync(
IEnumerable<ChatMessage> chatMessages,
ChatOptions? options = null,
CancellationToken cancellationToken = default)
{
_logger.LogInformation("Completing chat for {message}", chatMessages.Last().Text);
return await _innerClient.GetResponseAsync(chatMessages, options, cancellationToken);
}
public override async IAsyncEnumerable<ChatResponseUpdate> GetStreamingResponseAsync(
IEnumerable<ChatMessage> chatMessages,
ChatOptions? options = null,
[EnumeratorCancellation] CancellationToken cancellationToken = default)
{
_logger.LogInformation("Completing chat for {message}", chatMessages.Last().Text);
await foreach (var message in _innerClient.GetStreamingResponseAsync(chatMessages, options, cancellationToken))
{
yield return message;
}
}
}