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

variables.py 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. from collections.abc import Sequence
  2. from typing import Annotated, Any, TypeAlias
  3. from uuid import uuid4
  4. from pydantic import BaseModel, Discriminator, Field, Tag
  5. from core.helper import encrypter
  6. from .segments import (
  7. ArrayAnySegment,
  8. ArrayBooleanSegment,
  9. ArrayFileSegment,
  10. ArrayNumberSegment,
  11. ArrayObjectSegment,
  12. ArraySegment,
  13. ArrayStringSegment,
  14. BooleanSegment,
  15. FileSegment,
  16. FloatSegment,
  17. IntegerSegment,
  18. NoneSegment,
  19. ObjectSegment,
  20. Segment,
  21. StringSegment,
  22. get_segment_discriminator,
  23. )
  24. from .types import SegmentType
  25. class Variable(Segment):
  26. """
  27. A variable is a segment that has a name.
  28. It is mainly used to store segments and their selector in VariablePool.
  29. Note: this class is abstract, you should use subclasses of this class instead.
  30. """
  31. id: str = Field(
  32. default_factory=lambda: str(uuid4()),
  33. description="Unique identity for variable.",
  34. )
  35. name: str
  36. description: str = Field(default="", description="Description of the variable.")
  37. selector: Sequence[str] = Field(default_factory=list)
  38. class StringVariable(StringSegment, Variable):
  39. pass
  40. class FloatVariable(FloatSegment, Variable):
  41. pass
  42. class IntegerVariable(IntegerSegment, Variable):
  43. pass
  44. class ObjectVariable(ObjectSegment, Variable):
  45. pass
  46. class ArrayVariable(ArraySegment, Variable):
  47. pass
  48. class ArrayAnyVariable(ArrayAnySegment, ArrayVariable):
  49. pass
  50. class ArrayStringVariable(ArrayStringSegment, ArrayVariable):
  51. pass
  52. class ArrayNumberVariable(ArrayNumberSegment, ArrayVariable):
  53. pass
  54. class ArrayObjectVariable(ArrayObjectSegment, ArrayVariable):
  55. pass
  56. class SecretVariable(StringVariable):
  57. value_type: SegmentType = SegmentType.SECRET
  58. @property
  59. def log(self) -> str:
  60. return encrypter.obfuscated_token(self.value)
  61. class NoneVariable(NoneSegment, Variable):
  62. value_type: SegmentType = SegmentType.NONE
  63. value: None = None
  64. class FileVariable(FileSegment, Variable):
  65. pass
  66. class BooleanVariable(BooleanSegment, Variable):
  67. pass
  68. class ArrayFileVariable(ArrayFileSegment, ArrayVariable):
  69. pass
  70. class ArrayBooleanVariable(ArrayBooleanSegment, ArrayVariable):
  71. pass
  72. class RAGPipelineVariable(BaseModel):
  73. belong_to_node_id: str = Field(description="belong to which node id, shared means public")
  74. type: str = Field(description="variable type, text-input, paragraph, select, number, file, file-list")
  75. label: str = Field(description="label")
  76. description: str | None = Field(description="description", default="")
  77. variable: str = Field(description="variable key", default="")
  78. max_length: int | None = Field(
  79. description="max length, applicable to text-input, paragraph, and file-list", default=0
  80. )
  81. default_value: Any = Field(description="default value", default="")
  82. placeholder: str | None = Field(description="placeholder", default="")
  83. unit: str | None = Field(description="unit, applicable to Number", default="")
  84. tooltips: str | None = Field(description="helpful text", default="")
  85. allowed_file_types: list[str] | None = Field(
  86. description="image, document, audio, video, custom.", default_factory=list
  87. )
  88. allowed_file_extensions: list[str] | None = Field(description="e.g. ['.jpg', '.mp3']", default_factory=list)
  89. allowed_file_upload_methods: list[str] | None = Field(
  90. description="remote_url, local_file, tool_file.", default_factory=list
  91. )
  92. required: bool = Field(description="optional, default false", default=False)
  93. options: list[str] | None = Field(default_factory=list)
  94. class RAGPipelineVariableInput(BaseModel):
  95. variable: RAGPipelineVariable
  96. value: Any
  97. # The `VariableUnion`` type is used to enable serialization and deserialization with Pydantic.
  98. # Use `Variable` for type hinting when serialization is not required.
  99. #
  100. # Note:
  101. # - All variants in `VariableUnion` must inherit from the `Variable` class.
  102. # - The union must include all non-abstract subclasses of `Segment`, except:
  103. VariableUnion: TypeAlias = Annotated[
  104. (
  105. Annotated[NoneVariable, Tag(SegmentType.NONE)]
  106. | Annotated[StringVariable, Tag(SegmentType.STRING)]
  107. | Annotated[FloatVariable, Tag(SegmentType.FLOAT)]
  108. | Annotated[IntegerVariable, Tag(SegmentType.INTEGER)]
  109. | Annotated[ObjectVariable, Tag(SegmentType.OBJECT)]
  110. | Annotated[FileVariable, Tag(SegmentType.FILE)]
  111. | Annotated[BooleanVariable, Tag(SegmentType.BOOLEAN)]
  112. | Annotated[ArrayAnyVariable, Tag(SegmentType.ARRAY_ANY)]
  113. | Annotated[ArrayStringVariable, Tag(SegmentType.ARRAY_STRING)]
  114. | Annotated[ArrayNumberVariable, Tag(SegmentType.ARRAY_NUMBER)]
  115. | Annotated[ArrayObjectVariable, Tag(SegmentType.ARRAY_OBJECT)]
  116. | Annotated[ArrayFileVariable, Tag(SegmentType.ARRAY_FILE)]
  117. | Annotated[ArrayBooleanVariable, Tag(SegmentType.ARRAY_BOOLEAN)]
  118. | Annotated[SecretVariable, Tag(SegmentType.SECRET)]
  119. ),
  120. Discriminator(get_segment_discriminator),
  121. ]