huggingface.py 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. import importlib
  2. import logging
  3. import os
  4. from typing import Optional
  5. from langchain.llms.huggingface_hub import HuggingFaceHub
  6. from embedchain.config import BaseLlmConfig
  7. from embedchain.helpers.json_serializable import register_deserializable
  8. from embedchain.llm.base import BaseLlm
  9. @register_deserializable
  10. class HuggingFaceLlm(BaseLlm):
  11. def __init__(self, config: Optional[BaseLlmConfig] = None):
  12. if "HUGGINGFACE_ACCESS_TOKEN" not in os.environ:
  13. raise ValueError("Please set the HUGGINGFACE_ACCESS_TOKEN environment variable.")
  14. try:
  15. importlib.import_module("huggingface_hub")
  16. except ModuleNotFoundError:
  17. raise ModuleNotFoundError(
  18. "The required dependencies for HuggingFaceHub are not installed."
  19. 'Please install with `pip install --upgrade "embedchain[huggingface-hub]"`'
  20. ) from None
  21. super().__init__(config=config)
  22. def get_llm_model_answer(self, prompt):
  23. if self.config.system_prompt:
  24. raise ValueError("HuggingFaceLlm does not support `system_prompt`")
  25. return HuggingFaceLlm._get_answer(prompt=prompt, config=self.config)
  26. @staticmethod
  27. def _get_answer(prompt: str, config: BaseLlmConfig) -> str:
  28. model_kwargs = {
  29. "temperature": config.temperature or 0.1,
  30. "max_new_tokens": config.max_tokens,
  31. }
  32. if config.top_p > 0.0 and config.top_p < 1.0:
  33. model_kwargs["top_p"] = config.top_p
  34. else:
  35. raise ValueError("`top_p` must be > 0.0 and < 1.0")
  36. model = config.model or "google/flan-t5-xxl"
  37. logging.info(f"Using HuggingFaceHub with model {model}")
  38. llm = HuggingFaceHub(
  39. huggingfacehub_api_token=os.environ["HUGGINGFACE_ACCESS_TOKEN"],
  40. repo_id=model,
  41. model_kwargs=model_kwargs,
  42. )
  43. return llm(prompt)