-
Notifications
You must be signed in to change notification settings - Fork 16k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
pinecone: Review pinecone tests (#29073)
Title: langchain-pinecone: improve test structure and async handling Description: This PR improves the test infrastructure for the langchain-pinecone package by: 1. Implementing LangChain's standard test patterns for embeddings 2. Adding comprehensive configuration testing 3. Improving async test coverage 4. Fixing integration test issues with namespaces and async markers The changes make the tests more robust, maintainable, and aligned with LangChain's testing standards while ensuring proper async behavior in the embeddings implementation. Key improvements: - Added standard EmbeddingsTests implementation - Split custom configuration tests into a separate test class - Added proper async test coverage with pytest-asyncio - Fixed namespace handling in vector store integration tests - Improved test organization and documentation Dependencies: None (uses existing test dependencies) Tests and Documentation: - ✅ Added standard test implementation following LangChain's patterns - ✅ Added comprehensive unit tests for configuration and async behavior - ✅ All tests passing locally - No documentation changes needed (internal test improvements only) Twitter handle: N/A --------- Co-authored-by: Erick Friis <[email protected]>
- Loading branch information
1 parent
d9c51b7
commit ce9e9f9
Showing
7 changed files
with
616 additions
and
519 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
Large diffs are not rendered by default.
Oops, something went wrong.
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
87 changes: 71 additions & 16 deletions
87
libs/partners/pinecone/tests/unit_tests/test_embeddings.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 |
---|---|---|
@@ -1,28 +1,83 @@ | ||
from typing import Any, Type | ||
from unittest.mock import patch | ||
|
||
import aiohttp | ||
import pytest | ||
from langchain_core.utils import convert_to_secret_str | ||
from langchain_tests.unit_tests.embeddings import EmbeddingsTests | ||
|
||
from langchain_pinecone import PineconeEmbeddings | ||
|
||
API_KEY = convert_to_secret_str("NOT_A_VALID_KEY") | ||
MODEL_NAME = "multilingual-e5-large" | ||
|
||
|
||
def test_default_config() -> None: | ||
e = PineconeEmbeddings( | ||
pinecone_api_key=API_KEY, # type: ignore[call-arg] | ||
model=MODEL_NAME, | ||
) | ||
assert e.batch_size == 96 | ||
@pytest.fixture(autouse=True) | ||
def mock_pinecone() -> Any: | ||
"""Mock Pinecone client for all tests.""" | ||
with patch("langchain_pinecone.embeddings.PineconeClient") as mock: | ||
yield mock | ||
|
||
|
||
class TestPineconeEmbeddingsStandard(EmbeddingsTests): | ||
"""Standard LangChain embeddings tests.""" | ||
|
||
@property | ||
def embeddings_class(self) -> Type[PineconeEmbeddings]: | ||
"""Get the class under test.""" | ||
return PineconeEmbeddings | ||
|
||
@property | ||
def embedding_model_params(self) -> dict: | ||
"""Get the parameters for initializing the embeddings model.""" | ||
return { | ||
"model": MODEL_NAME, | ||
"pinecone_api_key": API_KEY, | ||
} | ||
|
||
|
||
class TestPineconeEmbeddingsConfig: | ||
"""Additional configuration tests for PineconeEmbeddings.""" | ||
|
||
def test_default_config(self) -> None: | ||
"""Test default configuration is set correctly.""" | ||
embeddings = PineconeEmbeddings(model=MODEL_NAME, pinecone_api_key=API_KEY) # type: ignore | ||
assert embeddings.batch_size == 96 | ||
assert embeddings.query_params == {"input_type": "query", "truncation": "END"} | ||
assert embeddings.document_params == { | ||
"input_type": "passage", | ||
"truncation": "END", | ||
} | ||
assert embeddings.dimension == 1024 | ||
|
||
def test_custom_config(self) -> None: | ||
"""Test custom configuration overrides defaults.""" | ||
embeddings = PineconeEmbeddings( | ||
model=MODEL_NAME, | ||
api_key=API_KEY, | ||
batch_size=128, | ||
query_params={"custom": "param"}, | ||
document_params={"other": "param"}, | ||
) | ||
assert embeddings.batch_size == 128 | ||
assert embeddings.query_params == {"custom": "param"} | ||
assert embeddings.document_params == {"other": "param"} | ||
|
||
def test_default_config_with_api_key() -> None: | ||
e = PineconeEmbeddings(api_key=API_KEY, model=MODEL_NAME) | ||
assert e.batch_size == 96 | ||
@pytest.mark.asyncio | ||
async def test_async_client_initialization(self) -> None: | ||
"""Test async client is initialized correctly and only when needed.""" | ||
embeddings = PineconeEmbeddings(model=MODEL_NAME, api_key=API_KEY) | ||
assert embeddings._async_client is None | ||
|
||
# Access async_client property | ||
client = embeddings.async_client | ||
assert client is not None | ||
assert isinstance(client, aiohttp.ClientSession) | ||
|
||
def test_custom_config() -> None: | ||
e = PineconeEmbeddings( | ||
pinecone_api_key=API_KEY, # type: ignore[call-arg] | ||
model=MODEL_NAME, | ||
batch_size=128, | ||
) | ||
assert e.batch_size == 128 | ||
# Ensure headers are set correctly | ||
expected_headers = { | ||
"Api-Key": API_KEY.get_secret_value(), | ||
"Content-Type": "application/json", | ||
"X-Pinecone-API-Version": "2024-10", | ||
} | ||
assert client._default_headers == expected_headers |
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