Refactor agent streaming behavior and update formatter panel title color

pull/933/head
harshalmore31 4 days ago
parent 20c3d8a19c
commit 50a69950f2

@ -3,10 +3,10 @@ from swarms import Agent
# Enable real-time streaming # Enable real-time streaming
agent = Agent( agent = Agent(
agent_name="StoryAgent", agent_name="StoryAgent",
model_name="gpt-4o-mini", model_name="gpt-4o-mini", # 🔥 This enables real streaming!
streaming_on=True, # 🔥 This enables real streaming! max_loops=4,
max_loops=1, streaming_on=True,
print_on=False, # By Default its False, raw streaming !! print_on=True,
output_type="all", output_type="all",
) )
@ -14,4 +14,4 @@ agent = Agent(
response = agent.run( response = agent.run(
"Tell me a detailed story about Humanity colonizing the stars" "Tell me a detailed story about Humanity colonizing the stars"
) )
print(response) # print(response)

@ -2518,13 +2518,8 @@ class Agent:
) and not isinstance(streaming_response, str): ) and not isinstance(streaming_response, str):
# Check print_on parameter for different streaming behaviors # Check print_on parameter for different streaming behaviors
if self.print_on is False: if self.print_on is False:
# Show raw streaming text without formatting panels # Silent streaming - no printing, just collect chunks
chunks = [] chunks = []
print(
f"\n{self.agent_name}: ",
end="",
flush=True,
)
for chunk in streaming_response: for chunk in streaming_response:
if ( if (
hasattr(chunk, "choices") hasattr(chunk, "choices")
@ -2533,11 +2528,7 @@ class Agent:
content = chunk.choices[ content = chunk.choices[
0 0
].delta.content ].delta.content
print(
content, end="", flush=True
) # Print raw streaming text
chunks.append(content) chunks.append(content)
print() # New line after streaming completes
complete_response = "".join(chunks) complete_response = "".join(chunks)
else: else:
# Collect chunks for conversation saving # Collect chunks for conversation saving
@ -2802,8 +2793,8 @@ class Agent:
return return
if self.print_on is False: if self.print_on is False:
# Show raw text without formatting panels # Silent mode - no printing at all
print(f"\n{self.agent_name}: {response}\n") return
else: else:
# Use formatted panels (default behavior when print_on=True) # Use formatted panels (default behavior when print_on=True)
formatter.print_panel( formatter.print_panel(

@ -181,7 +181,7 @@ class Formatter:
def create_streaming_panel(text_obj, is_complete=False): def create_streaming_panel(text_obj, is_complete=False):
"""Create panel with proper text wrapping using Rich's built-in capabilities""" """Create panel with proper text wrapping using Rich's built-in capabilities"""
panel_title = f"[bold cyan]{title}[/bold cyan]" panel_title = f"[white]{title}[/white]"
if is_complete: if is_complete:
panel_title += " [bold green]✅[/bold green]" panel_title += " [bold green]✅[/bold green]"

Loading…
Cancel
Save