mood_music_tool.py 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. # src/tools/mood_music_tool.py
  2. from hello_agents.tools import Tool as BaseTool
  3. from src.utils.loader import load_mood_music_map
  4. class MoodMusicTool(BaseTool):
  5. """
  6. 情绪 -> 音乐推荐工具(完全模拟)
  7. """
  8. def __init__(self):
  9. super().__init__(
  10. name="mood_music_tool",
  11. description = "根据用户描述的心境,返回对应的音乐推荐列表"
  12. )
  13. self.name = "mood_music_tool"
  14. self.description = "根据用户描述的心境,返回对应的音乐推荐列表"
  15. self.mood_map = load_mood_music_map()
  16. def get_parameters(self):
  17. return {
  18. "type": "object",
  19. "properties": {
  20. "query": {"type": "string", "description": "用户输入"}
  21. },
  22. "required": ["query"]
  23. }
  24. def run(self, query: str) -> str:
  25. """
  26. query: 用户输入的心境描述
  27. """
  28. # 极简规则匹配(稳)
  29. for mood, songs in self.mood_map.items():
  30. if mood in query:
  31. return self._format_result(mood, songs)
  32. # fallback
  33. return self._format_result(
  34. "未识别",
  35. ["Tycho - Awake", "Ólafur Arnalds - Near Light"]
  36. )
  37. def _format_result(self, mood, songs):
  38. result = f"🎧 当前识别的心境:{mood}\n\n推荐音乐:\n"
  39. for i, song in enumerate(songs, 1):
  40. result += f"{i}. {song}\n"
  41. return result