Refactor streaming response handling to support print_on parameter for flexible output

pull/927/head
harshalmore31 5 days ago
parent 5e5819fc48
commit 92a9eac8d8

@ -6,6 +6,7 @@ agent = Agent(
model_name="gpt-4o-mini", model_name="gpt-4o-mini",
streaming_on=True, # 🔥 This enables real streaming! streaming_on=True, # 🔥 This enables real streaming!
max_loops=1, max_loops=1,
print_on=True, # By Default its False, raw streaming !!
) )
# This will now stream in real-time with beautiful UI! # This will now stream in real-time with beautiful UI!

@ -2488,6 +2488,19 @@ class Agent:
# If we get a streaming response, handle it with the new streaming panel # If we get a streaming response, handle it with the new streaming panel
if hasattr(streaming_response, '__iter__') and not isinstance(streaming_response, str): if hasattr(streaming_response, '__iter__') and not isinstance(streaming_response, str):
# Check print_on parameter for different streaming behaviors
if self.print_on is False:
# Show raw streaming text without formatting panels
chunks = []
print(f"\n{self.agent_name}: ", end="", flush=True)
for chunk in streaming_response:
if hasattr(chunk, 'choices') and chunk.choices[0].delta.content:
content = chunk.choices[0].delta.content
print(content, end="", flush=True) # Print raw streaming text
chunks.append(content)
print() # New line after streaming completes
complete_response = ''.join(chunks)
else:
# Collect chunks for conversation saving # Collect chunks for conversation saving
collected_chunks = [] collected_chunks = []
@ -2744,12 +2757,10 @@ class Agent:
def pretty_print(self, response: str, loop_count: int): def pretty_print(self, response: str, loop_count: int):
if self.print_on is False: if self.print_on is False:
if self.streaming_on is True: if self.streaming_on is True:
# self.stream_response(response) # Skip printing here since real streaming is handled in call_llm
formatter.print_panel_token_by_token( # This avoids double printing when streaming_on=True
f"{self.agent_name}: {response}", pass
title=f"Agent Name: {self.agent_name} [Max Loops: {loop_count}]", elif self.no_print is True:
)
elif self.print_on is True:
pass pass
else: else:
# logger.info(f"Response: {response}") # logger.info(f"Response: {response}")

Loading…
Cancel
Save