From 5e5abc589ae9aceba3f47b0f1d8072de403d93a6 Mon Sep 17 00:00:00 2001 From: zhonghanjun Date: Sun, 17 Dec 2023 01:43:42 +0800 Subject: [PATCH] fix: update path & merge tests --- .../chat_models/baidu_qianfan_endpoint.py | 3 -- .../chat_models/test_baiduqianfan.py | 53 ------------------ .../chat_models/test_qianfan_endpoint.py | 54 ++++++++++++++++++- 3 files changed, 52 insertions(+), 58 deletions(-) diff --git a/libs/community/langchain_community/chat_models/baidu_qianfan_endpoint.py b/libs/community/langchain_community/chat_models/baidu_qianfan_endpoint.py index ca96fc075e893..1d7e59c298277 100644 --- a/libs/community/langchain_community/chat_models/baidu_qianfan_endpoint.py +++ b/libs/community/langchain_community/chat_models/baidu_qianfan_endpoint.py @@ -19,9 +19,6 @@ from langchain_core.pydantic_v1 import Field, SecretStr, root_validator from langchain_core.utils import convert_to_secret_str, get_from_dict_or_env -from langchain_core.language_models.base import BaseChatModel -from langchain.utils import get_from_dict_or_env - logger = logging.getLogger(__name__) diff --git a/libs/community/tests/integration_tests/chat_models/test_baiduqianfan.py b/libs/community/tests/integration_tests/chat_models/test_baiduqianfan.py index c059693761869..e69de29bb2d1d 100644 --- a/libs/community/tests/integration_tests/chat_models/test_baiduqianfan.py +++ b/libs/community/tests/integration_tests/chat_models/test_baiduqianfan.py @@ -1,53 +0,0 @@ -from typing import cast - -from langchain_core.pydantic_v1 import SecretStr -from pytest import CaptureFixture, MonkeyPatch - -from langchain_community.chat_models.baidu_qianfan_endpoint import ( - QianfanChatEndpoint, -) - - -def test_qianfan_key_masked_when_passed_from_env( - monkeypatch: MonkeyPatch, capsys: CaptureFixture -) -> None: - """Test initialization with an API key provided via an env variable""" - monkeypatch.setenv("QIANFAN_AK", "test-api-key") - monkeypatch.setenv("QIANFAN_SK", "test-secret-key") - - chat = QianfanChatEndpoint() - print(chat.qianfan_ak, end="") - captured = capsys.readouterr() - assert captured.out == "**********" - - print(chat.qianfan_sk, end="") - captured = capsys.readouterr() - assert captured.out == "**********" - - -def test_qianfan_key_masked_when_passed_via_constructor( - capsys: CaptureFixture, -) -> None: - """Test initialization with an API key provided via the initializer""" - chat = QianfanChatEndpoint( - qianfan_ak="test-api-key", - qianfan_sk="test-secret-key", - ) - print(chat.qianfan_ak, end="") - captured = capsys.readouterr() - assert captured.out == "**********" - - print(chat.qianfan_sk, end="") - captured = capsys.readouterr() - - assert captured.out == "**********" - - -def test_uses_actual_secret_value_from_secret_str() -> None: - """Test that actual secret is retrieved using `.get_secret_value()`.""" - chat = QianfanChatEndpoint( - qianfan_ak="test-api-key", - qianfan_sk="test-secret-key", - ) - assert cast(SecretStr, chat.qianfan_ak).get_secret_value() == "test-api-key" - assert cast(SecretStr, chat.qianfan_sk).get_secret_value() == "test-secret-key" diff --git a/libs/community/tests/integration_tests/chat_models/test_qianfan_endpoint.py b/libs/community/tests/integration_tests/chat_models/test_qianfan_endpoint.py index f4fed76b20b0a..70145fc04d870 100644 --- a/libs/community/tests/integration_tests/chat_models/test_qianfan_endpoint.py +++ b/libs/community/tests/integration_tests/chat_models/test_qianfan_endpoint.py @@ -1,19 +1,24 @@ """Test Baidu Qianfan Chat Endpoint.""" -from typing import Any +from typing import Any, cast import pytest from langchain_core.callbacks import CallbackManager from langchain_core.messages import ( AIMessage, BaseMessage, + BaseMessageChunk, FunctionMessage, HumanMessage, ) from langchain_core.outputs import ChatGeneration, LLMResult from langchain_core.prompts import ChatPromptTemplate, HumanMessagePromptTemplate +from langchain_core.pydantic_v1 import SecretStr +from pytest import CaptureFixture, MonkeyPatch -from langchain_community.chat_models.baidu_qianfan_endpoint import QianfanChatEndpoint +from langchain_community.chat_models.baidu_qianfan_endpoint import ( + QianfanChatEndpoint, +) from tests.unit_tests.callbacks.fake_callback_handler import FakeCallbackHandler _FUNCTIONS: Any = [ @@ -288,3 +293,48 @@ def test_functions_call() -> None: chain = prompt | chat.bind(functions=_FUNCTIONS) resp = chain.invoke({}) assert isinstance(resp, AIMessage) + + +def test_qianfan_key_masked_when_passed_from_env( + monkeypatch: MonkeyPatch, capsys: CaptureFixture +) -> None: + """Test initialization with an API key provided via an env variable""" + monkeypatch.setenv("QIANFAN_AK", "test-api-key") + monkeypatch.setenv("QIANFAN_SK", "test-secret-key") + + chat = QianfanChatEndpoint() + print(chat.qianfan_ak, end="") + captured = capsys.readouterr() + assert captured.out == "**********" + + print(chat.qianfan_sk, end="") + captured = capsys.readouterr() + assert captured.out == "**********" + + +def test_qianfan_key_masked_when_passed_via_constructor( + capsys: CaptureFixture, +) -> None: + """Test initialization with an API key provided via the initializer""" + chat = QianfanChatEndpoint( + qianfan_ak="test-api-key", + qianfan_sk="test-secret-key", + ) + print(chat.qianfan_ak, end="") + captured = capsys.readouterr() + assert captured.out == "**********" + + print(chat.qianfan_sk, end="") + captured = capsys.readouterr() + + assert captured.out == "**********" + + +def test_uses_actual_secret_value_from_secret_str() -> None: + """Test that actual secret is retrieved using `.get_secret_value()`.""" + chat = QianfanChatEndpoint( + qianfan_ak="test-api-key", + qianfan_sk="test-secret-key", + ) + assert cast(SecretStr, chat.qianfan_ak).get_secret_value() == "test-api-key" + assert cast(SecretStr, chat.qianfan_sk).get_secret_value() == "test-secret-key"