hosting_configuration.py 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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. ]
  64. )
  65. quotas.append(trial_quota)
  66. return HostingProvider(
  67. enabled=True,
  68. credentials=credentials,
  69. quota_unit=quota_unit,
  70. quotas=quotas
  71. )
  72. return HostingProvider(
  73. enabled=False,
  74. quota_unit=quota_unit,
  75. )
  76. def init_openai(self, app_config: Config) -> HostingProvider:
  77. quota_unit = QuotaUnit.CREDITS
  78. quotas = []
  79. if app_config.get("HOSTED_OPENAI_TRIAL_ENABLED"):
  80. hosted_quota_limit = int(app_config.get("HOSTED_OPENAI_QUOTA_LIMIT", "200"))
  81. trial_models = self.parse_restrict_models_from_env(app_config, "HOSTED_OPENAI_TRIAL_MODELS")
  82. trial_quota = TrialHostingQuota(
  83. quota_limit=hosted_quota_limit,
  84. restrict_models=trial_models
  85. )
  86. quotas.append(trial_quota)
  87. if app_config.get("HOSTED_OPENAI_PAID_ENABLED"):
  88. paid_models = self.parse_restrict_models_from_env(app_config, "HOSTED_OPENAI_PAID_MODELS")
  89. paid_quota = PaidHostingQuota(
  90. restrict_models=paid_models
  91. )
  92. quotas.append(paid_quota)
  93. if len(quotas) > 0:
  94. credentials = {
  95. "openai_api_key": app_config.get("HOSTED_OPENAI_API_KEY"),
  96. }
  97. if app_config.get("HOSTED_OPENAI_API_BASE"):
  98. credentials["openai_api_base"] = app_config.get("HOSTED_OPENAI_API_BASE")
  99. if app_config.get("HOSTED_OPENAI_API_ORGANIZATION"):
  100. credentials["openai_organization"] = app_config.get("HOSTED_OPENAI_API_ORGANIZATION")
  101. return HostingProvider(
  102. enabled=True,
  103. credentials=credentials,
  104. quota_unit=quota_unit,
  105. quotas=quotas
  106. )
  107. return HostingProvider(
  108. enabled=False,
  109. quota_unit=quota_unit,
  110. )
  111. def init_anthropic(self, app_config: Config) -> HostingProvider:
  112. quota_unit = QuotaUnit.TOKENS
  113. quotas = []
  114. if app_config.get("HOSTED_ANTHROPIC_TRIAL_ENABLED"):
  115. hosted_quota_limit = int(app_config.get("HOSTED_ANTHROPIC_QUOTA_LIMIT", "0"))
  116. trial_quota = TrialHostingQuota(
  117. quota_limit=hosted_quota_limit
  118. )
  119. quotas.append(trial_quota)
  120. if app_config.get("HOSTED_ANTHROPIC_PAID_ENABLED"):
  121. paid_quota = PaidHostingQuota()
  122. quotas.append(paid_quota)
  123. if len(quotas) > 0:
  124. credentials = {
  125. "anthropic_api_key": app_config.get("HOSTED_ANTHROPIC_API_KEY"),
  126. }
  127. if app_config.get("HOSTED_ANTHROPIC_API_BASE"):
  128. credentials["anthropic_api_url"] = app_config.get("HOSTED_ANTHROPIC_API_BASE")
  129. return HostingProvider(
  130. enabled=True,
  131. credentials=credentials,
  132. quota_unit=quota_unit,
  133. quotas=quotas
  134. )
  135. return HostingProvider(
  136. enabled=False,
  137. quota_unit=quota_unit,
  138. )
  139. def init_minimax(self, app_config: Config) -> HostingProvider:
  140. quota_unit = QuotaUnit.TOKENS
  141. if app_config.get("HOSTED_MINIMAX_ENABLED"):
  142. quotas = [FreeHostingQuota()]
  143. return HostingProvider(
  144. enabled=True,
  145. credentials=None, # use credentials from the provider
  146. quota_unit=quota_unit,
  147. quotas=quotas
  148. )
  149. return HostingProvider(
  150. enabled=False,
  151. quota_unit=quota_unit,
  152. )
  153. def init_spark(self, app_config: Config) -> HostingProvider:
  154. quota_unit = QuotaUnit.TOKENS
  155. if app_config.get("HOSTED_SPARK_ENABLED"):
  156. quotas = [FreeHostingQuota()]
  157. return HostingProvider(
  158. enabled=True,
  159. credentials=None, # use credentials from the provider
  160. quota_unit=quota_unit,
  161. quotas=quotas
  162. )
  163. return HostingProvider(
  164. enabled=False,
  165. quota_unit=quota_unit,
  166. )
  167. def init_zhipuai(self, app_config: Config) -> HostingProvider:
  168. quota_unit = QuotaUnit.TOKENS
  169. if app_config.get("HOSTED_ZHIPUAI_ENABLED"):
  170. quotas = [FreeHostingQuota()]
  171. return HostingProvider(
  172. enabled=True,
  173. credentials=None, # use credentials from the provider
  174. quota_unit=quota_unit,
  175. quotas=quotas
  176. )
  177. return HostingProvider(
  178. enabled=False,
  179. quota_unit=quota_unit,
  180. )
  181. def init_moderation_config(self, app_config: Config) -> HostedModerationConfig:
  182. if app_config.get("HOSTED_MODERATION_ENABLED") \
  183. and app_config.get("HOSTED_MODERATION_PROVIDERS"):
  184. return HostedModerationConfig(
  185. enabled=True,
  186. providers=app_config.get("HOSTED_MODERATION_PROVIDERS").split(',')
  187. )
  188. return HostedModerationConfig(
  189. enabled=False
  190. )
  191. @staticmethod
  192. def parse_restrict_models_from_env(app_config: Config, env_var: str) -> list[RestrictModel]:
  193. models_str = app_config.get(env_var)
  194. models_list = models_str.split(",") if models_str else []
  195. return [RestrictModel(model=model_name.strip(), model_type=ModelType.LLM) for model_name in models_list if
  196. model_name.strip()]