-
Notifications
You must be signed in to change notification settings - Fork 110
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add colbert sample app and re-factor simple-semantic-search (#1372)
* Add colbert sample app and re-factor simple-semantic-search * messed up spaces and tabs * rm comment * space * remove vespa version note
- Loading branch information
Jo Kristian Bergum
authored
Jan 11, 2024
1 parent
73fe23a
commit e8f8db8
Showing
10 changed files
with
252 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
application.zip | ||
src/main/application/security/ | ||
.idea/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
|
||
<!-- Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. --> | ||
|
||
<picture> | ||
<source media="(prefers-color-scheme: dark)" srcset="https://vespa.ai/assets/vespa-ai-logo-heather.svg"> | ||
<source media="(prefers-color-scheme: light)" srcset="https://vespa.ai/assets/vespa-ai-logo-rock.svg"> | ||
<img alt="#Vespa" width="200" src="https://vespa.ai/assets/vespa-ai-logo-rock.svg" style="margin-bottom: 25px;"> | ||
</picture> | ||
|
||
# Vespa sample applications - Simple hybrid search with ColBERT | ||
|
||
This semantic search application uses a single vector embedding model for retrieval and ColBERT (multi-token vector representation) | ||
for re-ranking. It also features reciprocal rank fusion to fuse different rankings. | ||
|
||
- Query and document text is converted to embeddings by the application using Vespa's [embedder](https://docs.vespa.ai/en/embedding.html) functionality. | ||
- Search by embedding or text match, and fuse the rankings each produces using [reciprocal rank fusion](https://docs.vespa.ai/en/phased-ranking.html#cross-hit-normalization-including-reciprocal-rank-fusion). | ||
|
||
<p data-test="run-macro init-deploy colbert"> | ||
Requires at least Vespa 8.283.46 | ||
</p> | ||
|
||
## To try this application | ||
|
||
Follow the [vespa quick start guide](https://docs.vespa.ai/en/vespa-quick-start.html) | ||
through the <code>vespa deploy</code> step, cloning `colbert` instead of `album-recommendation`. | ||
|
||
Feed documents (this includes embed inference in Vespa): | ||
<pre data-test="exec"> | ||
vespa document ext/1.json | ||
vespa document ext/2.json | ||
vespa document ext/3.json | ||
</pre> | ||
|
||
Example queries: | ||
<pre data-test="exec" data-test-assert-contains="id:doc:doc::1"> | ||
vespa query 'yql=select * from doc where userQuery() or ({targetHits: 100}nearestNeighbor(embedding, q))'\ | ||
'input.query(q)=embed(e5, "query: space contains many suns")' \ | ||
'input.query(qt)=embed(colbert, "space contains many suns")' \ | ||
'query=space contains many suns' | ||
</pre> | ||
|
||
<pre data-test="exec" data-test-assert-contains="id:doc:doc::1"> | ||
vespa query 'yql=select * from doc where userQuery() or ({targetHits: 100}nearestNeighbor(embedding, q))'\ | ||
'input.query(q)=embed(e5, "query: shipping stuff over the sea")' \ | ||
'input.query(qt)=embed(colbert, "shipping stuff over the sea")' \ | ||
'query=shipping stuff over the sea' | ||
</pre> | ||
|
||
<pre data-test="exec" data-test-assert-contains="id:doc:doc::1"> | ||
vespa query 'yql=select * from doc where userQuery() or ({targetHits: 100}nearestNeighbor(embedding, q))'\ | ||
'input.query(q)=embed(e5, "query: exchanging information by sound")' \ | ||
'input.query(qt)=embed(colbert, "exchanging information by sound")' \ | ||
'query=exchanging information by sound' | ||
</pre> | ||
|
||
|
||
### Terminate container | ||
|
||
Remove the container after use: | ||
<pre data-test="exec"> | ||
$ docker rm -f vespa | ||
</pre> | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{ "put": "id:doc:doc::1", "fields": { "chunk": "To transport goods on water, use a boat" } } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{ "put": "id:doc:doc::2", "fields": { "chunk": "Human interaction is often done by talking" } } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{ "put": "id:doc:doc::3", "fields": { "chunk": "The galaxy is filled with stars" } } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
# Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. | ||
|
||
# See https://docs.vespa.ai/en/schemas.html | ||
schema doc { | ||
|
||
document doc { | ||
|
||
field id type string { | ||
indexing: summary | ||
} | ||
field title type string { | ||
indexing: index | summary | ||
index: enable-bm25 | ||
} | ||
field chunk type string { | ||
indexing: index | summary | ||
index: enable-bm25 | ||
} | ||
} | ||
fieldset default { | ||
fields: title, chunk | ||
} | ||
|
||
field embedding type tensor<bfloat16>(x[384]) { | ||
# e5 prefix instructions | ||
indexing: "passage: " . (input title || "") . " " . (input chunk || "") | embed e5 | attribute | index | ||
attribute { | ||
distance-metric: angular | ||
} | ||
} | ||
|
||
field colbert type tensor<int8>(dt{}, x[16]) { | ||
indexing: (input title || "") . " " . (input chunk || "") | embed colbert | attribute | ||
} | ||
|
||
# See https://docs.vespa.ai/en/ranking.html | ||
rank-profile default inherits default { | ||
inputs { | ||
query(qt) tensor<float>(qt{}, x[128]) | ||
query(q) tensor<bfloat16>(x[384]) | ||
} | ||
function unpack() { | ||
expression: unpack_bits(attribute(colbert)) | ||
} | ||
function cos_sim() { | ||
expression: cos(distance(field, embedding)) | ||
} | ||
function max_sim() { | ||
expression { | ||
sum( | ||
reduce( | ||
sum( | ||
query(qt) * unpack() , x | ||
), | ||
max, dt | ||
), | ||
qt | ||
) | ||
} | ||
} | ||
first-phase { | ||
expression: cos_sim | ||
} | ||
second-phase { | ||
expression: max_sim | ||
} | ||
match-features: max_sim cos_sim bm25(title) bm25(chunk) | ||
} | ||
|
||
rank-profile bm25 inherits default { | ||
first-phase { | ||
expression: bm25(title) + bm25(chunk) | ||
} | ||
second-phase { | ||
expression: firstPhase | ||
} | ||
} | ||
|
||
rank-profile e5 inherits default { | ||
first-phase { | ||
expression: cos_sim | ||
} | ||
second-phase { | ||
expression: firstPhase | ||
} | ||
} | ||
|
||
rank-profile hybrid inherits default { | ||
global-phase { | ||
expression { | ||
reciprocal_rank(max_sim) + | ||
reciprocal_rank(cos_sim) + | ||
reciprocal_rank(bm25(chunk)) + | ||
reciprocal_rank(bm25(title)) | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
<?xml version="1.0" encoding="utf-8" ?> | ||
|
||
<services version="1.0" xmlns:deploy="vespa" xmlns:preprocess="properties"> | ||
|
||
<!-- See https://docs.vespa.ai/en/reference/services-container.html --> | ||
<container id="default" version="1.0"> | ||
|
||
<!-- See https://docs.vespa.ai/en/embedding.html#huggingface-embedder --> | ||
<component id="e5" type="hugging-face-embedder"> | ||
<transformer-model url="https://huggingface.co/intfloat/e5-small-v2/resolve/main/model.onnx"/> | ||
<tokenizer-model url="https://huggingface.co/intfloat/e5-small-v2/raw/main/tokenizer.json"/> | ||
</component> | ||
|
||
<!-- See https://docs.vespa.ai/en/embedding.html#colbert-embedder --> | ||
<component id="colbert" type="colbert-embedder"> | ||
<transformer-model url="https://huggingface.co/colbert-ir/colbertv2.0/resolve/main/model.onnx"/> | ||
<tokenizer-model url="https://huggingface.co/colbert-ir/colbertv2.0/raw/main/tokenizer.json"/> | ||
</component> | ||
|
||
<document-api/> | ||
<search/> | ||
<nodes count="1"> | ||
<resources vcpu="4" memory="16Gb" disk="125Gb"> | ||
<gpu count="1" memory="16Gb"/> | ||
</resources> | ||
</nodes> | ||
|
||
</container> | ||
|
||
<!-- See https://docs.vespa.ai/en/reference/services-content.html --> | ||
<content id="text" version="1.0"> | ||
<redundancy>2</redundancy> | ||
<documents> | ||
<document type="doc" mode="index" /> | ||
</documents> | ||
<nodes count="2"/> | ||
</content> | ||
|
||
</services> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters