-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Expand file tree
/
Copy pathsemantic-search.py
More file actions
552 lines (419 loc) · 15.4 KB
/
Copy pathsemantic-search.py
File metadata and controls
552 lines (419 loc) · 15.4 KB
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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
# /// script
# requires-python = ">=3.10"
# dependencies = [
# "datasets==3.5.1",
# "marimo>=0.23.6",
# "pinecone==9.0.1",
# ]
# ///
import marimo
__generated_with = "0.23.6"
app = marimo.App()
@app.cell
def _():
import marimo as mo
return (mo,)
@app.cell(hide_code=True)
def _(mo):
mo.md(r"""
# Semantic Search
This notebook demonstrates semantic search using Pinecone and a multilingual translation dataset.
We'll work with a corpus of English sentences and retrieve results by meaning — not by keyword match.
Semantic search finds documents similar in meaning to a query, regardless of the exact words used.
It works well for use cases where intent matters more than vocabulary, such as question answering
over a document corpus or multilingual search.
""")
return
@app.cell
def _():
import os
from datasets import load_dataset
from pinecone import Pinecone
return Pinecone, load_dataset, os
@app.cell(hide_code=True)
def _(mo):
mo.md(r"""
## Setting up
### Pinecone API Key
You'll need a free Pinecone API key to run this notebook. Get one at
[app.pinecone.io](https://app.pinecone.io).
**Running locally?** Set `PINECONE_API_KEY` in your environment or in a `.env`
file — marimo reads `.env` files automatically on startup. The cell below will
detect the key and confirm it's loaded.
**Running in molab?** Enter your key directly in the input field below.
""")
return
@app.cell(hide_code=True)
def _(mo, os):
env_key = os.environ.get("PINECONE_API_KEY", "")
api_key_input = mo.ui.text(
kind="password",
placeholder="pcsk_...",
label="Pinecone API Key",
value=env_key,
full_width=True,
)
(
mo.callout(mo.md("API key loaded from environment."), kind="success")
if env_key
else mo.vstack(
[
mo.callout(
mo.md(
"Enter your Pinecone API key. Get a free key at [app.pinecone.io](https://app.pinecone.io)."
),
kind="info",
),
api_key_input,
]
)
)
return (api_key_input,)
@app.cell(hide_code=True)
def _(api_key_input, mo):
api_key = api_key_input.value
mo.stop(
not api_key,
mo.callout(
mo.md("**API key required.** Enter your key above to continue."),
kind="danger",
),
)
return (api_key,)
@app.cell(hide_code=True)
def _(mo):
mo.md(r"""
### Instantiating the Client
With the API key in hand, we can create a `Pinecone` client. This is the entry point for all
control-plane operations — creating and managing indexes, listing namespaces, and so on.
The `source_tag` parameter is used internally by Pinecone to attribute API usage from example
notebooks. You would not include this in your own applications.
""")
return
@app.cell(hide_code=True)
def _(Pinecone, api_key):
pc = Pinecone(
api_key=api_key,
source_tag="pinecone_examples:docs:semantic_search",
)
return (pc,)
@app.cell(hide_code=True)
def _(mo):
mo.md(r"""
### Creating an Index
Semantic search typically requires three components: records to search over, an embedding model
to encode meaning, and a vector database to store and query embeddings.
With Pinecone's Integrated Inference, the index is paired with a hosted embedding model.
Pinecone handles embedding automatically when you upsert and query records — no separate
embedding step required.
We'll use the [multilingual-e5-large](https://docs.pinecone.io/models/multilingual-e5-large)
model, which encodes text from many languages into the same vector space. This means a query
in English can return results in Spanish (and vice versa) without any translation step.
""")
return
@app.cell
def _(pc):
index_name = "semantic-search"
if pc.indexes.exists(name=index_name):
pc.indexes.delete(name=index_name)
pc.create_index_for_model(
name=index_name,
cloud="aws",
region="us-east-1",
embed={
"model": "multilingual-e5-large",
"field_map": {"text": "chunk_text"},
},
)
index = pc.index(name=index_name)
index.describe_index_stats()
return index, index_name
@app.cell(hide_code=True)
def _(mo):
mo.md(r"""
## Creating the Dataset
We're using a subset of [Tatoeba](https://tatoeba.org/), a multilingual sentence translation
dataset with hundreds of thousands of pairs. Here are a few records:
""")
return
@app.cell
def _(load_dataset):
tatoeba = load_dataset(
"Helsinki-NLP/tatoeba",
lang1="en",
lang2="es",
trust_remote_code=True,
split="train",
)
return (tatoeba,)
@app.cell
def _(tatoeba):
tatoeba[0:3]
return
@app.cell(hide_code=True)
def _(mo):
mo.md(r"""
Before embedding, we filter the dataset to a focused subset. We filter on the English side
of each translation pair, then extract sentences for both languages separately — so the
Spanish sentences are the actual translations of the matched English sentences.
""")
return
@app.cell
def _():
def simple_keyword_filter(sentence, keywords):
for keyword in keywords:
if keyword in sentence:
return True
return False
def filter_pairs(dataset, keywords):
"""Filter translation pairs where the English sentence contains any keyword."""
return dataset.filter(
lambda x: simple_keyword_filter(x["translation"]["en"], keywords)
).flatten()
def extract_sentences(pairs, lang):
"""Extract and deduplicate sentences for one language from filtered pairs."""
other = "es" if lang == "en" else "en"
dataset = pairs.remove_columns(f"translation.{other}")
dataset = dataset.rename_column(f"translation.{lang}", "sentence")
seen = set()
def is_unique(example):
if example["sentence"] in seen:
return False
seen.add(example["sentence"])
return True
dataset = dataset.filter(is_unique)
return dataset.add_column("lang", [lang] * len(dataset))
return extract_sentences, filter_pairs
@app.cell(hide_code=True)
def _(mo):
mo.md(r"""
The English sentences contain mostly what we expect, but also some where "park" appears as a
substring — for example, "A glass of sparkling water, please." This isn't a problem: the
embedding model encodes meaning, so these sentences will land in a different region of the
vector space and won't surface as relevant results.
""")
return
@app.cell
def _(extract_sentences, filter_pairs, mo, tatoeba):
keywords = ["park"]
filtered_pairs = filter_pairs(tatoeba, keywords=keywords)
english = extract_sentences(filtered_pairs, lang="en")
spanish = extract_sentences(filtered_pairs, lang="es")
mo.vstack(
[
mo.md(f"**English** — {len(english)} sentences"),
mo.ui.table(english, page_size=5),
mo.md(f"**Spanish** — {len(spanish)} sentences"),
mo.ui.table(spanish, page_size=5),
]
)
return english, spanish
@app.cell(hide_code=True)
def _(mo):
mo.md(r"""
`to_records` converts the prepared sentences into the format Pinecone's `upsert_records`
expects: a list of dicts, each with a unique `id`, a `chunk_text` field, and any additional
metadata — here, `lang`.
The `chunk_text` field name comes from the `field_map` we set when creating the index.
Pinecone uses that mapping to know which field to embed automatically on upsert.
We prefix IDs with the language code (`en-`, `es-`) to avoid collisions when combining
records from multiple languages.
""")
return
@app.function
def to_records(sentences, column, id_prefix=""):
return [
{
"id": f"{id_prefix}{idx}",
"chunk_text": sentence[column],
"lang": sentence["lang"],
}
for idx, sentence in enumerate(sentences)
]
@app.cell
def _(english, mo, spanish):
records = to_records(english, column="sentence", id_prefix="en-") + to_records(
spanish, column="sentence", id_prefix="es-"
)
mo.ui.table(records, page_size=10)
return (records,)
@app.cell(hide_code=True)
def _(mo):
mo.md(r"""
## Upserting Records
Each record flows through the embedding model specified at index creation time, producing a vector.
We store these vectors in Pinecone alongside any metadata fields.
We use a namespace (`english-sentences`) to group records.
[Namespaces](https://docs.pinecone.io/guides/index-data/indexing-overview#namespaces) partition
an index and scope queries to a subset of records.
Records also include a `lang` metadata field.
[Metadata filtering](https://docs.pinecone.io/guides/index-data/indexing-overview#metadata)
lets you narrow results by field value — useful if you later add sentences in other languages
to the same index.
""")
return
@app.cell
def _(index, mo, records):
batch_size = 96
namespace = "sentences"
# Batching avoids hitting the embedding model's rate limit
for start in mo.status.progress_bar(
range(0, len(records), batch_size),
title="Upserting records",
show_rate=True,
show_eta=True,
):
index.upsert_records(
records=records[start : start + batch_size], namespace=namespace
)
return (namespace,)
@app.cell(hide_code=True)
def _(mo):
mo.md(r"""
## Querying
With Integrated Inference, querying works the same way as upserting: pass text directly and
Pinecone embeds it using the same model. The query vector is compared against all stored vectors
and the closest matches are returned.
Because both English and Spanish sentences share the same vector space, an English query can
surface Spanish results — and vice versa. The `lang` column in the results shows where each
match came from.
We'll run the same query in English and Spanish to show they retrieve semantically similar results.
""")
return
@app.cell
def _(index, mo, namespace):
def print_results(query, results):
data = [
{
"lang": hit.fields.get("lang", ""),
"sentence": hit.fields["chunk_text"],
"score": round(hit.score, 4),
}
for hit in results.result.hits
]
return mo.vstack(
[
mo.md(f"**Query:** {query}"),
mo.ui.table(data, show_column_summaries=False),
]
)
def search(query, top_k=10, lang=None):
results = index.search(
namespace=namespace,
top_k=top_k,
inputs={"text": query},
filter={"lang": {"$eq": lang}} if lang else None,
)
return print_results(query, results)
return (search,)
@app.cell
def _(search):
search("I want to go to the park and relax")
return
@app.cell
def _(search):
search("Quiero ir al parque a relajarme")
return
@app.cell(hide_code=True)
def _(mo):
mo.md(r"""
### Meaning Over Keywords
The next query contains no form of the word "park" — yet it will still retrieve sentences
about parking a car. This is the key distinction between semantic search and keyword search:
results are **ranked by meaning**, not by word overlap.
""")
return
@app.cell
def _(search):
search("where can I leave my car downtown")
return
@app.cell(hide_code=True)
def _(mo):
mo.md(r"""
## How It Works
When you call `index.search` with a text string, Pinecone first embeds it using the same model
configured at index creation — in this case, `multilingual-e5-large`. This produces a query vector
in the same embedding space as the stored sentence vectors.
Pinecone then compares the query vector against all stored vectors using cosine similarity: the
cosine of the angle between two vectors. A score of 1.0 means the vectors point in the same
direction (identical meaning); a score near 0 means they are unrelated. The `top_k` results
with the highest scores are returned.
Because `multilingual-e5-large` encodes text from many languages into the same vector space,
a query in English can retrieve Spanish results — and vice versa — without any translation step.
Proximity in vector space reflects proximity in meaning, regardless of which language the text
is in.
**Model selection determines what the vector space looks like.** A model trained only on English
text would not place Spanish and English sentences near each other. A model trained on code would
cluster programs by functionality rather than natural language meaning. Choosing the right model
for your data and use case is the most consequential decision in a semantic search system —
Pinecone's [model catalog](https://docs.pinecone.io/models/overview) lists available options
with guidance on when to use each.
At scale, comparing a query vector against every stored vector would be slow. Pinecone uses
approximate nearest neighbor (ANN) algorithms to find the closest matches in sub-linear time,
maintaining low latency even across billions of vectors.
""")
return
@app.cell(hide_code=True)
def _(mo):
mo.md(r"""
## Filtering by Language
Querying in one language to find semantically similar results in another is a basic form of
translation — without any explicit translation step. But sometimes you want to search within
a single language instead.
The `lang` metadata field on each record lets you scope results using Pinecone's
[metadata filtering](https://docs.pinecone.io/guides/search/filter-by-metadata).
The embedding model still encodes the query the same way — the filter simply restricts which
records are eligible to be returned.
""")
return
@app.cell
def _(search):
search("I am meeting a friend at the park", lang="en")
return
@app.cell
def _(search):
search("Quiero reunirme con un amigo en el parque", lang="es")
return
@app.cell(hide_code=True)
def _(mo):
mo.md(r"""
## Try It Yourself
Enter a query and select a language filter. Results update when you finish typing.
""")
return
@app.cell(hide_code=True)
def _(mo):
query_input = mo.ui.text(
placeholder="Enter a search query...",
value="The park is crowded today",
full_width=True,
)
lang_select = mo.ui.radio(
options={"All languages": None, "English only": "en", "Spanish only": "es"},
value="All languages",
)
mo.vstack([query_input, lang_select])
return lang_select, query_input
@app.cell(hide_code=True)
def _(lang_select, query_input, search):
search(query_input.value, lang=lang_select.value)
return
@app.cell(hide_code=True)
def _(mo):
mo.md(r"""
## Cleanup
Delete the index when you're done to free up resources.
""")
return
@app.cell
def _(mo):
delete_button = mo.ui.run_button(label="Delete index")
delete_button
return (delete_button,)
@app.cell
def _(delete_button, index_name, mo, pc):
mo.stop(not delete_button.value)
pc.indexes.delete(name=index_name)
return
if __name__ == "__main__":
app.run()