Spaces:
Running on Zero

File size: 4,435 Bytes
24f7855
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
from qdrant_client import QdrantClient
from qdrant_client.models import Distance, VectorParams, PointStruct
from langchain_core.documents import Document


class QdrantStore:
    """
    Qdrant Dense Vector Store:
    Supports in-memory (':memory:'), local file persistence, or remote Docker instances.
    Stores dense vectors alongside structured document payload metadata.
    """

    def __init__(
        self,
        collection_name: str = "rag_chunks",
        vector_size: int = 768,
        location: str = ":memory:",
    ):
        self.collection_name = collection_name
        self.vector_size = vector_size
        self.client = QdrantClient(location=location)
        self._ensure_collection()

    def _ensure_collection(self) -> None:
        """Creates the collection if it does not already exist."""
        if not self.client.collection_exists(self.collection_name):
            self.client.create_collection(
                collection_name=self.collection_name,
                vectors_config=VectorParams(
                    size=self.vector_size,
                    distance=Distance.COSINE,
                ),
            )

    def upsert_documents(
        self,
        documents: list[Document],
        vectors: list[list[float]],
    ) -> None:
        """
        Uploads documents paired with their dense embedding vectors into Qdrant.
        """
        if len(documents) != len(vectors):
            raise ValueError("Number of documents must match the number of vectors")

        points = []
        for idx, (doc, vector) in enumerate(zip(documents, vectors)):
            payload = {
                "page_content": doc.page_content,
                **doc.metadata,
            }
            points.append(
                PointStruct(
                    id=idx,
                    vector=vector,
                    payload=payload,
                )
            )

        self.client.upsert(
            collection_name=self.collection_name,
            points=points,
            wait=True,
        )

    def search(
        self,
        query_vector: list[float],
        top_k: int = 5,
    ) -> list[Document]:
        """
        Queries Qdrant for nearest neighbors and reconstructs Document objects with scores.
        """
        results = self.client.query_points(
            collection_name=self.collection_name,
            query=query_vector,
            limit=top_k,
        )

        documents = []
        for point in results.points:
            payload = point.payload or {}
            content = payload.get("page_content", "")
            metadata = {k: v for k, v in payload.items() if k != "page_content"}
            metadata["score"] = point.score

            documents.append(Document(page_content=content, metadata=metadata))

        return documents


if __name__ == "__main__":
    print("Testing QdrantStore in-memory...")
    store = QdrantStore(collection_name="test_collection", vector_size=4, location=":memory:")

    # 1. Prepare sample documents and normalized vectors
    docs = [
        Document(
            page_content="Apple reports strong iPhone sales in fiscal year 2025.",
            metadata={"chunk_id": "chunk_001", "source": "apple_10k.htm"},
        ),
        Document(
            page_content="Tesla announces new autonomous vehicle production timeline.",
            metadata={"chunk_id": "chunk_002", "source": "tesla_10k.htm"},
        ),
    ]
    vectors = [
        [1.0, 0.0, 0.0, 0.0],
        [0.0, 1.0, 0.0, 0.0],
    ]

    # 2. Upsert
    store.upsert_documents(docs, vectors)
    print("Upserted 2 test documents successfully.")

    # 3. Query (search vector strongly aligned with Document 0)
    query_vector = [0.99, 0.01, 0.0, 0.0]
    search_results = store.search(query_vector=query_vector, top_k=2)

    print(f"\nSearch returned {len(search_results)} documents:")
    print(f"Top result: {search_results[0].page_content}")
    print(f"Top result metadata: {search_results[0].metadata}")
    print(f"Similarity Score: {search_results[0].metadata['score']:.4f}")

    # 4. Assertions
    assert len(search_results) == 2, "Expected 2 search results"
    assert search_results[0].metadata["chunk_id"] == "chunk_001", "Top match should be chunk_001"
    assert search_results[0].metadata["score"] > 0.90, "Expected high cosine similarity score"

    print("\nAll QdrantStore assertions passed successfully!")