huggingface.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. import importlib
  2. import logging
  3. import os
  4. from typing import Optional
  5. from langchain_community.llms.huggingface_endpoint import HuggingFaceEndpoint
  6. from langchain_community.llms.huggingface_hub import HuggingFaceHub
  7. from langchain_community.llms.huggingface_pipeline import HuggingFacePipeline
  8. from embedchain.config import BaseLlmConfig
  9. from embedchain.helpers.json_serializable import register_deserializable
  10. from embedchain.llm.base import BaseLlm
  11. @register_deserializable
  12. class HuggingFaceLlm(BaseLlm):
  13. def __init__(self, config: Optional[BaseLlmConfig] = None):
  14. if "HUGGINGFACE_ACCESS_TOKEN" not in os.environ:
  15. raise ValueError("Please set the HUGGINGFACE_ACCESS_TOKEN environment variable.")
  16. try:
  17. importlib.import_module("huggingface_hub")
  18. except ModuleNotFoundError:
  19. raise ModuleNotFoundError(
  20. "The required dependencies for HuggingFaceHub are not installed."
  21. 'Please install with `pip install --upgrade "embedchain[huggingface-hub]"`'
  22. ) from None
  23. super().__init__(config=config)
  24. def get_llm_model_answer(self, prompt):
  25. if self.config.system_prompt:
  26. raise ValueError("HuggingFaceLlm does not support `system_prompt`")
  27. return HuggingFaceLlm._get_answer(prompt=prompt, config=self.config)
  28. @staticmethod
  29. def _get_answer(prompt: str, config: BaseLlmConfig) -> str:
  30. # If the user wants to run the model locally, they can do so by setting the `local` flag to True
  31. if config.model and config.local:
  32. return HuggingFaceLlm._from_pipeline(prompt=prompt, config=config)
  33. elif config.model:
  34. return HuggingFaceLlm._from_model(prompt=prompt, config=config)
  35. elif config.endpoint:
  36. return HuggingFaceLlm._from_endpoint(prompt=prompt, config=config)
  37. else:
  38. raise ValueError("Either `model` or `endpoint` must be set in config")
  39. @staticmethod
  40. def _from_model(prompt: str, config: BaseLlmConfig) -> str:
  41. model_kwargs = {
  42. "temperature": config.temperature or 0.1,
  43. "max_new_tokens": config.max_tokens,
  44. }
  45. if 0.0 < config.top_p < 1.0:
  46. model_kwargs["top_p"] = config.top_p
  47. else:
  48. raise ValueError("`top_p` must be > 0.0 and < 1.0")
  49. model = config.model
  50. logging.info(f"Using HuggingFaceHub with model {model}")
  51. llm = HuggingFaceHub(
  52. huggingfacehub_api_token=os.environ["HUGGINGFACE_ACCESS_TOKEN"],
  53. repo_id=model,
  54. model_kwargs=model_kwargs,
  55. )
  56. return llm.invoke(prompt)
  57. @staticmethod
  58. def _from_endpoint(prompt: str, config: BaseLlmConfig) -> str:
  59. llm = HuggingFaceEndpoint(
  60. huggingfacehub_api_token=os.environ["HUGGINGFACE_ACCESS_TOKEN"],
  61. endpoint_url=config.endpoint,
  62. task="text-generation",
  63. model_kwargs=config.model_kwargs,
  64. )
  65. return llm.invoke(prompt)
  66. @staticmethod
  67. def _from_pipeline(prompt: str, config: BaseLlmConfig) -> str:
  68. model_kwargs = {
  69. "temperature": config.temperature or 0.1,
  70. "max_new_tokens": config.max_tokens,
  71. }
  72. if 0.0 < config.top_p < 1.0:
  73. model_kwargs["top_p"] = config.top_p
  74. else:
  75. raise ValueError("`top_p` must be > 0.0 and < 1.0")
  76. llm = HuggingFacePipeline.from_model_id(
  77. model_id=config.model,
  78. task="text-generation",
  79. pipeline_kwargs=model_kwargs,
  80. )
  81. return llm.invoke(prompt)