From 16a422f3fa45e2ac35f8aee2c91d6b2852e8b74b Mon Sep 17 00:00:00 2001 From: ccurme Date: Sat, 1 Feb 2025 17:02:57 -0500 Subject: [PATCH] community: add standard tests for Perplexity (#29534) --- .../chat_models/test_perplexity.py | 33 +++++++++++++++++++ .../unit_tests/chat_models/test_perplexity.py | 19 ++++++++++- 2 files changed, 51 insertions(+), 1 deletion(-) create mode 100644 libs/community/tests/integration_tests/chat_models/test_perplexity.py diff --git a/libs/community/tests/integration_tests/chat_models/test_perplexity.py b/libs/community/tests/integration_tests/chat_models/test_perplexity.py new file mode 100644 index 00000000000..63cdd4a59d3 --- /dev/null +++ b/libs/community/tests/integration_tests/chat_models/test_perplexity.py @@ -0,0 +1,33 @@ +"""Standard LangChain interface tests""" + +from typing import Type + +import pytest +from langchain_core.language_models import BaseChatModel +from langchain_tests.integration_tests import ChatModelIntegrationTests + +from langchain_community.chat_models import ChatPerplexity + + +class TestPerplexityStandard(ChatModelIntegrationTests): + @property + def chat_model_class(self) -> Type[BaseChatModel]: + return ChatPerplexity + + @property + def chat_model_params(self) -> dict: + return {"model": "sonar"} + + @property + def returns_usage_metadata(self) -> bool: + # TODO: add usage metadata and delete this property + # https://docs.perplexity.ai/api-reference/chat-completions#response-usage + return False + + @pytest.mark.xfail(reason="TODO: handle in integration.") + def test_double_messages_conversation(self, model: BaseChatModel) -> None: + super().test_double_messages_conversation(model) + + @pytest.mark.xfail(reason="Raises 400: Custom stop words not supported.") + def test_stop_sequence(self, model: BaseChatModel) -> None: + super().test_stop_sequence(model) diff --git a/libs/community/tests/unit_tests/chat_models/test_perplexity.py b/libs/community/tests/unit_tests/chat_models/test_perplexity.py index d4991de422a..c5a745cbcea 100644 --- a/libs/community/tests/unit_tests/chat_models/test_perplexity.py +++ b/libs/community/tests/unit_tests/chat_models/test_perplexity.py @@ -1,11 +1,13 @@ """Test Perplexity Chat API wrapper.""" import os -from typing import Any, Dict, List, Optional +from typing import Any, Dict, List, Optional, Tuple, Type from unittest.mock import MagicMock import pytest +from langchain_core.language_models import BaseChatModel from langchain_core.messages import AIMessageChunk, BaseMessageChunk +from langchain_tests.unit_tests import ChatModelUnitTests from pytest_mock import MockerFixture from langchain_community.chat_models import ChatPerplexity @@ -13,6 +15,21 @@ from langchain_community.chat_models import ChatPerplexity os.environ["PPLX_API_KEY"] = "foo" +@pytest.mark.requires("openai") +class TestPerplexityStandard(ChatModelUnitTests): + @property + def chat_model_class(self) -> Type[BaseChatModel]: + return ChatPerplexity + + @property + def init_from_env_params(self) -> Tuple[dict, dict, dict]: + return ( + {"PPLX_API_KEY": "api_key"}, + {}, + {"pplx_api_key": "api_key"}, + ) + + @pytest.mark.requires("openai") def test_perplexity_model_name_param() -> None: llm = ChatPerplexity(model="foo") # type: ignore[call-arg]