nvidia-haystack 0.0.4__tar.gz → 0.1.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- nvidia_haystack-0.1.0/CHANGELOG.md +63 -0
- {nvidia_haystack-0.0.4 → nvidia_haystack-0.1.0}/PKG-INFO +2 -3
- {nvidia_haystack-0.0.4 → nvidia_haystack-0.1.0}/pydoc/config.yml +3 -0
- {nvidia_haystack-0.0.4 → nvidia_haystack-0.1.0}/pyproject.toml +27 -10
- {nvidia_haystack-0.0.4 → nvidia_haystack-0.1.0}/src/haystack_integrations/components/embedders/nvidia/document_embedder.py +42 -6
- {nvidia_haystack-0.0.4 → nvidia_haystack-0.1.0}/src/haystack_integrations/components/embedders/nvidia/text_embedder.py +40 -6
- {nvidia_haystack-0.0.4 → nvidia_haystack-0.1.0}/src/haystack_integrations/components/embedders/nvidia/truncate.py +2 -0
- {nvidia_haystack-0.0.4 → nvidia_haystack-0.1.0}/src/haystack_integrations/components/generators/nvidia/generator.py +44 -12
- nvidia_haystack-0.1.0/src/haystack_integrations/components/rankers/nvidia/__init__.py +3 -0
- nvidia_haystack-0.1.0/src/haystack_integrations/components/rankers/nvidia/ranker.py +207 -0
- nvidia_haystack-0.1.0/src/haystack_integrations/components/rankers/nvidia/truncate.py +27 -0
- nvidia_haystack-0.1.0/src/haystack_integrations/utils/nvidia/__init__.py +4 -0
- nvidia_haystack-0.1.0/src/haystack_integrations/utils/nvidia/nim_backend.py +169 -0
- nvidia_haystack-0.1.0/src/haystack_integrations/utils/nvidia/utils.py +47 -0
- {nvidia_haystack-0.0.4 → nvidia_haystack-0.1.0}/tests/__init__.py +3 -0
- nvidia_haystack-0.1.0/tests/conftest.py +45 -0
- nvidia_haystack-0.1.0/tests/test_base_url.py +75 -0
- {nvidia_haystack-0.0.4 → nvidia_haystack-0.1.0}/tests/test_document_embedder.py +122 -50
- nvidia_haystack-0.1.0/tests/test_embedding_truncate_mode.py +40 -0
- {nvidia_haystack-0.0.4 → nvidia_haystack-0.1.0}/tests/test_generator.py +57 -1
- nvidia_haystack-0.1.0/tests/test_ranker.py +258 -0
- {nvidia_haystack-0.0.4 → nvidia_haystack-0.1.0}/tests/test_text_embedder.py +80 -34
- nvidia_haystack-0.0.4/src/haystack_integrations/components/embedders/nvidia/_nim_backend.py +0 -52
- nvidia_haystack-0.0.4/src/haystack_integrations/components/embedders/nvidia/backend.py +0 -29
- nvidia_haystack-0.0.4/src/haystack_integrations/components/generators/nvidia/_nim_backend.py +0 -80
- nvidia_haystack-0.0.4/src/haystack_integrations/components/generators/nvidia/backend.py +0 -29
- {nvidia_haystack-0.0.4 → nvidia_haystack-0.1.0}/.gitignore +0 -0
- {nvidia_haystack-0.0.4 → nvidia_haystack-0.1.0}/LICENSE.txt +0 -0
- {nvidia_haystack-0.0.4 → nvidia_haystack-0.1.0}/README.md +0 -0
- {nvidia_haystack-0.0.4 → nvidia_haystack-0.1.0}/src/haystack_integrations/components/embedders/nvidia/__init__.py +0 -0
- {nvidia_haystack-0.0.4 → nvidia_haystack-0.1.0}/src/haystack_integrations/components/generators/nvidia/__init__.py +0 -0
- {nvidia_haystack-0.0.4 → nvidia_haystack-0.1.0}/src/haystack_integrations/components/generators/nvidia/chat/__init__.py +0 -0
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
## [unreleased]
|
|
4
|
+
|
|
5
|
+
### 🚀 Features
|
|
6
|
+
|
|
7
|
+
- Update default embedding model to nvidia/nv-embedqa-e5-v5 (#1015)
|
|
8
|
+
- Add NVIDIA NIM ranker support (#1023)
|
|
9
|
+
|
|
10
|
+
### 🐛 Bug Fixes
|
|
11
|
+
|
|
12
|
+
- Lints in `nvidia-haystack` (#993)
|
|
13
|
+
|
|
14
|
+
### 🚜 Refactor
|
|
15
|
+
|
|
16
|
+
- Remove deprecated Nvidia Cloud Functions backend and related code. (#803)
|
|
17
|
+
|
|
18
|
+
### 📚 Documentation
|
|
19
|
+
|
|
20
|
+
- Update Nvidia API docs (#1031)
|
|
21
|
+
|
|
22
|
+
### 🧪 Testing
|
|
23
|
+
|
|
24
|
+
- Do not retry tests in `hatch run test` command (#954)
|
|
25
|
+
|
|
26
|
+
### ⚙️ Miscellaneous Tasks
|
|
27
|
+
|
|
28
|
+
- Retry tests to reduce flakyness (#836)
|
|
29
|
+
- Update ruff invocation to include check parameter (#853)
|
|
30
|
+
|
|
31
|
+
### Docs
|
|
32
|
+
|
|
33
|
+
- Update NvidiaGenerator docstrings (#966)
|
|
34
|
+
|
|
35
|
+
## [integrations/nvidia-v0.0.3] - 2024-05-22
|
|
36
|
+
|
|
37
|
+
### 📚 Documentation
|
|
38
|
+
|
|
39
|
+
- Update docstrings of Nvidia integrations (#599)
|
|
40
|
+
|
|
41
|
+
### ⚙️ Miscellaneous Tasks
|
|
42
|
+
|
|
43
|
+
- Add generate docs to Nvidia workflow (#603)
|
|
44
|
+
|
|
45
|
+
## [integrations/nvidia-v0.0.2] - 2024-03-18
|
|
46
|
+
|
|
47
|
+
### 📚 Documentation
|
|
48
|
+
|
|
49
|
+
- Disable-class-def (#556)
|
|
50
|
+
|
|
51
|
+
## [integrations/nvidia-v0.0.1] - 2024-03-07
|
|
52
|
+
|
|
53
|
+
### 🚀 Features
|
|
54
|
+
|
|
55
|
+
- Add `NvidiaTextEmbedder`, `NvidiaDocumentEmbedder` and co. (#537)
|
|
56
|
+
|
|
57
|
+
### 🐛 Bug Fixes
|
|
58
|
+
|
|
59
|
+
- `nvidia-haystack`- Handle non-strict env var secrets correctly (#543)
|
|
60
|
+
|
|
61
|
+
## [integrations/nvidia-v0.0.0] - 2024-03-01
|
|
62
|
+
|
|
63
|
+
<!-- generated by git-cliff -->
|
|
@@ -1,12 +1,11 @@
|
|
|
1
1
|
Metadata-Version: 2.3
|
|
2
2
|
Name: nvidia-haystack
|
|
3
|
-
Version: 0.0
|
|
3
|
+
Version: 0.1.0
|
|
4
4
|
Project-URL: Documentation, https://github.com/deepset-ai/haystack-core-integrations/tree/main/integrations/nvidia#readme
|
|
5
5
|
Project-URL: Issues, https://github.com/deepset-ai/haystack-core-integrations/issues
|
|
6
6
|
Project-URL: Source, https://github.com/deepset-ai/haystack-core-integrations/tree/main/integrations/nvidia
|
|
7
7
|
Author-email: deepset GmbH <info@deepset.ai>
|
|
8
|
-
License
|
|
9
|
-
License-File: LICENSE.txt
|
|
8
|
+
License: Apache-2.0
|
|
10
9
|
Classifier: Development Status :: 4 - Beta
|
|
11
10
|
Classifier: License :: OSI Approved :: Apache Software License
|
|
12
11
|
Classifier: Programming Language :: Python
|
|
@@ -5,7 +5,10 @@ loaders:
|
|
|
5
5
|
[
|
|
6
6
|
"haystack_integrations.components.embedders.nvidia.document_embedder",
|
|
7
7
|
"haystack_integrations.components.embedders.nvidia.text_embedder",
|
|
8
|
+
"haystack_integrations.components.embedders.nvidia.truncate",
|
|
8
9
|
"haystack_integrations.components.generators.nvidia.generator",
|
|
10
|
+
"haystack_integrations.components.rankers.nvidia.ranker",
|
|
11
|
+
"haystack_integrations.components.rankers.nvidia.truncate",
|
|
9
12
|
]
|
|
10
13
|
ignore_when_discovered: ["__init__"]
|
|
11
14
|
processors:
|
|
@@ -42,24 +42,37 @@ root = "../.."
|
|
|
42
42
|
git_describe_command = 'git describe --tags --match="integrations/nvidia-v[0-9]*"'
|
|
43
43
|
|
|
44
44
|
[tool.hatch.envs.default]
|
|
45
|
-
|
|
45
|
+
installer = "uv"
|
|
46
|
+
dependencies = [
|
|
47
|
+
"coverage[toml]>=6.5",
|
|
48
|
+
"pytest",
|
|
49
|
+
"pytest-rerunfailures",
|
|
50
|
+
"haystack-pydoc-tools",
|
|
51
|
+
"requests_mock",
|
|
52
|
+
]
|
|
46
53
|
[tool.hatch.envs.default.scripts]
|
|
47
|
-
test = "pytest
|
|
48
|
-
test-cov = "coverage run -m pytest
|
|
54
|
+
test = "pytest {args:tests}"
|
|
55
|
+
test-cov = "coverage run -m pytest {args:tests}"
|
|
56
|
+
test-cov-retry = "test-cov --reruns 3 --reruns-delay 30 -x"
|
|
49
57
|
cov-report = ["- coverage combine", "coverage report"]
|
|
50
58
|
cov = ["test-cov", "cov-report"]
|
|
59
|
+
cov-retry = ["test-cov-retry", "cov-report"]
|
|
51
60
|
docs = ["pydoc-markdown pydoc/config.yml"]
|
|
52
61
|
|
|
53
62
|
[[tool.hatch.envs.all.matrix]]
|
|
54
63
|
python = ["3.8", "3.9", "3.10", "3.11"]
|
|
55
64
|
|
|
56
65
|
[tool.hatch.envs.lint]
|
|
66
|
+
installer = "uv"
|
|
57
67
|
detached = true
|
|
58
|
-
dependencies = ["black>=23.1.0", "mypy>=1.0.0", "ruff>=0.0.243"]
|
|
68
|
+
dependencies = ["pip", "black>=23.1.0", "mypy>=1.0.0", "ruff>=0.0.243"]
|
|
59
69
|
[tool.hatch.envs.lint.scripts]
|
|
60
70
|
typing = "mypy --install-types --non-interactive --explicit-package-bases {args:src/ tests}"
|
|
61
|
-
style = [
|
|
62
|
-
|
|
71
|
+
style = [
|
|
72
|
+
"ruff check {args:}",
|
|
73
|
+
"black --check --diff {args:.}",
|
|
74
|
+
]
|
|
75
|
+
fmt = ["black {args:.}", "ruff check --fix {args:}", "style"]
|
|
63
76
|
all = ["style", "typing"]
|
|
64
77
|
|
|
65
78
|
[tool.black]
|
|
@@ -70,6 +83,8 @@ skip-string-normalization = true
|
|
|
70
83
|
[tool.ruff]
|
|
71
84
|
target-version = "py38"
|
|
72
85
|
line-length = 120
|
|
86
|
+
|
|
87
|
+
[tool.ruff.lint]
|
|
73
88
|
select = [
|
|
74
89
|
"A",
|
|
75
90
|
"ARG",
|
|
@@ -118,13 +133,13 @@ unfixable = [
|
|
|
118
133
|
"F401",
|
|
119
134
|
]
|
|
120
135
|
|
|
121
|
-
[tool.ruff.isort]
|
|
122
|
-
known-first-party = ["
|
|
136
|
+
[tool.ruff.lint.isort]
|
|
137
|
+
known-first-party = ["haystack_integrations"]
|
|
123
138
|
|
|
124
|
-
[tool.ruff.flake8-tidy-imports]
|
|
139
|
+
[tool.ruff.lint.flake8-tidy-imports]
|
|
125
140
|
ban-relative-imports = "parents"
|
|
126
141
|
|
|
127
|
-
[tool.ruff.per-file-ignores]
|
|
142
|
+
[tool.ruff.lint.per-file-ignores]
|
|
128
143
|
# Tests can use magic values, assertions, and relative imports
|
|
129
144
|
"tests/**/*" = ["PLR2004", "S101", "TID252"]
|
|
130
145
|
|
|
@@ -147,6 +162,8 @@ module = [
|
|
|
147
162
|
"haystack_integrations.*",
|
|
148
163
|
"pytest.*",
|
|
149
164
|
"numpy.*",
|
|
165
|
+
"requests_mock.*",
|
|
166
|
+
"pydantic.*",
|
|
150
167
|
]
|
|
151
168
|
ignore_missing_imports = true
|
|
152
169
|
|
|
@@ -1,13 +1,16 @@
|
|
|
1
|
+
import warnings
|
|
1
2
|
from typing import Any, Dict, List, Optional, Tuple, Union
|
|
2
3
|
|
|
3
4
|
from haystack import Document, component, default_from_dict, default_to_dict
|
|
4
5
|
from haystack.utils import Secret, deserialize_secrets_inplace
|
|
5
6
|
from tqdm import tqdm
|
|
6
7
|
|
|
7
|
-
from .
|
|
8
|
-
|
|
8
|
+
from haystack_integrations.utils.nvidia import NimBackend, is_hosted, url_validation
|
|
9
|
+
|
|
9
10
|
from .truncate import EmbeddingTruncateMode
|
|
10
11
|
|
|
12
|
+
_DEFAULT_API_URL = "https://ai.api.nvidia.com/v1/retrieval/nvidia"
|
|
13
|
+
|
|
11
14
|
|
|
12
15
|
@component
|
|
13
16
|
class NvidiaDocumentEmbedder:
|
|
@@ -31,9 +34,9 @@ class NvidiaDocumentEmbedder:
|
|
|
31
34
|
|
|
32
35
|
def __init__(
|
|
33
36
|
self,
|
|
34
|
-
model: str =
|
|
37
|
+
model: Optional[str] = None,
|
|
35
38
|
api_key: Optional[Secret] = Secret.from_env_var("NVIDIA_API_KEY"),
|
|
36
|
-
api_url: str =
|
|
39
|
+
api_url: str = _DEFAULT_API_URL,
|
|
37
40
|
prefix: str = "",
|
|
38
41
|
suffix: str = "",
|
|
39
42
|
batch_size: int = 32,
|
|
@@ -47,10 +50,13 @@ class NvidiaDocumentEmbedder:
|
|
|
47
50
|
|
|
48
51
|
:param model:
|
|
49
52
|
Embedding model to use.
|
|
53
|
+
If no specific model along with locally hosted API URL is provided,
|
|
54
|
+
the system defaults to the available model found using /models API.
|
|
50
55
|
:param api_key:
|
|
51
56
|
API key for the NVIDIA NIM.
|
|
52
57
|
:param api_url:
|
|
53
58
|
Custom API URL for the NVIDIA NIM.
|
|
59
|
+
Format for API URL is http://host:port
|
|
54
60
|
:param prefix:
|
|
55
61
|
A string to add to the beginning of each text.
|
|
56
62
|
:param suffix:
|
|
@@ -71,7 +77,7 @@ class NvidiaDocumentEmbedder:
|
|
|
71
77
|
|
|
72
78
|
self.api_key = api_key
|
|
73
79
|
self.model = model
|
|
74
|
-
self.api_url = api_url
|
|
80
|
+
self.api_url = url_validation(api_url, _DEFAULT_API_URL, ["v1/embeddings"])
|
|
75
81
|
self.prefix = prefix
|
|
76
82
|
self.suffix = suffix
|
|
77
83
|
self.batch_size = batch_size
|
|
@@ -83,9 +89,31 @@ class NvidiaDocumentEmbedder:
|
|
|
83
89
|
truncate = EmbeddingTruncateMode.from_str(truncate)
|
|
84
90
|
self.truncate = truncate
|
|
85
91
|
|
|
86
|
-
self.backend: Optional[
|
|
92
|
+
self.backend: Optional[Any] = None
|
|
87
93
|
self._initialized = False
|
|
88
94
|
|
|
95
|
+
if is_hosted(api_url) and not self.model: # manually set default model
|
|
96
|
+
self.model = "nvidia/nv-embedqa-e5-v5"
|
|
97
|
+
|
|
98
|
+
def default_model(self):
|
|
99
|
+
"""Set default model in local NIM mode."""
|
|
100
|
+
valid_models = [
|
|
101
|
+
model.id for model in self.backend.models() if not model.base_model or model.base_model == model.id
|
|
102
|
+
]
|
|
103
|
+
name = next(iter(valid_models), None)
|
|
104
|
+
if name:
|
|
105
|
+
warnings.warn(
|
|
106
|
+
f"Default model is set as: {name}. \n"
|
|
107
|
+
"Set model using model parameter. \n"
|
|
108
|
+
"To get available models use available_models property.",
|
|
109
|
+
UserWarning,
|
|
110
|
+
stacklevel=2,
|
|
111
|
+
)
|
|
112
|
+
self.model = self.backend.model = name
|
|
113
|
+
else:
|
|
114
|
+
error_message = "No locally hosted model was found."
|
|
115
|
+
raise ValueError(error_message)
|
|
116
|
+
|
|
89
117
|
def warm_up(self):
|
|
90
118
|
"""
|
|
91
119
|
Initializes the component.
|
|
@@ -105,6 +133,9 @@ class NvidiaDocumentEmbedder:
|
|
|
105
133
|
|
|
106
134
|
self._initialized = True
|
|
107
135
|
|
|
136
|
+
if not self.model:
|
|
137
|
+
self.default_model()
|
|
138
|
+
|
|
108
139
|
def to_dict(self) -> Dict[str, Any]:
|
|
109
140
|
"""
|
|
110
141
|
Serializes the component to a dictionary.
|
|
@@ -200,6 +231,11 @@ class NvidiaDocumentEmbedder:
|
|
|
200
231
|
)
|
|
201
232
|
raise TypeError(msg)
|
|
202
233
|
|
|
234
|
+
for doc in documents:
|
|
235
|
+
if not doc.content:
|
|
236
|
+
msg = f"Document '{doc.id}' has no content to embed."
|
|
237
|
+
raise ValueError(msg)
|
|
238
|
+
|
|
203
239
|
texts_to_embed = self._prepare_texts_to_embed(documents)
|
|
204
240
|
embeddings, metadata = self._embed_batch(texts_to_embed, self.batch_size)
|
|
205
241
|
for doc, emb in zip(documents, embeddings):
|
|
@@ -1,12 +1,15 @@
|
|
|
1
|
+
import warnings
|
|
1
2
|
from typing import Any, Dict, List, Optional, Union
|
|
2
3
|
|
|
3
4
|
from haystack import component, default_from_dict, default_to_dict
|
|
4
5
|
from haystack.utils import Secret, deserialize_secrets_inplace
|
|
5
6
|
|
|
6
|
-
from .
|
|
7
|
-
|
|
7
|
+
from haystack_integrations.utils.nvidia import NimBackend, is_hosted, url_validation
|
|
8
|
+
|
|
8
9
|
from .truncate import EmbeddingTruncateMode
|
|
9
10
|
|
|
11
|
+
_DEFAULT_API_URL = "https://ai.api.nvidia.com/v1/retrieval/nvidia"
|
|
12
|
+
|
|
10
13
|
|
|
11
14
|
@component
|
|
12
15
|
class NvidiaTextEmbedder:
|
|
@@ -32,9 +35,9 @@ class NvidiaTextEmbedder:
|
|
|
32
35
|
|
|
33
36
|
def __init__(
|
|
34
37
|
self,
|
|
35
|
-
model: str =
|
|
38
|
+
model: Optional[str] = None,
|
|
36
39
|
api_key: Optional[Secret] = Secret.from_env_var("NVIDIA_API_KEY"),
|
|
37
|
-
api_url: str =
|
|
40
|
+
api_url: str = _DEFAULT_API_URL,
|
|
38
41
|
prefix: str = "",
|
|
39
42
|
suffix: str = "",
|
|
40
43
|
truncate: Optional[Union[EmbeddingTruncateMode, str]] = None,
|
|
@@ -44,10 +47,13 @@ class NvidiaTextEmbedder:
|
|
|
44
47
|
|
|
45
48
|
:param model:
|
|
46
49
|
Embedding model to use.
|
|
50
|
+
If no specific model along with locally hosted API URL is provided,
|
|
51
|
+
the system defaults to the available model found using /models API.
|
|
47
52
|
:param api_key:
|
|
48
53
|
API key for the NVIDIA NIM.
|
|
49
54
|
:param api_url:
|
|
50
55
|
Custom API URL for the NVIDIA NIM.
|
|
56
|
+
Format for API URL is http://host:port
|
|
51
57
|
:param prefix:
|
|
52
58
|
A string to add to the beginning of each text.
|
|
53
59
|
:param suffix:
|
|
@@ -59,7 +65,7 @@ class NvidiaTextEmbedder:
|
|
|
59
65
|
|
|
60
66
|
self.api_key = api_key
|
|
61
67
|
self.model = model
|
|
62
|
-
self.api_url = api_url
|
|
68
|
+
self.api_url = url_validation(api_url, _DEFAULT_API_URL, ["v1/embeddings"])
|
|
63
69
|
self.prefix = prefix
|
|
64
70
|
self.suffix = suffix
|
|
65
71
|
|
|
@@ -67,9 +73,31 @@ class NvidiaTextEmbedder:
|
|
|
67
73
|
truncate = EmbeddingTruncateMode.from_str(truncate)
|
|
68
74
|
self.truncate = truncate
|
|
69
75
|
|
|
70
|
-
self.backend: Optional[
|
|
76
|
+
self.backend: Optional[Any] = None
|
|
71
77
|
self._initialized = False
|
|
72
78
|
|
|
79
|
+
if is_hosted(api_url) and not self.model: # manually set default model
|
|
80
|
+
self.model = "nvidia/nv-embedqa-e5-v5"
|
|
81
|
+
|
|
82
|
+
def default_model(self):
|
|
83
|
+
"""Set default model in local NIM mode."""
|
|
84
|
+
valid_models = [
|
|
85
|
+
model.id for model in self.backend.models() if not model.base_model or model.base_model == model.id
|
|
86
|
+
]
|
|
87
|
+
name = next(iter(valid_models), None)
|
|
88
|
+
if name:
|
|
89
|
+
warnings.warn(
|
|
90
|
+
f"Default model is set as: {name}. \n"
|
|
91
|
+
"Set model using model parameter. \n"
|
|
92
|
+
"To get available models use available_models property.",
|
|
93
|
+
UserWarning,
|
|
94
|
+
stacklevel=2,
|
|
95
|
+
)
|
|
96
|
+
self.model = self.backend.model = name
|
|
97
|
+
else:
|
|
98
|
+
error_message = "No locally hosted model was found."
|
|
99
|
+
raise ValueError(error_message)
|
|
100
|
+
|
|
73
101
|
def warm_up(self):
|
|
74
102
|
"""
|
|
75
103
|
Initializes the component.
|
|
@@ -89,6 +117,9 @@ class NvidiaTextEmbedder:
|
|
|
89
117
|
|
|
90
118
|
self._initialized = True
|
|
91
119
|
|
|
120
|
+
if not self.model:
|
|
121
|
+
self.default_model()
|
|
122
|
+
|
|
92
123
|
def to_dict(self) -> Dict[str, Any]:
|
|
93
124
|
"""
|
|
94
125
|
Serializes the component to a dictionary.
|
|
@@ -144,6 +175,9 @@ class NvidiaTextEmbedder:
|
|
|
144
175
|
"In case you want to embed a list of Documents, please use the NvidiaDocumentEmbedder."
|
|
145
176
|
)
|
|
146
177
|
raise TypeError(msg)
|
|
178
|
+
elif not text:
|
|
179
|
+
msg = "Cannot embed an empty string."
|
|
180
|
+
raise ValueError(msg)
|
|
147
181
|
|
|
148
182
|
assert self.backend is not None
|
|
149
183
|
text_to_embed = self.prefix + text + self.suffix
|
|
@@ -6,10 +6,12 @@ class EmbeddingTruncateMode(Enum):
|
|
|
6
6
|
Specifies how inputs to the NVIDIA embedding components are truncated.
|
|
7
7
|
If START, the input will be truncated from the start.
|
|
8
8
|
If END, the input will be truncated from the end.
|
|
9
|
+
If NONE, an error will be returned (if the input is too long).
|
|
9
10
|
"""
|
|
10
11
|
|
|
11
12
|
START = "START"
|
|
12
13
|
END = "END"
|
|
14
|
+
NONE = "NONE"
|
|
13
15
|
|
|
14
16
|
def __str__(self):
|
|
15
17
|
return self.value
|
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
# SPDX-FileCopyrightText: 2024-present deepset GmbH <info@deepset.ai>
|
|
2
2
|
#
|
|
3
3
|
# SPDX-License-Identifier: Apache-2.0
|
|
4
|
+
import warnings
|
|
4
5
|
from typing import Any, Dict, List, Optional
|
|
5
6
|
|
|
6
7
|
from haystack import component, default_from_dict, default_to_dict
|
|
7
8
|
from haystack.utils.auth import Secret, deserialize_secrets_inplace
|
|
8
9
|
|
|
9
|
-
from .
|
|
10
|
-
from .backend import GeneratorBackend
|
|
10
|
+
from haystack_integrations.utils.nvidia import NimBackend, is_hosted, url_validation
|
|
11
11
|
|
|
12
12
|
_DEFAULT_API_URL = "https://integrate.api.nvidia.com/v1"
|
|
13
13
|
|
|
@@ -15,10 +15,11 @@ _DEFAULT_API_URL = "https://integrate.api.nvidia.com/v1"
|
|
|
15
15
|
@component
|
|
16
16
|
class NvidiaGenerator:
|
|
17
17
|
"""
|
|
18
|
-
|
|
19
|
-
[NVIDIA
|
|
18
|
+
Generates text using generative models hosted with
|
|
19
|
+
[NVIDIA NIM](https://ai.nvidia.com) on on the [NVIDIA API Catalog](https://build.nvidia.com/explore/discover).
|
|
20
|
+
|
|
21
|
+
### Usage example
|
|
20
22
|
|
|
21
|
-
Usage example:
|
|
22
23
|
```python
|
|
23
24
|
from haystack_integrations.components.generators.nvidia import NvidiaGenerator
|
|
24
25
|
|
|
@@ -37,11 +38,13 @@ class NvidiaGenerator:
|
|
|
37
38
|
print(result["meta"])
|
|
38
39
|
print(result["usage"])
|
|
39
40
|
```
|
|
41
|
+
|
|
42
|
+
You need an NVIDIA API key for this component to work.
|
|
40
43
|
"""
|
|
41
44
|
|
|
42
45
|
def __init__(
|
|
43
46
|
self,
|
|
44
|
-
model: str,
|
|
47
|
+
model: Optional[str] = None,
|
|
45
48
|
api_url: str = _DEFAULT_API_URL,
|
|
46
49
|
api_key: Optional[Secret] = Secret.from_env_var("NVIDIA_API_KEY"),
|
|
47
50
|
model_arguments: Optional[Dict[str, Any]] = None,
|
|
@@ -53,21 +56,47 @@ class NvidiaGenerator:
|
|
|
53
56
|
Name of the model to use for text generation.
|
|
54
57
|
See the [NVIDIA NIMs](https://ai.nvidia.com)
|
|
55
58
|
for more information on the supported models.
|
|
59
|
+
`Note`: If no specific model along with locally hosted API URL is provided,
|
|
60
|
+
the system defaults to the available model found using /models API.
|
|
61
|
+
Check supported models at [NVIDIA NIM](https://ai.nvidia.com).
|
|
56
62
|
:param api_key:
|
|
57
|
-
API key for the NVIDIA NIM.
|
|
63
|
+
API key for the NVIDIA NIM. Set it as the `NVIDIA_API_KEY` environment
|
|
64
|
+
variable or pass it here.
|
|
58
65
|
:param api_url:
|
|
59
66
|
Custom API URL for the NVIDIA NIM.
|
|
60
67
|
:param model_arguments:
|
|
61
|
-
Additional arguments to pass to the model provider.
|
|
62
|
-
|
|
63
|
-
|
|
68
|
+
Additional arguments to pass to the model provider. These arguments are
|
|
69
|
+
specific to a model.
|
|
70
|
+
Search your model in the [NVIDIA NIM](https://ai.nvidia.com)
|
|
71
|
+
to find the arguments it accepts.
|
|
64
72
|
"""
|
|
65
73
|
self._model = model
|
|
66
|
-
self._api_url = api_url
|
|
74
|
+
self._api_url = url_validation(api_url, _DEFAULT_API_URL, ["v1/chat/completions"])
|
|
67
75
|
self._api_key = api_key
|
|
68
76
|
self._model_arguments = model_arguments or {}
|
|
69
77
|
|
|
70
|
-
self._backend: Optional[
|
|
78
|
+
self._backend: Optional[Any] = None
|
|
79
|
+
|
|
80
|
+
self.is_hosted = is_hosted(api_url)
|
|
81
|
+
|
|
82
|
+
def default_model(self):
|
|
83
|
+
"""Set default model in local NIM mode."""
|
|
84
|
+
valid_models = [
|
|
85
|
+
model.id for model in self._backend.models() if not model.base_model or model.base_model == model.id
|
|
86
|
+
]
|
|
87
|
+
name = next(iter(valid_models), None)
|
|
88
|
+
if name:
|
|
89
|
+
warnings.warn(
|
|
90
|
+
f"Default model is set as: {name}. \n"
|
|
91
|
+
"Set model using model parameter. \n"
|
|
92
|
+
"To get available models use available_models property.",
|
|
93
|
+
UserWarning,
|
|
94
|
+
stacklevel=2,
|
|
95
|
+
)
|
|
96
|
+
self._model = self._backend.model_name = name
|
|
97
|
+
else:
|
|
98
|
+
error_message = "No locally hosted model was found."
|
|
99
|
+
raise ValueError(error_message)
|
|
71
100
|
|
|
72
101
|
def warm_up(self):
|
|
73
102
|
"""
|
|
@@ -86,6 +115,9 @@ class NvidiaGenerator:
|
|
|
86
115
|
model_kwargs=self._model_arguments,
|
|
87
116
|
)
|
|
88
117
|
|
|
118
|
+
if not self.is_hosted and not self._model:
|
|
119
|
+
self.default_model()
|
|
120
|
+
|
|
89
121
|
def to_dict(self) -> Dict[str, Any]:
|
|
90
122
|
"""
|
|
91
123
|
Serializes the component to a dictionary.
|