Views
No views yet
vsearch repo:git clone git@github.com:jzhoubu/vsearch.git
poetry install
poetry shell1import torch
2from src.ir import Retriever
3
4query = "Who first proposed the theory of relativity?"
5passages = [
6 "Albert Einstein (14 March 1879 – 18 April 1955) was a German-born theoretical physicist who is widely held to be one of the greatest and most influential scientists of all time. He is best known for developing the theory of relativity.",
7 "Sir Isaac Newton FRS (25 December 1642 – 20 March 1727) was an English polymath active as a mathematician, physicist, astronomer, alchemist, theologian, and author who was described in his time as a natural philosopher.",
8 "Nikola Tesla (10 July 1856 – 7 January 1943) was a Serbian-American inventor, electrical engineer, mechanical engineer, and futurist. He is known for his contributions to the design of the modern alternating current (AC) electricity supply system."
9]
10
11ir = Retriever.from_pretrained("vsearch/svdr-nq")
12ir = ir.to("cuda")
13
14# Embed the query and passages
15q_emb = ir.encoder_q.embed(query) # Shape: [1, V]
16p_emb = ir.encoder_p.embed(passages) # Shape: [4, V]
17
18scores = q_emb @ p_emb.t()
19print(scores)
20
21# Output:
22tensor([[61.5432, 10.3108, 8.6709]], device='cuda:0')1# Build the sparse index for the passages
2ir.build_index(passages, index_type="sparse")
3print(ir.index)
4
5# Output:
6# Index Type : SparseIndex
7# Vector Type : torch.sparse_csr
8# Vector Shape : torch.Size([3, 29523])
9# Vector Device : cuda:0
10# Number of Texts : 3
11
12# Save the index to disk
13index_file = "/path/to/index.npz"
14ir.save_index(path)
15
16# Load the index from disk
17index_file = "/path/to/index.npz"
18data_file = "/path/to/texts.jsonl"
19ir.load_index(index_file=index_file, data_file=data_file)
20
21# Search top-k results for queries
22queries = [query]
23results = ir.retrieve(queries, k=3)
24print(results)
25
26# Output:
27# SearchResults(
28# ids=tensor([[0, 1, 2]], device='cuda:0'),
29# scores=tensor([[61.5432, 10.3108, 8.6709]], device='cuda:0')
30# )
31
32query_id = 0
33top1_psg_id = results.ids[query_id][0]
34top1_psg = ir.index.get_sample(top1_psg_id)
35print(top1_psg)
36# Output:
37
38# Albert Einstein (14 March 1879 – 18 April 1955) was a German-born theoretical physicist who is widely held to be one of the greatest and most influential scientists of all time. He is best known for developing the theory of relativity.
391# Build the bag-of-token index for the passages
2ir.build_index(passages, index_type="bag_of_token")
3print(ir.index)
4
5# Output:
6# Index Type : BoTIndex
7# Vector Type : torch.sparse_csr
8# Vector Shape : torch.Size([3, 29523])
9# Vector Device : cuda:0
10# Number of Texts : 3
11
12# Search top-k results from bag-of-token index, and embed and rerank them on-the-fly
13queries = [query]
14results = ir.retrieve(queries, k=3, rerank=True)
15print(results)
16
17# Output:
18# SearchResults(
19# ids=tensor([0, 2, 1], device='cuda:3'),
20# scores=tensor([61.5432, 10.3108, 8.6709], device='cuda:0')
21# )@article{zhou2024semi,
title={Semi-Parametric Retrieval via Binary Token Index},
author={Zhou, Jiawei and Dong, Li and Wei, Furu and Chen, Lei},
journal={arXiv preprint arXiv:2405.01924},
year={2024}
}