@@ -44,7 +44,7 @@ protected async Task<string> InternalCompleteTextAsync(
4444 Verify . NotNull ( requestSettings ) ;
4545
4646 ValidateMaxTokens ( requestSettings . MaxTokens ) ;
47- var options = this . CreateCompletionsOptions ( text , requestSettings ) ;
47+ var options = CreateCompletionsOptions ( text , requestSettings ) ;
4848
4949 Response < Completions > ? response = await RunRequestAsync < Response < Completions > ? > (
5050 ( ) => this . Client . GetCompletionsAsync ( this . ModelId , options , cancellationToken ) ) . ConfigureAwait ( false ) ;
@@ -72,7 +72,7 @@ protected async IAsyncEnumerable<string> InternalCompletionStreamAsync(
7272 Verify . NotNull ( requestSettings ) ;
7373
7474 ValidateMaxTokens ( requestSettings . MaxTokens ) ;
75- var options = this . CreateCompletionsOptions ( text , requestSettings ) ;
75+ var options = CreateCompletionsOptions ( text , requestSettings ) ;
7676
7777 Response < StreamingCompletions > ? response = await RunRequestAsync < Response < StreamingCompletions > > (
7878 ( ) => this . Client . GetCompletionsStreamingAsync ( this . ModelId , options , cancellationToken ) ) . ConfigureAwait ( false ) ;
@@ -135,31 +135,35 @@ protected async Task<string> InternalGenerateChatMessageAsync(
135135 Verify . NotNull ( chat ) ;
136136 Verify . NotNull ( requestSettings ) ;
137137
138- if ( requestSettings . MaxTokens < 1 )
139- {
140- throw new AIException (
141- AIException . ErrorCodes . InvalidRequest ,
142- $ "MaxTokens { requestSettings . MaxTokens } is not valid, the value must be greater than zero") ;
143- }
138+ ValidateMaxTokens ( requestSettings . MaxTokens ) ;
139+ var options = CreateChatCompletionsOptions ( requestSettings , chat ) ;
144140
145- var options = new ChatCompletionsOptions
146- {
147- MaxTokens = requestSettings . MaxTokens ,
148- Temperature = ( float ? ) requestSettings . Temperature ,
149- NucleusSamplingFactor = ( float ? ) requestSettings . TopP ,
150- FrequencyPenalty = ( float ? ) requestSettings . FrequencyPenalty ,
151- PresencePenalty = ( float ? ) requestSettings . PresencePenalty ,
152- ChoicesPerPrompt = 1 ,
153- } ;
141+ Response < ChatCompletions > ? response = await RunRequestAsync < Response < ChatCompletions > ? > (
142+ ( ) => this . Client . GetChatCompletionsAsync ( this . ModelId , options , cancellationToken ) ) . ConfigureAwait ( false ) ;
154143
155- if ( requestSettings . StopSequences is { Count : > 0 } )
144+ if ( response == null || response . Value . Choices . Count < 1 )
156145 {
157- foreach ( var s in requestSettings . StopSequences )
158- {
159- options . StopSequences . Add ( s ) ;
160- }
146+ throw new AIException ( AIException . ErrorCodes . InvalidResponseContent , "Chat completions not found" ) ;
161147 }
162148
149+ return response . Value . Choices [ 0 ] . Message . Content ;
150+ }
151+
152+ /// <summary>
153+ /// Generate a new chat message stream
154+ /// </summary>
155+ /// <param name="chat">Chat history</param>
156+ /// <param name="requestSettings">AI request settings</param>
157+ /// <param name="cancellationToken">Async cancellation token</param>
158+ /// <returns>Streaming of generated chat message in string format</returns>
159+ protected async IAsyncEnumerable < string > InternalGenerateChatMessageStreamAsync (
160+ ChatHistory chat ,
161+ ChatRequestSettings requestSettings ,
162+ [ EnumeratorCancellation ] CancellationToken cancellationToken = default )
163+ {
164+ Verify . NotNull ( chat ) ;
165+ Verify . NotNull ( requestSettings ) ;
166+
163167 foreach ( ChatHistory . Message message in chat . Messages )
164168 {
165169 var role = message . AuthorRole switch
@@ -169,27 +173,38 @@ protected async Task<string> InternalGenerateChatMessageAsync(
169173 ChatHistory . AuthorRoles . System => ChatRole . System ,
170174 _ => throw new ArgumentException ( $ "Invalid chat message author: { message . AuthorRole : G} ")
171175 } ;
172-
173- options . Messages . Add ( new ChatMessage ( role , message . Content ) ) ;
174176 }
175177
176- Response < ChatCompletions > ? response = await RunRequestAsync < Response < ChatCompletions > ? > (
177- ( ) => this . Client . GetChatCompletionsAsync ( this . ModelId , options , cancellationToken ) ) . ConfigureAwait ( false ) ;
178+ ValidateMaxTokens ( requestSettings . MaxTokens ) ;
179+ var options = CreateChatCompletionsOptions ( requestSettings , chat ) ;
178180
179- if ( response == null || response . Value . Choices . Count < 1 )
181+ Response < StreamingChatCompletions > ? response = await RunRequestAsync < Response < StreamingChatCompletions > > (
182+ ( ) => this . Client . GetChatCompletionsStreamingAsync ( this . ModelId , options , cancellationToken ) ) . ConfigureAwait ( false ) ;
183+
184+ using StreamingChatCompletions streamingChatCompletions = response . Value ;
185+
186+ if ( response is null )
180187 {
181188 throw new AIException ( AIException . ErrorCodes . InvalidResponseContent , "Chat completions not found" ) ;
182189 }
183190
184- return response . Value . Choices [ 0 ] . Message . Content ;
191+ await foreach ( StreamingChatChoice choice in streamingChatCompletions . GetChoicesStreaming ( cancellationToken ) )
192+ {
193+ await foreach ( ChatMessage message in choice . GetMessageStreaming ( cancellationToken ) )
194+ {
195+ yield return message . Content ;
196+ }
197+
198+ yield return Environment . NewLine ;
199+ }
185200 }
186201
187202 /// <summary>
188203 /// Create a new empty chat instance
189204 /// </summary>
190205 /// <param name="instructions">Optional chat instructions for the AI service</param>
191206 /// <returns>Chat object</returns>
192- protected ChatHistory InternalCreateNewChat ( string instructions = "" )
207+ protected static ChatHistory InternalCreateNewChat ( string instructions = "" )
193208 {
194209 return new OpenAIChatHistory ( instructions ) ;
195210 }
@@ -206,9 +221,26 @@ protected async Task<string> InternalCompleteTextUsingChatAsync(
206221 CompleteRequestSettings requestSettings ,
207222 CancellationToken cancellationToken = default )
208223 {
209- var chat = this . InternalCreateNewChat ( ) ;
224+ ChatHistory chat = PrepareChatHistory ( text , requestSettings , out ChatRequestSettings settings ) ;
225+
226+ return await this . InternalGenerateChatMessageAsync ( chat , settings , cancellationToken ) . ConfigureAwait ( false ) ;
227+ }
228+
229+ protected IAsyncEnumerable < string > InternalCompleteTextUsingChatStreamAsync (
230+ string text ,
231+ CompleteRequestSettings requestSettings ,
232+ CancellationToken cancellationToken = default )
233+ {
234+ ChatHistory chat = PrepareChatHistory ( text , requestSettings , out ChatRequestSettings settings ) ;
235+
236+ return this . InternalGenerateChatMessageStreamAsync ( chat , settings , cancellationToken ) ;
237+ }
238+
239+ private static ChatHistory PrepareChatHistory ( string text , CompleteRequestSettings requestSettings , out ChatRequestSettings settings )
240+ {
241+ var chat = InternalCreateNewChat ( ) ;
210242 chat . AddMessage ( ChatHistory . AuthorRoles . User , text ) ;
211- var settings = new ChatRequestSettings
243+ settings = new ChatRequestSettings
212244 {
213245 MaxTokens = requestSettings . MaxTokens ,
214246 Temperature = requestSettings . Temperature ,
@@ -217,11 +249,10 @@ protected async Task<string> InternalCompleteTextUsingChatAsync(
217249 FrequencyPenalty = requestSettings . FrequencyPenalty ,
218250 StopSequences = requestSettings . StopSequences ,
219251 } ;
220-
221- return await this . InternalGenerateChatMessageAsync ( chat , settings , cancellationToken ) . ConfigureAwait ( false ) ;
252+ return chat ;
222253 }
223254
224- private CompletionsOptions CreateCompletionsOptions ( string text , CompleteRequestSettings requestSettings )
255+ private static CompletionsOptions CreateCompletionsOptions ( string text , CompleteRequestSettings requestSettings )
225256 {
226257 var options = new CompletionsOptions
227258 {
@@ -249,6 +280,42 @@ private CompletionsOptions CreateCompletionsOptions(string text, CompleteRequest
249280 return options ;
250281 }
251282
283+ private static ChatCompletionsOptions CreateChatCompletionsOptions ( ChatRequestSettings requestSettings , ChatHistory chat )
284+ {
285+ var options = new ChatCompletionsOptions
286+ {
287+ MaxTokens = requestSettings . MaxTokens ,
288+ Temperature = ( float ? ) requestSettings . Temperature ,
289+ NucleusSamplingFactor = ( float ? ) requestSettings . TopP ,
290+ FrequencyPenalty = ( float ? ) requestSettings . FrequencyPenalty ,
291+ PresencePenalty = ( float ? ) requestSettings . PresencePenalty ,
292+ ChoicesPerPrompt = 1 ,
293+ } ;
294+
295+ if ( requestSettings . StopSequences is { Count : > 0 } )
296+ {
297+ foreach ( var s in requestSettings . StopSequences )
298+ {
299+ options . StopSequences . Add ( s ) ;
300+ }
301+ }
302+
303+ foreach ( ChatHistory . Message message in chat . Messages )
304+ {
305+ var role = message . AuthorRole switch
306+ {
307+ ChatHistory . AuthorRoles . User => ChatRole . User ,
308+ ChatHistory . AuthorRoles . Assistant => ChatRole . Assistant ,
309+ ChatHistory . AuthorRoles . System => ChatRole . System ,
310+ _ => throw new ArgumentException ( $ "Invalid chat message author: { message . AuthorRole : G} ")
311+ } ;
312+
313+ options . Messages . Add ( new ChatMessage ( role , message . Content ) ) ;
314+ }
315+
316+ return options ;
317+ }
318+
252319 private static void ValidateMaxTokens ( int maxTokens )
253320 {
254321 if ( maxTokens < 1 )
0 commit comments