La recherche à l’ère de l’IA

A presentation at SunnyTech in July 2024 in Montpellier, France by David Pilato

Slide 1

Slide 1

Search & AI a new era David Pilato | @dadoonet

Slide 2

Slide 2

Agenda ● ● ● ● “Classic” search and its limitations ML model and usage Vector search or hybrid search in Elasticsearch OpenAI’s ChatGPT or LLMs with Elasticsearch

Slide 3

Slide 3

Elasticsearch You Know, for Search

Slide 4

Slide 4

Slide 5

Slide 5

These are not the droids you are looking for.

Slide 6

Slide 6

GET /_analyze { “char_filter”: [ “html_strip” ], “tokenizer”: “standard”, “filter”: [ “lowercase”, “stop”, “snowball” ], “text”: “These are <em>not</em> the droids you are looking for.” }

Slide 7

Slide 7

These are <em>not</em> the droids you are looking for. { “tokens”: [{ “token”: “droid”, “start_offset”: 27, “end_offset”: 33, “type”: “<ALPHANUM>”, “position”: 4 },{ “token”: “you”, “start_offset”: 34, “end_offset”: 37, “type”: “<ALPHANUM>”, “position”: 5 }, { “token”: “look”, “start_offset”: 42, “end_offset”: 49, “type”: “<ALPHANUM>”, “position”: 7 }]}

Slide 8

Slide 8

Semantic search ≠ Literal matches

Slide 9

Slide 9

TODAY X-wing starfighter squadron TOMORROW What ships and crews do I need to destroy an almost finished death star? Or is there a secret weakness?

Slide 10

Slide 10

Elasticsearch You Know, for Vector Search

Slide 11

Slide 11

What is a Vector ?

Slide 12

Slide 12

Embeddings represent your data Example: 1-dimensional vector Character Vector [ 1  Gentil Méchant 1

Slide 13

Slide 13

Multiple dimensions represent different data aspects Human Character Vector [ 1, 1  Gentil Méchant Machine  1, 0 

Slide 14

Slide 14

Similar data is grouped together Human Character Vector [ 1.0, 1.0   1.0, 0.0  Gentil Méchant [ 1.0, 0.8   1.0, 1.0  [ 1.0, 1.0  Machine

Slide 15

Slide 15

Vector search ranks objects by similarity (~relevance) to the query Human Rank Query 1 Gentil Méchant 2 3 4 5 Machine Result

Slide 16

Slide 16

Choice of Embedding Model Start with Off-the Shelf Models Extend to Higher Relevance ●Text data: Hugging Face (like Microsoft’s E5 ●Apply hybrid scoring ●Images: OpenAI’s CLIP ●Bring Your Own Model: requires expertise + labeled data

Slide 17

Slide 17

Problem training vs actual use-case

Slide 18

Slide 18

Architecture of Vector Search

Slide 19

Slide 19

How do you index vectors ?

Slide 20

Slide 20

Data Ingestion and Embedding Generation POST /_doc { } Source data “_id”:”product-1234”, “product_name”:”Summer Dress”, “description”:”Our best-selling…”, “Price”: 118, “color”:”blue”, “fabric”:”cotton”

Slide 21

Slide 21

Data Ingestion and Embedding Generation { } Source data POST /_doc “_id”:”product-1234”, “product_name”:”Summer Dress”, “description”:”Our best-selling…”, “Price”: 118, “color”:”blue”, “fabric”:”cotton”, “desc_embedding”:[0.452,0.3242,…], “img_embedding”:[0.012,0.0,…]

Slide 22

Slide 22

Co m m er ci With Elastic ML al { } Source data POST /_doc “_id”:”product-1234”, “product_name”:”Summer Dress”, “description”:”Our best-selling…”, “Price”: 118, “color”:”blue”, “fabric”:”cotton”, “desc_embedding”:[0.452,0.3242,…]

Slide 23

Slide 23

Elastic’s range of supported NLP models Co m m er ci ● Fill mask model Mask some of the words in a sentence and predict words that replace masks ● Named entity recognition model NLP method that extracts information from text ● Text embedding model Represent individual words as numerical vectors in a predefined vector space ● Text classification model Assign a set of predefined categories to open-ended text ● Question answering model Model that can answer questions given some or no context ● Zero-shot text classification model Model trained on a set of labeled examples, that is able to classify previously unseen examples Full list at: ela.st/nlp-supported-models al

Slide 24

Slide 24

How do you search vectors ?

Slide 25

Slide 25

Vector Query GET product-catalog/_search { “query” : { “bool”: { “must”: [{ “knn”: { “field”: “desc_embbeding”, “num_candidates”: 50, “query_vector”: [0.123, 0.244,…] } }], “filter”: { “term”: { “department”: “women” } } } } }, “size”: 10

Slide 26

Slide 26

Vector Query Transformer model Co m m er ci al GET product-catalog/_search { “query” : { “bool”: { “must”: [{ “knn”: { “field”: “desc_embbeding”, “num_candidates”: 50, “query_vector_builder”: { “text_embedding”: { “model_text”: “summer clothes”, “model_id”: <text-embedding-model> } } } }], “filter”: { “term”: { “department”: “women” } } } }, “size”: 10 }

Slide 27

Slide 27

But how does it really work?

Slide 28

Slide 28

Similarity: cosine (cosine) Human q cos(θ) = d1 d2 Realistic θ q⃗ × d ⃗ | q⃗ | × | d |⃗ _score = 1 + cos(θ) 2

Slide 29

Slide 29

Similarity: cosine (cosine) 1+1 _score = =1 2 1+0 _score = = 0.5 2 1−1 _score = =0 2

Slide 30

Slide 30

Similarity: Dot Product (dot_product) q d q⃗ × d ⃗ = | q⃗ | × cos(θ) × | d |⃗ θ | q⃗ | × co s (θ ) 1 + dot_ product(q, d) scorefloat = 2 0.5 + dot product(q, d) _scorebyte = 32768 × dims

Slide 31

Slide 31

Similarity: Euclidean distance (l2_norm) y 2 n i (x ∑ 1 i= − y i) q l2_normq,d = y1 d x1 y2 x2 n ∑ i=1 (xi − yi) 1 _score = 1 + (l2_normq,d )2 x 2

Slide 32

Slide 32

Brute Force

Slide 33

Slide 33

Hierarchical Navigable Small Worlds (HNSW One popular approach HNSW: a layered approach that simplifies access to the nearest neighbor Tiered: from coarse to fine approximation over a few steps Balance: Bartering a little accuracy for a lot of scalability Speed: Excellent query latency on large scale indices

Slide 34

Slide 34

Filtering KNN Vector Similarity Automatically choose between brute force and HNSW Brute force (not real numbers; used only to demonstrate the linearity) HNSW Bound worst case to 2*(brute force) • Brute force scales O(n) of filtered • HNSW scales ~O(log(n)) of all docs

Slide 35

Slide 35

Elasticsearch + Lucene = fast progress ❤

Slide 36

Slide 36

Scaling Vector Search Vector search Best practices

  1. Needs lots of memory
  2. Avoid searches during indexing
  3. Indexing is slower
  4. Exclude vectors from _source
  5. Merging is slow
  6. Reduce vector dimensionality 4. Use byte rather than float
  • Continuous improvements in Lucene + Elasticsearch

Slide 37

Slide 37

Reduce Required Memory 2. Reduce of number of dimensions per vector

  1. Vector element size reduction (“quantize”)

Slide 38

Slide 38

Benchmarketing

Slide 39

Slide 39

Elasticsearch You Know, for Hybrid Search

Slide 40

Slide 40

Hybrid scoring Term-based score Linear Combination manual boosting Vector similarity score Combine

Slide 41

Slide 41

GET product-catalog/_search { “query” : { “bool” : { “must” : [{ “match”: { “description”: { “query”: “summer clothes”, “boost”: 0.9 } } },{ “knn”: { “field”: “desc_embbeding”, “query_vector”: [0.123, 0.244,…], “num_candidates”: 50, “boost”: 0.1, “filter”: { “term”: { “department”: “women” } } } }], “filter” : { “range” : { “price”: { “lte”: 30 } } } } } } summer clothes pre-filter post-filter

Slide 42

Slide 42

GET product-catalog/_search { “query” : { “bool” : { “must” : [{ “match”: { “description”: { “query”: “summer clothes”, “boost”: 0.9 } } },{ “knn”: { “field”: “image-vector”, “query_vector”: [54, 10, -2], “num_candidates”: 50, “boost”: 0.1 } },{ “knn”: { “field”: “title-vector”, “query_vector”: [1, 20, -52, 23, 10], “num_candidates”: 10, “boost”: 0.5 } }] } } }

Slide 43

Slide 43

https://djdadoo.pilato.fr/

Slide 44

Slide 44

https://github.com/dadoonet/music-search/

Slide 45

Slide 45

ChatGPT Elastic and LLM

Slide 46

Slide 46

Gen AI Search engines

Slide 47

Slide 47

LLM opportunities and limits your question one answer your question GAI / LLM public internet data

Slide 48

Slide 48

Slide 49

Slide 49

Retrieval Augmented Generation your question the right answer your question + context window GAI / LLM public internet data your business data documents images audio

Slide 50

Slide 50

Slide 51

Slide 51

Elasticsearch You Know, for Semantic Search

Slide 52

Slide 52

Search & AI a new era David Pilato | @dadoonet