base.py 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. # copyright (c) 2024 PaddlePaddle Authors. All Rights Reserve.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. from abc import ABC, abstractmethod
  15. from ...utils.subclass_register import AutoRegisterABCMetaClass
  16. import yaml
  17. import codecs
  18. from pathlib import Path
  19. from typing import Any, Dict, Optional
  20. from ..models import create_predictor
  21. from .components.chat_server.base import BaseChat
  22. from .components.retriever.base import BaseRetriever
  23. from .components.prompt_engeering.base import BaseGeneratePrompt
  24. class BasePipeline(ABC, metaclass=AutoRegisterABCMetaClass):
  25. """Base Pipeline"""
  26. __is_base = True
  27. def __init__(self,
  28. device=None,
  29. pp_option=None,
  30. use_hpip: bool = False,
  31. hpi_params: Optional[Dict[str, Any]] = None) -> None:
  32. super().__init__()
  33. self.device = device
  34. self.pp_option = pp_option
  35. self.use_hpip = use_hpip
  36. self.hpi_params = hpi_params
  37. @abstractmethod
  38. def predict(self, input, **kwargs):
  39. raise NotImplementedError(
  40. "The method `predict` has not been implemented yet."
  41. )
  42. def create_model(self, config):
  43. model_dir = config['model_dir']
  44. if model_dir == None:
  45. model_dir = config['model_name']
  46. model = create_predictor(
  47. model_dir,
  48. device=self.device,
  49. pp_option=self.pp_option,
  50. use_hpip=self.use_hpip,
  51. hpi_params=self.hpi_params)
  52. ########### [TODO]支持初始化传参能力
  53. if "batch_size" in config:
  54. batch_size = config["batch_size"]
  55. model.set_predictor(batch_size=batch_size)
  56. return model
  57. def create_pipeline(self, config):
  58. pipeline_name = config['pipeline_name']
  59. pipeline = BasePipeline.get(pipeline_name)(
  60. config=config,
  61. device=self.device,
  62. pp_option=self.pp_option,
  63. use_hpip=self.use_hpip,
  64. hpi_params=self.hpi_params)
  65. return pipeline
  66. def create_chat_bot(self, config):
  67. model_name = config['model_name']
  68. chat_bot = BaseChat.get(model_name)(config)
  69. return chat_bot
  70. def create_retriever(self, config):
  71. model_name = config['model_name']
  72. retriever = BaseRetriever.get(model_name)(config)
  73. return retriever
  74. def create_prompt_engeering(self, config):
  75. task_type = config['task_type']
  76. pe = BaseGeneratePrompt.get(task_type)(config)
  77. return pe
  78. def __call__(self, input, **kwargs):
  79. return self.predict(input, **kwargs)