hosting_configuration.py 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. from typing import Optional
  2. from flask import Flask
  3. from pydantic import BaseModel
  4. from configs import dify_config
  5. from core.entities.provider_entities import ProviderQuotaType, QuotaUnit, RestrictModel
  6. from core.model_runtime.entities.model_entities import ModelType
  7. class HostingQuota(BaseModel):
  8. quota_type: ProviderQuotaType
  9. restrict_models: list[RestrictModel] = []
  10. class TrialHostingQuota(HostingQuota):
  11. quota_type: ProviderQuotaType = ProviderQuotaType.TRIAL
  12. quota_limit: int = 0
  13. """Quota limit for the hosting provider models. -1 means unlimited."""
  14. class PaidHostingQuota(HostingQuota):
  15. quota_type: ProviderQuotaType = ProviderQuotaType.PAID
  16. class FreeHostingQuota(HostingQuota):
  17. quota_type: ProviderQuotaType = ProviderQuotaType.FREE
  18. class HostingProvider(BaseModel):
  19. enabled: bool = False
  20. credentials: Optional[dict] = None
  21. quota_unit: Optional[QuotaUnit] = None
  22. quotas: list[HostingQuota] = []
  23. class HostedModerationConfig(BaseModel):
  24. enabled: bool = False
  25. providers: list[str] = []
  26. class HostingConfiguration:
  27. provider_map: dict[str, HostingProvider] = {}
  28. moderation_config: HostedModerationConfig = None
  29. def init_app(self, app: Flask) -> None:
  30. if dify_config.EDITION != "CLOUD":
  31. return
  32. self.provider_map["azure_openai"] = self.init_azure_openai()
  33. self.provider_map["openai"] = self.init_openai()
  34. self.provider_map["anthropic"] = self.init_anthropic()
  35. self.provider_map["minimax"] = self.init_minimax()
  36. self.provider_map["spark"] = self.init_spark()
  37. self.provider_map["zhipuai"] = self.init_zhipuai()
  38. self.moderation_config = self.init_moderation_config()
  39. @staticmethod
  40. def init_azure_openai() -> HostingProvider:
  41. quota_unit = QuotaUnit.TIMES
  42. if dify_config.HOSTED_AZURE_OPENAI_ENABLED:
  43. credentials = {
  44. "openai_api_key": dify_config.HOSTED_AZURE_OPENAI_API_KEY,
  45. "openai_api_base": dify_config.HOSTED_AZURE_OPENAI_API_BASE,
  46. "base_model_name": "gpt-35-turbo",
  47. }
  48. quotas = []
  49. hosted_quota_limit = dify_config.HOSTED_AZURE_OPENAI_QUOTA_LIMIT
  50. trial_quota = TrialHostingQuota(
  51. quota_limit=hosted_quota_limit,
  52. restrict_models=[
  53. RestrictModel(model="gpt-4", base_model_name="gpt-4", model_type=ModelType.LLM),
  54. RestrictModel(model="gpt-4o", base_model_name="gpt-4o", model_type=ModelType.LLM),
  55. RestrictModel(model="gpt-4o-mini", base_model_name="gpt-4o-mini", model_type=ModelType.LLM),
  56. RestrictModel(model="gpt-4-32k", base_model_name="gpt-4-32k", model_type=ModelType.LLM),
  57. RestrictModel(
  58. model="gpt-4-1106-preview", base_model_name="gpt-4-1106-preview", model_type=ModelType.LLM
  59. ),
  60. RestrictModel(
  61. model="gpt-4-vision-preview", base_model_name="gpt-4-vision-preview", model_type=ModelType.LLM
  62. ),
  63. RestrictModel(model="gpt-35-turbo", base_model_name="gpt-35-turbo", model_type=ModelType.LLM),
  64. RestrictModel(
  65. model="gpt-35-turbo-1106", base_model_name="gpt-35-turbo-1106", model_type=ModelType.LLM
  66. ),
  67. RestrictModel(
  68. model="gpt-35-turbo-instruct", base_model_name="gpt-35-turbo-instruct", model_type=ModelType.LLM
  69. ),
  70. RestrictModel(
  71. model="gpt-35-turbo-16k", base_model_name="gpt-35-turbo-16k", model_type=ModelType.LLM
  72. ),
  73. RestrictModel(
  74. model="text-davinci-003", base_model_name="text-davinci-003", model_type=ModelType.LLM
  75. ),
  76. RestrictModel(
  77. model="text-embedding-ada-002",
  78. base_model_name="text-embedding-ada-002",
  79. model_type=ModelType.TEXT_EMBEDDING,
  80. ),
  81. RestrictModel(
  82. model="text-embedding-3-small",
  83. base_model_name="text-embedding-3-small",
  84. model_type=ModelType.TEXT_EMBEDDING,
  85. ),
  86. RestrictModel(
  87. model="text-embedding-3-large",
  88. base_model_name="text-embedding-3-large",
  89. model_type=ModelType.TEXT_EMBEDDING,
  90. ),
  91. ],
  92. )
  93. quotas.append(trial_quota)
  94. return HostingProvider(enabled=True, credentials=credentials, quota_unit=quota_unit, quotas=quotas)
  95. return HostingProvider(
  96. enabled=False,
  97. quota_unit=quota_unit,
  98. )
  99. def init_openai(self) -> HostingProvider:
  100. quota_unit = QuotaUnit.CREDITS
  101. quotas = []
  102. if dify_config.HOSTED_OPENAI_TRIAL_ENABLED:
  103. hosted_quota_limit = dify_config.HOSTED_OPENAI_QUOTA_LIMIT
  104. trial_models = self.parse_restrict_models_from_env("HOSTED_OPENAI_TRIAL_MODELS")
  105. trial_quota = TrialHostingQuota(quota_limit=hosted_quota_limit, restrict_models=trial_models)
  106. quotas.append(trial_quota)
  107. if dify_config.HOSTED_OPENAI_PAID_ENABLED:
  108. paid_models = self.parse_restrict_models_from_env("HOSTED_OPENAI_PAID_MODELS")
  109. paid_quota = PaidHostingQuota(restrict_models=paid_models)
  110. quotas.append(paid_quota)
  111. if len(quotas) > 0:
  112. credentials = {
  113. "openai_api_key": dify_config.HOSTED_OPENAI_API_KEY,
  114. }
  115. if dify_config.HOSTED_OPENAI_API_BASE:
  116. credentials["openai_api_base"] = dify_config.HOSTED_OPENAI_API_BASE
  117. if dify_config.HOSTED_OPENAI_API_ORGANIZATION:
  118. credentials["openai_organization"] = dify_config.HOSTED_OPENAI_API_ORGANIZATION
  119. return HostingProvider(enabled=True, credentials=credentials, quota_unit=quota_unit, quotas=quotas)
  120. return HostingProvider(
  121. enabled=False,
  122. quota_unit=quota_unit,
  123. )
  124. @staticmethod
  125. def init_anthropic() -> HostingProvider:
  126. quota_unit = QuotaUnit.TOKENS
  127. quotas = []
  128. if dify_config.HOSTED_ANTHROPIC_TRIAL_ENABLED:
  129. hosted_quota_limit = dify_config.HOSTED_ANTHROPIC_QUOTA_LIMIT
  130. trial_quota = TrialHostingQuota(quota_limit=hosted_quota_limit)
  131. quotas.append(trial_quota)
  132. if dify_config.HOSTED_ANTHROPIC_PAID_ENABLED:
  133. paid_quota = PaidHostingQuota()
  134. quotas.append(paid_quota)
  135. if len(quotas) > 0:
  136. credentials = {
  137. "anthropic_api_key": dify_config.HOSTED_ANTHROPIC_API_KEY,
  138. }
  139. if dify_config.HOSTED_ANTHROPIC_API_BASE:
  140. credentials["anthropic_api_url"] = dify_config.HOSTED_ANTHROPIC_API_BASE
  141. return HostingProvider(enabled=True, credentials=credentials, quota_unit=quota_unit, quotas=quotas)
  142. return HostingProvider(
  143. enabled=False,
  144. quota_unit=quota_unit,
  145. )
  146. @staticmethod
  147. def init_minimax() -> HostingProvider:
  148. quota_unit = QuotaUnit.TOKENS
  149. if dify_config.HOSTED_MINIMAX_ENABLED:
  150. quotas = [FreeHostingQuota()]
  151. return HostingProvider(
  152. enabled=True,
  153. credentials=None, # use credentials from the provider
  154. quota_unit=quota_unit,
  155. quotas=quotas,
  156. )
  157. return HostingProvider(
  158. enabled=False,
  159. quota_unit=quota_unit,
  160. )
  161. @staticmethod
  162. def init_spark() -> HostingProvider:
  163. quota_unit = QuotaUnit.TOKENS
  164. if dify_config.HOSTED_SPARK_ENABLED:
  165. quotas = [FreeHostingQuota()]
  166. return HostingProvider(
  167. enabled=True,
  168. credentials=None, # use credentials from the provider
  169. quota_unit=quota_unit,
  170. quotas=quotas,
  171. )
  172. return HostingProvider(
  173. enabled=False,
  174. quota_unit=quota_unit,
  175. )
  176. @staticmethod
  177. def init_zhipuai() -> HostingProvider:
  178. quota_unit = QuotaUnit.TOKENS
  179. if dify_config.HOSTED_ZHIPUAI_ENABLED:
  180. quotas = [FreeHostingQuota()]
  181. return HostingProvider(
  182. enabled=True,
  183. credentials=None, # use credentials from the provider
  184. quota_unit=quota_unit,
  185. quotas=quotas,
  186. )
  187. return HostingProvider(
  188. enabled=False,
  189. quota_unit=quota_unit,
  190. )
  191. @staticmethod
  192. def init_moderation_config() -> HostedModerationConfig:
  193. if dify_config.HOSTED_MODERATION_ENABLED and dify_config.HOSTED_MODERATION_PROVIDERS:
  194. return HostedModerationConfig(enabled=True, providers=dify_config.HOSTED_MODERATION_PROVIDERS.split(","))
  195. return HostedModerationConfig(enabled=False)
  196. @staticmethod
  197. def parse_restrict_models_from_env(env_var: str) -> list[RestrictModel]:
  198. models_str = dify_config.model_dump().get(env_var)
  199. models_list = models_str.split(",") if models_str else []
  200. return [
  201. RestrictModel(model=model_name.strip(), model_type=ModelType.LLM)
  202. for model_name in models_list
  203. if model_name.strip()
  204. ]