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

variables.py 4.7KB

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