Repository: microsoft/semantic-kernel
File: dotnet/src/Connectors/Connectors.Onnx/OnnxServiceCollectionExtensions.DependencyInjection.cs (C#)

Code before:

    /// <summary>
    /// Add OnnxRuntimeGenAI Chat Client to the service collection.
    /// </summary>
    /// <param name="services">The service collection.</param>
    /// <param name="modelId">Model Id.</param>
    /// <param name="modelPath">The generative AI ONNX model path.</param>
    /// <param name="serviceId">The optional service ID.</param>
    /// <returns>The updated service collection.</returns>
    public static IServiceCollection AddOnnxRuntimeGenAIChatClient(
        this IServiceCollection services,
        string modelId,
        string modelPath,
        string? serviceId = null)
    {
        Verify.NotNull(services);
        Verify.NotNullOrWhiteSpace(modelId);
        Verify.NotNullOrWhiteSpace(modelPath);

        IChatClient Factory(IServiceProvider serviceProvider, object? _)
        {
            var loggerFactory = serviceProvider.GetService<ILoggerFactory>();

            var chatClient = new OnnxRuntimeGenAIChatClient(modelPath, new OnnxRuntimeGenAIChatClientOptions()
            {
                PromptFormatter = (messages, options) =>
                {
                    StringBuilder promptBuilder = new();
                    foreach (var message in messages)
                    {
                        promptBuilder.Append($"<|{message.Role}|>\n{message.Text}");
                    }
                    promptBuilder.Append("
\n
");

                    return promptBuilder.ToString();
                }
            });

            var builder = chatClient.AsBuilder().UseKernelFunctionInvocation(loggerFactory);

            if (loggerFactory is not null)
            {
                builder.UseLogging(loggerFactory);
            }

            return builder.Build();
        }

        services.AddKeyedSingleton<IChatClient>(serviceId, (Func<IServiceProvider, object?, IChatClient>)Factory);

        return services;

Review comment (suggestion):
Can this be static?
```suggestion
                PromptFormatter = static (messages, options) =>
```

Code after:

    /// <summary>
    /// Add OnnxRuntimeGenAI Chat Client to the service collection.
    /// </summary>
    /// <param name="services">The service collection.</param>
    /// <param name="modelPath">The generative AI ONNX model path.</param>
    /// <param name="chatClientOptions">The options for the chat client.</param>
    /// <param name="serviceId">The optional service ID.</param>
    /// <returns>The updated service collection.</returns>
    public static IServiceCollection AddOnnxRuntimeGenAIChatClient(
        this IServiceCollection services,
        string modelPath,
        OnnxRuntimeGenAIChatClientOptions? chatClientOptions = null,
        string? serviceId = null)
    {
        Verify.NotNull(services);
        Verify.NotNullOrWhiteSpace(modelPath);

        IChatClient Factory(IServiceProvider serviceProvider, object? _)
        {
            var loggerFactory = serviceProvider.GetService<ILoggerFactory>();

            var chatClient = new OnnxRuntimeGenAIChatClient(modelPath, chatClientOptions ?? new OnnxRuntimeGenAIChatClientOptions()
            {
                PromptFormatter = static (messages, _) =>
                {
                    StringBuilder promptBuilder = new();
                    foreach (var message in messages)
                    {
                        promptBuilder.Append($"<|{message.Role}|>\n{message.Text}");
                    }
                    promptBuilder.Append("
\n
");

                    return promptBuilder.ToString();
                }
            });

            var builder = chatClient.AsBuilder()
                .UseKernelFunctionInvocation(loggerFactory);

            if (loggerFactory is not null)
            {
                builder.UseLogging(loggerFactory);
            }

            return builder.Build();
        }

        services.AddKeyedSingleton<IChatClient>(serviceId, (Func<IServiceProvider, object?, IChatClient>)Factory);

        return services;