fix: resolve Model Issues and add huggingface dependency (#2339)

* chore: adding default values to Azure OpenAI mandatory component

* fix: huggingface model component:
  - Change Huggingface-hub version from 0.20.0 to 0.22.0;
  - Internal model_id resolver not working, create a field to model_id;

* feat: add HuggingFace as extra dependency

* chore: remove redundant atribution on children

* fix: remove user environment variables from ChatLiteLLMModelComponent

---------

Co-authored-by: joaoguilhermeS <j.guilherme.s.oliveira2@gmail.com>
This commit is contained in:
João Pedro Miranda C. Hluchan 2024-07-02 11:04:26 -03:00 committed by GitHub
commit 805df8298a
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 65 additions and 66 deletions

42
poetry.lock generated
View file

@ -3434,36 +3434,38 @@ files = [
[[package]]
name = "huggingface-hub"
version = "0.20.3"
version = "0.22.2"
description = "Client library to download and publish models, datasets and other repos on the huggingface.co hub"
optional = false
python-versions = ">=3.8.0"
files = [
{file = "huggingface_hub-0.20.3-py3-none-any.whl", hash = "sha256:d988ae4f00d3e307b0c80c6a05ca6dbb7edba8bba3079f74cda7d9c2e562a7b6"},
{file = "huggingface_hub-0.20.3.tar.gz", hash = "sha256:94e7f8e074475fbc67d6a71957b678e1b4a74ff1b64a644fd6cbb83da962d05d"},
{file = "huggingface_hub-0.22.2-py3-none-any.whl", hash = "sha256:3429e25f38ccb834d310804a3b711e7e4953db5a9e420cc147a5e194ca90fd17"},
{file = "huggingface_hub-0.22.2.tar.gz", hash = "sha256:32e9a9a6843c92f253ff9ca16b9985def4d80a93fb357af5353f770ef74a81be"},
]
[package.dependencies]
aiohttp = {version = "*", optional = true, markers = "extra == \"inference\""}
filelock = "*"
fsspec = ">=2023.5.0"
minijinja = {version = ">=1.0", optional = true, markers = "extra == \"inference\""}
packaging = ">=20.9"
pydantic = {version = ">1.1,<3.0", optional = true, markers = "python_version > \"3.8\" and extra == \"inference\""}
pyyaml = ">=5.1"
requests = "*"
tqdm = ">=4.42.1"
typing-extensions = ">=3.7.4.3"
[package.extras]
all = ["InquirerPy (==0.3.4)", "Jinja2", "Pillow", "aiohttp", "gradio", "jedi", "mypy (==1.5.1)", "numpy", "pydantic (>1.1,<2.0)", "pydantic (>1.1,<3.0)", "pytest", "pytest-asyncio", "pytest-cov", "pytest-env", "pytest-rerunfailures", "pytest-vcr", "pytest-xdist", "ruff (>=0.1.3)", "soundfile", "types-PyYAML", "types-requests", "types-simplejson", "types-toml", "types-tqdm", "types-urllib3", "typing-extensions (>=4.8.0)", "urllib3 (<2.0)"]
all = ["InquirerPy (==0.3.4)", "Jinja2", "Pillow", "aiohttp", "gradio", "jedi", "minijinja (>=1.0)", "mypy (==1.5.1)", "numpy", "pytest", "pytest-asyncio", "pytest-cov", "pytest-env", "pytest-rerunfailures", "pytest-vcr", "pytest-xdist", "ruff (>=0.3.0)", "soundfile", "types-PyYAML", "types-requests", "types-simplejson", "types-toml", "types-tqdm", "types-urllib3", "typing-extensions (>=4.8.0)", "urllib3 (<2.0)"]
cli = ["InquirerPy (==0.3.4)"]
dev = ["InquirerPy (==0.3.4)", "Jinja2", "Pillow", "aiohttp", "gradio", "jedi", "mypy (==1.5.1)", "numpy", "pydantic (>1.1,<2.0)", "pydantic (>1.1,<3.0)", "pytest", "pytest-asyncio", "pytest-cov", "pytest-env", "pytest-rerunfailures", "pytest-vcr", "pytest-xdist", "ruff (>=0.1.3)", "soundfile", "types-PyYAML", "types-requests", "types-simplejson", "types-toml", "types-tqdm", "types-urllib3", "typing-extensions (>=4.8.0)", "urllib3 (<2.0)"]
dev = ["InquirerPy (==0.3.4)", "Jinja2", "Pillow", "aiohttp", "gradio", "jedi", "minijinja (>=1.0)", "mypy (==1.5.1)", "numpy", "pytest", "pytest-asyncio", "pytest-cov", "pytest-env", "pytest-rerunfailures", "pytest-vcr", "pytest-xdist", "ruff (>=0.3.0)", "soundfile", "types-PyYAML", "types-requests", "types-simplejson", "types-toml", "types-tqdm", "types-urllib3", "typing-extensions (>=4.8.0)", "urllib3 (<2.0)"]
fastai = ["fastai (>=2.4)", "fastcore (>=1.3.27)", "toml"]
inference = ["aiohttp", "pydantic (>1.1,<2.0)", "pydantic (>1.1,<3.0)"]
quality = ["mypy (==1.5.1)", "ruff (>=0.1.3)"]
hf-transfer = ["hf-transfer (>=0.1.4)"]
inference = ["aiohttp", "minijinja (>=1.0)"]
quality = ["mypy (==1.5.1)", "ruff (>=0.3.0)"]
tensorflow = ["graphviz", "pydot", "tensorflow"]
testing = ["InquirerPy (==0.3.4)", "Jinja2", "Pillow", "aiohttp", "gradio", "jedi", "numpy", "pydantic (>1.1,<2.0)", "pydantic (>1.1,<3.0)", "pytest", "pytest-asyncio", "pytest-cov", "pytest-env", "pytest-rerunfailures", "pytest-vcr", "pytest-xdist", "soundfile", "urllib3 (<2.0)"]
torch = ["torch"]
tensorflow-testing = ["keras (<3.0)", "tensorflow"]
testing = ["InquirerPy (==0.3.4)", "Jinja2", "Pillow", "aiohttp", "gradio", "jedi", "minijinja (>=1.0)", "numpy", "pytest", "pytest-asyncio", "pytest-cov", "pytest-env", "pytest-rerunfailures", "pytest-vcr", "pytest-xdist", "soundfile", "urllib3 (<2.0)"]
torch = ["safetensors", "torch"]
typing = ["types-PyYAML", "types-requests", "types-simplejson", "types-toml", "types-tqdm", "types-urllib3", "typing-extensions (>=4.8.0)"]
[[package]]
@ -5009,6 +5011,23 @@ files = [
[package.dependencies]
requests = "*"
[[package]]
name = "minijinja"
version = "2.0.1"
description = "An experimental Python binding of the Rust MiniJinja template engine."
optional = false
python-versions = ">=3.8"
files = [
{file = "minijinja-2.0.1-cp38-abi3-macosx_10_12_x86_64.macosx_11_0_arm64.macosx_10_12_universal2.whl", hash = "sha256:063b291cb31f5c33eb77bb4cb457f67f14426ca1418232b8ae9f267155d330cc"},
{file = "minijinja-2.0.1-cp38-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:4a4e9d639dd89ce7fef86e82147082ab3c248a36950fa3fbe793685ba322c1b7"},
{file = "minijinja-2.0.1-cp38-abi3-manylinux_2_17_armv7l.manylinux2014_armv7l.whl", hash = "sha256:a20373af4ee5430356c196c7fe5f19e3261a4fa16c944542b4de7a2349bac7a6"},
{file = "minijinja-2.0.1-cp38-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:ade637bf4826258811a785ccc4e5d41cd2bdf4ec317b1ed3daa4dbbdd020f37d"},
{file = "minijinja-2.0.1-cp38-abi3-manylinux_2_5_i686.manylinux1_i686.whl", hash = "sha256:d5ec956d777e0fee8e214af48363334c04f098e986038a9e8cb92a0564f81943"},
{file = "minijinja-2.0.1-cp38-abi3-win32.whl", hash = "sha256:039f4d1a1a73f90917cff1ed7c617eb56e2b2f91bbbdc551adaa448e1673e5c2"},
{file = "minijinja-2.0.1-cp38-abi3-win_amd64.whl", hash = "sha256:dca5d7689905dce340e36e47348b505c788daf297253b85a1aff506ea63ad1b8"},
{file = "minijinja-2.0.1.tar.gz", hash = "sha256:e774beffebfb8a1ad17e638ef70917cf5e94593f79acb8a8fff7d983169f3a4e"},
]
[[package]]
name = "mkl"
version = "2021.4.0"
@ -5671,6 +5690,7 @@ description = "Nvidia JIT LTO Library"
optional = true
python-versions = ">=3"
files = [
{file = "nvidia_nvjitlink_cu12-12.5.40-py3-none-manylinux2014_aarch64.whl", hash = "sha256:004186d5ea6a57758fd6d57052a123c73a4815adf365eb8dd6a85c9eaa7535ff"},
{file = "nvidia_nvjitlink_cu12-12.5.40-py3-none-manylinux2014_x86_64.whl", hash = "sha256:d9714f27c1d0f0895cd8915c07a87a1d0029a0aa36acaf9156952ec2a8a12189"},
{file = "nvidia_nvjitlink_cu12-12.5.40-py3-none-win_amd64.whl", hash = "sha256:c3401dc8543b52d3a8158007a0c1ab4e9c768fcbd24153a48c86972102197ddd"},
]
@ -10555,4 +10575,4 @@ local = ["ctransformers", "llama-cpp-python", "sentence-transformers"]
[metadata]
lock-version = "2.0"
python-versions = ">=3.10,<3.13"
content-hash = "3e72b6faa1c674615a7e5dec3e7d962349e736bf6675c08a49080b7f336cc75b"
content-hash = "14772c35473f812d12f117d18c1a861da9dda27492e8e0007fbd76f4eeac9ab4"

View file

@ -30,7 +30,7 @@ langflow-base = { path = "./src/backend/base", develop = true }
beautifulsoup4 = "^4.12.2"
google-search-results = "^2.4.1"
google-api-python-client = "^2.130.0"
huggingface-hub = { version = "^0.20.0", extras = ["inference"] }
huggingface-hub = { version = "^0.22.0", extras = ["inference"] }
llama-cpp-python = { version = "~0.2.0", optional = true }
networkx = "^3.1"
fake-useragent = "^1.5.0"

View file

@ -1,5 +1,3 @@
from typing import Optional
from langchain_community.chat_models.litellm import ChatLiteLLM, ChatLiteLLMException
from langflow.base.constants import STREAM_INFO_TEXT
@ -12,7 +10,6 @@ from langflow.io import (
FloatInput,
IntInput,
MessageInput,
Output,
SecretStrInput,
StrInput,
)
@ -35,7 +32,7 @@ class ChatLiteLLMModelComponent(LCModelComponent):
),
SecretStrInput(
name="api_key",
display_name="API key",
display_name="API Key",
advanced=False,
required=False,
),
@ -59,25 +56,24 @@ class ChatLiteLLMModelComponent(LCModelComponent):
required=False,
value=0.7,
),
DictInput(
name="kwargs",
display_name="Kwargs",
advanced=True,
required=False,
is_list=True,
value={},
),
DictInput(
name="model_kwargs",
display_name="Model kwargs",
advanced=True,
required=False,
is_list=True,
value={},
),
FloatInput(
name="top_p",
display_name="Top p",
advanced=True,
required=False,
),
IntInput(
name="top_k",
display_name="Top k",
advanced=True,
required=False,
),
FloatInput(name="top_p", display_name="Top p", advanced=True, required=False, value=0.5),
IntInput(name="top_k", display_name="Top k", advanced=True, required=False, value=35),
IntInput(
name="n",
display_name="N",
@ -122,11 +118,6 @@ class ChatLiteLLMModelComponent(LCModelComponent):
),
]
outputs = [
Output(display_name="Text", name="text_output", method="text_response"),
Output(display_name="Language Model", name="model_output", method="build_model"),
]
def build_model(self) -> LanguageModel: # type: ignore[type-var]
try:
import litellm # type: ignore
@ -137,28 +128,19 @@ class ChatLiteLLMModelComponent(LCModelComponent):
raise ChatLiteLLMException(
"Could not import litellm python package. " "Please install it with `pip install litellm`"
)
provider_map = {
"OpenAI": "openai_api_key",
"Azure": "azure_api_key",
"Anthropic": "anthropic_api_key",
"Replicate": "replicate_api_key",
"Cohere": "cohere_api_key",
"OpenRouter": "openrouter_api_key",
}
# Set the API key based on the provider
api_keys: dict[str, Optional[str]] = {v: None for v in provider_map.values()}
if variable_name := provider_map.get(self.provider):
api_keys[variable_name] = self.api_key
else:
raise ChatLiteLLMException(
f"Provider {self.provider} is not supported. Supported providers are: {', '.join(provider_map.keys())}"
)
# Remove empty keys
if "" in self.kwargs:
del self.kwargs[""]
if "" in self.model_kwargs:
del self.model_kwargs[""]
# Report missing fields for Azure provider
if self.provider == "Azure":
if "api_base" not in self.kwargs:
raise Exception("Missing api_base on kwargs")
if "api_version" not in self.model_kwargs:
raise Exception("Missing api_version on model_kwargs")
output = ChatLiteLLM(
model=self.model,
model=f"{self.provider.lower()}/{self.model}",
client=None,
streaming=self.stream,
temperature=self.temperature,
@ -168,12 +150,8 @@ class ChatLiteLLMModelComponent(LCModelComponent):
n=self.n,
max_tokens=self.max_tokens,
max_retries=self.max_retries,
openai_api_key=api_keys["openai_api_key"],
azure_api_key=api_keys["azure_api_key"],
anthropic_api_key=api_keys["anthropic_api_key"],
replicate_api_key=api_keys["replicate_api_key"],
cohere_api_key=api_keys["cohere_api_key"],
openrouter_api_key=api_keys["openrouter_api_key"],
**self.kwargs,
)
output.client.api_key = self.api_key
return output # type: ignore

View file

@ -4,7 +4,7 @@ from langflow.base.constants import STREAM_INFO_TEXT
from langflow.base.models.model import LCModelComponent
from langflow.field_typing import LanguageModel
from langflow.inputs import MessageTextInput
from langflow.io import BoolInput, DictInput, DropdownInput, MessageInput, Output
from langflow.io import BoolInput, DictInput, DropdownInput, MessageInput
class AmazonBedrockComponent(LCModelComponent):
@ -64,10 +64,6 @@ class AmazonBedrockComponent(LCModelComponent):
),
BoolInput(name="stream", display_name="Stream", info=STREAM_INFO_TEXT, advanced=True),
]
outputs = [
Output(display_name="Text", name="text_output", method="text_response"),
Output(display_name="Language Model", name="model_output", method="build_model"),
]
def build_model(self) -> LanguageModel: # type: ignore[type-var]
model_id = self.model_id

View file

@ -15,6 +15,11 @@ class HuggingFaceEndpointsComponent(LCModelComponent):
inputs = [
MessageInput(name="input_value", display_name="Input"),
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.",
),
DropdownInput(
name="task",
display_name="Task",
@ -47,5 +52,5 @@ class HuggingFaceEndpointsComponent(LCModelComponent):
except Exception as e:
raise ValueError("Could not connect to HuggingFace Endpoints API.") from e
output = ChatHuggingFace(llm=llm)
output = ChatHuggingFace(llm=llm, model_id=self.model_id)
return output # type: ignore