Skip to content

Commit

Permalink
feat: ✨ remove api key from advanced and update HuggingFace components (
Browse files Browse the repository at this point in the history
#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>
  • Loading branch information
3 people authored and anovazzi1 committed Aug 26, 2024
1 parent 5d477bc commit 54859ac
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 64 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@


class HuggingFaceInferenceAPIEmbeddingsComponent(LCModelComponent):
display_name = "Hugging Face API Embeddings"
display_name = "HuggingFace Embeddings"
description = "Generate embeddings using Hugging Face Inference API models."
documentation = "https://github.com/huggingface/text-embeddings-inference"
icon = "HuggingFace"
name = "HuggingFaceInferenceAPIEmbeddings"

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="model_name", display_name="Model Name", value="BAAI/bge-large-en-v1.5"),
]
Expand Down
2 changes: 0 additions & 2 deletions src/backend/base/langflow/components/embeddings/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
from .AstraVectorize import AstraVectorizeComponent
from .AzureOpenAIEmbeddings import AzureOpenAIEmbeddingsComponent
from .CohereEmbeddings import CohereEmbeddingsComponent
from .HuggingFaceEmbeddings import HuggingFaceEmbeddingsComponent
from .HuggingFaceInferenceAPIEmbeddings import HuggingFaceInferenceAPIEmbeddingsComponent
from .OllamaEmbeddings import OllamaEmbeddingsComponent
from .OpenAIEmbeddings import OpenAIEmbeddingsComponent
Expand All @@ -15,7 +14,6 @@
"AstraVectorizeComponent",
"AzureOpenAIEmbeddingsComponent",
"CohereEmbeddingsComponent",
"HuggingFaceEmbeddingsComponent",
"HuggingFaceInferenceAPIEmbeddingsComponent",
"OllamaEmbeddingsComponent",
"OpenAIEmbeddingsComponent",
Expand Down
47 changes: 23 additions & 24 deletions src/backend/base/langflow/components/models/HuggingFaceModel.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
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 langflow.base.models.model import LCModelComponent
Expand All @@ -9,53 +7,54 @@


class HuggingFaceEndpointsComponent(LCModelComponent):
display_name: str = "Hugging Face API"
display_name: str = "HuggingFace"
description: str = "Generate text using Hugging Face Inference APIs."
icon = "HuggingFace"
name = "HuggingFaceModel"

inputs = LCModelComponent._base_inputs + [
SecretStrInput(name="endpoint_url", display_name="Endpoint URL", password=True),
StrInput(
name="model_id",
display_name="Model Id",
info="Id field of endpoint_url response.",
display_name="Model ID",
value="openai-community/gpt2",
),
DropdownInput(
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),
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):
@retry(stop=stop_after_attempt(self.retry_attempts), wait=wait_fixed(2))
def create_huggingface_endpoint(
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():
try:
return HuggingFaceEndpoint( # type: ignore
endpoint_url=endpoint_url,
task=task,
huggingfacehub_api_token=huggingfacehub_api_token,
model_kwargs=model_kwargs,
)
except Exception as e:
raise ValueError("Could not connect to HuggingFace Endpoints API.") from e
return HuggingFaceEndpoint(
endpoint_url=endpoint_url,
task=task,
huggingfacehub_api_token=huggingfacehub_api_token,
model_kwargs=model_kwargs,
)

return _attempt_create()

def build_model(self) -> LanguageModel: # type: ignore[type-var]
endpoint_url = self.endpoint_url
model_id = self.model_id
task = self.task
huggingfacehub_api_token = self.huggingfacehub_api_token
model_kwargs = self.model_kwargs or {}

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:
raise ValueError("Could not connect to HuggingFace Endpoints API.") from e

output = ChatHuggingFace(llm=llm, model_id=self.model_id)
return output # type: ignore
return llm

0 comments on commit 54859ac

Please sign in to comment.