app_invoke_entities.py 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. from collections.abc import Mapping, Sequence
  2. from enum import Enum
  3. from typing import Any, Optional
  4. from pydantic import BaseModel, ConfigDict, Field, ValidationInfo, field_validator
  5. from constants import UUID_NIL
  6. from core.app.app_config.entities import AppConfig, EasyUIBasedAppConfig, WorkflowUIBasedAppConfig
  7. from core.entities.provider_configuration import ProviderModelBundle
  8. from core.file import File, FileUploadConfig
  9. from core.model_runtime.entities.model_entities import AIModelEntity
  10. from core.ops.ops_trace_manager import TraceQueueManager
  11. class InvokeFrom(Enum):
  12. """
  13. Invoke From.
  14. """
  15. SERVICE_API = "service-api"
  16. WEB_APP = "web-app"
  17. EXPLORE = "explore"
  18. DEBUGGER = "debugger"
  19. @classmethod
  20. def value_of(cls, value: str):
  21. """
  22. Get value of given mode.
  23. :param value: mode value
  24. :return: mode
  25. """
  26. for mode in cls:
  27. if mode.value == value:
  28. return mode
  29. raise ValueError(f"invalid invoke from value {value}")
  30. def to_source(self) -> str:
  31. """
  32. Get source of invoke from.
  33. :return: source
  34. """
  35. if self == InvokeFrom.WEB_APP:
  36. return "web_app"
  37. elif self == InvokeFrom.DEBUGGER:
  38. return "dev"
  39. elif self == InvokeFrom.EXPLORE:
  40. return "explore_app"
  41. elif self == InvokeFrom.SERVICE_API:
  42. return "api"
  43. return "dev"
  44. class ModelConfigWithCredentialsEntity(BaseModel):
  45. """
  46. Model Config With Credentials Entity.
  47. """
  48. provider: str
  49. model: str
  50. model_schema: AIModelEntity
  51. mode: str
  52. provider_model_bundle: ProviderModelBundle
  53. credentials: dict[str, Any] = {}
  54. parameters: dict[str, Any] = {}
  55. stop: list[str] = []
  56. # pydantic configs
  57. model_config = ConfigDict(protected_namespaces=())
  58. class AppGenerateEntity(BaseModel):
  59. """
  60. App Generate Entity.
  61. """
  62. task_id: str
  63. # app config
  64. app_config: AppConfig
  65. file_upload_config: Optional[FileUploadConfig] = None
  66. inputs: Mapping[str, Any]
  67. files: Sequence[File]
  68. user_id: str
  69. # extras
  70. stream: bool
  71. invoke_from: InvokeFrom
  72. # invoke call depth
  73. call_depth: int = 0
  74. # extra parameters, like: auto_generate_conversation_name
  75. extras: dict[str, Any] = {}
  76. # tracing instance
  77. trace_manager: Optional[TraceQueueManager] = None
  78. class Config:
  79. arbitrary_types_allowed = True
  80. class EasyUIBasedAppGenerateEntity(AppGenerateEntity):
  81. """
  82. Chat Application Generate Entity.
  83. """
  84. # app config
  85. app_config: EasyUIBasedAppConfig
  86. model_conf: ModelConfigWithCredentialsEntity
  87. query: Optional[str] = None
  88. # pydantic configs
  89. model_config = ConfigDict(protected_namespaces=())
  90. class ConversationAppGenerateEntity(AppGenerateEntity):
  91. """
  92. Base entity for conversation-based app generation.
  93. """
  94. conversation_id: Optional[str] = None
  95. parent_message_id: Optional[str] = Field(
  96. default=None,
  97. description=(
  98. "Starting from v0.9.0, parent_message_id is used to support message regeneration for internal chat API."
  99. "For service API, we need to ensure its forward compatibility, "
  100. "so passing in the parent_message_id as request arg is not supported for now. "
  101. "It needs to be set to UUID_NIL so that the subsequent processing will treat it as legacy messages."
  102. ),
  103. )
  104. @field_validator("parent_message_id")
  105. @classmethod
  106. def validate_parent_message_id(cls, v, info: ValidationInfo):
  107. if info.data.get("invoke_from") == InvokeFrom.SERVICE_API and v != UUID_NIL:
  108. raise ValueError("parent_message_id should be UUID_NIL for service API")
  109. return v
  110. class ChatAppGenerateEntity(ConversationAppGenerateEntity, EasyUIBasedAppGenerateEntity):
  111. """
  112. Chat Application Generate Entity.
  113. """
  114. pass
  115. class CompletionAppGenerateEntity(EasyUIBasedAppGenerateEntity):
  116. """
  117. Completion Application Generate Entity.
  118. """
  119. pass
  120. class AgentChatAppGenerateEntity(ConversationAppGenerateEntity, EasyUIBasedAppGenerateEntity):
  121. """
  122. Agent Chat Application Generate Entity.
  123. """
  124. pass
  125. class AdvancedChatAppGenerateEntity(ConversationAppGenerateEntity):
  126. """
  127. Advanced Chat Application Generate Entity.
  128. """
  129. # app config
  130. app_config: WorkflowUIBasedAppConfig
  131. workflow_run_id: Optional[str] = None
  132. query: str
  133. class SingleIterationRunEntity(BaseModel):
  134. """
  135. Single Iteration Run Entity.
  136. """
  137. node_id: str
  138. inputs: dict
  139. single_iteration_run: Optional[SingleIterationRunEntity] = None
  140. class WorkflowAppGenerateEntity(AppGenerateEntity):
  141. """
  142. Workflow Application Generate Entity.
  143. """
  144. # app config
  145. app_config: WorkflowUIBasedAppConfig
  146. workflow_run_id: Optional[str] = None
  147. class SingleIterationRunEntity(BaseModel):
  148. """
  149. Single Iteration Run Entity.
  150. """
  151. node_id: str
  152. inputs: dict
  153. single_iteration_run: Optional[SingleIterationRunEntity] = None