hosting_configuration.py 10 KB

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