-
Notifications
You must be signed in to change notification settings - Fork 779
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d2efdba
commit 4f3ab61
Showing
5 changed files
with
114 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
91 changes: 91 additions & 0 deletions
91
...ibraries/Microsoft.Extensions.AI.Tests/Embeddings/OpenTelemetryEmbeddingGeneratorTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
// 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 System.Collections.Generic; | ||
using System.Diagnostics; | ||
using System.Runtime.CompilerServices; | ||
using System.Threading.Tasks; | ||
using Microsoft.Extensions.Logging; | ||
using Microsoft.Extensions.Logging.Testing; | ||
using OpenTelemetry.Trace; | ||
using Xunit; | ||
|
||
namespace Microsoft.Extensions.AI; | ||
|
||
public class OpenTelemetryEmbeddingGeneratorTests | ||
{ | ||
[Fact] | ||
public async Task ExpectedInformationLogged_Async() | ||
{ | ||
var sourceName = Guid.NewGuid().ToString(); | ||
var activities = new List<Activity>(); | ||
using var tracerProvider = OpenTelemetry.Sdk.CreateTracerProviderBuilder() | ||
.AddSource(sourceName) | ||
.AddInMemoryExporter(activities) | ||
.Build(); | ||
|
||
var collector = new FakeLogCollector(); | ||
using ILoggerFactory loggerFactory = LoggerFactory.Create(b => b.AddProvider(new FakeLoggerProvider(collector))); | ||
|
||
using var innerGenerator = new TestEmbeddingGenerator | ||
{ | ||
Metadata = new("testservice", new Uri("http://localhost:12345/something"), "amazingmodel", 384), | ||
GenerateAsyncCallback = async (values, options, cancellationToken) => | ||
{ | ||
await Task.Yield(); | ||
return new GeneratedEmbeddings<Embedding<float>>([new Embedding<float>(new float[] { 1, 2, 3 })]) | ||
{ | ||
Usage = new() | ||
{ | ||
InputTokenCount = 10, | ||
TotalTokenCount = 10, | ||
}, | ||
AdditionalProperties = new() | ||
{ | ||
["system_fingerprint"] = "abcdefgh", | ||
["AndSomethingElse"] = "value2", | ||
} | ||
}; | ||
}, | ||
}; | ||
|
||
using var generator = innerGenerator | ||
.AsBuilder() | ||
.UseOpenTelemetry(loggerFactory, sourceName) | ||
.Build(); | ||
|
||
var options = new EmbeddingGenerationOptions | ||
{ | ||
ModelId = "replacementmodel", | ||
AdditionalProperties = new() | ||
{ | ||
["service_tier"] = "value1", | ||
["SomethingElse"] = "value2", | ||
}, | ||
}; | ||
|
||
await generator.GenerateEmbeddingVectorAsync("hello", options); | ||
|
||
var activity = Assert.Single(activities); | ||
|
||
Assert.NotNull(activity.Id); | ||
Assert.NotEmpty(activity.Id); | ||
|
||
Assert.Equal("http://localhost:12345/something", activity.GetTagItem("server.address")); | ||
Assert.Equal(12345, (int)activity.GetTagItem("server.port")!); | ||
|
||
Assert.Equal("embeddings replacementmodel", activity.DisplayName); | ||
Assert.Equal("testservice", activity.GetTagItem("gen_ai.system")); | ||
|
||
Assert.Equal("replacementmodel", activity.GetTagItem("gen_ai.request.model")); | ||
Assert.Equal("value1", activity.GetTagItem("gen_ai.testservice.request.service_tier")); | ||
Assert.Equal("value2", activity.GetTagItem("gen_ai.testservice.request.something_else")); | ||
|
||
Assert.Equal(10, activity.GetTagItem("gen_ai.response.input_tokens")); | ||
Assert.Equal("abcdefgh", activity.GetTagItem("gen_ai.testservice.response.system_fingerprint")); | ||
Assert.Equal("value2", activity.GetTagItem("gen_ai.testservice.response.and_something_else")); | ||
|
||
Assert.True(activity.Duration.TotalMilliseconds > 0); | ||
} | ||
} |