Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make UseLogging a nop when NullLoggerFactory is used #5717

Merged
merged 1 commit into from
Dec 3, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;
using Microsoft.Shared.Diagnostics;

namespace Microsoft.Extensions.AI;
Expand All @@ -29,6 +30,14 @@ public static ChatClientBuilder UseLogging(
return builder.Use((innerClient, services) =>
{
loggerFactory ??= services.GetRequiredService<ILoggerFactory>();

// If the factory we resolve is for the null logger, the LoggingChatClient will end up
// being an expensive nop, so skip adding it and just return the inner client.
if (loggerFactory == NullLoggerFactory.Instance)
{
return innerClient;
}

var chatClient = new LoggingChatClient(innerClient, loggerFactory.CreateLogger(typeof(LoggingChatClient)));
configure?.Invoke(chatClient);
return chatClient;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;
using Microsoft.Shared.Diagnostics;

namespace Microsoft.Extensions.AI;
Expand Down Expand Up @@ -32,6 +33,14 @@ public static EmbeddingGeneratorBuilder<TInput, TEmbedding> UseLogging<TInput, T
return builder.Use((innerGenerator, services) =>
{
loggerFactory ??= services.GetRequiredService<ILoggerFactory>();

// If the factory we resolve is for the null logger, the LoggingEmbeddingGenerator will end up
// being an expensive nop, so skip adding it and just return the inner generator.
if (loggerFactory == NullLoggerFactory.Instance)
{
return innerGenerator;
}

var generator = new LoggingEmbeddingGenerator<TInput, TEmbedding>(innerGenerator, loggerFactory.CreateLogger(typeof(LoggingEmbeddingGenerator<TInput, TEmbedding>)));
configure?.Invoke(generator);
return generator;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ namespace Microsoft.Extensions.AI;

public sealed class TestChatClient : IChatClient
{
public TestChatClient()
{
GetServiceCallback = DefaultGetServiceCallback;
}

public IServiceProvider? Services { get; set; }

public ChatClientMetadata Metadata { get; set; } = new();
Expand All @@ -18,7 +23,10 @@ public sealed class TestChatClient : IChatClient

public Func<IList<ChatMessage>, ChatOptions?, CancellationToken, IAsyncEnumerable<StreamingChatCompletionUpdate>>? CompleteStreamingAsyncCallback { get; set; }

public Func<Type, object?, object?> GetServiceCallback { get; set; } = (_, _) => null;
public Func<Type, object?, object?> GetServiceCallback { get; set; }

private object? DefaultGetServiceCallback(Type serviceType, object? serviceKey) =>
serviceType is not null && serviceKey is null && serviceType.IsInstanceOfType(this) ? this : null;

public Task<ChatCompletion> CompleteAsync(IList<ChatMessage> chatMessages, ChatOptions? options = null, CancellationToken cancellationToken = default)
=> CompleteAsyncCallback!.Invoke(chatMessages, options, cancellationToken);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,19 @@ namespace Microsoft.Extensions.AI;

public sealed class TestEmbeddingGenerator : IEmbeddingGenerator<string, Embedding<float>>
{
public TestEmbeddingGenerator()
{
GetServiceCallback = DefaultGetServiceCallback;
}

public EmbeddingGeneratorMetadata Metadata { get; } = new();

public Func<IEnumerable<string>, EmbeddingGenerationOptions?, CancellationToken, Task<GeneratedEmbeddings<Embedding<float>>>>? GenerateAsyncCallback { get; set; }

public Func<Type, object?, object?> GetServiceCallback { get; set; } = (_, _) => null;
public Func<Type, object?, object?> GetServiceCallback { get; set; }

private object? DefaultGetServiceCallback(Type serviceType, object? serviceKey) =>
serviceType is not null && serviceKey is null && serviceType.IsInstanceOfType(this) ? this : null;

public Task<GeneratedEmbeddings<Embedding<float>>> GenerateAsync(IEnumerable<string> values, EmbeddingGenerationOptions? options = null, CancellationToken cancellationToken = default)
=> GenerateAsyncCallback!.Invoke(values, options, cancellationToken);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,25 @@ public void LoggingChatClient_InvalidArgs_Throws()
Assert.Throws<ArgumentNullException>("logger", () => new LoggingChatClient(new TestChatClient(), null!));
}

[Fact]
public void UseLogging_AvoidsInjectingNopClient()
{
using var innerClient = new TestChatClient();

Assert.Null(innerClient.AsBuilder().UseLogging(NullLoggerFactory.Instance).Build().GetService(typeof(LoggingChatClient)));
Assert.Same(innerClient, innerClient.AsBuilder().UseLogging(NullLoggerFactory.Instance).Build().GetService(typeof(IChatClient)));

using var factory = LoggerFactory.Create(b => b.AddFakeLogging());
Assert.NotNull(innerClient.AsBuilder().UseLogging(factory).Build().GetService(typeof(LoggingChatClient)));

ServiceCollection c = new();
c.AddFakeLogging();
var services = c.BuildServiceProvider();
Assert.NotNull(innerClient.AsBuilder().UseLogging().Build(services).GetService(typeof(LoggingChatClient)));
Assert.NotNull(innerClient.AsBuilder().UseLogging(null).Build(services).GetService(typeof(LoggingChatClient)));
Assert.Null(innerClient.AsBuilder().UseLogging(NullLoggerFactory.Instance).Build(services).GetService(typeof(LoggingChatClient)));
}

[Theory]
[InlineData(LogLevel.Trace)]
[InlineData(LogLevel.Debug)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,25 @@ public void LoggingEmbeddingGenerator_InvalidArgs_Throws()
Assert.Throws<ArgumentNullException>("logger", () => new LoggingEmbeddingGenerator<string, Embedding<float>>(new TestEmbeddingGenerator(), null!));
}

[Fact]
public void UseLogging_AvoidsInjectingNopClient()
{
using var innerGenerator = new TestEmbeddingGenerator();

Assert.Null(innerGenerator.AsBuilder().UseLogging(NullLoggerFactory.Instance).Build().GetService(typeof(LoggingEmbeddingGenerator<string, Embedding<float>>)));
Assert.Same(innerGenerator, innerGenerator.AsBuilder().UseLogging(NullLoggerFactory.Instance).Build().GetService(typeof(IEmbeddingGenerator<string, Embedding<float>>)));

using var factory = LoggerFactory.Create(b => b.AddFakeLogging());
Assert.NotNull(innerGenerator.AsBuilder().UseLogging(factory).Build().GetService(typeof(LoggingEmbeddingGenerator<string, Embedding<float>>)));

ServiceCollection c = new();
c.AddFakeLogging();
var services = c.BuildServiceProvider();
Assert.NotNull(innerGenerator.AsBuilder().UseLogging().Build(services).GetService(typeof(LoggingEmbeddingGenerator<string, Embedding<float>>)));
Assert.NotNull(innerGenerator.AsBuilder().UseLogging(null).Build(services).GetService(typeof(LoggingEmbeddingGenerator<string, Embedding<float>>)));
Assert.Null(innerGenerator.AsBuilder().UseLogging(NullLoggerFactory.Instance).Build(services).GetService(typeof(LoggingEmbeddingGenerator<string, Embedding<float>>)));
}

[Theory]
[InlineData(LogLevel.Trace)]
[InlineData(LogLevel.Debug)]
Expand Down