app_invoke_entities.py 3.7 KB

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