-
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.
Raise warning for base_url ../embeddings .../completions .../rankings (…
…#922) * add validation for base url routes * move url validation to utils * update docstring for url validation * add typing for arg type * return docstring update Co-authored-by: Madeesh Kannan <[email protected]> * fix typo error Co-authored-by: Madeesh Kannan <[email protected]> --------- Co-authored-by: Madeesh Kannan <[email protected]>
- Loading branch information
Showing
9 changed files
with
160 additions
and
43 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
3 changes: 3 additions & 0 deletions
3
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 |
---|---|---|
@@ -0,0 +1,3 @@ | ||
from .utils import url_validation | ||
|
||
__all__ = ["url_validation"] |
39 changes: 39 additions & 0 deletions
39
integrations/nvidia/src/haystack_integrations/utils/nvidia/utils.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,39 @@ | ||
import warnings | ||
from typing import List | ||
from urllib.parse import urlparse, urlunparse | ||
|
||
|
||
def url_validation(api_url: str, default_api_url: str, allowed_paths: List[str]) -> str: | ||
""" | ||
Validate and normalize an API URL. | ||
:param api_url: | ||
The API URL to validate and normalize. | ||
:param default_api_url: | ||
The default API URL for comparison. | ||
:param allowed_paths: | ||
A list of allowed base paths that are valid if present in the URL. | ||
:returns: | ||
A normalized version of the API URL with '/v1' path appended, if needed. | ||
:raises ValueError: | ||
If the base URL path is not recognized or does not match expected format. | ||
""" | ||
## Making sure /v1 in added to the url, followed by infer_path | ||
result = urlparse(api_url) | ||
expected_format = "Expected format is 'http://host:port'." | ||
|
||
if api_url == default_api_url: | ||
return api_url | ||
if result.path: | ||
normalized_path = result.path.strip("/") | ||
if normalized_path == "v1": | ||
pass | ||
elif normalized_path in allowed_paths: | ||
warn_msg = f"{expected_format} Rest is ignored." | ||
warnings.warn(warn_msg, stacklevel=2) | ||
else: | ||
err_msg = f"Base URL path is not recognized. {expected_format}" | ||
raise ValueError(err_msg) | ||
|
||
base_url = urlunparse((result.scheme, result.netloc, "v1", "", "", "")) | ||
return base_url |
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,64 @@ | ||
import pytest | ||
from haystack_integrations.components.embedders.nvidia import NvidiaDocumentEmbedder, NvidiaTextEmbedder | ||
from haystack_integrations.components.generators.nvidia import NvidiaGenerator | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"base_url", | ||
[ | ||
"http://localhost:8888/embeddings", | ||
"http://0.0.0.0:8888/rankings", | ||
"http://0.0.0.0:8888/v1/rankings", | ||
"http://localhost:8888/chat/completions", | ||
"http://localhost:8888/v1/chat/completions", | ||
], | ||
) | ||
@pytest.mark.parametrize( | ||
"embedder", | ||
[NvidiaDocumentEmbedder, NvidiaTextEmbedder], | ||
) | ||
def test_base_url_invalid_not_hosted(base_url: str, embedder) -> None: | ||
with pytest.raises(ValueError): | ||
embedder(api_url=base_url, model="x") | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"base_url", | ||
["http://localhost:8080/v1/embeddings", "http://0.0.0.0:8888/v1/embeddings"], | ||
) | ||
@pytest.mark.parametrize( | ||
"embedder", | ||
[NvidiaDocumentEmbedder, NvidiaTextEmbedder], | ||
) | ||
def test_base_url_valid_embedder(base_url: str, embedder) -> None: | ||
with pytest.warns(UserWarning): | ||
embedder(api_url=base_url) | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"base_url", | ||
[ | ||
"http://localhost:8080/v1/chat/completions", | ||
"http://0.0.0.0:8888/v1/chat/completions", | ||
], | ||
) | ||
def test_base_url_valid_generator(base_url: str) -> None: | ||
with pytest.warns(UserWarning): | ||
NvidiaGenerator( | ||
api_url=base_url, | ||
model="mistralai/mixtral-8x7b-instruct-v0.1", | ||
) | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"base_url", | ||
[ | ||
"http://localhost:8888/embeddings", | ||
"http://0.0.0.0:8888/rankings", | ||
"http://0.0.0.0:8888/v1/rankings", | ||
"http://localhost:8888/chat/completions", | ||
], | ||
) | ||
def test_base_url_invalid_generator(base_url: str) -> None: | ||
with pytest.raises(ValueError): | ||
NvidiaGenerator(api_url=base_url, model="x") |
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