base.py 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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__(
  28. self,
  29. device=None,
  30. pp_option=None,
  31. use_hpip: bool = False,
  32. hpi_params: Optional[Dict[str, Any]] = None,
  33. ) -> None:
  34. super().__init__()
  35. self.device = device
  36. self.pp_option = pp_option
  37. self.use_hpip = use_hpip
  38. self.hpi_params = hpi_params
  39. @abstractmethod
  40. def predict(self, input, **kwargs):
  41. raise NotImplementedError("The method `predict` has not been implemented yet.")
  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. )
  53. ########### [TODO]支持初始化传参能力
  54. if "batch_size" in config:
  55. batch_size = config["batch_size"]
  56. model.set_predictor(batch_size=batch_size)
  57. return model
  58. def create_pipeline(self, config):
  59. pipeline_name = config["pipeline_name"]
  60. pipeline = BasePipeline.get(pipeline_name)(
  61. config=config,
  62. device=self.device,
  63. pp_option=self.pp_option,
  64. use_hpip=self.use_hpip,
  65. hpi_params=self.hpi_params,
  66. )
  67. return pipeline
  68. def create_chat_bot(self, config):
  69. model_name = config["model_name"]
  70. chat_bot = BaseChat.get(model_name)(config)
  71. return chat_bot
  72. def create_retriever(self, config):
  73. model_name = config["model_name"]
  74. retriever = BaseRetriever.get(model_name)(config)
  75. return retriever
  76. def create_prompt_engeering(self, config):
  77. task_type = config["task_type"]
  78. pe = BaseGeneratePrompt.get(task_type)(config)
  79. return pe
  80. def __call__(self, input, **kwargs):
  81. return self.predict(input, **kwargs)