diff --git a/cookbooks/HuggingFace/hf.py b/cookbooks/HuggingFace/hf.py index 8257b326e..4235b919f 100644 --- a/cookbooks/HuggingFace/hf.py +++ b/cookbooks/HuggingFace/hf.py @@ -128,7 +128,7 @@ class HuggingFaceTextParser(ParameterizedModelParser): A model parser for HuggingFace text generation models. """ - def __init__(self, model_id: str = None, use_api_token=False): + def __init__(self, model_id: str = None, use_api_token=True): """ Args: model_id (str): The model ID of the model to use. @@ -153,7 +153,9 @@ def __init__(self, model_id: str = None, use_api_token=False): token = None if use_api_token: - token = get_api_key_from_environment("HUGGING_FACE_API_TOKEN") + # You are allowed to use Hugging Face for a bit before you get + # rate limited, in which case you will receive a clear error + token = get_api_key_from_environment("HUGGING_FACE_API_TOKEN", required=False) self.client = InferenceClient(model_id, token=token) diff --git a/cookbooks/HuggingFace/python/hf.py b/cookbooks/HuggingFace/python/hf.py index 8257b326e..4235b919f 100644 --- a/cookbooks/HuggingFace/python/hf.py +++ b/cookbooks/HuggingFace/python/hf.py @@ -128,7 +128,7 @@ class HuggingFaceTextParser(ParameterizedModelParser): A model parser for HuggingFace text generation models. """ - def __init__(self, model_id: str = None, use_api_token=False): + def __init__(self, model_id: str = None, use_api_token=True): """ Args: model_id (str): The model ID of the model to use. @@ -153,7 +153,9 @@ def __init__(self, model_id: str = None, use_api_token=False): token = None if use_api_token: - token = get_api_key_from_environment("HUGGING_FACE_API_TOKEN") + # You are allowed to use Hugging Face for a bit before you get + # rate limited, in which case you will receive a clear error + token = get_api_key_from_environment("HUGGING_FACE_API_TOKEN", required=False) self.client = InferenceClient(model_id, token=token) diff --git a/python/src/aiconfig/default_parsers/hf.py b/python/src/aiconfig/default_parsers/hf.py index caedf246d..6220daa67 100644 --- a/python/src/aiconfig/default_parsers/hf.py +++ b/python/src/aiconfig/default_parsers/hf.py @@ -99,7 +99,7 @@ def construct_stream_output( return output -def construct_regular_output(response: TextGenerationResponse, response_includes_details: bool) -> Output: +def construct_regular_output(response: str, response_includes_details: bool) -> Output: metadata = {"raw_response": response} if response_includes_details: metadata["details"] = response.details @@ -107,7 +107,7 @@ def construct_regular_output(response: TextGenerationResponse, response_includes output = ExecuteResult( **{ "output_type": "execute_result", - "data": response.generated_text or "", + "data": response, "execution_count": 0, "metadata": metadata, } @@ -120,7 +120,7 @@ class HuggingFaceTextGenerationParser(ParameterizedModelParser): A model parser for HuggingFace text generation models. """ - def __init__(self, model_id: str = None, use_api_token=False): + def __init__(self, model_id: str = None, use_api_token=True): """ Args: model_id (str): The model ID of the model to use. @@ -145,7 +145,9 @@ def __init__(self, model_id: str = None, use_api_token=False): token = None if use_api_token: - token = get_api_key_from_environment("HUGGING_FACE_API_TOKEN") + # You are allowed to use Hugging Face for a bit before you get + # rate limited, in which case you will receive a clear error + token = get_api_key_from_environment("HUGGING_FACE_API_TOKEN", required=False) self.client = InferenceClient(model_id, token=token)