monitoring.py 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. #!/usr/bin/env python
  2. # coding=utf-8
  3. # Copyright 2024 The HuggingFace Inc. team. All rights reserved.
  4. #
  5. # Licensed under the Apache License, Version 2.0 (the "License");
  6. # you may not use this file except in compliance with the License.
  7. # You may obtain a copy of the License at
  8. #
  9. # http://www.apache.org/licenses/LICENSE-2.0
  10. #
  11. # Unless required by applicable law or agreed to in writing, software
  12. # distributed under the License is distributed on an "AS IS" BASIS,
  13. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. # See the License for the specific language governing permissions and
  15. # limitations under the License.
  16. from .agent_types import AgentAudio, AgentImage, AgentText
  17. from .agents import ReactAgent
  18. def pull_message(step_log: dict):
  19. try:
  20. from gradio import ChatMessage
  21. except ImportError:
  22. raise ImportError("Gradio should be installed in order to launch a gradio demo.")
  23. if step_log.get("rationale"):
  24. yield ChatMessage(role="assistant", content=step_log["rationale"])
  25. if step_log.get("tool_call"):
  26. used_code = step_log["tool_call"]["tool_name"] == "code interpreter"
  27. content = step_log["tool_call"]["tool_arguments"]
  28. if used_code:
  29. content = f"```py\n{content}\n```"
  30. yield ChatMessage(
  31. role="assistant",
  32. metadata={"title": f"🛠️ Used tool {step_log['tool_call']['tool_name']}"},
  33. content=str(content),
  34. )
  35. if step_log.get("observation"):
  36. yield ChatMessage(role="assistant", content=f"```\n{step_log['observation']}\n```")
  37. if step_log.get("error"):
  38. yield ChatMessage(
  39. role="assistant",
  40. content=str(step_log["error"]),
  41. metadata={"title": "💥 Error"},
  42. )
  43. def stream_to_gradio(agent: ReactAgent, task: str, **kwargs):
  44. """Runs an agent with the given task and streams the messages from the agent as gradio ChatMessages."""
  45. try:
  46. from gradio import ChatMessage
  47. except ImportError:
  48. raise ImportError("Gradio should be installed in order to launch a gradio demo.")
  49. for step_log in agent.run(task, stream=True, **kwargs):
  50. if isinstance(step_log, dict):
  51. for message in pull_message(step_log):
  52. yield message
  53. if isinstance(step_log, AgentText):
  54. yield ChatMessage(role="assistant", content=f"**Final answer:**\n```\n{step_log.to_string()}\n```")
  55. elif isinstance(step_log, AgentImage):
  56. yield ChatMessage(
  57. role="assistant",
  58. content={"path": step_log.to_string(), "mime_type": "image/png"},
  59. )
  60. elif isinstance(step_log, AgentAudio):
  61. yield ChatMessage(
  62. role="assistant",
  63. content={"path": step_log.to_string(), "mime_type": "audio/wav"},
  64. )
  65. else:
  66. yield ChatMessage(role="assistant", content=str(step_log))