@@ -840,7 +840,26 @@ def chat_completion_with_tools(
840840 # Process as regular non-streaming response
841841 if display_fn and console :
842842 # Show display_generating animation for non-streaming mode when display_fn is provided
843- with Live (display_fn ("" , start_time ), console = console , refresh_per_second = 4 ) as live :
843+ try :
844+ with Live (display_fn ("" , start_time ), console = console , refresh_per_second = 4 ) as live :
845+ final_response = self .create_completion (
846+ messages = messages ,
847+ model = model ,
848+ temperature = temperature ,
849+ tools = formatted_tools ,
850+ stream = False ,
851+ ** kwargs
852+ )
853+ # Update display with final content if available
854+ if final_response and final_response .choices and len (final_response .choices ) > 0 :
855+ content = final_response .choices [0 ].message .content or ""
856+ live .update (display_fn (content , start_time ))
857+
858+ # Clear the last generating display with a blank line
859+ console .print ()
860+ except Exception as e :
861+ self .logger .error (f"Error in Live display for non-streaming: { e } " )
862+ # Fallback to regular completion without display
844863 final_response = self .create_completion (
845864 messages = messages ,
846865 model = model ,
@@ -849,10 +868,6 @@ def chat_completion_with_tools(
849868 stream = False ,
850869 ** kwargs
851870 )
852- # Update display with empty content as we don't have streaming chunks
853- if final_response and final_response .choices :
854- content = final_response .choices [0 ].message .content or ""
855- live .update (display_fn (content , start_time ))
856871 else :
857872 final_response = self .create_completion (
858873 messages = messages ,
@@ -985,14 +1000,45 @@ async def achat_completion_with_tools(
9851000 )
9861001 else :
9871002 # Process as regular non-streaming response
988- final_response = await self .acreate_completion (
989- messages = messages ,
990- model = model ,
991- temperature = temperature ,
992- tools = formatted_tools ,
993- stream = False ,
994- ** kwargs
995- )
1003+ if display_fn and console :
1004+ # Show display_generating animation for non-streaming mode when display_fn is provided
1005+ try :
1006+ with Live (display_fn ("" , start_time ), console = console , refresh_per_second = 4 ) as live :
1007+ final_response = await self .acreate_completion (
1008+ messages = messages ,
1009+ model = model ,
1010+ temperature = temperature ,
1011+ tools = formatted_tools ,
1012+ stream = False ,
1013+ ** kwargs
1014+ )
1015+ # Update display with final content if available
1016+ if final_response and final_response .choices and len (final_response .choices ) > 0 :
1017+ content = final_response .choices [0 ].message .content or ""
1018+ live .update (display_fn (content , start_time ))
1019+
1020+ # Clear the last generating display with a blank line
1021+ console .print ()
1022+ except Exception as e :
1023+ self .logger .error (f"Error in Live display for async non-streaming: { e } " )
1024+ # Fallback to regular completion without display
1025+ final_response = await self .acreate_completion (
1026+ messages = messages ,
1027+ model = model ,
1028+ temperature = temperature ,
1029+ tools = formatted_tools ,
1030+ stream = False ,
1031+ ** kwargs
1032+ )
1033+ else :
1034+ final_response = await self .acreate_completion (
1035+ messages = messages ,
1036+ model = model ,
1037+ temperature = temperature ,
1038+ tools = formatted_tools ,
1039+ stream = False ,
1040+ ** kwargs
1041+ )
9961042
9971043 if not final_response :
9981044 return None
0 commit comments