Skip to content

Commit

Permalink
Merge pull request #1 from langchain-ai/mattf/patch-1
Browse files Browse the repository at this point in the history
fix lint and add tests
  • Loading branch information
VKudlay authored Mar 29, 2024
2 parents 1240d59 + 4850623 commit b824a70
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 1 deletion.
7 changes: 6 additions & 1 deletion libs/ai-endpoints/langchain_nvidia_ai_endpoints/_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,12 @@ def _try_raise(self, response: Response) -> None:
except Exception:
rd = {"detail": rd}
status = rd.get("status") or rd.get("status_code") or "###"
title = rd.get("title") or rd.get("error") or rd.get("reason") or "Unknown Error"
title = (
rd.get("title")
or rd.get("error")
or rd.get("reason")
or "Unknown Error"
)
header = f"[{status}] {title}"
body = ""
if "requestId" in rd:
Expand Down
27 changes: 27 additions & 0 deletions libs/ai-endpoints/tests/integration_tests/test_api_key.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import pytest

from langchain_nvidia_ai_endpoints import ChatNVIDIA

from ..unit_tests.test_api_key import no_env_var


def test_missing_api_key_error() -> None:
with no_env_var("NVIDIA_API_KEY"):
chat = ChatNVIDIA()
with pytest.raises(ValueError) as exc_info:
chat.invoke("Hello, world!")
message = str(exc_info.value)
assert "401" in message
assert "Unauthorized" in message
assert "API key" in message


def test_bogus_api_key_error() -> None:
with no_env_var("NVIDIA_API_KEY"):
chat = ChatNVIDIA(nvidia_api_key="BOGUS")
with pytest.raises(ValueError) as exc_info:
chat.invoke("Hello, world!")
message = str(exc_info.value)
assert "401" in message
assert "Unauthorized" in message
assert "API key" in message
26 changes: 26 additions & 0 deletions libs/ai-endpoints/tests/unit_tests/test_api_key.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import os
from contextlib import contextmanager
from typing import Generator

from langchain_nvidia_ai_endpoints import ChatNVIDIA, NVIDIAEmbeddings


@contextmanager
def no_env_var(var: str) -> Generator[None, None, None]:
try:
if key := os.environ.get(var, None):
del os.environ[var]
yield
finally:
if key:
os.environ[var] = key


def test_create_chat_without_api_key() -> None:
with no_env_var("NVIDIA_API_KEY"):
ChatNVIDIA()


def test_create_embeddings_without_api_key() -> None:
with no_env_var("NVIDIA_API_KEY"):
NVIDIAEmbeddings()

0 comments on commit b824a70

Please sign in to comment.