File size: 9,778 Bytes
777dbeb
556f1d5
 
 
 
 
 
777dbeb
556f1d5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
import streamlit as st
import json
from typing import List
from fastembed import LateInteractionTextEmbedding, TextEmbedding
from fastembed import SparseTextEmbedding, SparseEmbedding
from qdrant_client import QdrantClient, models
from tokenizers import Tokenizer

#############################
# 1. Utility / Helper Code
#############################

@st.cache_resource
def load_tokenizer():
    """
    Load the tokenizer for interpreting sparse embeddings (optional usage).
    """
    return Tokenizer.from_pretrained(SparseTextEmbedding.list_supported_models()[0]["sources"]["hf"])

@st.cache_resource
def load_models():
    """
    Load/initialize your models once and cache them.
    """
    # Dense embedding model
    dense_embedding_model = TextEmbedding("BAAI/bge-small-en-v1.5")

    # Late interaction model (ColBERTv2)
    late_embedding_model = LateInteractionTextEmbedding("colbert-ir/colbertv2.0")

    # Sparse embedding model
    sparse_model_name = "Qdrant/bm25"
    sparse_model = SparseTextEmbedding(model_name=sparse_model_name)

    return dense_embedding_model, late_embedding_model, sparse_model

def build_qdrant_index(data):
    """
    Given the parsed data (list of items), build an in-memory Qdrant index
    with dense, late, and sparse vectors.
    """
    # Extract fields
    items = data["items"]
    descriptions = [f"{item['name']} - {item['description']}" for item in items]
    names = [item["name"] for item in items]
    metadata = [
        {"name": item["name"]}  # You can store more fields if you like
        for item in items
    ]

    # Load models
    dense_embedding_model, late_embedding_model, sparse_model = load_models()

    # Generate embeddings
    dense_embeddings = list(dense_embedding_model.embed(descriptions))
    name_dense_embeddings = list(dense_embedding_model.embed(names))
    late_embeddings = list(late_embedding_model.embed(descriptions))
    sparse_embeddings: List[SparseEmbedding] = list(sparse_model.embed(descriptions, batch_size=6))

    # Create an in-memory Qdrant instance
    qdrant_client = QdrantClient(":memory:")

    # Create collection schema
    qdrant_client.create_collection(
        collection_name="items",
        vectors_config={
            "dense": models.VectorParams(
                size=len(dense_embeddings[0]),
                distance=models.Distance.COSINE,
            ),
            "late": models.VectorParams(
                size=len(late_embeddings[0][0]),
                distance=models.Distance.COSINE,
                multivector_config=models.MultiVectorConfig(
                    comparator=models.MultiVectorComparator.MAX_SIM
                ),
            ),
        },
        sparse_vectors_config={
            "sparse": models.SparseVectorParams(
                modifier=models.Modifier.IDF,
            ),
        }
    )

    # Upload points
    points = []
    for idx, _ in enumerate(metadata):
        points.append(
            models.PointStruct(
                id=idx,
                payload=metadata[idx],
                vector={
                    "late": late_embeddings[idx].tolist(),
                    "dense": dense_embeddings[idx],
                    "sparse": sparse_embeddings[idx].as_object(),
                },
            )
        )

    qdrant_client.upload_points(
        collection_name="items",
        points=points,
    )

    return qdrant_client

def run_queries(qdrant_client, query_text):
    """
    Run all the different query types and return results in a dictionary.
    """
    # Load models
    dense_embedding_model, late_embedding_model, sparse_model = load_models()

    # Generate single-query embeddings
    dense_query = next(dense_embedding_model.query_embed(query_text))
    late_query = next(late_embedding_model.query_embed(query_text))
    sparse_query = next(sparse_model.query_embed(query_text))

    # For the fusion approach, we need a list form for prefetch
    tsq = list(sparse_model.embed(query_text, batch_size=6))

    # We'll store top-5 results for each approach
    results = {}

    # 1) ColBERT (late)
    results["C"] = qdrant_client.query_points(
        collection_name="items",
        query=late_query,
        using="late",
        limit=5,
        with_payload=True
    )

    # 2) Sparse only
    results["S"] = qdrant_client.query_points(
        collection_name="items",
        query=models.SparseVector(**sparse_query.as_object()),
        using="sparse",
        limit=5,
        with_payload=True
    )

    # 3) Dense only
    results["D"] = qdrant_client.query_points(
        collection_name="items",
        query=dense_query,
        using="dense",
        limit=5,
        with_payload=True
    )

    # 4) Hybrid fusion (RRF for Sparse+Dense)
    results["S+D-F"] = qdrant_client.query_points(
        collection_name="items",
        prefetch=[
            models.Prefetch(
                query=dense_query,
                using="dense",
                limit=100,
            ),
            models.Prefetch(
                query=tsq[0].as_object(),
                using="sparse",
                limit=50,
            )
        ],
        query=models.FusionQuery(fusion=models.Fusion.RRF),
        limit=5,
        with_payload=True
    )

    # 5) Hybrid fusion + ColBERT
    sparse_dense_prefetch = models.Prefetch(
        prefetch=[
            models.Prefetch(query=dense_query, using="dense", limit=100),
            models.Prefetch(query=tsq[0].as_object(), using="sparse", limit=50),
        ],
        limit=10,
        query=models.FusionQuery(fusion=models.Fusion.RRF),
    )
    results["S+D-F-C"] = qdrant_client.query_points(
        collection_name="items",
        prefetch=[sparse_dense_prefetch],
        query=late_query,
        using="late",
        limit=5,
        with_payload=True
    )

    # 6) Hybrid no-fusion + ColBERT
    old_prefetch = models.Prefetch(
        prefetch=[
            models.Prefetch(
                prefetch=[
                    models.Prefetch(query=dense_query, using="dense", limit=100)
                ],
                query=tsq[0].as_object(),
                using="sparse",
                limit=50,
            )
        ]
    )
    results["S+D-C"] = qdrant_client.query_points(
        collection_name="items",
        prefetch=[old_prefetch],
        query=late_query,
        using="late",
        limit=5,
        with_payload=True
    )

    return results

#############################
# 2. Streamlit Main App
#############################

def main():
    st.title("Semantic Search Sandbox")

    # Initialize session state if not present
    if "json_loaded" not in st.session_state:
        st.session_state["json_loaded"] = False
    if "qdrant_client" not in st.session_state:
        st.session_state["qdrant_client"] = None

    #######################################
    # Show JSON input only if not loaded
    #######################################
    if not st.session_state["json_loaded"]:
        st.subheader("Paste items.json Here")
        default_json = """
{
  "items": [
    {
      "name": "Example1",
      "description": "An example item"
    },
    {
      "name": "Example2",
      "description": "Another item for demonstration"
    }
  ]
}
""".strip()

        json_text = st.text_area("JSON Input", value=default_json, height=300)

        if st.button("Load JSON"):
            try:
                data = json.loads(json_text)
                # Build Qdrant index in memory
                st.session_state["qdrant_client"] = build_qdrant_index(data)
                st.session_state["json_loaded"] = True
                st.success("JSON loaded and Qdrant index built successfully!")
                st.rerun()
            except Exception as e:
                st.error(f"Error parsing JSON: {e}")

    else:
        # The data is loaded, show a button to reset if you want to load new JSON
        if st.button("Load a different JSON"):
            st.session_state["json_loaded"] = False
            st.session_state["qdrant_client"] = None
            #st.experimental_rerun()  # Refresh the page
        else:
            # Show the search interface
            query_text = st.text_input("Search Query", value="ACB 1.0 Ports")
            if st.button("Search"):
                if st.session_state["qdrant_client"] is None:
                    st.warning("Please load valid JSON first.")
                    return

                # Run queries
                results_dict = run_queries(st.session_state["qdrant_client"], query_text)

                # Display results in columns
                col_names = list(results_dict.keys())
                # You can split into multiple rows if there are more than 3
                n_cols = 3
                # We'll create enough columns to handle all search types
                rows_needed = (len(col_names) + n_cols - 1) // n_cols

                for row_idx in range(rows_needed):
                    cols = st.columns(n_cols)
                    for col_idx in range(n_cols):
                        method_idx = row_idx * n_cols + col_idx
                        if method_idx < len(col_names):
                            method = col_names[method_idx]
                            qdrant_result = results_dict[method]

                            with cols[col_idx]:
                                st.markdown(f"### {method}")
                                for point in qdrant_result.points:
                                    name = point.payload.get("name", "Unnamed")
                                    score = round(point.score, 4) if point.score else "N/A"
                                    st.write(f"- **{name}** (score={score})")

if __name__ == "__main__":
    main()