repositories
loading repo index
repositories
loading repo index
repository
loading code, commits, and activity
public Clawd ADK gateway launch mirror
stars
latest
clone command
git clone gitlawb://did:key:z6Mkq5mY...iFZ5/my-project-publ...git clone gitlawb://did:key:z6Mkq5mY.../my-project-publ...2fa351d6docs: add automaton and perps launch sources16d ago| #1 | import importlib |
| #2 | import logging |
| #3 | import os |
| #4 | from typing import Optional |
| #5 | |
| #6 | from langchain_community.llms.huggingface_endpoint import HuggingFaceEndpoint |
| #7 | from langchain_community.llms.huggingface_hub import HuggingFaceHub |
| #8 | from langchain_community.llms.huggingface_pipeline import HuggingFacePipeline |
| #9 | |
| #10 | from embedchain.config import BaseLlmConfig |
| #11 | from embedchain.helpers.json_serializable import register_deserializable |
| #12 | from embedchain.llm.base import BaseLlm |
| #13 | |
| #14 | logger = logging.getLogger(__name__) |
| #15 | |
| #16 | |
| #17 | @register_deserializable |
| #18 | class HuggingFaceLlm(BaseLlm): |
| #19 | def __init__(self, config: Optional[BaseLlmConfig] = None): |
| #20 | try: |
| #21 | importlib.import_module("huggingface_hub") |
| #22 | except ModuleNotFoundError: |
| #23 | raise ModuleNotFoundError( |
| #24 | "The required dependencies for HuggingFaceHub are not installed." |
| #25 | "Please install with `pip install huggingface-hub==0.23.0`" |
| #26 | ) from None |
| #27 | |
| #28 | super().__init__(config=config) |
| #29 | if not self.config.api_key and "HUGGINGFACE_ACCESS_TOKEN" not in os.environ: |
| #30 | raise ValueError("Please set the HUGGINGFACE_ACCESS_TOKEN environment variable or pass it in the config.") |
| #31 | |
| #32 | def get_llm_model_answer(self, prompt): |
| #33 | if self.config.system_prompt: |
| #34 | raise ValueError("HuggingFaceLlm does not support `system_prompt`") |
| #35 | return HuggingFaceLlm._get_answer(prompt=prompt, config=self.config) |
| #36 | |
| #37 | @staticmethod |
| #38 | def _get_answer(prompt: str, config: BaseLlmConfig) -> str: |
| #39 | # If the user wants to run the model locally, they can do so by setting the `local` flag to True |
| #40 | if config.model and config.local: |
| #41 | return HuggingFaceLlm._from_pipeline(prompt=prompt, config=config) |
| #42 | elif config.model: |
| #43 | return HuggingFaceLlm._from_model(prompt=prompt, config=config) |
| #44 | elif config.endpoint: |
| #45 | return HuggingFaceLlm._from_endpoint(prompt=prompt, config=config) |
| #46 | else: |
| #47 | raise ValueError("Either `model` or `endpoint` must be set in config") |
| #48 | |
| #49 | @staticmethod |
| #50 | def _from_model(prompt: str, config: BaseLlmConfig) -> str: |
| #51 | model_kwargs = { |
| #52 | "temperature": config.temperature or 0.1, |
| #53 | "max_new_tokens": config.max_tokens, |
| #54 | } |
| #55 | |
| #56 | if 0.0 < config.top_p < 1.0: |
| #57 | model_kwargs["top_p"] = config.top_p |
| #58 | else: |
| #59 | raise ValueError("`top_p` must be > 0.0 and < 1.0") |
| #60 | |
| #61 | model = config.model |
| #62 | api_key = config.api_key or os.getenv("HUGGINGFACE_ACCESS_TOKEN") |
| #63 | logger.info(f"Using HuggingFaceHub with model {model}") |
| #64 | llm = HuggingFaceHub( |
| #65 | huggingfacehub_api_token=api_key, |
| #66 | repo_id=model, |
| #67 | model_kwargs=model_kwargs, |
| #68 | ) |
| #69 | return llm.invoke(prompt) |
| #70 | |
| #71 | @staticmethod |
| #72 | def _from_endpoint(prompt: str, config: BaseLlmConfig) -> str: |
| #73 | api_key = config.api_key or os.getenv("HUGGINGFACE_ACCESS_TOKEN") |
| #74 | llm = HuggingFaceEndpoint( |
| #75 | huggingfacehub_api_token=api_key, |
| #76 | endpoint_url=config.endpoint, |
| #77 | task="text-generation", |
| #78 | model_kwargs=config.model_kwargs, |
| #79 | ) |
| #80 | return llm.invoke(prompt) |
| #81 | |
| #82 | @staticmethod |
| #83 | def _from_pipeline(prompt: str, config: BaseLlmConfig) -> str: |
| #84 | model_kwargs = { |
| #85 | "temperature": config.temperature or 0.1, |
| #86 | "max_new_tokens": config.max_tokens, |
| #87 | } |
| #88 | |
| #89 | if 0.0 < config.top_p < 1.0: |
| #90 | model_kwargs["top_p"] = config.top_p |
| #91 | else: |
| #92 | raise ValueError("`top_p` must be > 0.0 and < 1.0") |
| #93 | |
| #94 | llm = HuggingFacePipeline.from_model_id( |
| #95 | model_id=config.model, |
| #96 | task="text-generation", |
| #97 | pipeline_kwargs=model_kwargs, |
| #98 | ) |
| #99 | return llm.invoke(prompt) |
| #100 |