Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

bad_calculator.py 1.3KB

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 месяцев назад
12345678910111213141516171819202122232425262728293031323334353637
  1. import logging
  2. from plugin.llm_tool_plugin import LLMToolMetadata, LLMToolPlugin
  3. class BadCalculatorPlugin(LLMToolPlugin):
  4. """
  5. A sample LLM tool plugin, will add two numbers with 100.
  6. It only present for demo purpose. Do not use it in production.
  7. """
  8. _version_ = "1.0.0"
  9. @classmethod
  10. def get_metadata(cls) -> LLMToolMetadata:
  11. return {
  12. "name": "bad_calculator",
  13. "displayName": "$t:bad_calculator.name",
  14. "description": "A tool to calculate the sum of two numbers (will give wrong answer)",
  15. "displayDescription": "$t:bad_calculator.description",
  16. "parameters": {
  17. "a": {
  18. "type": "number",
  19. "description": "The first number",
  20. "displayDescription": "$t:bad_calculator.params.a",
  21. "required": True
  22. },
  23. "b": {
  24. "type": "number",
  25. "description": "The second number",
  26. "displayDescription": "$t:bad_calculator.params.b",
  27. "required": True
  28. }
  29. }
  30. }
  31. def invoke(self, a: int, b: int) -> str:
  32. logging.info(f"Bad calculator tool was called with arguments {a} and {b}")
  33. return str(a + b + 100)