@@ -152,12 +152,12 @@ internal async Task<ClientResult<ChatCompletion>> CompleteChatAsync(IEnumerable<
152152 }
153153
154154 options ??= new ( ) ;
155- CreateChatCompletionOptions ( messages , ref options ) ;
156- using OpenTelemetryScope scope = _telemetry . StartChatScope ( options ) ;
155+ var clonedOptions = CreateChatCompletionOptions ( messages , options ) ;
156+ using OpenTelemetryScope scope = _telemetry . StartChatScope ( clonedOptions ) ;
157157
158158 try
159159 {
160- using BinaryContent content = options . ToBinaryContent ( ) ;
160+ using BinaryContent content = clonedOptions . ToBinaryContent ( ) ;
161161
162162 ClientResult result = await CompleteChatAsync ( content , requestOptions ) . ConfigureAwait ( false ) ;
163163 ChatCompletion chatCompletion = ( ChatCompletion ) result ;
@@ -182,12 +182,12 @@ public virtual ClientResult<ChatCompletion> CompleteChat(IEnumerable<ChatMessage
182182 Argument . AssertNotNullOrEmpty ( messages , nameof ( messages ) ) ;
183183
184184 options ??= new ( ) ;
185- CreateChatCompletionOptions ( messages , ref options ) ;
186- using OpenTelemetryScope scope = _telemetry . StartChatScope ( options ) ;
185+ var clonedOptions = CreateChatCompletionOptions ( messages , options ) ;
186+ using OpenTelemetryScope scope = _telemetry . StartChatScope ( clonedOptions ) ;
187187
188188 try
189189 {
190- using BinaryContent content = options . ToBinaryContent ( ) ;
190+ using BinaryContent content = clonedOptions . ToBinaryContent ( ) ;
191191 ClientResult result = CompleteChat ( content , cancellationToken . ToRequestOptions ( ) ) ;
192192 ChatCompletion chatCompletion = ( ChatCompletion ) result ;
193193
@@ -243,9 +243,9 @@ internal AsyncCollectionResult<StreamingChatCompletionUpdate> CompleteChatStream
243243 }
244244
245245 options ??= new ( ) ;
246- CreateChatCompletionOptions ( messages , ref options , stream : true ) ;
246+ var clonedOptions = CreateChatCompletionOptions ( messages , options , stream : true ) ;
247247
248- using BinaryContent content = options . ToBinaryContent ( ) ;
248+ using BinaryContent content = clonedOptions . ToBinaryContent ( ) ;
249249 return new AsyncSseUpdateCollection < StreamingChatCompletionUpdate > (
250250 async ( ) => await CompleteChatAsync ( content , requestOptions ) . ConfigureAwait ( false ) ,
251251 StreamingChatCompletionUpdate . DeserializeStreamingChatCompletionUpdate ,
@@ -270,9 +270,9 @@ public virtual CollectionResult<StreamingChatCompletionUpdate> CompleteChatStrea
270270 Argument . AssertNotNull ( messages , nameof ( messages ) ) ;
271271
272272 options ??= new ( ) ;
273- CreateChatCompletionOptions ( messages , ref options , stream : true ) ;
273+ var clonedOptions = CreateChatCompletionOptions ( messages , options , stream : true ) ;
274274
275- using BinaryContent content = options . ToBinaryContent ( ) ;
275+ using BinaryContent content = clonedOptions . ToBinaryContent ( ) ;
276276 return new SseUpdateCollection < StreamingChatCompletionUpdate > (
277277 ( ) => CompleteChat ( content , cancellationToken . ToRequestOptions ( streaming : true ) ) ,
278278 StreamingChatCompletionUpdate . DeserializeStreamingChatCompletionUpdate ,
@@ -381,19 +381,24 @@ public virtual ClientResult<ChatCompletionDeletionResult> DeleteChatCompletion(s
381381 return ClientResult . FromValue ( ( ChatCompletionDeletionResult ) result , result . GetRawResponse ( ) ) ;
382382 }
383383
384- private void CreateChatCompletionOptions ( IEnumerable < ChatMessage > messages , ref ChatCompletionOptions options , bool stream = false )
384+ private ChatCompletionOptions CreateChatCompletionOptions ( IEnumerable < ChatMessage > messages , ChatCompletionOptions options , bool stream = false )
385385 {
386- options . Messages = messages . ToList ( ) ;
387- options . Model = _model ;
386+ var clonedOptions = options . Clone ( ) ;
387+ foreach ( var message in messages )
388+ {
389+ clonedOptions . Messages . Add ( message ) ;
390+ }
391+ clonedOptions . Model ??= _model ;
388392 if ( stream )
389393 {
390- options . Stream = true ;
391- options . StreamOptions = s_includeUsageStreamOptions ;
394+ clonedOptions . Stream = true ;
395+ clonedOptions . StreamOptions = s_includeUsageStreamOptions ;
392396 }
393397 else
394398 {
395- options . Stream = null ;
396- options . StreamOptions = null ;
399+ clonedOptions . Stream = null ;
400+ clonedOptions . StreamOptions = null ;
397401 }
402+ return clonedOptions ;
398403 }
399404}
0 commit comments