// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Shared.Diagnostics;
namespace Microsoft.Extensions.AI;
/// Provides extensions for configuring instances.
public static class OpenTelemetryEmbeddingGeneratorBuilderExtensions
{
///
/// Adds OpenTelemetry support to the embedding generator pipeline, following the OpenTelemetry Semantic Conventions for Generative AI systems.
///
///
/// The draft specification this follows is available at .
/// The specification is still experimental and subject to change; as such, the telemetry output by this generator is also subject to change.
///
/// The type of input used to produce embeddings.
/// The type of embedding generated.
/// The .
/// An optional to use to create a logger for logging events.
/// An optional source name that will be used on the telemetry data.
/// An optional callback that can be used to configure the instance.
/// The .
public static EmbeddingGeneratorBuilder UseOpenTelemetry(
this EmbeddingGeneratorBuilder builder,
ILoggerFactory? loggerFactory = null,
string? sourceName = null,
Action>? configure = null)
where TEmbedding : Embedding =>
Throw.IfNull(builder).Use((innerGenerator, services) =>
{
loggerFactory ??= services.GetService();
var generator = new OpenTelemetryEmbeddingGenerator(
innerGenerator,
loggerFactory?.CreateLogger(typeof(OpenTelemetryEmbeddingGenerator)),
sourceName);
configure?.Invoke(generator);
return generator;
});
}