test_mmr.py — langchain Source File
Architecture documentation for test_mmr.py, a python file in the langchain codebase. 7 imports, 0 dependents.
Entity Profile
Dependency Diagram
graph LR b7cc5382_afa1_ea73_55d1_76959ef81011["test_mmr.py"] 120e2591_3e15_b895_72b6_cb26195e40a6["pytest"] b7cc5382_afa1_ea73_55d1_76959ef81011 --> 120e2591_3e15_b895_72b6_cb26195e40a6 c554676d_b731_47b2_a98f_c1c2d537c0aa["langchain_core.documents"] b7cc5382_afa1_ea73_55d1_76959ef81011 --> c554676d_b731_47b2_a98f_c1c2d537c0aa e9f800f2_8227_1095_42ef_324e02810451["qdrant_client"] b7cc5382_afa1_ea73_55d1_76959ef81011 --> e9f800f2_8227_1095_42ef_324e02810451 77801658_6b3a_bc26_7e54_388e5c04807d["langchain_qdrant"] b7cc5382_afa1_ea73_55d1_76959ef81011 --> 77801658_6b3a_bc26_7e54_388e5c04807d 69f1b364_4f43_76af_9887_ecab38a966d8["langchain_qdrant.qdrant"] b7cc5382_afa1_ea73_55d1_76959ef81011 --> 69f1b364_4f43_76af_9887_ecab38a966d8 a50ef027_f19e_86a8_94ef_895b4566f94e["tests.integration_tests.common"] b7cc5382_afa1_ea73_55d1_76959ef81011 --> a50ef027_f19e_86a8_94ef_895b4566f94e 513f2bf4_0acd_14e4_0a43_45f7716ce101["tests.integration_tests.fixtures"] b7cc5382_afa1_ea73_55d1_76959ef81011 --> 513f2bf4_0acd_14e4_0a43_45f7716ce101 style b7cc5382_afa1_ea73_55d1_76959ef81011 fill:#6366f1,stroke:#818cf8,color:#fff
Relationship Graph
Source Code
import pytest # type: ignore[import-not-found]
from langchain_core.documents import Document
from qdrant_client import models
from langchain_qdrant import QdrantVectorStore, RetrievalMode
from langchain_qdrant.qdrant import QdrantVectorStoreError
from tests.integration_tests.common import (
ConsistentFakeEmbeddings,
ConsistentFakeSparseEmbeddings,
assert_documents_equals,
)
from tests.integration_tests.fixtures import qdrant_locations, retrieval_modes
# MMR is supported when dense embeddings are available
# i.e. In Dense and Hybrid retrieval modes
@pytest.mark.parametrize("location", qdrant_locations())
@pytest.mark.parametrize(
"content_payload_key", [QdrantVectorStore.CONTENT_KEY, "test_content"]
)
@pytest.mark.parametrize(
"metadata_payload_key", [QdrantVectorStore.METADATA_KEY, "test_metadata"]
)
@pytest.mark.parametrize("retrieval_mode", retrieval_modes(sparse=False))
@pytest.mark.parametrize("vector_name", ["", "my-vector"])
def test_qdrant_mmr_search(
location: str,
content_payload_key: str,
metadata_payload_key: str,
retrieval_mode: RetrievalMode,
vector_name: str,
) -> None:
"""Test end to end construction and MRR search."""
filter_ = models.Filter(
must=[
models.FieldCondition(
key=f"{metadata_payload_key}.page",
match=models.MatchValue(
value=2,
),
),
],
)
texts = ["foo", "bar", "baz"]
metadatas = [{"page": i} for i in range(len(texts))]
docsearch = QdrantVectorStore.from_texts(
texts,
ConsistentFakeEmbeddings(),
metadatas=metadatas,
content_payload_key=content_payload_key,
metadata_payload_key=metadata_payload_key,
location=location,
retrieval_mode=retrieval_mode,
vector_name=vector_name,
distance=models.Distance.EUCLID,
sparse_embedding=ConsistentFakeSparseEmbeddings(),
)
output = docsearch.max_marginal_relevance_search(
"foo", k=2, fetch_k=3, lambda_mult=0.0
)
assert_documents_equals(
output,
[
Document(page_content="foo", metadata={"page": 0}),
Document(page_content="bar", metadata={"page": 1}),
],
)
output = docsearch.max_marginal_relevance_search(
"foo", k=2, fetch_k=3, lambda_mult=0.0, filter=filter_
)
assert_documents_equals(
output,
[Document(page_content="baz", metadata={"page": 2})],
)
# MMR shouldn't work with only sparse retrieval mode
@pytest.mark.parametrize("location", qdrant_locations())
@pytest.mark.parametrize(
"content_payload_key", [QdrantVectorStore.CONTENT_KEY, "test_content"]
)
@pytest.mark.parametrize(
"metadata_payload_key", [QdrantVectorStore.METADATA_KEY, "test_metadata"]
)
@pytest.mark.parametrize("retrieval_mode", retrieval_modes(dense=False, hybrid=False))
@pytest.mark.parametrize("vector_name", ["", "my-vector"])
def test_invalid_qdrant_mmr_with_sparse(
location: str,
content_payload_key: str,
metadata_payload_key: str,
retrieval_mode: RetrievalMode,
vector_name: str,
) -> None:
"""Test end to end construction and MRR search."""
texts = ["foo", "bar", "baz"]
metadatas = [{"page": i} for i in range(len(texts))]
docsearch = QdrantVectorStore.from_texts(
texts,
ConsistentFakeEmbeddings(),
metadatas=metadatas,
content_payload_key=content_payload_key,
metadata_payload_key=metadata_payload_key,
location=location,
retrieval_mode=retrieval_mode,
vector_name=vector_name,
distance=models.Distance.EUCLID,
sparse_embedding=ConsistentFakeSparseEmbeddings(),
)
with pytest.raises(QdrantVectorStoreError) as excinfo:
docsearch.max_marginal_relevance_search("foo", k=2, fetch_k=3, lambda_mult=0.0)
expected_message = "does not contain dense vector named"
assert expected_message in str(excinfo.value)
Domain
Subdomains
Dependencies
- langchain_core.documents
- langchain_qdrant
- langchain_qdrant.qdrant
- pytest
- qdrant_client
- tests.integration_tests.common
- tests.integration_tests.fixtures
Source
Frequently Asked Questions
What does test_mmr.py do?
test_mmr.py is a source file in the langchain codebase, written in python. It belongs to the CoreAbstractions domain, RunnableInterface subdomain.
What functions are defined in test_mmr.py?
test_mmr.py defines 2 function(s): test_invalid_qdrant_mmr_with_sparse, test_qdrant_mmr_search.
What does test_mmr.py depend on?
test_mmr.py imports 7 module(s): langchain_core.documents, langchain_qdrant, langchain_qdrant.qdrant, pytest, qdrant_client, tests.integration_tests.common, tests.integration_tests.fixtures.
Where is test_mmr.py in the architecture?
test_mmr.py is located at libs/partners/qdrant/tests/integration_tests/qdrant_vector_store/test_mmr.py (domain: CoreAbstractions, subdomain: RunnableInterface, directory: libs/partners/qdrant/tests/integration_tests/qdrant_vector_store).
Analyze Your Own Codebase
Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.
Try Supermodel Free