-
Notifications
You must be signed in to change notification settings - Fork 27.2k
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 messages from agent run for gradio chatbot #32142
Merged
Merged
Changes from 5 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
8396a8c
Add stream messages from agent run for gradio chatbot
aymeric-roucher cacc3e2
Change inits to allow easy imports
aymeric-roucher 23edba9
Add demo in docs
aymeric-roucher a89045a
Add autodoc for stream_from_transformers_agent
aymeric-roucher 2d4e333
Simplify name
aymeric-roucher 7ecdb4c
Answer comments
aymeric-roucher 98a8611
Merge branch 'main'
aymeric-roucher 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 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 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 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 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 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 |
---|---|---|
@@ -0,0 +1,80 @@ | ||
#!/usr/bin/env python | ||
# coding=utf-8 | ||
|
||
# Copyright 2024 The HuggingFace Inc. team. All rights reserved. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
from .agent_types import AgentAudio, AgentImage, AgentText, AgentType | ||
from .agents import ReactAgent | ||
|
||
|
||
def pull_message(step_log: dict): | ||
try: | ||
from gradio import ChatMessage | ||
except ImportError: | ||
raise ImportError("Gradio should be installed in order to launch a gradio demo.") | ||
|
||
if step_log.get("rationale"): | ||
yield ChatMessage(role="assistant", content=step_log["rationale"]) | ||
if step_log.get("tool_call"): | ||
used_code = step_log["tool_call"]["tool_name"] == "code interpreter" | ||
content = step_log["tool_call"]["tool_arguments"] | ||
if used_code: | ||
content = f"```py\n{content}\n```" | ||
yield ChatMessage( | ||
role="assistant", | ||
metadata={"title": f"🛠️ Used tool {step_log['tool_call']['tool_name']}"}, | ||
content=content, | ||
) | ||
if step_log.get("observation"): | ||
yield ChatMessage(role="assistant", content=f"```\n{step_log['observation']}\n```") | ||
if step_log.get("error"): | ||
yield ChatMessage( | ||
role="assistant", | ||
content=str(step_log["error"]), | ||
metadata={"title": "💥 Error"}, | ||
) | ||
|
||
|
||
def stream_to_gradio(agent: ReactAgent, task: str, **kwargs): | ||
"""Runs an agent with the given task and streams the messages from the agent as gradio ChatMessages.""" | ||
|
||
try: | ||
from gradio import ChatMessage | ||
except ImportError: | ||
raise ImportError("Gradio should be installed in order to launch a gradio demo.") | ||
|
||
class Output: | ||
output: AgentType | str = None | ||
|
||
for step_log in agent.run(task, stream=True, **kwargs): | ||
if isinstance(step_log, dict): | ||
for message in pull_message(step_log): | ||
print("message", message) | ||
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. Remove this print |
||
yield message | ||
|
||
Output.output = step_log | ||
if isinstance(Output.output, AgentText): | ||
yield ChatMessage(role="assistant", content=f"**Final answer:**\n```\n{Output.output.to_string()}\n```") | ||
elif isinstance(Output.output, AgentImage): | ||
yield ChatMessage( | ||
role="assistant", | ||
content={"path": Output.output.to_string(), "mime_type": "image/png"}, | ||
) | ||
elif isinstance(Output.output, AgentAudio): | ||
yield ChatMessage( | ||
role="assistant", | ||
content={"path": Output.output.to_string(), "mime_type": "audio/wav"}, | ||
) | ||
else: | ||
yield ChatMessage(role="assistant", content=Output.output) |
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.
I'm actually not sure why I originally needed this temporary class. I think we can probably remove and use
step_log
directly?