[Feat]: gradio_ui.stream_to_gradio without accumulate_delta
fwwucn opened this issue · 1 comments
fwwucn commented
Is your feature request related to a problem? Please describe.
May I request to customise gradio_ui.stream_to_gradio to output delta with additional_args["accumulate_delta"]?
Describe the solution you'd like
I monkey patched like this:
def stream_to_gradio(
agent,
task: str,
task_images: list | None = None,
reset_agent_memory: bool = False,
additional_args: dict | None = None,
):
"""Runs an agent with the given task and streams the messages from the agent as gradio ChatMessages."""
if not _is_package_available("gradio"):
raise ModuleNotFoundError(
"Please install 'gradio' extra to use the GradioUI: `pip install 'smolagents[gradio]'`"
)
from smolagents.models import ChatMessageStreamDelta, agglomerate_stream_deltas
from smolagents.memory import ActionStep, FinalAnswerStep
from smolagents.agents import PlanningStep
# Check additional_args["accumulate_delta"]
accumulate_delta = True
if additional_args and "accumulate_delta" in additional_args:
accumulate_delta = additional_args["accumulate_delta"]
accumulated_events: list[ChatMessageStreamDelta] = []
for event in agent.run(
task, images=task_images, stream=True, reset=reset_agent_memory, additional_args=additional_args
):
if isinstance(event, (ActionStep, PlanningStep, FinalAnswerStep)):
for message in gradio_ui.pull_messages_from_step(
event,
# If we're streaming model outputs, no need to display them twice
skip_model_outputs=getattr(agent, "stream_outputs", False),
):
yield message
accumulated_events = []
elif isinstance(event, ChatMessageStreamDelta):
if accumulate_delta:
accumulated_events.append(event)
text = agglomerate_stream_deltas(accumulated_events).render_as_markdown()
else:
text = event.content
yield text
Describe alternatives you've considered
No response
Additional context
No response
Code of Conduct
- I agree to follow this project's Code of Conduct
holtskinner commented
I'm not sure this was filed in the correct repository. If this is for the A2A Python SDK, can you provide context of where this is needed?