model_provider_service.py 21 KB

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