-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Updated to_dict / from_dict to handle 'token' correctly ; Added tests
- Loading branch information
1 parent
505b9d4
commit 35edc33
Showing
4 changed files
with
76 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
# SPDX-FileCopyrightText: 2022-present deepset GmbH <[email protected]> | ||
# | ||
# SPDX-License-Identifier: Apache-2.0 | ||
from haystack.utils.auth import Secret | ||
import pytest | ||
|
||
from haystack import ComponentError, DeserializationError, Pipeline | ||
|
@@ -11,7 +12,7 @@ | |
def test_named_entity_extractor_backend(): | ||
_ = NamedEntityExtractor(backend=NamedEntityExtractorBackend.HUGGING_FACE, model="dslim/bert-base-NER") | ||
|
||
# private model, will fail if HF_API_TOKEN is not set | ||
# private model | ||
_ = NamedEntityExtractor(backend=NamedEntityExtractorBackend.HUGGING_FACE, model="deepset/bert-base-NER") | ||
|
||
_ = NamedEntityExtractor(backend="hugging_face", model="dslim/bert-base-NER") | ||
|
@@ -43,7 +44,58 @@ def test_named_entity_extractor_serde(): | |
_ = NamedEntityExtractor.from_dict(serde_data) | ||
|
||
|
||
def test_named_entity_extractor_from_dict_no_default_parameters_hf(): | ||
def test_to_dict_default(monkeypatch): | ||
monkeypatch.delenv("HF_API_TOKEN", raising=False) | ||
|
||
component = NamedEntityExtractor( | ||
backend=NamedEntityExtractorBackend.HUGGING_FACE, | ||
model="dslim/bert-base-NER", | ||
device=ComponentDevice.from_str("mps"), | ||
) | ||
data = component.to_dict() | ||
|
||
assert data == { | ||
"type": "haystack.components.extractors.named_entity_extractor.NamedEntityExtractor", | ||
"init_parameters": { | ||
"backend": "HUGGING_FACE", | ||
"model": "dslim/bert-base-NER", | ||
"device": {"type": "single", "device": "mps"}, | ||
"pipeline_kwargs": {"model": "dslim/bert-base-NER", "device": "mps", "task": "ner"}, | ||
"token": {"type": "env_var", "env_vars": ["HF_API_TOKEN", "HF_TOKEN"], "strict": False}, | ||
}, | ||
} | ||
|
||
|
||
def test_to_dict_with_parameters(): | ||
component = NamedEntityExtractor( | ||
backend=NamedEntityExtractorBackend.HUGGING_FACE, | ||
model="dslim/bert-base-NER", | ||
device=ComponentDevice.from_str("mps"), | ||
pipeline_kwargs={"model_kwargs": {"load_in_4bit": True}}, | ||
token=Secret.from_env_var("ENV_VAR", strict=False), | ||
) | ||
data = component.to_dict() | ||
|
||
assert data == { | ||
"type": "haystack.components.extractors.named_entity_extractor.NamedEntityExtractor", | ||
"init_parameters": { | ||
"backend": "HUGGING_FACE", | ||
"model": "dslim/bert-base-NER", | ||
"device": {"type": "single", "device": "mps"}, | ||
"pipeline_kwargs": { | ||
"model": "dslim/bert-base-NER", | ||
"device": "mps", | ||
"task": "ner", | ||
"model_kwargs": {"load_in_4bit": True}, | ||
}, | ||
"token": {"env_vars": ["ENV_VAR"], "strict": False, "type": "env_var"}, | ||
}, | ||
} | ||
|
||
|
||
def test_named_entity_extractor_from_dict_no_default_parameters_hf(monkeypatch): | ||
monkeypatch.delenv("HF_API_TOKEN", raising=False) | ||
|
||
data = { | ||
"type": "haystack.components.extractors.named_entity_extractor.NamedEntityExtractor", | ||
"init_parameters": {"backend": "HUGGING_FACE", "model": "dslim/bert-base-NER"}, | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters