您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

Feat: Support tool calling in Generate component (#7572) ### What problem does this PR solve? Hello, our use case requires LLM agent to invoke some tools, so I made a simple implementation here. This PR does two things: 1. A simple plugin mechanism based on `pluginlib`: This mechanism lives in the `plugin` directory. It will only load plugins from `plugin/embedded_plugins` for now. A sample plugin `bad_calculator.py` is placed in `plugin/embedded_plugins/llm_tools`, it accepts two numbers `a` and `b`, then give a wrong result `a + b + 100`. In the future, it can load plugins from external location with little code change. Plugins are divided into different types. The only plugin type supported in this PR is `llm_tools`, which must implement the `LLMToolPlugin` class in the `plugin/llm_tool_plugin.py`. More plugin types can be added in the future. 2. A tool selector in the `Generate` component: Added a tool selector to select one or more tools for LLM: ![image](https://github.com/user-attachments/assets/74a21fdf-9333-4175-991b-43df6524c5dc) And with the `bad_calculator` tool, it results this with the `qwen-max` model: ![image](https://github.com/user-attachments/assets/93aff9c4-8550-414a-90a2-1a15a5249d94) ### Type of change - [ ] Bug Fix (non-breaking change which fixes an issue) - [x] New Feature (non-breaking change which adds functionality) - [ ] Documentation Update - [ ] Refactoring - [ ] Performance Improvement - [ ] Other (please describe): Co-authored-by: Yingfeng <yingfeng.zhang@gmail.com>
5 个月前
123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. import logging
  2. import os
  3. from pathlib import Path
  4. import pluginlib
  5. from .common import PLUGIN_TYPE_LLM_TOOLS
  6. from .llm_tool_plugin import LLMToolPlugin
  7. class PluginManager:
  8. _llm_tool_plugins: dict[str, LLMToolPlugin]
  9. def __init__(self) -> None:
  10. self._llm_tool_plugins = {}
  11. def load_plugins(self) -> None:
  12. loader = pluginlib.PluginLoader(
  13. paths=[str(Path(os.path.dirname(__file__), "embedded_plugins"))]
  14. )
  15. for type, plugins in loader.plugins.items():
  16. for name, plugin in plugins.items():
  17. logging.info(f"Loaded {type} plugin {name} version {plugin.version}")
  18. if type == PLUGIN_TYPE_LLM_TOOLS:
  19. metadata = plugin.get_metadata()
  20. self._llm_tool_plugins[metadata["name"]] = plugin
  21. def get_llm_tools(self) -> list[LLMToolPlugin]:
  22. return list(self._llm_tool_plugins.values())
  23. def get_llm_tool_by_name(self, name: str) -> LLMToolPlugin | None:
  24. return self._llm_tool_plugins.get(name)
  25. def get_llm_tools_by_names(self, tool_names: list[str]) -> list[LLMToolPlugin]:
  26. results = []
  27. for name in tool_names:
  28. plugin = self._llm_tool_plugins.get(name)
  29. if plugin is not None:
  30. results.append(plugin)
  31. return results