hosting_configuration.py 9.4 KB

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