model_provider_service.py 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539
  1. import logging
  2. import os
  3. from typing import Optional
  4. import requests
  5. from core.entities.model_entities import ModelStatus, ProviderModelWithStatusEntity
  6. from core.model_runtime.entities.model_entities import ModelType, ParameterRule
  7. from core.model_runtime.model_providers.model_provider_factory import ModelProviderFactory
  8. from core.provider_manager import ProviderManager
  9. from models.provider import ProviderType
  10. from services.entities.model_provider_entities import (
  11. CustomConfigurationResponse,
  12. CustomConfigurationStatus,
  13. DefaultModelResponse,
  14. ModelWithProviderEntityResponse,
  15. ProviderResponse,
  16. ProviderWithModelsResponse,
  17. SimpleProviderEntityResponse,
  18. SystemConfigurationResponse,
  19. )
  20. logger = logging.getLogger(__name__)
  21. class ModelProviderService:
  22. """
  23. Model Provider Service
  24. """
  25. def __init__(self) -> None:
  26. self.provider_manager = ProviderManager()
  27. def get_provider_list(self, tenant_id: str, model_type: Optional[str] = None) -> list[ProviderResponse]:
  28. """
  29. get provider list.
  30. :param tenant_id: workspace id
  31. :param model_type: model type
  32. :return:
  33. """
  34. # Get all provider configurations of the current workspace
  35. provider_configurations = self.provider_manager.get_configurations(tenant_id)
  36. provider_responses = []
  37. for provider_configuration in provider_configurations.values():
  38. if model_type:
  39. model_type_entity = ModelType.value_of(model_type)
  40. if model_type_entity not in provider_configuration.provider.supported_model_types:
  41. continue
  42. provider_response = ProviderResponse(
  43. provider=provider_configuration.provider.provider,
  44. label=provider_configuration.provider.label,
  45. description=provider_configuration.provider.description,
  46. icon_small=provider_configuration.provider.icon_small,
  47. icon_large=provider_configuration.provider.icon_large,
  48. background=provider_configuration.provider.background,
  49. help=provider_configuration.provider.help,
  50. supported_model_types=provider_configuration.provider.supported_model_types,
  51. configurate_methods=provider_configuration.provider.configurate_methods,
  52. provider_credential_schema=provider_configuration.provider.provider_credential_schema,
  53. model_credential_schema=provider_configuration.provider.model_credential_schema,
  54. preferred_provider_type=provider_configuration.preferred_provider_type,
  55. custom_configuration=CustomConfigurationResponse(
  56. status=CustomConfigurationStatus.ACTIVE
  57. if provider_configuration.is_custom_configuration_available()
  58. else CustomConfigurationStatus.NO_CONFIGURE
  59. ),
  60. system_configuration=SystemConfigurationResponse(
  61. enabled=provider_configuration.system_configuration.enabled,
  62. current_quota_type=provider_configuration.system_configuration.current_quota_type,
  63. quota_configurations=provider_configuration.system_configuration.quota_configurations,
  64. ),
  65. )
  66. provider_responses.append(provider_response)
  67. return provider_responses
  68. def get_models_by_provider(self, tenant_id: str, provider: str) -> list[ModelWithProviderEntityResponse]:
  69. """
  70. get provider models.
  71. For the model provider page,
  72. only supports passing in a single provider to query the list of supported models.
  73. :param tenant_id:
  74. :param provider:
  75. :return:
  76. """
  77. # Get all provider configurations of the current workspace
  78. provider_configurations = self.provider_manager.get_configurations(tenant_id)
  79. # Get provider available models
  80. return [
  81. ModelWithProviderEntityResponse(model) for model in provider_configurations.get_models(provider=provider)
  82. ]
  83. def get_provider_credentials(self, tenant_id: str, provider: str) -> Optional[dict]:
  84. """
  85. get provider credentials.
  86. :param tenant_id:
  87. :param provider:
  88. :return:
  89. """
  90. # Get all provider configurations of the current workspace
  91. provider_configurations = self.provider_manager.get_configurations(tenant_id)
  92. # Get provider configuration
  93. provider_configuration = provider_configurations.get(provider)
  94. if not provider_configuration:
  95. raise ValueError(f"Provider {provider} does not exist.")
  96. # Get provider custom credentials from workspace
  97. return provider_configuration.get_custom_credentials(obfuscated=True)
  98. def provider_credentials_validate(self, tenant_id: str, provider: str, credentials: dict) -> None:
  99. """
  100. validate provider credentials.
  101. :param tenant_id:
  102. :param provider:
  103. :param credentials:
  104. """
  105. # Get all provider configurations of the current workspace
  106. provider_configurations = self.provider_manager.get_configurations(tenant_id)
  107. # Get provider configuration
  108. provider_configuration = provider_configurations.get(provider)
  109. if not provider_configuration:
  110. raise ValueError(f"Provider {provider} does not exist.")
  111. provider_configuration.custom_credentials_validate(credentials)
  112. def save_provider_credentials(self, tenant_id: str, provider: str, credentials: dict) -> None:
  113. """
  114. save custom provider config.
  115. :param tenant_id: workspace id
  116. :param provider: provider name
  117. :param credentials: provider credentials
  118. :return:
  119. """
  120. # Get all provider configurations of the current workspace
  121. provider_configurations = self.provider_manager.get_configurations(tenant_id)
  122. # Get provider configuration
  123. provider_configuration = provider_configurations.get(provider)
  124. if not provider_configuration:
  125. raise ValueError(f"Provider {provider} does not exist.")
  126. # Add or update custom provider credentials.
  127. provider_configuration.add_or_update_custom_credentials(credentials)
  128. def remove_provider_credentials(self, tenant_id: str, provider: str) -> None:
  129. """
  130. remove custom provider config.
  131. :param tenant_id: workspace id
  132. :param provider: provider name
  133. :return:
  134. """
  135. # Get all provider configurations of the current workspace
  136. provider_configurations = self.provider_manager.get_configurations(tenant_id)
  137. # Get provider configuration
  138. provider_configuration = provider_configurations.get(provider)
  139. if not provider_configuration:
  140. raise ValueError(f"Provider {provider} does not exist.")
  141. # Remove custom provider credentials.
  142. provider_configuration.delete_custom_credentials()
  143. def get_model_credentials(self, tenant_id: str, provider: str, model_type: str, model: str) -> Optional[dict]:
  144. """
  145. get model credentials.
  146. :param tenant_id: workspace id
  147. :param provider: provider name
  148. :param model_type: model type
  149. :param model: model name
  150. :return:
  151. """
  152. # Get all provider configurations of the current workspace
  153. provider_configurations = self.provider_manager.get_configurations(tenant_id)
  154. # Get provider configuration
  155. provider_configuration = provider_configurations.get(provider)
  156. if not provider_configuration:
  157. raise ValueError(f"Provider {provider} does not exist.")
  158. # Get model custom credentials from ProviderModel if exists
  159. return provider_configuration.get_custom_model_credentials(
  160. model_type=ModelType.value_of(model_type), model=model, obfuscated=True
  161. )
  162. def model_credentials_validate(
  163. self, tenant_id: str, provider: str, model_type: str, model: str, credentials: dict
  164. ) -> None:
  165. """
  166. validate model credentials.
  167. :param tenant_id: workspace id
  168. :param provider: provider name
  169. :param model_type: model type
  170. :param model: model name
  171. :param credentials: model credentials
  172. :return:
  173. """
  174. # Get all provider configurations of the current workspace
  175. provider_configurations = self.provider_manager.get_configurations(tenant_id)
  176. # Get provider configuration
  177. provider_configuration = provider_configurations.get(provider)
  178. if not provider_configuration:
  179. raise ValueError(f"Provider {provider} does not exist.")
  180. # Validate model credentials
  181. provider_configuration.custom_model_credentials_validate(
  182. model_type=ModelType.value_of(model_type), model=model, credentials=credentials
  183. )
  184. def save_model_credentials(
  185. self, tenant_id: str, provider: str, model_type: str, model: str, credentials: dict
  186. ) -> None:
  187. """
  188. save model credentials.
  189. :param tenant_id: workspace id
  190. :param provider: provider name
  191. :param model_type: model type
  192. :param model: model name
  193. :param credentials: model credentials
  194. :return:
  195. """
  196. # Get all provider configurations of the current workspace
  197. provider_configurations = self.provider_manager.get_configurations(tenant_id)
  198. # Get provider configuration
  199. provider_configuration = provider_configurations.get(provider)
  200. if not provider_configuration:
  201. raise ValueError(f"Provider {provider} does not exist.")
  202. # Add or update custom model credentials
  203. provider_configuration.add_or_update_custom_model_credentials(
  204. model_type=ModelType.value_of(model_type), model=model, credentials=credentials
  205. )
  206. def remove_model_credentials(self, tenant_id: str, provider: str, model_type: str, model: str) -> None:
  207. """
  208. remove model credentials.
  209. :param tenant_id: workspace id
  210. :param provider: provider name
  211. :param model_type: model type
  212. :param model: model name
  213. :return:
  214. """
  215. # Get all provider configurations of the current workspace
  216. provider_configurations = self.provider_manager.get_configurations(tenant_id)
  217. # Get provider configuration
  218. provider_configuration = provider_configurations.get(provider)
  219. if not provider_configuration:
  220. raise ValueError(f"Provider {provider} does not exist.")
  221. # Remove custom model credentials
  222. provider_configuration.delete_custom_model_credentials(model_type=ModelType.value_of(model_type), model=model)
  223. def get_models_by_model_type(self, tenant_id: str, model_type: str) -> list[ProviderWithModelsResponse]:
  224. """
  225. get models by model type.
  226. :param tenant_id: workspace id
  227. :param model_type: model type
  228. :return:
  229. """
  230. # Get all provider configurations of the current workspace
  231. provider_configurations = self.provider_manager.get_configurations(tenant_id)
  232. # Get provider available models
  233. models = provider_configurations.get_models(model_type=ModelType.value_of(model_type))
  234. # Group models by provider
  235. provider_models = {}
  236. for model in models:
  237. if model.provider.provider not in provider_models:
  238. provider_models[model.provider.provider] = []
  239. if model.deprecated:
  240. continue
  241. if model.status != ModelStatus.ACTIVE:
  242. continue
  243. provider_models[model.provider.provider].append(model)
  244. # convert to ProviderWithModelsResponse list
  245. providers_with_models: list[ProviderWithModelsResponse] = []
  246. for provider, models in provider_models.items():
  247. if not models:
  248. continue
  249. first_model = models[0]
  250. providers_with_models.append(
  251. ProviderWithModelsResponse(
  252. provider=provider,
  253. label=first_model.provider.label,
  254. icon_small=first_model.provider.icon_small,
  255. icon_large=first_model.provider.icon_large,
  256. status=CustomConfigurationStatus.ACTIVE,
  257. models=[
  258. ProviderModelWithStatusEntity(
  259. model=model.model,
  260. label=model.label,
  261. model_type=model.model_type,
  262. features=model.features,
  263. fetch_from=model.fetch_from,
  264. model_properties=model.model_properties,
  265. status=model.status,
  266. load_balancing_enabled=model.load_balancing_enabled,
  267. )
  268. for model in models
  269. ],
  270. )
  271. )
  272. return providers_with_models
  273. def get_model_parameter_rules(self, tenant_id: str, provider: str, model: str) -> list[ParameterRule]:
  274. """
  275. get model parameter rules.
  276. Only supports LLM.
  277. :param tenant_id: workspace id
  278. :param provider: provider name
  279. :param model: model name
  280. :return:
  281. """
  282. # Get all provider configurations of the current workspace
  283. provider_configurations = self.provider_manager.get_configurations(tenant_id)
  284. # Get provider configuration
  285. provider_configuration = provider_configurations.get(provider)
  286. if not provider_configuration:
  287. raise ValueError(f"Provider {provider} does not exist.")
  288. # fetch credentials
  289. credentials = provider_configuration.get_current_credentials(model_type=ModelType.LLM, model=model)
  290. if not credentials:
  291. return []
  292. model_schema = provider_configuration.get_model_schema(
  293. model_type=ModelType.LLM, model=model, credentials=credentials
  294. )
  295. return model_schema.parameter_rules if model_schema else []
  296. def get_default_model_of_model_type(self, tenant_id: str, model_type: str) -> Optional[DefaultModelResponse]:
  297. """
  298. get default model of model type.
  299. :param tenant_id: workspace id
  300. :param model_type: model type
  301. :return:
  302. """
  303. model_type_enum = ModelType.value_of(model_type)
  304. result = self.provider_manager.get_default_model(tenant_id=tenant_id, model_type=model_type_enum)
  305. try:
  306. return (
  307. DefaultModelResponse(
  308. model=result.model,
  309. model_type=result.model_type,
  310. provider=SimpleProviderEntityResponse(
  311. provider=result.provider.provider,
  312. label=result.provider.label,
  313. icon_small=result.provider.icon_small,
  314. icon_large=result.provider.icon_large,
  315. supported_model_types=result.provider.supported_model_types,
  316. ),
  317. )
  318. if result
  319. else None
  320. )
  321. except Exception as e:
  322. logger.info(f"get_default_model_of_model_type error: {e}")
  323. return None
  324. def update_default_model_of_model_type(self, tenant_id: str, model_type: str, provider: str, model: str) -> None:
  325. """
  326. update default model of model type.
  327. :param tenant_id: workspace id
  328. :param model_type: model type
  329. :param provider: provider name
  330. :param model: model name
  331. :return:
  332. """
  333. model_type_enum = ModelType.value_of(model_type)
  334. self.provider_manager.update_default_model_record(
  335. tenant_id=tenant_id, model_type=model_type_enum, provider=provider, model=model
  336. )
  337. def get_model_provider_icon(
  338. self, tenant_id: str, provider: str, icon_type: str, lang: str
  339. ) -> tuple[Optional[bytes], Optional[str]]:
  340. """
  341. get model provider icon.
  342. :param tenant_id: workspace id
  343. :param provider: provider name
  344. :param icon_type: icon type (icon_small or icon_large)
  345. :param lang: language (zh_Hans or en_US)
  346. :return:
  347. """
  348. model_provider_factory = ModelProviderFactory(tenant_id)
  349. byte_data = model_provider_factory.get_provider_icon(provider, icon_type, lang)
  350. return byte_data, "application/octet-stream"
  351. def switch_preferred_provider(self, tenant_id: str, provider: str, preferred_provider_type: str) -> None:
  352. """
  353. switch preferred provider.
  354. :param tenant_id: workspace id
  355. :param provider: provider name
  356. :param preferred_provider_type: preferred provider type
  357. :return:
  358. """
  359. # Get all provider configurations of the current workspace
  360. provider_configurations = self.provider_manager.get_configurations(tenant_id)
  361. # Convert preferred_provider_type to ProviderType
  362. preferred_provider_type_enum = ProviderType.value_of(preferred_provider_type)
  363. # Get provider configuration
  364. provider_configuration = provider_configurations.get(provider)
  365. if not provider_configuration:
  366. raise ValueError(f"Provider {provider} does not exist.")
  367. # Switch preferred provider type
  368. provider_configuration.switch_preferred_provider_type(preferred_provider_type_enum)
  369. def enable_model(self, tenant_id: str, provider: str, model: str, model_type: str) -> None:
  370. """
  371. enable model.
  372. :param tenant_id: workspace id
  373. :param provider: provider name
  374. :param model: model name
  375. :param model_type: model type
  376. :return:
  377. """
  378. # Get all provider configurations of the current workspace
  379. provider_configurations = self.provider_manager.get_configurations(tenant_id)
  380. # Get provider configuration
  381. provider_configuration = provider_configurations.get(provider)
  382. if not provider_configuration:
  383. raise ValueError(f"Provider {provider} does not exist.")
  384. # Enable model
  385. provider_configuration.enable_model(model=model, model_type=ModelType.value_of(model_type))
  386. def disable_model(self, tenant_id: str, provider: str, model: str, model_type: str) -> None:
  387. """
  388. disable model.
  389. :param tenant_id: workspace id
  390. :param provider: provider name
  391. :param model: model name
  392. :param model_type: model type
  393. :return:
  394. """
  395. # Get all provider configurations of the current workspace
  396. provider_configurations = self.provider_manager.get_configurations(tenant_id)
  397. # Get provider configuration
  398. provider_configuration = provider_configurations.get(provider)
  399. if not provider_configuration:
  400. raise ValueError(f"Provider {provider} does not exist.")
  401. # Enable model
  402. provider_configuration.disable_model(model=model, model_type=ModelType.value_of(model_type))
  403. def free_quota_submit(self, tenant_id: str, provider: str):
  404. api_key = os.environ.get("FREE_QUOTA_APPLY_API_KEY")
  405. api_base_url = os.environ.get("FREE_QUOTA_APPLY_BASE_URL")
  406. if not api_base_url:
  407. raise Exception("FREE_QUOTA_APPLY_BASE_URL is not set")
  408. api_url = api_base_url + "/api/v1/providers/apply"
  409. headers = {"Content-Type": "application/json", "Authorization": f"Bearer {api_key}"}
  410. response = requests.post(api_url, headers=headers, json={"workspace_id": tenant_id, "provider_name": provider})
  411. if not response.ok:
  412. logger.error(f"Request FREE QUOTA APPLY SERVER Error: {response.status_code} ")
  413. raise ValueError(f"Error: {response.status_code} ")
  414. if response.json()["code"] != "success":
  415. raise ValueError(f"error: {response.json()['message']}")
  416. rst = response.json()
  417. if rst["type"] == "redirect":
  418. return {"type": rst["type"], "redirect_url": rst["redirect_url"]}
  419. else:
  420. return {"type": rst["type"], "result": "success"}
  421. def free_quota_qualification_verify(self, tenant_id: str, provider: str, token: Optional[str]):
  422. api_key = os.environ.get("FREE_QUOTA_APPLY_API_KEY")
  423. api_base_url = os.environ.get("FREE_QUOTA_APPLY_BASE_URL")
  424. if not api_base_url:
  425. raise Exception("FREE_QUOTA_APPLY_BASE_URL is not set")
  426. api_url = api_base_url + "/api/v1/providers/qualification-verify"
  427. headers = {"Content-Type": "application/json", "Authorization": f"Bearer {api_key}"}
  428. json_data = {"workspace_id": tenant_id, "provider_name": provider}
  429. if token:
  430. json_data["token"] = token
  431. response = requests.post(api_url, headers=headers, json=json_data)
  432. if not response.ok:
  433. logger.error(f"Request FREE QUOTA APPLY SERVER Error: {response.status_code} ")
  434. raise ValueError(f"Error: {response.status_code} ")
  435. rst = response.json()
  436. if rst["code"] != "success":
  437. raise ValueError(f"error: {rst['message']}")
  438. data = rst["data"]
  439. if data["qualified"] is True:
  440. return {"result": "success", "provider_name": provider, "flag": True}
  441. else:
  442. return {"result": "success", "provider_name": provider, "flag": False, "reason": data["reason"]}