-
Notifications
You must be signed in to change notification settings - Fork 35
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
Add stream output for Agent #835
base: main
Are you sure you want to change the base?
Conversation
Related to #833 Add stream output support for ToolAgent. * **ToolAgent Class**: - Add `stream` parameter to the `run` method. - Implement logic to handle the `stream` parameter. - Modify the `_run` method to support streaming output. - Raise error if `stream=True` and `output_schema` is not provided. * **BaseAgent Class**: - Add `stream` parameter to the `run` method. - Implement `_run_stream` method to handle streaming output. * **Example**: - Update `example/agent/tool_agent_usage.py` to demonstrate the usage of `agent.run(..., stream=True)`. * **Tests**: - Add tests in `tests/agents/test_tool_agent.py` to verify the stream mode output functionality. --- For more details, open the [Copilot Workspace session](https://copilot-workspace.githubnext.com/Undertone0809/promptulate/issues/833?shareId=XXXX-XXXX-XXXX-XXXX).
WalkthroughThe recent changes enhance the functionality of the Changes
Possibly related issues
Poem
Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media? TipsChatThere are 3 ways to chat with CodeRabbit:
Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (invoked as PR comments)
Additionally, you can add CodeRabbit Configuration File (
|
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.
Actionable comments posted: 3
Outside diff range, codebase verification and nitpick comments (1)
promptulate/agents/base.py (1)
Line range hint
28-45
:
LGTM! But fix the line length issues.The changes to the
run
method correctly implement the streaming functionality. However, some lines exceed the maximum line length.- stream: bool = False, + stream: bool = False, - return self._run_stream(instruction, output_schema, examples, *args, **kwargs) + return self._run_stream( + instruction, output_schema, examples, *args, **kwargs + )Tools
Ruff
44-44: Line too long (90 > 88)
(E501)
Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Files selected for processing (4)
- example/agent/tool_agent_usage.py (1 hunks)
- promptulate/agents/base.py (4 hunks)
- promptulate/agents/tool_agent/agent.py (2 hunks)
- tests/agents/test_tool_agent.py (1 hunks)
Additional context used
Ruff
promptulate/agents/base.py
44-44: Line too long (90 > 88)
(E501)
91-91: Line too long (90 > 88)
(E501)
promptulate/agents/tool_agent/agent.py
122-122: Line too long (93 > 88)
(E501)
Additional comments not posted (5)
example/agent/tool_agent_usage.py (1)
13-14
: LGTM! Verify the usage ofagent.run
withstream=True
.The changes correctly implement the streaming functionality. Ensure that the
agent.run
method supports streaming as expected.Verification successful
LGTM! The
agent.run
method supports streaming withstream=True
.The implementation correctly handles the
stream
parameter and calls the_run_stream
method when streaming is enabled.
promptulate/agents/base.py
:
run
method: Calls_run_stream
whenstream=True
.Scripts executed
The following scripts were executed for the analysis:
Script:
#!/bin/bash # Description: Verify the usage of `agent.run` with `stream=True`. # Test: Search for the implementation of `agent.run`. rg --type python -A 5 $'def run'Length of output: 64
Script:
#!/bin/bash # Description: Verify the usage of `agent.run` with `stream=True`. # Test: Search for the implementation of `agent.run`. rg -A 5 'def run'Length of output: 4917
Script:
#!/bin/bash # Description: Verify the usage of `agent.run` with `stream=True` in `promptulate/agents/base.py`. # Test: Search for the complete implementation of the `run` method in `promptulate/agents/base.py`. rg -A 50 'def run' promptulate/agents/base.pyLength of output: 1616
tests/agents/test_tool_agent.py (1)
58-64
: LGTM! The test for streaming mode is well-implemented.The
test_stream_mode
function correctly validates the streaming functionality of theToolAgent
.promptulate/agents/tool_agent/agent.py (3)
129-129
: Update: Document new parameter.The method documentation has been updated to include the new
stream
parameter.
134-135
: Validation: Ensureoutput_schema
is provided for streaming.The code correctly raises a
ValueError
ifstream=True
andoutput_schema
is not provided inkwargs
.
181-183
: Enhancement: Yield results for streaming.The code correctly yields
tool_result
whenstream
is enabled, allowing for real-time output.
@@ -119,17 +119,21 @@ def current_date(self) -> str: | |||
return f"Current date: {time.strftime('%Y-%m-%d %H:%M:%S')}" | |||
|
|||
def _run( | |||
self, instruction: str, return_raw_data: bool = False, **kwargs | |||
self, instruction: str, return_raw_data: bool = False, stream: bool = False, **kwargs |
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.
Refactor: Break down long line.
The line exceeds the maximum line length of 88 characters. Refactor it to improve readability.
- self, instruction: str, return_raw_data: bool = False, stream: bool = False, **kwargs
+ self, instruction: str, return_raw_data: bool = False,
+ stream: bool = False, **kwargs
Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
self, instruction: str, return_raw_data: bool = False, stream: bool = False, **kwargs | |
self, instruction: str, return_raw_data: bool = False, | |
stream: bool = False, **kwargs |
Tools
Ruff
122-122: Line too long (93 > 88)
(E501)
def _run_stream( | ||
self, | ||
instruction: str, | ||
output_schema: Optional[type(BaseModel)] = None, | ||
examples: Optional[List[BaseModel]] = None, | ||
*args, | ||
**kwargs, | ||
) -> Generator[Any, None, None]: | ||
"""Run the tool including specified function and hooks with streaming output""" | ||
Hook.call_hook( | ||
HookTable.ON_AGENT_START, | ||
self, | ||
instruction, | ||
output_schema, | ||
*args, | ||
agent_type=self._agent_type, | ||
**kwargs, | ||
) | ||
|
||
for result in self._run(instruction, *args, **kwargs): | ||
# TODO: need to optimize | ||
# if output_schema: | ||
# formatter = OutputFormatter(output_schema, examples) | ||
# prompt = ( | ||
# f"{formatter.get_formatted_instructions()}\n##User input:\n{result}" | ||
# ) | ||
# json_response: str = self.get_llm()(prompt) | ||
# yield formatter.formatting_result(json_response) | ||
# else: | ||
yield result | ||
|
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.
LGTM! Address the TODO comment.
The _run_stream
method correctly handles streaming output. However, there is a TODO comment indicating a need for optimization.
Do you want me to help optimize this method or open a GitHub issue to track this task?
Tools
Ruff
91-91: Line too long (90 > 88)
(E501)
# if output_schema: | ||
# formatter = OutputFormatter(output_schema, examples) | ||
# prompt = ( | ||
# f"{formatter.get_formatted_instructions()}\n##User input:\n{result}" |
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.
Fix the line length issue.
The line exceeds the maximum line length.
- # f"{formatter.get_formatted_instructions()}\n##User input:\n{result}"
+ # f"{formatter.get_formatted_instructions()}\n##User input:\n{result}"
Committable suggestion was skipped due to low confidence.
Tools
Ruff
91-91: Line too long (90 > 88)
(E501)
Related to #833
Add stream output support for ToolAgent.
ToolAgent Class:
stream
parameter to therun
method.stream
parameter._run
method to support streaming output.stream=True
andoutput_schema
is not provided.BaseAgent Class:
stream
parameter to therun
method._run_stream
method to handle streaming output.Example:
example/agent/tool_agent_usage.py
to demonstrate the usage ofagent.run(..., stream=True)
.Tests:
tests/agents/test_tool_agent.py
to verify the stream mode output functionality.For more details, open the Copilot Workspace session.
Summary by CodeRabbit
New Features
Bug Fixes
Tests