model_provider_service.py 21 KB

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