22// The .NET Foundation licenses this file to you under the MIT license.
33
44using System ;
5+ using System . Threading . Tasks ;
56using Azure ;
67using Azure . AI . Inference ;
8+ using Azure . Core ;
9+ using Azure . Core . Pipeline ;
710
811namespace Microsoft . Extensions . AI ;
912
@@ -21,12 +24,49 @@ internal static class IntegrationTestHelpers
2124 /// <summary>Gets a <see cref="ChatResponsesClient"/> to use for testing, or null if the associated tests should be disabled.</summary>
2225 public static ChatCompletionsClient ? GetChatCompletionsClient ( ) =>
2326 _apiKey is string apiKey ?
24- new ChatCompletionsClient ( new Uri ( _endpoint ) , new AzureKeyCredential ( apiKey ) ) :
27+ new ChatCompletionsClient ( new Uri ( _endpoint ) , new AzureKeyCredential ( apiKey ) , CreateOptions ( ) ) :
2528 null ;
2629
2730 /// <summary>Gets an <see cref="EmbeddingsClient"/> to use for testing, or null if the associated tests should be disabled.</summary>
2831 public static EmbeddingsClient ? GetEmbeddingsClient ( ) =>
2932 _apiKey is string apiKey ?
30- new EmbeddingsClient ( new Uri ( _endpoint ) , new AzureKeyCredential ( apiKey ) ) :
33+ new EmbeddingsClient ( new Uri ( _endpoint ) , new AzureKeyCredential ( apiKey ) , CreateOptions ( ) ) :
3134 null ;
35+
36+ private static AzureAIInferenceClientOptions CreateOptions ( )
37+ {
38+ var result = new AzureAIInferenceClientOptions ( ) ;
39+
40+ // The API vesion set here corresponds to the value used by AzureOpenAIClientOptions
41+ // if the AZURE_OPENAI_GA flag is set during its compilation. This API version is the
42+ // minimum required for structured output with JSON schema.
43+ result . AddPolicy ( new OverrideApiVersionPolicy ( "2024-08-01-preview" ) , HttpPipelinePosition . PerCall ) ;
44+
45+ return result ;
46+ }
47+
48+ // From https://github.com/Azure/azure-sdk-for-net/issues/48405#issuecomment-2704360548
49+ private class OverrideApiVersionPolicy : HttpPipelinePolicy
50+ {
51+ private string ApiVersion { get ; }
52+
53+ public OverrideApiVersionPolicy ( string apiVersion )
54+ {
55+ ApiVersion = apiVersion ;
56+ }
57+
58+ public override void Process ( HttpMessage message , ReadOnlyMemory < HttpPipelinePolicy > pipeline )
59+ {
60+ message . Request . Uri . Query = $ "?api-version={ ApiVersion } ";
61+ ProcessNext ( message , pipeline ) ;
62+ }
63+
64+ public override ValueTask ProcessAsync ( HttpMessage message , ReadOnlyMemory < HttpPipelinePolicy > pipeline )
65+ {
66+ message . Request . Uri . Query = $ "?api-version={ ApiVersion } ";
67+ var task = ProcessNextAsync ( message , pipeline ) ;
68+
69+ return task ;
70+ }
71+ }
3272}
0 commit comments