-
Notifications
You must be signed in to change notification settings - Fork 199
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[C#] refactor: refactor embeddings to sync with JS sdk (#1227)
## Linked issues closes: #1210 #907 ## Details Provide a list of your changes here. If you are fixing a bug, please provide steps to reproduce the bug. #### Change details - refactor embeddings to sync with the JS part and fix #1210 - add integration tests for embeddings #907 ## Attestation Checklist - [x] My code follows the style guidelines of this project - I have checked for/fixed spelling, linting, and other errors - I have commented my code for clarity - I have made corresponding changes to the documentation (updating the doc strings in the code is sufficient) - My changes generate no new warnings - I have added tests that validates my changes, and provides sufficient test coverage. I have tested with: - Local testing - E2E testing in Teams - New and existing unit tests pass locally with my changes ### Additional information > Feel free to add other relevant information below
- Loading branch information
Showing
9 changed files
with
280 additions
and
92 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
88 changes: 88 additions & 0 deletions
88
...kages/Microsoft.TeamsAI/Microsoft.TeamsAI.Tests/IntegrationTests/OpenAIEmbeddingsTests.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,88 @@ | ||
using Microsoft.Extensions.Configuration; | ||
using Microsoft.Teams.AI.AI.Embeddings; | ||
using Microsoft.Teams.AI.Tests.TestUtils; | ||
using System.Reflection; | ||
using Xunit.Abstractions; | ||
using Microsoft.Extensions.Logging; | ||
|
||
namespace Microsoft.Teams.AI.Tests.IntegrationTests | ||
{ | ||
public sealed class OpenAIEmbeddingsTests | ||
{ | ||
private readonly IConfigurationRoot _configuration; | ||
private readonly RedirectOutput _output; | ||
private readonly ILoggerFactory _loggerFactory; | ||
|
||
public OpenAIEmbeddingsTests(ITestOutputHelper output) | ||
{ | ||
_output = new RedirectOutput(output); | ||
_loggerFactory = new TestLoggerFactory(_output); | ||
|
||
var currentAssemblyDirectory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location); | ||
|
||
if (string.IsNullOrWhiteSpace(currentAssemblyDirectory)) | ||
{ | ||
throw new InvalidOperationException("Unable to determine current assembly directory."); | ||
} | ||
|
||
var directoryPath = Path.GetFullPath(Path.Combine(currentAssemblyDirectory, $"../../../IntegrationTests/")); | ||
var settingsPath = Path.Combine(directoryPath, "testsettings.json"); | ||
|
||
_configuration = new ConfigurationBuilder() | ||
.AddJsonFile(path: settingsPath, optional: false, reloadOnChange: true) | ||
.AddEnvironmentVariables() | ||
.AddUserSecrets<OpenAIEmbeddingsTests>() | ||
.Build(); | ||
} | ||
|
||
[Theory(Skip = "This test should only be run manually.")] | ||
public async Task Test_CreateEmbeddingsAsync_OpenAI() | ||
{ | ||
// Arrange | ||
var config = _configuration.GetSection("OpenAI").Get<OpenAIConfiguration>(); | ||
var options = new OpenAIEmbeddingsOptions(config.ApiKey, config.EmbeddingModelId!); | ||
var embeddings = new OpenAIEmbeddings(options, _loggerFactory); | ||
var inputs = new List<string>() | ||
{ | ||
"test-input1", | ||
"test-input2" | ||
}; | ||
var dimension = config.EmbeddingModelId!.Equals("text-embedding-3-large") ? 3072 : 1536; | ||
|
||
// Act | ||
var result = await embeddings.CreateEmbeddingsAsync(inputs); | ||
|
||
// Assert | ||
Assert.Equal(EmbeddingsResponseStatus.Success, result.Status); | ||
Assert.NotNull(result.Output); | ||
Assert.Equal(2, result.Output.Count); | ||
Assert.Equal(dimension, result.Output[0].Length); | ||
Assert.Equal(dimension, result.Output[1].Length); | ||
} | ||
|
||
[Theory(Skip = "This test should only be run manually.")] | ||
public async Task Test_CreateEmbeddingsAsync_AzureOpenAI() | ||
{ | ||
// Arrange | ||
var config = _configuration.GetSection("AzureOpenAI").Get<AzureOpenAIConfiguration>(); | ||
var options = new AzureOpenAIEmbeddingsOptions(config.ApiKey, config.EmbeddingModelId!, config.Endpoint); | ||
var embeddings = new OpenAIEmbeddings(options, _loggerFactory); | ||
var inputs = new List<string>() | ||
{ | ||
"test-input1", | ||
"test-input2" | ||
}; | ||
var dimension = config.EmbeddingModelId!.Equals("text-embedding-3-large") ? 3072 : 1536; | ||
|
||
// Act | ||
var result = await embeddings.CreateEmbeddingsAsync(inputs); | ||
|
||
// Assert | ||
Assert.Equal(EmbeddingsResponseStatus.Success, result.Status); | ||
Assert.NotNull(result.Output); | ||
Assert.Equal(2, result.Output.Count); | ||
Assert.Equal(dimension, result.Output[0].Length); | ||
Assert.Equal(dimension, result.Output[1].Length); | ||
} | ||
} | ||
} |
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
26 changes: 26 additions & 0 deletions
26
...packages/Microsoft.TeamsAI/Microsoft.TeamsAI/AI/Embeddings/BaseOpenAIEmbeddingsOptions.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,26 @@ | ||
namespace Microsoft.Teams.AI.AI.Embeddings | ||
{ | ||
/// <summary> | ||
/// Base embeddings options common to both OpenAI and Azure OpenAI services. | ||
/// </summary> | ||
public class BaseOpenAIEmbeddingsOptions | ||
{ | ||
/// <summary> | ||
/// Optional. Whether to log requests to the console. | ||
/// </summary> | ||
/// <remarks> | ||
/// This is useful for debugging prompts. | ||
/// The default value is `false`. | ||
/// </remarks> | ||
public bool? LogRequests { get; set; } | ||
|
||
/// <summary> | ||
/// Optional. Retry policy to use when calling the OpenAI API. | ||
/// </summary> | ||
/// <remarks> | ||
/// The default retry policy is `{ TimeSpan.FromMilliseconds(2000), TimeSpan.FromMilliseconds(5000) }` | ||
/// which means that the first retry will be after 2 seconds and the second retry will be after 5 seconds. | ||
/// </remarks> | ||
public List<TimeSpan>? RetryPolicy { get; set; } | ||
} | ||
} |
10 changes: 4 additions & 6 deletions
10
dotnet/packages/Microsoft.TeamsAI/Microsoft.TeamsAI/AI/Embeddings/IEmbeddings.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
Oops, something went wrong.