entities.py 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. from typing import Literal, Union
  2. from pydantic import BaseModel, validator
  3. from core.workflow.entities.base_node_data_entities import BaseNodeData
  4. ToolParameterValue = Union[str, int, float, bool]
  5. class ToolEntity(BaseModel):
  6. provider_id: str
  7. provider_type: Literal['builtin', 'api']
  8. provider_name: str # redundancy
  9. tool_name: str
  10. tool_label: str # redundancy
  11. tool_configurations: dict[str, ToolParameterValue]
  12. class ToolNodeData(BaseNodeData, ToolEntity):
  13. class ToolInput(BaseModel):
  14. value: Union[ToolParameterValue, list[str]]
  15. type: Literal['mixed', 'variable', 'constant']
  16. @validator('type', pre=True, always=True)
  17. def check_type(cls, value, values):
  18. typ = value
  19. value = values.get('value')
  20. if typ == 'mixed' and not isinstance(value, str):
  21. raise ValueError('value must be a string')
  22. elif typ == 'variable' and not isinstance(value, list):
  23. raise ValueError('value must be a list')
  24. elif typ == 'constant' and not isinstance(value, ToolParameterValue):
  25. raise ValueError('value must be a string, int, float, or bool')
  26. return typ
  27. """
  28. Tool Node Schema
  29. """
  30. tool_parameters: dict[str, ToolInput]