Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

partners: support reading HuggingFace params from env #23309

Merged
merged 2 commits into from
Jul 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,7 @@ def __init__(self, **kwargs: Any):
else self.tokenizer
)

@root_validator()
@root_validator(pre=False, skip_on_failure=True)
def validate_llm(cls, values: dict) -> dict:
if (
not _is_huggingface_hub(values["llm"])
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import json
import os
from typing import Any, Dict, List, Optional

from langchain_core.embeddings import Embeddings
from langchain_core.pydantic_v1 import BaseModel, Extra, root_validator
from langchain_core.utils import get_from_dict_or_env

DEFAULT_MODEL = "sentence-transformers/all-mpnet-base-v2"
VALID_TASKS = ("feature-extraction",)
Expand Down Expand Up @@ -46,11 +46,15 @@ class Config:

extra = Extra.forbid

@root_validator()
@root_validator(pre=False, skip_on_failure=True)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be pre=True based on the logic below -- all of it can be done as pre-init logic

Just requires re-writing values['model'] to values.get('model')

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Merging as is, but if you can please re-write this with pre=True

def validate_environment(cls, values: Dict) -> Dict:
"""Validate that api key and python package exists in environment."""
huggingfacehub_api_token = values["huggingfacehub_api_token"] or os.getenv(
"HUGGINGFACEHUB_API_TOKEN"
values["huggingfacehub_api_token"] = get_from_dict_or_env(
values, "huggingfacehub_api_token", "HUGGINGFACEHUB_API_TOKEN", None
)

huggingfacehub_api_token = get_from_dict_or_env(
values, "huggingfacehub_api_token", "HF_TOKEN", None
)

try:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import json
import logging
import os
from typing import Any, AsyncIterator, Dict, Iterator, List, Mapping, Optional

from langchain_core.callbacks import (
Expand All @@ -10,7 +9,7 @@
from langchain_core.language_models.llms import LLM
from langchain_core.outputs import GenerationChunk
from langchain_core.pydantic_v1 import Extra, Field, root_validator
from langchain_core.utils import get_pydantic_field_names
from langchain_core.utils import get_from_dict_or_env, get_pydantic_field_names

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -146,18 +145,23 @@ def build_extra(cls, values: Dict[str, Any]) -> Dict[str, Any]:
)

values["model_kwargs"] = extra
if "endpoint_url" not in values and "repo_id" not in values:

values["endpoint_url"] = get_from_dict_or_env(
values, "endpoint_url", "HF_INFERENCE_ENDPOINT", None
)

if values["endpoint_url"] is None and "repo_id" not in values:
raise ValueError(
"Please specify an `endpoint_url` or `repo_id` for the model."
)
if "endpoint_url" in values and "repo_id" in values:
if values["endpoint_url"] is not None and "repo_id" in values:
raise ValueError(
"Please specify either an `endpoint_url` OR a `repo_id`, not both."
)
values["model"] = values.get("endpoint_url") or values.get("repo_id")
return values

@root_validator()
@root_validator(pre=False, skip_on_failure=True)
def validate_environment(cls, values: Dict) -> Dict:
"""Validate that package is installed and that the API token is valid."""
try:
Expand All @@ -168,9 +172,15 @@ def validate_environment(cls, values: Dict) -> Dict:
"Could not import huggingface_hub python package. "
"Please install it with `pip install huggingface_hub`."
)
huggingfacehub_api_token = values["huggingfacehub_api_token"] or os.getenv(
"HUGGINGFACEHUB_API_TOKEN"

values["huggingfacehub_api_token"] = get_from_dict_or_env(
values, "huggingfacehub_api_token", "HUGGINGFACEHUB_API_TOKEN", None
)

huggingfacehub_api_token = get_from_dict_or_env(
values, "huggingfacehub_api_token", "HF_TOKEN", None
)

if huggingfacehub_api_token is not None:
try:
login(token=huggingfacehub_api_token)
Expand Down
Loading