app_invoke_entities.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. from collections.abc import Mapping
  2. from enum import Enum
  3. from typing import Any, Optional
  4. from pydantic import BaseModel, ConfigDict
  5. from core.app.app_config.entities import AppConfig, EasyUIBasedAppConfig, WorkflowUIBasedAppConfig
  6. from core.entities.provider_configuration import ProviderModelBundle
  7. from core.file.file_obj import FileVar
  8. from core.model_runtime.entities.model_entities import AIModelEntity
  9. from core.ops.ops_trace_manager import TraceQueueManager
  10. class InvokeFrom(Enum):
  11. """
  12. Invoke From.
  13. """
  14. SERVICE_API = "service-api"
  15. WEB_APP = "web-app"
  16. EXPLORE = "explore"
  17. DEBUGGER = "debugger"
  18. @classmethod
  19. def value_of(cls, value: str) -> "InvokeFrom":
  20. """
  21. Get value of given mode.
  22. :param value: mode value
  23. :return: mode
  24. """
  25. for mode in cls:
  26. if mode.value == value:
  27. return mode
  28. raise ValueError(f"invalid invoke from value {value}")
  29. def to_source(self) -> str:
  30. """
  31. Get source of invoke from.
  32. :return: source
  33. """
  34. if self == InvokeFrom.WEB_APP:
  35. return "web_app"
  36. elif self == InvokeFrom.DEBUGGER:
  37. return "dev"
  38. elif self == InvokeFrom.EXPLORE:
  39. return "explore_app"
  40. elif self == InvokeFrom.SERVICE_API:
  41. return "api"
  42. return "dev"
  43. class ModelConfigWithCredentialsEntity(BaseModel):
  44. """
  45. Model Config With Credentials Entity.
  46. """
  47. provider: str
  48. model: str
  49. model_schema: AIModelEntity
  50. mode: str
  51. provider_model_bundle: ProviderModelBundle
  52. credentials: dict[str, Any] = {}
  53. parameters: dict[str, Any] = {}
  54. stop: list[str] = []
  55. # pydantic configs
  56. model_config = ConfigDict(protected_namespaces=())
  57. class AppGenerateEntity(BaseModel):
  58. """
  59. App Generate Entity.
  60. """
  61. task_id: str
  62. # app config
  63. app_config: AppConfig
  64. inputs: Mapping[str, Any]
  65. files: list[FileVar] = []
  66. user_id: str
  67. # extras
  68. stream: bool
  69. invoke_from: InvokeFrom
  70. # invoke call depth
  71. call_depth: int = 0
  72. # extra parameters, like: auto_generate_conversation_name
  73. extras: dict[str, Any] = {}
  74. # tracing instance
  75. trace_manager: Optional[TraceQueueManager] = None
  76. class Config:
  77. arbitrary_types_allowed = True
  78. class EasyUIBasedAppGenerateEntity(AppGenerateEntity):
  79. """
  80. Chat Application Generate Entity.
  81. """
  82. # app config
  83. app_config: EasyUIBasedAppConfig
  84. model_conf: ModelConfigWithCredentialsEntity
  85. query: Optional[str] = None
  86. # pydantic configs
  87. model_config = ConfigDict(protected_namespaces=())
  88. class ChatAppGenerateEntity(EasyUIBasedAppGenerateEntity):
  89. """
  90. Chat Application Generate Entity.
  91. """
  92. conversation_id: Optional[str] = None
  93. parent_message_id: Optional[str] = None
  94. class CompletionAppGenerateEntity(EasyUIBasedAppGenerateEntity):
  95. """
  96. Completion Application Generate Entity.
  97. """
  98. pass
  99. class AgentChatAppGenerateEntity(EasyUIBasedAppGenerateEntity):
  100. """
  101. Agent Chat Application Generate Entity.
  102. """
  103. conversation_id: Optional[str] = None
  104. parent_message_id: Optional[str] = None
  105. class AdvancedChatAppGenerateEntity(AppGenerateEntity):
  106. """
  107. Advanced Chat Application Generate Entity.
  108. """
  109. # app config
  110. app_config: WorkflowUIBasedAppConfig
  111. conversation_id: Optional[str] = None
  112. parent_message_id: Optional[str] = None
  113. query: str
  114. class SingleIterationRunEntity(BaseModel):
  115. """
  116. Single Iteration Run Entity.
  117. """
  118. node_id: str
  119. inputs: dict
  120. single_iteration_run: Optional[SingleIterationRunEntity] = None
  121. class WorkflowAppGenerateEntity(AppGenerateEntity):
  122. """
  123. Workflow Application Generate Entity.
  124. """
  125. # app config
  126. app_config: WorkflowUIBasedAppConfig
  127. class SingleIterationRunEntity(BaseModel):
  128. """
  129. Single Iteration Run Entity.
  130. """
  131. node_id: str
  132. inputs: dict
  133. single_iteration_run: Optional[SingleIterationRunEntity] = None