huggingface.py 3.6 KB

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