model_provider_service.py 21 KB

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