From e7f94f23e480c6b3951cc33608df8b762fbf47f1 Mon Sep 17 00:00:00 2001 From: Alexandre Henrique Date: Tue, 29 Aug 2023 00:31:55 -0300 Subject: [PATCH 1/2] added huggingface endpoints --- .../components/llms/HuggingFaceEndpoints.py | 23 +++++++++++++++++++ .../langflow/components/llms/__init__.py | 0 2 files changed, 23 insertions(+) create mode 100644 src/backend/langflow/components/llms/HuggingFaceEndpoints.py create mode 100644 src/backend/langflow/components/llms/__init__.py diff --git a/src/backend/langflow/components/llms/HuggingFaceEndpoints.py b/src/backend/langflow/components/llms/HuggingFaceEndpoints.py new file mode 100644 index 000000000..c1c21ebfb --- /dev/null +++ b/src/backend/langflow/components/llms/HuggingFaceEndpoints.py @@ -0,0 +1,23 @@ +from typing import Optional +from langflow import CustomComponent +from langchain.llms import HuggingFaceEndpoint + +class HuggingFaceEndpointsComponent(CustomComponent): + display_name: str = "Hugging Face Endpoints" + description: str = "LLM model from Inference Hugging Face Endpoints." + + def build_config(self): + return { + "endpoint_url": {"display_name": "Endpoint URL", "password": True}, + "task": {"display_name": "Task", "type": "select", "options": ["text2text-generation", "text-generation", "summarization"]}, + "code": {"show": False}, + } + + def build( + self, endpoint_url: str, task="text2text-generation", + ) -> HuggingFaceEndpoint: + try: + output = HuggingFaceEndpoint(endpoint_url=endpoint_url, task=task) + except Exception as e: + raise ValueError("Could not connect to HuggingFace Endpoints API.") from e + return output diff --git a/src/backend/langflow/components/llms/__init__.py b/src/backend/langflow/components/llms/__init__.py new file mode 100644 index 000000000..e69de29bb From 9530230c2c7a83ae53ce455c01060beeb4284656 Mon Sep 17 00:00:00 2001 From: Alexandre Henrique Date: Tue, 29 Aug 2023 16:34:40 -0300 Subject: [PATCH 2/2] Fixed the connection to the forward component --- .../components/llms/HuggingFaceEndpoints.py | 33 +++++++++++++++---- 1 file changed, 26 insertions(+), 7 deletions(-) diff --git a/src/backend/langflow/components/llms/HuggingFaceEndpoints.py b/src/backend/langflow/components/llms/HuggingFaceEndpoints.py index c1c21ebfb..ea2b4f20b 100644 --- a/src/backend/langflow/components/llms/HuggingFaceEndpoints.py +++ b/src/backend/langflow/components/llms/HuggingFaceEndpoints.py @@ -1,23 +1,42 @@ from typing import Optional from langflow import CustomComponent from langchain.llms import HuggingFaceEndpoint +from langchain.llms.base import BaseLLM + class HuggingFaceEndpointsComponent(CustomComponent): - display_name: str = "Hugging Face Endpoints" - description: str = "LLM model from Inference Hugging Face Endpoints." + display_name: str = "Hugging Face Inference API" + description: str = "LLM model from Hugging Face Inference API." - def build_config(self): + def build_config(self): return { "endpoint_url": {"display_name": "Endpoint URL", "password": True}, - "task": {"display_name": "Task", "type": "select", "options": ["text2text-generation", "text-generation", "summarization"]}, + "task": { + "display_name": "Task", + "type": "select", + "options": ["text2text-generation", "text-generation", "summarization"], + }, + "huggingfacehub_api_token": {"display_name": "API token", "password": True}, + "model_kwargs": { + "display_name": "Model Keyword Arguments", + "field_type": "code", + }, "code": {"show": False}, } def build( - self, endpoint_url: str, task="text2text-generation", - ) -> HuggingFaceEndpoint: + self, + endpoint_url: str, + task="text2text-generation", + huggingfacehub_api_token: Optional[str] = None, + model_kwargs: Optional[dict] = None, + ) -> BaseLLM: try: - output = HuggingFaceEndpoint(endpoint_url=endpoint_url, task=task) + output = HuggingFaceEndpoint( + endpoint_url=endpoint_url, + task=task, + huggingfacehub_api_token=huggingfacehub_api_token, + ) except Exception as e: raise ValueError("Could not connect to HuggingFace Endpoints API.") from e return output