Skip to content

Commit

Permalink
test: tests with optional deps
Browse files Browse the repository at this point in the history
  • Loading branch information
yanomaly committed Nov 14, 2024
1 parent 8897656 commit 0d0e505
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 9 deletions.
18 changes: 14 additions & 4 deletions libs/community/langchain_community/chat_models/writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,8 @@ def _default_params(self) -> Dict[str, Any]:
}

@model_validator(mode="before")
def validate_environment(self, values: Dict) -> Any:
@classmethod
def validate_environment(cls, values: Dict) -> Any:
"""Validates that api key is passed and creates Writer clients."""
try:
from writerai import AsyncClient, Client
Expand All @@ -120,10 +121,19 @@ def validate_environment(self, values: Dict) -> Any:
"Please install it with `pip install writerai`."
) from e

if not (values["client"] and values["async_client"]):
if not (values.get("client") and values.get("async_client")):
api_key = get_from_dict_or_env(values, "api_key", "WRITER_API_KEY")
values["client"] = Client(api_key=api_key)
values["async_client"] = AsyncClient(api_key=api_key)
values.update({"client": Client(api_key=api_key)})
values.update({"async_client": AsyncClient(api_key=api_key)})

if not (
type(values.get("client")) is Client
and type(values.get("async_client")) is AsyncClient
):
raise ValueError(
"'client' attribute must be with type 'Client' and "
"'async_client' must be with type 'AsyncClient' from 'writerai' package"
)

return values

Expand Down
18 changes: 13 additions & 5 deletions libs/community/tests/unit_tests/chat_models/test_writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,25 +106,31 @@ def __init__(
self.choices = choices


@pytest.mark.requires("writer-sdk")
@pytest.mark.requires("writerai")
class TestChatWriterCustom:
"""Test case for ChatWriter"""

from writerai import AsyncClient, Client

def test_writer_model_param(self) -> None:
"""Test different ways to initialize the chat model."""
test_cases: List[dict] = [
{
"model_name": "palmyra-x-004",
"api_key": "key",
},
{
"model": "palmyra-x-004",
"api_key": "key",
},
{
"model_name": "palmyra-x-004",
"api_key": "key",
},
{
"model": "palmyra-x-004",
"temperature": 0.5,
"api_key": "key",
},
]

Expand Down Expand Up @@ -280,10 +286,12 @@ def test_sync_completion(
self, mock_unstreaming_completion: List[ChatCompletionChunk]
) -> None:
"""Test basic chat completion with mocked response."""
mock_client = MagicMock()
mock_client.chat.chat.return_value = mock_unstreaming_completion
mock_client = self.Client(api_key="key")
mock_client.chat.chat = MagicMock(return_value=mock_unstreaming_completion)

chat = ChatWriter(client=mock_client, async_client=AsyncMock())
async_client = self.AsyncClient(api_key="key")

chat = ChatWriter(client=mock_client, async_client=async_client)

message = HumanMessage(content="Hi there!")
response = chat.invoke([message])
Expand Down Expand Up @@ -416,7 +424,7 @@ class GetWeather(BaseModel):
assert response.tool_calls[0]["args"]["location"] == "London"


@pytest.mark.requires("writer-sdk")
@pytest.mark.requires("writerai")
class TestChatWriterStandart(ChatModelUnitTests):
"""Test case for ChatWriter that inherits from standard LangChain tests."""

Expand Down

0 comments on commit 0d0e505

Please sign in to comment.