-
Notifications
You must be signed in to change notification settings - Fork 1.6k
feat: add streaming chunk events #3403
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
JINO-ROHIT
wants to merge
1
commit into
master
Choose a base branch
from
feat-3379
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -24,6 +24,7 @@ | |
| from camel.agents import ChatAgent | ||
| from camel.agents.chat_agent import AsyncStreamingChatAgentResponse | ||
| from camel.logger import get_logger | ||
| from camel.societies.workforce.events import TaskStreamingChunkEvent | ||
| from camel.societies.workforce.prompts import PROCESS_TASK_PROMPT | ||
| from camel.societies.workforce.structured_output_handler import ( | ||
| StructuredOutputHandler, | ||
|
|
@@ -393,9 +394,32 @@ async def _process_task( | |
| # Handle streaming response | ||
| if isinstance(response, AsyncStreamingChatAgentResponse): | ||
| content = "" | ||
| chunk_index = 0 | ||
| async for chunk in response: | ||
| if chunk.msg: | ||
| content = chunk.msg.content | ||
| if chunk.msg and chunk.msg.content: | ||
| chunk_event = TaskStreamingChunkEvent( | ||
| task_id=task.id, | ||
| worker_id=self.node_id, | ||
| chunk=chunk.msg.content, | ||
| chunk_index=chunk_index, | ||
| ) | ||
| if ( | ||
| hasattr(self, 'callback') | ||
| and self.callback is not None | ||
| ): | ||
| try: | ||
| await ( | ||
| self.callback.log_task_streaming_chunk( | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. log_task_streaming_chunk is defined as a synchronous method in WorkforceCallback |
||
| chunk_event | ||
| ) | ||
| ) | ||
| except Exception as e: | ||
| logger.warning( | ||
| f"Failed to log streaming chunk: {e}" | ||
| ) | ||
|
|
||
| content += chunk.msg.content | ||
| chunk_index += 1 | ||
| response_content = content | ||
| else: | ||
| # Regular ChatAgentResponse | ||
|
|
@@ -422,10 +446,34 @@ async def _process_task( | |
| # Handle streaming response for native output | ||
| if isinstance(response, AsyncStreamingChatAgentResponse): | ||
| task_result = None | ||
| chunk_index = 0 | ||
| async for chunk in response: | ||
| if chunk.msg and chunk.msg.parsed: | ||
| task_result = chunk.msg.parsed | ||
| response_content = chunk.msg.content | ||
| if chunk.msg: | ||
| if chunk.msg.content: | ||
| chunk_event = TaskStreamingChunkEvent( | ||
| task_id=task.id, | ||
| worker_id=self.node_id, | ||
| chunk=chunk.msg.content, | ||
| chunk_index=chunk_index, | ||
| ) | ||
| if ( | ||
| hasattr(self, 'callback') | ||
| and self.callback is not None | ||
| ): | ||
| try: | ||
| await self.callback.log_task_streaming_chunk( # noqa: E501 | ||
| chunk_event | ||
| ) | ||
| except Exception as e: | ||
| logger.warning( | ||
| f"Failed to log chunk: {e}" | ||
| ) | ||
| chunk_index += 1 | ||
|
|
||
| if chunk.msg.parsed: | ||
| task_result = chunk.msg.parsed | ||
| response_content = chunk.msg.content | ||
|
|
||
| # If no parsed result found in streaming, create fallback | ||
| if task_result is None: | ||
| task_result = TaskResult( | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
here assumes self.callback exists in SingleAgentWorker, but it doesn't. Workers have no way to access the callbacks registered in Workforce.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
maybe we need add a mechanism to propagate callbacks from Workforce to Worker instances.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For the worker callback, I suggest implementing callback support at the BaseNode level, since all node types (both Worker and Workforce) could benefit from this change.
However, it might be good to get some feedback from @Wendong-Fan on this idea.