-
Notifications
You must be signed in to change notification settings - Fork 127
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add default model for NVIDIA HayStack local NIM endpoints (#915)
* initial embedder code * default model code * docs: update model docstring * tests: add userwarning * docs: literal lint fix * review changes * remove pydantic dependency * move backend, nim_backend under utils * move is_hosted to warm_up * test cases, docstring fix * error message updation Co-authored-by: Madeesh Kannan <[email protected]> * move is_hosted code to util * remove backend code * update import for is_hosted * remove util and move code to utils * fix api key issue for failing test cases * Update integrations/nvidia/tests/conftest.py --------- Co-authored-by: Madeesh Kannan <[email protected]>
- Loading branch information
Showing
15 changed files
with
358 additions
and
172 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
52 changes: 0 additions & 52 deletions
52
integrations/nvidia/src/haystack_integrations/components/embedders/nvidia/_nim_backend.py
This file was deleted.
Oops, something went wrong.
29 changes: 0 additions & 29 deletions
29
integrations/nvidia/src/haystack_integrations/components/embedders/nvidia/backend.py
This file was deleted.
Oops, something went wrong.
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
29 changes: 0 additions & 29 deletions
29
integrations/nvidia/src/haystack_integrations/components/generators/nvidia/backend.py
This file was deleted.
Oops, something went wrong.
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,14 +1,12 @@ | ||
# SPDX-FileCopyrightText: 2024-present deepset GmbH <[email protected]> | ||
# | ||
# SPDX-License-Identifier: Apache-2.0 | ||
import warnings | ||
from typing import Any, Dict, List, Optional | ||
|
||
from haystack import component, default_from_dict, default_to_dict | ||
from haystack.utils.auth import Secret, deserialize_secrets_inplace | ||
from haystack_integrations.utils.nvidia import url_validation | ||
|
||
from ._nim_backend import NimBackend | ||
from .backend import GeneratorBackend | ||
from haystack_integrations.utils.nvidia import NimBackend, is_hosted, url_validation | ||
|
||
_DEFAULT_API_URL = "https://integrate.api.nvidia.com/v1" | ||
|
||
|
@@ -45,7 +43,7 @@ class NvidiaGenerator: | |
|
||
def __init__( | ||
self, | ||
model: str, | ||
model: Optional[str] = None, | ||
api_url: str = _DEFAULT_API_URL, | ||
api_key: Optional[Secret] = Secret.from_env_var("NVIDIA_API_KEY"), | ||
model_arguments: Optional[Dict[str, Any]] = None, | ||
|
@@ -55,6 +53,10 @@ def __init__( | |
:param model: | ||
Name of the model to use for text generation. | ||
See the [NVIDIA NIMs](https://ai.nvidia.com) | ||
for more information on the supported models. | ||
`Note`: If no specific model along with locally hosted API URL is provided, | ||
the system defaults to the available model found using /models API. | ||
Check supported models at [NVIDIA NIM](https://ai.nvidia.com). | ||
:param api_key: | ||
API key for the NVIDIA NIM. Set it as the `NVIDIA_API_KEY` environment | ||
|
@@ -72,7 +74,28 @@ def __init__( | |
self._api_key = api_key | ||
self._model_arguments = model_arguments or {} | ||
|
||
self._backend: Optional[GeneratorBackend] = None | ||
self._backend: Optional[Any] = None | ||
|
||
self.is_hosted = is_hosted(api_url) | ||
|
||
def default_model(self): | ||
"""Set default model in local NIM mode.""" | ||
valid_models = [ | ||
model.id for model in self._backend.models() if not model.base_model or model.base_model == model.id | ||
] | ||
name = next(iter(valid_models), None) | ||
if name: | ||
warnings.warn( | ||
f"Default model is set as: {name}. \n" | ||
"Set model using model parameter. \n" | ||
"To get available models use available_models property.", | ||
UserWarning, | ||
stacklevel=2, | ||
) | ||
self._model = self._backend.model_name = name | ||
else: | ||
error_message = "No locally hosted model was found." | ||
raise ValueError(error_message) | ||
|
||
def warm_up(self): | ||
""" | ||
|
@@ -91,6 +114,9 @@ def warm_up(self): | |
model_kwargs=self._model_arguments, | ||
) | ||
|
||
if not self.is_hosted and not self._model: | ||
self.default_model() | ||
|
||
def to_dict(self) -> Dict[str, Any]: | ||
""" | ||
Serializes the component to a dictionary. | ||
|
5 changes: 3 additions & 2 deletions
5
integrations/nvidia/src/haystack_integrations/utils/nvidia/__init__.py
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,3 +1,4 @@ | ||
from .utils import url_validation | ||
from .nim_backend import Model, NimBackend | ||
from .utils import is_hosted, url_validation | ||
|
||
__all__ = ["url_validation"] | ||
__all__ = ["NimBackend", "Model", "is_hosted", "url_validation"] |
Oops, something went wrong.