hosting_configuration.py 9.4 KB

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