app_invoke_entities.py 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. from collections.abc import Mapping
  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.file_obj import FileVar
  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) -> "InvokeFrom":
  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. inputs: Mapping[str, Any]
  66. files: list[FileVar] = []
  67. user_id: str
  68. # extras
  69. stream: bool
  70. invoke_from: InvokeFrom
  71. # invoke call depth
  72. call_depth: int = 0
  73. # extra parameters, like: auto_generate_conversation_name
  74. extras: dict[str, Any] = {}
  75. # tracing instance
  76. trace_manager: Optional[TraceQueueManager] = None
  77. class Config:
  78. arbitrary_types_allowed = True
  79. class EasyUIBasedAppGenerateEntity(AppGenerateEntity):
  80. """
  81. Chat Application Generate Entity.
  82. """
  83. # app config
  84. app_config: EasyUIBasedAppConfig
  85. model_conf: ModelConfigWithCredentialsEntity
  86. query: Optional[str] = None
  87. # pydantic configs
  88. model_config = ConfigDict(protected_namespaces=())
  89. class ConversationAppGenerateEntity(AppGenerateEntity):
  90. """
  91. Base entity for conversation-based app generation.
  92. """
  93. conversation_id: Optional[str] = None
  94. parent_message_id: Optional[str] = Field(
  95. default=None,
  96. description=(
  97. "Starting from v0.9.0, parent_message_id is used to support message regeneration for internal chat API."
  98. "For service API, we need to ensure its forward compatibility, "
  99. "so passing in the parent_message_id as request arg is not supported for now. "
  100. "It needs to be set to UUID_NIL so that the subsequent processing will treat it as legacy messages."
  101. ),
  102. )
  103. @field_validator("parent_message_id")
  104. @classmethod
  105. def validate_parent_message_id(cls, v, info: ValidationInfo):
  106. if info.data.get("invoke_from") == InvokeFrom.SERVICE_API and v != UUID_NIL:
  107. raise ValueError("parent_message_id should be UUID_NIL for service API")
  108. return v
  109. class ChatAppGenerateEntity(ConversationAppGenerateEntity, EasyUIBasedAppGenerateEntity):
  110. """
  111. Chat Application Generate Entity.
  112. """
  113. pass
  114. class CompletionAppGenerateEntity(EasyUIBasedAppGenerateEntity):
  115. """
  116. Completion Application Generate Entity.
  117. """
  118. pass
  119. class AgentChatAppGenerateEntity(ConversationAppGenerateEntity, EasyUIBasedAppGenerateEntity):
  120. """
  121. Agent Chat Application Generate Entity.
  122. """
  123. pass
  124. class AdvancedChatAppGenerateEntity(ConversationAppGenerateEntity):
  125. """
  126. Advanced Chat Application Generate Entity.
  127. """
  128. # app config
  129. app_config: WorkflowUIBasedAppConfig
  130. workflow_run_id: Optional[str] = None
  131. query: str
  132. class SingleIterationRunEntity(BaseModel):
  133. """
  134. Single Iteration Run Entity.
  135. """
  136. node_id: str
  137. inputs: dict
  138. single_iteration_run: Optional[SingleIterationRunEntity] = None
  139. class WorkflowAppGenerateEntity(AppGenerateEntity):
  140. """
  141. Workflow Application Generate Entity.
  142. """
  143. # app config
  144. app_config: WorkflowUIBasedAppConfig
  145. workflow_run_id: Optional[str] = None
  146. class SingleIterationRunEntity(BaseModel):
  147. """
  148. Single Iteration Run Entity.
  149. """
  150. node_id: str
  151. inputs: dict
  152. single_iteration_run: Optional[SingleIterationRunEntity] = None