hosting_configuration.py 9.8 KB

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