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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. import json
  2. import sys
  3. from collections.abc import Mapping, Sequence
  4. from typing import Annotated, Any, TypeAlias
  5. from pydantic import BaseModel, ConfigDict, Discriminator, Tag, field_validator
  6. from core.file import File
  7. from .types import SegmentType
  8. class Segment(BaseModel):
  9. """Segment is runtime type used during the execution of workflow.
  10. Note: this class is abstract, you should use subclasses of this class instead.
  11. """
  12. model_config = ConfigDict(frozen=True)
  13. value_type: SegmentType
  14. value: Any
  15. @field_validator("value_type")
  16. @classmethod
  17. def validate_value_type(cls, value):
  18. """
  19. This validator checks if the provided value is equal to the default value of the 'value_type' field.
  20. If the value is different, a ValueError is raised.
  21. """
  22. if value != cls.model_fields["value_type"].default:
  23. raise ValueError("Cannot modify 'value_type'")
  24. return value
  25. @property
  26. def text(self) -> str:
  27. return str(self.value)
  28. @property
  29. def log(self) -> str:
  30. return str(self.value)
  31. @property
  32. def markdown(self) -> str:
  33. return str(self.value)
  34. @property
  35. def size(self) -> int:
  36. """
  37. Return the size of the value in bytes.
  38. """
  39. return sys.getsizeof(self.value)
  40. def to_object(self) -> Any:
  41. return self.value
  42. class NoneSegment(Segment):
  43. value_type: SegmentType = SegmentType.NONE
  44. value: None = None
  45. @property
  46. def text(self) -> str:
  47. return ""
  48. @property
  49. def log(self) -> str:
  50. return ""
  51. @property
  52. def markdown(self) -> str:
  53. return ""
  54. class StringSegment(Segment):
  55. value_type: SegmentType = SegmentType.STRING
  56. value: str
  57. class FloatSegment(Segment):
  58. value_type: SegmentType = SegmentType.FLOAT
  59. value: float
  60. # NOTE(QuantumGhost): seems that the equality for FloatSegment with `NaN` value has some problems.
  61. # The following tests cannot pass.
  62. #
  63. # def test_float_segment_and_nan():
  64. # nan = float("nan")
  65. # assert nan != nan
  66. #
  67. # f1 = FloatSegment(value=float("nan"))
  68. # f2 = FloatSegment(value=float("nan"))
  69. # assert f1 != f2
  70. #
  71. # f3 = FloatSegment(value=nan)
  72. # f4 = FloatSegment(value=nan)
  73. # assert f3 != f4
  74. class IntegerSegment(Segment):
  75. value_type: SegmentType = SegmentType.INTEGER
  76. value: int
  77. class ObjectSegment(Segment):
  78. value_type: SegmentType = SegmentType.OBJECT
  79. value: Mapping[str, Any]
  80. @property
  81. def text(self) -> str:
  82. return json.dumps(self.model_dump()["value"], ensure_ascii=False)
  83. @property
  84. def log(self) -> str:
  85. return json.dumps(self.model_dump()["value"], ensure_ascii=False, indent=2)
  86. @property
  87. def markdown(self) -> str:
  88. return json.dumps(self.model_dump()["value"], ensure_ascii=False, indent=2)
  89. class ArraySegment(Segment):
  90. @property
  91. def markdown(self) -> str:
  92. items = []
  93. for item in self.value:
  94. items.append(str(item))
  95. return "\n".join(items)
  96. class FileSegment(Segment):
  97. value_type: SegmentType = SegmentType.FILE
  98. value: File
  99. @property
  100. def markdown(self) -> str:
  101. return self.value.markdown
  102. @property
  103. def log(self) -> str:
  104. return ""
  105. @property
  106. def text(self) -> str:
  107. return ""
  108. class ArrayAnySegment(ArraySegment):
  109. value_type: SegmentType = SegmentType.ARRAY_ANY
  110. value: Sequence[Any]
  111. class ArrayStringSegment(ArraySegment):
  112. value_type: SegmentType = SegmentType.ARRAY_STRING
  113. value: Sequence[str]
  114. @property
  115. def text(self) -> str:
  116. return json.dumps(self.value, ensure_ascii=False)
  117. class ArrayNumberSegment(ArraySegment):
  118. value_type: SegmentType = SegmentType.ARRAY_NUMBER
  119. value: Sequence[float | int]
  120. class ArrayObjectSegment(ArraySegment):
  121. value_type: SegmentType = SegmentType.ARRAY_OBJECT
  122. value: Sequence[Mapping[str, Any]]
  123. class ArrayFileSegment(ArraySegment):
  124. value_type: SegmentType = SegmentType.ARRAY_FILE
  125. value: Sequence[File]
  126. @property
  127. def markdown(self) -> str:
  128. items = []
  129. for item in self.value:
  130. items.append(item.markdown)
  131. return "\n".join(items)
  132. @property
  133. def log(self) -> str:
  134. return ""
  135. @property
  136. def text(self) -> str:
  137. return ""
  138. def get_segment_discriminator(v: Any) -> SegmentType | None:
  139. if isinstance(v, Segment):
  140. return v.value_type
  141. elif isinstance(v, dict):
  142. value_type = v.get("value_type")
  143. if value_type is None:
  144. return None
  145. try:
  146. seg_type = SegmentType(value_type)
  147. except ValueError:
  148. return None
  149. return seg_type
  150. else:
  151. # return None if the discriminator value isn't found
  152. return None
  153. # The `SegmentUnion`` type is used to enable serialization and deserialization with Pydantic.
  154. # Use `Segment` for type hinting when serialization is not required.
  155. #
  156. # Note:
  157. # - All variants in `SegmentUnion` must inherit from the `Segment` class.
  158. # - The union must include all non-abstract subclasses of `Segment`, except:
  159. # - `SegmentGroup`, which is not added to the variable pool.
  160. # - `Variable` and its subclasses, which are handled by `VariableUnion`.
  161. SegmentUnion: TypeAlias = Annotated[
  162. (
  163. Annotated[NoneSegment, Tag(SegmentType.NONE)]
  164. | Annotated[StringSegment, Tag(SegmentType.STRING)]
  165. | Annotated[FloatSegment, Tag(SegmentType.FLOAT)]
  166. | Annotated[IntegerSegment, Tag(SegmentType.INTEGER)]
  167. | Annotated[ObjectSegment, Tag(SegmentType.OBJECT)]
  168. | Annotated[FileSegment, Tag(SegmentType.FILE)]
  169. | Annotated[ArrayAnySegment, Tag(SegmentType.ARRAY_ANY)]
  170. | Annotated[ArrayStringSegment, Tag(SegmentType.ARRAY_STRING)]
  171. | Annotated[ArrayNumberSegment, Tag(SegmentType.ARRAY_NUMBER)]
  172. | Annotated[ArrayObjectSegment, Tag(SegmentType.ARRAY_OBJECT)]
  173. | Annotated[ArrayFileSegment, Tag(SegmentType.ARRAY_FILE)]
  174. ),
  175. Discriminator(get_segment_discriminator),
  176. ]