@@ -367,8 +367,42 @@ public async Task<RoleDialogModel> GetChatCompletionsStreamingAsync(Agent agent,
367367 // Prepare instruction and functions
368368 var renderData = agentService . CollectRenderData ( agent ) ;
369369 var ( instruction , functions ) = agentService . PrepareInstructionAndFunctions ( agent , renderData ) ;
370+ if ( ! string . IsNullOrWhiteSpace ( instruction ) )
371+ {
372+ renderedInstructions . Add ( instruction ) ;
373+ messages . Add ( new SystemChatMessage ( instruction ) ) ;
374+ }
375+
376+ // Render functions
377+ if ( options . WebSearchOptions == null )
378+ {
379+ foreach ( var function in functions )
380+ {
381+ if ( ! agentService . RenderFunction ( agent , function , renderData ) )
382+ {
383+ continue ;
384+ }
385+
386+ var property = agentService . RenderFunctionProperty ( agent , function , renderData ) ;
387+
388+ options . Tools . Add ( ChatTool . CreateFunctionTool (
389+ functionName : function . Name ,
390+ functionDescription : function . Description ,
391+ functionParameters : BinaryData . FromObjectAsJson ( property ) ) ) ;
392+ }
393+ }
394+
395+ if ( ! string . IsNullOrEmpty ( agent . Knowledges ) )
396+ {
397+ messages . Add ( new SystemChatMessage ( agent . Knowledges ) ) ;
398+ }
399+
400+ var samples = ProviderHelper . GetChatSamples ( agent . Samples ) ;
401+ foreach ( var sample in samples )
402+ {
403+ messages . Add ( sample . Role == AgentRole . User ? new UserChatMessage ( sample . Content ) : new AssistantChatMessage ( sample . Content ) ) ;
404+ }
370405
371- // Build messages
372406 var filteredMessages = conversations . Select ( x => x ) . ToList ( ) ;
373407 var firstUserMsgIdx = filteredMessages . FindIndex ( x => x . Role == AgentRole . User ) ;
374408 if ( firstUserMsgIdx > 0 )
@@ -419,43 +453,6 @@ public async Task<RoleDialogModel> GetChatCompletionsStreamingAsync(Agent agent,
419453 }
420454 }
421455
422- // Build system messages
423- if ( ! string . IsNullOrWhiteSpace ( instruction ) )
424- {
425- renderedInstructions . Add ( instruction ) ;
426- messages . Add ( new SystemChatMessage ( instruction ) ) ;
427- }
428-
429- if ( ! string . IsNullOrEmpty ( agent . Knowledges ) )
430- {
431- messages . Add ( new SystemChatMessage ( agent . Knowledges ) ) ;
432- }
433-
434- var samples = ProviderHelper . GetChatSamples ( agent . Samples ) ;
435- foreach ( var sample in samples )
436- {
437- messages . Add ( sample . Role == AgentRole . User ? new UserChatMessage ( sample . Content ) : new AssistantChatMessage ( sample . Content ) ) ;
438- }
439-
440- // Render functions
441- if ( options . WebSearchOptions == null )
442- {
443- foreach ( var function in functions )
444- {
445- if ( ! agentService . RenderFunction ( agent , function , renderData ) )
446- {
447- continue ;
448- }
449-
450- var property = agentService . RenderFunctionProperty ( agent , function , renderData ) ;
451-
452- options . Tools . Add ( ChatTool . CreateFunctionTool (
453- functionName : function . Name ,
454- functionDescription : function . Description ,
455- functionParameters : BinaryData . FromObjectAsJson ( property ) ) ) ;
456- }
457- }
458-
459456 var prompt = GetPrompt ( messages , options ) ;
460457 return ( prompt , messages , options ) ;
461458 }
0 commit comments