@ -1,4 +1,5 @@
import tempfile
import uuid
from typing import Optional
import pytest
@ -9,13 +10,14 @@ from langchain.vectorstores.qdrant import QdrantException
from tests . integration_tests . vectorstores . fake_embeddings import (
ConsistentFakeEmbeddings ,
)
from tests . integration_tests . vectorstores . qdrant . common import qdrant_is_not_running
def test_qdrant_from_texts_stores_duplicated_texts ( ) - > None :
""" Test end to end Qdrant.from_texts stores duplicated texts separately. """
from qdrant_client import QdrantClient
collection_name = " test "
collection_name = uuid . uuid4 ( ) . hex
with tempfile . TemporaryDirectory ( ) as tmpdir :
vec_store = Qdrant . from_texts (
@ -38,7 +40,7 @@ def test_qdrant_from_texts_stores_ids(
""" Test end to end Qdrant.from_texts stores provided ids. """
from qdrant_client import QdrantClient
collection_name = " test "
collection_name = uuid . uuid4 ( ) . hex
with tempfile . TemporaryDirectory ( ) as tmpdir :
ids = [
" fa38d572-4c31-4579-aedc-1960d79df6df " ,
@ -66,7 +68,7 @@ def test_qdrant_from_texts_stores_embeddings_as_named_vectors(vector_name: str)
""" Test end to end Qdrant.from_texts stores named vectors if name is provided. """
from qdrant_client import QdrantClient
collection_name = " test "
collection_name = uuid . uuid4 ( ) . hex
with tempfile . TemporaryDirectory ( ) as tmpdir :
vec_store = Qdrant . from_texts (
[ " lorem " , " ipsum " , " dolor " , " sit " , " amet " ] ,
@ -90,7 +92,7 @@ def test_qdrant_from_texts_reuses_same_collection(vector_name: Optional[str]) ->
""" Test if Qdrant.from_texts reuses the same collection """
from qdrant_client import QdrantClient
collection_name = " test "
collection_name = uuid . uuid4 ( ) . hex
embeddings = ConsistentFakeEmbeddings ( )
with tempfile . TemporaryDirectory ( ) as tmpdir :
vec_store = Qdrant . from_texts (
@ -120,7 +122,7 @@ def test_qdrant_from_texts_raises_error_on_different_dimensionality(
vector_name : Optional [ str ] ,
) - > None :
""" Test if Qdrant.from_texts raises an exception if dimensionality does not match """
collection_name = " test "
collection_name = uuid . uuid4 ( ) . hex
with tempfile . TemporaryDirectory ( ) as tmpdir :
vec_store = Qdrant . from_texts (
[ " lorem " , " ipsum " , " dolor " , " sit " , " amet " ] ,
@ -154,7 +156,7 @@ def test_qdrant_from_texts_raises_error_on_different_vector_name(
second_vector_name : Optional [ str ] ,
) - > None :
""" Test if Qdrant.from_texts raises an exception if vector name does not match """
collection_name = " test "
collection_name = uuid . uuid4 ( ) . hex
with tempfile . TemporaryDirectory ( ) as tmpdir :
vec_store = Qdrant . from_texts (
[ " lorem " , " ipsum " , " dolor " , " sit " , " amet " ] ,
@ -177,26 +179,32 @@ def test_qdrant_from_texts_raises_error_on_different_vector_name(
def test_qdrant_from_texts_raises_error_on_different_distance ( ) - > None :
""" Test if Qdrant.from_texts raises an exception if distance does not match """
collection_name = " test "
collection_name = uuid . uuid4 ( ) . hex
with tempfile . TemporaryDirectory ( ) as tmpdir :
vec_store = Qdrant . from_texts (
[ " lorem " , " ipsum " , " dolor " , " sit " , " amet " ] ,
ConsistentFakeEmbeddings ( dimensionality = 10 ) ,
ConsistentFakeEmbeddings ( ) ,
collection_name = collection_name ,
path = str ( tmpdir ) ,
distance_func = " Cosine " ,
)
del vec_store
with pytest . raises ( QdrantException ) :
with pytest . raises ( QdrantException ) as excinfo :
Qdrant . from_texts (
[ " foo " , " bar " ] ,
ConsistentFakeEmbeddings ( dimensionality = 5 ) ,
ConsistentFakeEmbeddings ( ) ,
collection_name = collection_name ,
path = str ( tmpdir ) ,
distance_func = " Euclid " ,
)
expected_message = (
" configured for COSINE similarity, but requested EUCLID. Please set "
" `distance_func` parameter to `COSINE` "
)
assert expected_message in str ( excinfo . value )
@pytest.mark.parametrize ( " vector_name " , [ None , " custom-vector " ] )
def test_qdrant_from_texts_recreates_collection_on_force_recreate (
@ -205,7 +213,7 @@ def test_qdrant_from_texts_recreates_collection_on_force_recreate(
""" Test if Qdrant.from_texts recreates the collection even if config mismatches """
from qdrant_client import QdrantClient
collection_name = " test "
collection_name = uuid . uuid4 ( ) . hex
with tempfile . TemporaryDirectory ( ) as tmpdir :
vec_store = Qdrant . from_texts (
[ " lorem " , " ipsum " , " dolor " , " sit " , " amet " ] ,
@ -250,3 +258,27 @@ def test_qdrant_from_texts_stores_metadatas(
)
output = docsearch . similarity_search ( " foo " , k = 1 )
assert output == [ Document ( page_content = " foo " , metadata = { " page " : 0 } ) ]
@pytest.mark.skipif ( qdrant_is_not_running ( ) , reason = " Qdrant is not running " )
def test_from_texts_passed_optimizers_config_and_on_disk_payload ( ) - > None :
from qdrant_client import models
collection_name = uuid . uuid4 ( ) . hex
texts = [ " foo " , " bar " , " baz " ]
metadatas = [ { " page " : i } for i in range ( len ( texts ) ) ]
optimizers_config = models . OptimizersConfigDiff ( memmap_threshold = 1000 )
vec_store = Qdrant . from_texts (
texts ,
ConsistentFakeEmbeddings ( ) ,
metadatas = metadatas ,
optimizers_config = optimizers_config ,
on_disk_payload = True ,
on_disk = True ,
collection_name = collection_name ,
)
collection_info = vec_store . client . get_collection ( collection_name )
assert collection_info . config . params . vectors . on_disk is True # type: ignore
assert collection_info . config . optimizer_config . memmap_threshold == 1000
assert collection_info . config . params . on_disk_payload is True