-
Notifications
You must be signed in to change notification settings - Fork 126
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Abstract service endpoint backend * Abstract generators backend * Implement NimBackend * Implement NimBackend for embedders * Fix embedders backends arguments * Fix text embedder backend arguments * Make embedders nim backend consistent with nvcf one * Fix tests * Update headers, the generator endpoint, and the embedders input_type param * Update docstrings * Make api_key optional in generator * Remove api_key from NIM backend * Move usage in metadata in generator * Update tests * Remove OPENAI_API_KEY env var from workflow * Fix integration tests * Fix linting * Fix linting again * Address PR comments * Fix NVCF backend --------- Co-authored-by: shadeMe <[email protected]>
- Loading branch information
1 parent
1c31530
commit 5a8796b
Showing
17 changed files
with
662 additions
and
297 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
2 changes: 0 additions & 2 deletions
2
integrations/nvidia/src/haystack_integrations/components/embedders/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,9 +1,7 @@ | ||
from .document_embedder import NvidiaDocumentEmbedder | ||
from .models import NvidiaEmbeddingModel | ||
from .text_embedder import NvidiaTextEmbedder | ||
|
||
__all__ = [ | ||
"NvidiaDocumentEmbedder", | ||
"NvidiaEmbeddingModel", | ||
"NvidiaTextEmbedder", | ||
] |
46 changes: 46 additions & 0 deletions
46
integrations/nvidia/src/haystack_integrations/components/embedders/nvidia/_nim_backend.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 |
---|---|---|
@@ -0,0 +1,46 @@ | ||
from typing import Any, Dict, List, Optional, Tuple | ||
|
||
import requests | ||
|
||
from .backend import EmbedderBackend | ||
|
||
REQUEST_TIMEOUT = 60 | ||
|
||
|
||
class NimBackend(EmbedderBackend): | ||
def __init__( | ||
self, | ||
model: str, | ||
api_url: str, | ||
model_kwargs: Optional[Dict[str, Any]] = None, | ||
): | ||
headers = { | ||
"Content-Type": "application/json", | ||
"accept": "application/json", | ||
} | ||
self.session = requests.Session() | ||
self.session.headers.update(headers) | ||
|
||
self.model = model | ||
self.api_url = api_url | ||
self.model_kwargs = model_kwargs or {} | ||
|
||
def embed(self, texts: List[str]) -> Tuple[List[List[float]], Dict[str, Any]]: | ||
url = f"{self.api_url}/embeddings" | ||
|
||
res = self.session.post( | ||
url, | ||
json={ | ||
"model": self.model, | ||
"input": texts, | ||
**self.model_kwargs, | ||
}, | ||
timeout=REQUEST_TIMEOUT, | ||
) | ||
res.raise_for_status() | ||
|
||
data = res.json() | ||
# Sort the embeddings by index, we don't know whether they're out of order or not | ||
embeddings = [e["embedding"] for e in sorted(data["data"], key=lambda e: e["index"])] | ||
|
||
return embeddings, {"usage": data["usage"]} |
41 changes: 40 additions & 1 deletion
41
...ns/components/embedders/nvidia/_schema.py → ...ponents/embedders/nvidia/_nvcf_backend.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
29 changes: 29 additions & 0 deletions
29
integrations/nvidia/src/haystack_integrations/components/embedders/nvidia/backend.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 |
---|---|---|
@@ -0,0 +1,29 @@ | ||
from abc import ABC, abstractmethod | ||
from typing import Any, Dict, List, Optional, Tuple | ||
|
||
|
||
class EmbedderBackend(ABC): | ||
def __init__(self, model: str, model_kwargs: Optional[Dict[str, Any]] = None): | ||
""" | ||
Initialize the backend. | ||
:param model: | ||
The name of the model to use. | ||
:param model_kwargs: | ||
Additional keyword arguments to pass to the model. | ||
""" | ||
self.model_name = model | ||
self.model_kwargs = model_kwargs or {} | ||
|
||
@abstractmethod | ||
def embed(self, texts: List[str]) -> Tuple[List[List[float]], Dict[str, Any]]: | ||
""" | ||
Invoke the backend and embed the given texts. | ||
:param texts: | ||
Texts to embed. | ||
:return: | ||
Vector representation of the texts and | ||
metadata returned by the service. | ||
""" | ||
pass |
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
31 changes: 0 additions & 31 deletions
31
integrations/nvidia/src/haystack_integrations/components/embedders/nvidia/models.py
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.