app_invoke_entities.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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. class CompletionAppGenerateEntity(EasyUIBasedAppGenerateEntity):
  94. """
  95. Completion Application Generate Entity.
  96. """
  97. pass
  98. class AgentChatAppGenerateEntity(EasyUIBasedAppGenerateEntity):
  99. """
  100. Agent Chat Application Generate Entity.
  101. """
  102. conversation_id: Optional[str] = None
  103. class AdvancedChatAppGenerateEntity(AppGenerateEntity):
  104. """
  105. Advanced Chat Application Generate Entity.
  106. """
  107. # app config
  108. app_config: WorkflowUIBasedAppConfig
  109. conversation_id: Optional[str] = None
  110. query: str
  111. class SingleIterationRunEntity(BaseModel):
  112. """
  113. Single Iteration Run Entity.
  114. """
  115. node_id: str
  116. inputs: dict
  117. single_iteration_run: Optional[SingleIterationRunEntity] = None
  118. class WorkflowAppGenerateEntity(AppGenerateEntity):
  119. """
  120. Workflow Application Generate Entity.
  121. """
  122. # app config
  123. app_config: WorkflowUIBasedAppConfig
  124. class SingleIterationRunEntity(BaseModel):
  125. """
  126. Single Iteration Run Entity.
  127. """
  128. node_id: str
  129. inputs: dict
  130. single_iteration_run: Optional[SingleIterationRunEntity] = None