Skip to content

Commit 6271caa

Browse files
authored
Azure OpenAI test management update (Azure#33821)
* Initial update * More progress; token? * Token-based recordings * PR feedback and concurrency fix * Reverting opportunistic changes to client to proactively address merge conflicts with !33834 * PR feedback: concurrency/deployment initialization optimization * Merged files that weren't merged -- oops!
1 parent f2604f0 commit 6271caa

15 files changed

+493
-242
lines changed

sdk/openai/Azure.AI.OpenAI/tests/OpenAIInferenceTests.cs

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,18 +38,35 @@ public async Task CompletionTest()
3838
completionsRequest.Prompt.Add("Hello world");
3939
completionsRequest.Prompt.Add("running over the same old ground");
4040
Assert.That(completionsRequest, Is.InstanceOf<CompletionsOptions>());
41-
var response = await client.GetCompletionsAsync(DeploymentId, completionsRequest);
41+
var response = await client.GetCompletionsAsync(CompletionsDeploymentId, completionsRequest);
4242
Assert.That(response, Is.InstanceOf<Response<Completions>>());
4343
}
4444

45+
/// <summary>
46+
/// Test Completions using a TokenCredential.
47+
/// </summary>
48+
[RecordedTest]
49+
public async Task CompletionTestWithTokenCredential()
50+
{
51+
OpenAIClient client = GetClientWithCredential();
52+
CompletionsOptions requestOptions = new CompletionsOptions();
53+
requestOptions.Prompt.Add("Hello, world!");
54+
requestOptions.Prompt.Add("I can have multiple prompts");
55+
Assert.That(requestOptions, Is.InstanceOf<CompletionsOptions>());
56+
Response<Completions> response = await client.GetCompletionsAsync(CompletionsDeploymentId, requestOptions);
57+
Assert.That(response, Is.InstanceOf<Response<Completions>>());
58+
Assert.That(response.Value.Choices, Is.Not.Null.Or.Empty);
59+
Assert.That(response.Value.Choices.Count, Is.EqualTo(2));
60+
}
61+
4562
/// <summary>
4663
/// Test Simplified Completion API.
4764
/// </summary>
4865
[RecordedTest]
4966
public async Task SimpleCompletionTest()
5067
{
51-
var client = GetClientWithCompletionsDeploymentId();
52-
var response = await client.GetCompletionsAsync(DeploymentId, "Hello World!");
68+
var client = GetClientWithCredential();
69+
var response = await client.GetCompletionsAsync(CompletionsDeploymentId, "Hello World!");
5370
Assert.That(response, Is.InstanceOf<Response<Completions>>());
5471
}
5572

@@ -76,7 +93,7 @@ public void RequestFailedExceptionTest()
7693
CompletionsOptions completionsRequest = new CompletionsOptions();
7794
completionsRequest.Prompt.Add("Hello world");
7895
var exception = Assert.ThrowsAsync<RequestFailedException>(async () => { await client.GetCompletionsAsync("BAD_DEPLOYMENT_ID", completionsRequest); });
79-
Assert.AreEqual(401, exception.Status);
96+
Assert.AreEqual(404, exception.Status);
8097
}
8198
}
8299
}

sdk/openai/Azure.AI.OpenAI/tests/OpenAITestBase.cs

Lines changed: 149 additions & 99 deletions
Large diffs are not rendered by default.
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,35 @@
11
// Copyright (c) Microsoft Corporation. All rights reserved.
22
// Licensed under the MIT License.
33

4+
using System;
5+
using System.Linq;
46
using Azure.Core.TestFramework;
57

68
namespace Azure.AI.OpenAI.Tests
79
{
810
public class OpenAITestEnvironment : TestEnvironment
911
{
12+
public void ThrowIfCannotDeploy()
13+
{
14+
string[] requiredVariableNames = new string[]
15+
{
16+
"TENANT_ID",
17+
"AUTHORITY_HOST",
18+
"SUBSCRIPTION_ID",
19+
"CLIENT_ID",
20+
"CLIENT_SECRET",
21+
};
22+
23+
foreach (string requiredVariableName in requiredVariableNames)
24+
{
25+
string optionalVariableValue = GetOptionalVariable(requiredVariableName);
26+
if (string.IsNullOrEmpty(optionalVariableValue))
27+
{
28+
throw new InvalidOperationException($"Resource deployment is required and no environment value was found for required variable '{requiredVariableName}'.\n"
29+
+ $"'{Mode}' mode requires full environment specification of an Azure application (service principal) "
30+
+ "that manages test resources. This includes: Azure tenant ID; Azure authority host; managed subscription ID; and application ID + secret.");
31+
}
32+
}
33+
}
1034
}
1135
}

sdk/openai/Azure.AI.OpenAI/tests/SessionRecords/OpenAIInferenceTests/CompletionTest.json

Lines changed: 20 additions & 20 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

sdk/openai/Azure.AI.OpenAI/tests/SessionRecords/OpenAIInferenceTests/CompletionTestAsync.json

Lines changed: 18 additions & 18 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

sdk/openai/Azure.AI.OpenAI/tests/SessionRecords/OpenAIInferenceTests/CompletionTestWithTokenCredential.json

Lines changed: 74 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)