feat: ✨ remove api key from advanced and update HuggingFace components (#3397)
* feat: ✨ remove api key from advanced * refactor: 🎨 improve naming and descriptions * fix: 🐛 Fix hf api component * [autofix.ci] apply automated fixes * feat: 🎨 Add default values * [autofix.ci] apply automated fixes * fix: 🐛 fix hf api component * [autofix.ci] apply automated fixes * feat: 🔥 remove hugging face embeddings (local) * refactor: Removed HuggingFaceEmbeddingsComponent from __init__.py --------- Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com> Co-authored-by: Gabriel Luiz Freitas Almeida <gabriel@langflow.org>
This commit is contained in:
parent
5ffaa78e1c
commit
a700ea003d
4 changed files with 25 additions and 64 deletions
|
|
@ -1,36 +0,0 @@
|
||||||
from langchain_community.embeddings.huggingface import HuggingFaceEmbeddings
|
|
||||||
|
|
||||||
from langflow.base.models.model import LCModelComponent
|
|
||||||
from langflow.field_typing import Embeddings
|
|
||||||
from langflow.io import BoolInput, DictInput, MessageTextInput, Output
|
|
||||||
|
|
||||||
|
|
||||||
class HuggingFaceEmbeddingsComponent(LCModelComponent):
|
|
||||||
display_name = "Hugging Face Embeddings"
|
|
||||||
description = "Generate embeddings using HuggingFace models."
|
|
||||||
documentation = (
|
|
||||||
"https://python.langchain.com/docs/modules/data_connection/text_embedding/integrations/sentence_transformers"
|
|
||||||
)
|
|
||||||
icon = "HuggingFace"
|
|
||||||
name = "HuggingFaceEmbeddings"
|
|
||||||
|
|
||||||
inputs = [
|
|
||||||
MessageTextInput(name="cache_folder", display_name="Cache Folder", advanced=True),
|
|
||||||
DictInput(name="encode_kwargs", display_name="Encode Kwargs", advanced=True),
|
|
||||||
DictInput(name="model_kwargs", display_name="Model Kwargs", advanced=True),
|
|
||||||
MessageTextInput(name="model_name", display_name="Model Name", value="sentence-transformers/all-mpnet-base-v2"),
|
|
||||||
BoolInput(name="multi_process", display_name="Multi Process", advanced=True),
|
|
||||||
]
|
|
||||||
|
|
||||||
outputs = [
|
|
||||||
Output(display_name="Embeddings", name="embeddings", method="build_embeddings"),
|
|
||||||
]
|
|
||||||
|
|
||||||
def build_embeddings(self) -> Embeddings:
|
|
||||||
return HuggingFaceEmbeddings(
|
|
||||||
cache_folder=self.cache_folder,
|
|
||||||
encode_kwargs=self.encode_kwargs,
|
|
||||||
model_kwargs=self.model_kwargs,
|
|
||||||
model_name=self.model_name,
|
|
||||||
multi_process=self.multi_process,
|
|
||||||
)
|
|
||||||
|
|
@ -7,14 +7,14 @@ from langflow.io import MessageTextInput, Output, SecretStrInput
|
||||||
|
|
||||||
|
|
||||||
class HuggingFaceInferenceAPIEmbeddingsComponent(LCModelComponent):
|
class HuggingFaceInferenceAPIEmbeddingsComponent(LCModelComponent):
|
||||||
display_name = "Hugging Face API Embeddings"
|
display_name = "HuggingFace Embeddings"
|
||||||
description = "Generate embeddings using Hugging Face Inference API models."
|
description = "Generate embeddings using Hugging Face Inference API models."
|
||||||
documentation = "https://github.com/huggingface/text-embeddings-inference"
|
documentation = "https://github.com/huggingface/text-embeddings-inference"
|
||||||
icon = "HuggingFace"
|
icon = "HuggingFace"
|
||||||
name = "HuggingFaceInferenceAPIEmbeddings"
|
name = "HuggingFaceInferenceAPIEmbeddings"
|
||||||
|
|
||||||
inputs = [
|
inputs = [
|
||||||
SecretStrInput(name="api_key", display_name="API Key", advanced=True),
|
SecretStrInput(name="api_key", display_name="API Key"),
|
||||||
MessageTextInput(name="api_url", display_name="API URL", advanced=True, value="http://localhost:8080"),
|
MessageTextInput(name="api_url", display_name="API URL", advanced=True, value="http://localhost:8080"),
|
||||||
MessageTextInput(name="model_name", display_name="Model Name", value="BAAI/bge-large-en-v1.5"),
|
MessageTextInput(name="model_name", display_name="Model Name", value="BAAI/bge-large-en-v1.5"),
|
||||||
]
|
]
|
||||||
|
|
|
||||||
|
|
@ -3,7 +3,6 @@ from .AmazonBedrockEmbeddings import AmazonBedrockEmbeddingsComponent
|
||||||
from .AstraVectorize import AstraVectorizeComponent
|
from .AstraVectorize import AstraVectorizeComponent
|
||||||
from .AzureOpenAIEmbeddings import AzureOpenAIEmbeddingsComponent
|
from .AzureOpenAIEmbeddings import AzureOpenAIEmbeddingsComponent
|
||||||
from .CohereEmbeddings import CohereEmbeddingsComponent
|
from .CohereEmbeddings import CohereEmbeddingsComponent
|
||||||
from .HuggingFaceEmbeddings import HuggingFaceEmbeddingsComponent
|
|
||||||
from .HuggingFaceInferenceAPIEmbeddings import HuggingFaceInferenceAPIEmbeddingsComponent
|
from .HuggingFaceInferenceAPIEmbeddings import HuggingFaceInferenceAPIEmbeddingsComponent
|
||||||
from .OllamaEmbeddings import OllamaEmbeddingsComponent
|
from .OllamaEmbeddings import OllamaEmbeddingsComponent
|
||||||
from .OpenAIEmbeddings import OpenAIEmbeddingsComponent
|
from .OpenAIEmbeddings import OpenAIEmbeddingsComponent
|
||||||
|
|
@ -15,7 +14,6 @@ __all__ = [
|
||||||
"AstraVectorizeComponent",
|
"AstraVectorizeComponent",
|
||||||
"AzureOpenAIEmbeddingsComponent",
|
"AzureOpenAIEmbeddingsComponent",
|
||||||
"CohereEmbeddingsComponent",
|
"CohereEmbeddingsComponent",
|
||||||
"HuggingFaceEmbeddingsComponent",
|
|
||||||
"HuggingFaceInferenceAPIEmbeddingsComponent",
|
"HuggingFaceInferenceAPIEmbeddingsComponent",
|
||||||
"OllamaEmbeddingsComponent",
|
"OllamaEmbeddingsComponent",
|
||||||
"OpenAIEmbeddingsComponent",
|
"OpenAIEmbeddingsComponent",
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,4 @@
|
||||||
from tenacity import retry, stop_after_attempt, wait_fixed
|
from tenacity import retry, stop_after_attempt, wait_fixed
|
||||||
|
|
||||||
from langchain_community.chat_models.huggingface import ChatHuggingFace
|
|
||||||
from langchain_community.llms.huggingface_endpoint import HuggingFaceEndpoint
|
from langchain_community.llms.huggingface_endpoint import HuggingFaceEndpoint
|
||||||
|
|
||||||
from langflow.base.models.model import LCModelComponent
|
from langflow.base.models.model import LCModelComponent
|
||||||
|
|
@ -9,53 +7,54 @@ from langflow.io import DictInput, DropdownInput, SecretStrInput, StrInput, IntI
|
||||||
|
|
||||||
|
|
||||||
class HuggingFaceEndpointsComponent(LCModelComponent):
|
class HuggingFaceEndpointsComponent(LCModelComponent):
|
||||||
display_name: str = "Hugging Face API"
|
display_name: str = "HuggingFace"
|
||||||
description: str = "Generate text using Hugging Face Inference APIs."
|
description: str = "Generate text using Hugging Face Inference APIs."
|
||||||
icon = "HuggingFace"
|
icon = "HuggingFace"
|
||||||
name = "HuggingFaceModel"
|
name = "HuggingFaceModel"
|
||||||
|
|
||||||
inputs = LCModelComponent._base_inputs + [
|
inputs = LCModelComponent._base_inputs + [
|
||||||
SecretStrInput(name="endpoint_url", display_name="Endpoint URL", password=True),
|
|
||||||
StrInput(
|
StrInput(
|
||||||
name="model_id",
|
name="model_id",
|
||||||
display_name="Model Id",
|
display_name="Model ID",
|
||||||
info="Id field of endpoint_url response.",
|
value="openai-community/gpt2",
|
||||||
),
|
),
|
||||||
DropdownInput(
|
DropdownInput(
|
||||||
name="task",
|
name="task",
|
||||||
display_name="Task",
|
display_name="Task",
|
||||||
options=["text2text-generation", "text-generation", "summarization"],
|
options=["text2text-generation", "text-generation", "summarization", "translation"],
|
||||||
|
value="text-generation",
|
||||||
),
|
),
|
||||||
SecretStrInput(name="huggingfacehub_api_token", display_name="API token", password=True),
|
SecretStrInput(name="huggingfacehub_api_token", display_name="API Token", password=True),
|
||||||
DictInput(name="model_kwargs", display_name="Model Keyword Arguments", advanced=True),
|
DictInput(name="model_kwargs", display_name="Model Keyword Arguments", advanced=True),
|
||||||
IntInput(name="retry_attempts", display_name="Retry Attempts", value=1),
|
IntInput(name="retry_attempts", display_name="Retry Attempts", value=1, advanced=True),
|
||||||
]
|
]
|
||||||
|
|
||||||
def create_huggingface_endpoint(self, endpoint_url, task, huggingfacehub_api_token, model_kwargs):
|
def create_huggingface_endpoint(
|
||||||
@retry(stop=stop_after_attempt(self.retry_attempts), wait=wait_fixed(2))
|
self, model_id: str, task: str, huggingfacehub_api_token: str, model_kwargs: dict
|
||||||
|
) -> HuggingFaceEndpoint:
|
||||||
|
retry_attempts = self.retry_attempts # Access the retry attempts input
|
||||||
|
endpoint_url = f"https://api-inference.huggingface.co/models/{model_id}"
|
||||||
|
|
||||||
|
@retry(stop=stop_after_attempt(retry_attempts), wait=wait_fixed(2))
|
||||||
def _attempt_create():
|
def _attempt_create():
|
||||||
try:
|
return HuggingFaceEndpoint(
|
||||||
return HuggingFaceEndpoint( # type: ignore
|
|
||||||
endpoint_url=endpoint_url,
|
endpoint_url=endpoint_url,
|
||||||
task=task,
|
task=task,
|
||||||
huggingfacehub_api_token=huggingfacehub_api_token,
|
huggingfacehub_api_token=huggingfacehub_api_token,
|
||||||
model_kwargs=model_kwargs,
|
model_kwargs=model_kwargs,
|
||||||
)
|
)
|
||||||
except Exception as e:
|
|
||||||
raise ValueError("Could not connect to HuggingFace Endpoints API.") from e
|
|
||||||
|
|
||||||
return _attempt_create()
|
return _attempt_create()
|
||||||
|
|
||||||
def build_model(self) -> LanguageModel: # type: ignore[type-var]
|
def build_model(self) -> LanguageModel: # type: ignore[type-var]
|
||||||
endpoint_url = self.endpoint_url
|
model_id = self.model_id
|
||||||
task = self.task
|
task = self.task
|
||||||
huggingfacehub_api_token = self.huggingfacehub_api_token
|
huggingfacehub_api_token = self.huggingfacehub_api_token
|
||||||
model_kwargs = self.model_kwargs or {}
|
model_kwargs = self.model_kwargs or {}
|
||||||
|
|
||||||
try:
|
try:
|
||||||
llm = self.create_huggingface_endpoint(endpoint_url, task, huggingfacehub_api_token, model_kwargs)
|
llm = self.create_huggingface_endpoint(model_id, task, huggingfacehub_api_token, model_kwargs)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
raise ValueError("Could not connect to HuggingFace Endpoints API.") from e
|
raise ValueError("Could not connect to HuggingFace Endpoints API.") from e
|
||||||
|
|
||||||
output = ChatHuggingFace(llm=llm, model_id=self.model_id)
|
return llm
|
||||||
return output # type: ignore
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue