11using System ;
22using System . ClientModel ;
3+ using System . ClientModel . Primitives ;
34using System . Collections . Generic ;
45using System . Text . Json . Serialization ;
6+
57using Azure . AI . OpenAI ;
68using CommunityToolkit . Mvvm . ComponentModel ;
79using OpenAI ;
@@ -66,6 +68,12 @@ public string AdditionalPrompt
6668 set ;
6769 } = string . Empty ;
6870
71+ public string ExtraHeaders
72+ {
73+ get ;
74+ set ;
75+ } = string . Empty ;
76+
6977 public void FetchAvailableModels ( )
7078 {
7179 if ( ! _autoFetchAvailableModels )
@@ -92,9 +100,21 @@ public ChatClient GetChatClient()
92100 private OpenAIClient GetOpenAIClient ( )
93101 {
94102 var credential = new ApiKeyCredential ( ReadApiKeyFromEnv ? Environment . GetEnvironmentVariable ( ApiKey ) : ApiKey ) ;
95- return Server . Contains ( "openai.azure.com/" , StringComparison . Ordinal )
96- ? new AzureOpenAIClient ( new Uri ( Server ) , credential )
97- : new OpenAIClient ( credential , new ( ) { Endpoint = new Uri ( Server ) } ) ;
103+
104+ if ( Server . Contains ( "openai.azure.com/" , StringComparison . Ordinal ) )
105+ {
106+ if ( string . IsNullOrEmpty ( ExtraHeaders ) )
107+ return new AzureOpenAIClient ( new Uri ( Server ) , credential ) ;
108+
109+ var azureOptions = new AzureOpenAIClientOptions ( ) ;
110+ azureOptions . AddPolicy ( new ExtraHeadersPolicy ( ExtraHeaders ) , PipelinePosition . PerCall ) ;
111+ return new AzureOpenAIClient ( new Uri ( Server ) , credential , azureOptions ) ;
112+ }
113+
114+ var options = new OpenAIClientOptions ( ) { Endpoint = new Uri ( Server ) } ;
115+ if ( ! string . IsNullOrEmpty ( ExtraHeaders ) )
116+ options . AddPolicy ( new ExtraHeadersPolicy ( ExtraHeaders ) , PipelinePosition . PerCall ) ;
117+ return new OpenAIClient ( credential , options ) ;
98118 }
99119
100120 private string _name = string . Empty ;
0 commit comments