mirror of
https://github.com/neuml/txtai.git
synced 2026-10-12 02:19:52 +00:00
35 KiB
35 KiB
In [39]:
%%capture
!pip install git+https://github.com/neuml/txtai#egg=txtai[database,similarity] datasetsIn [ ]:
import timeit
from datasets import load_dataset
def timer(embeddings, query="red sox"):
elapsed = timeit.timeit(lambda: embeddings.search(query), number=250)
print(f"{elapsed / 250} seconds per query")
dataset = load_dataset("ag_news", split="train")["text"][:25000]In [41]:
from txtai.embeddings import Embeddings
# Create embeddings instance
embeddings = Embeddings({"path": "sentence-transformers/all-MiniLM-L6-v2", "backend": "numpy"})
# Index data
embeddings.index((x, text, None) for x, text in enumerate(dataset))In [42]:
embeddings.search("red sox")Out [42]:
[(19831, 0.6780003309249878), (18302, 0.6639199256896973), (16370, 0.6617192029953003)]
In [43]:
embeddings.info(){
"backend": "numpy",
"build": {
"create": "2023-05-16T13:38:32Z",
"python": "3.10.11",
"settings": {
"numpy": "1.22.4"
},
"system": "Linux (x86_64)",
"txtai": "5.6.0"
},
"dimensions": 384,
"offset": 25000,
"path": "sentence-transformers/all-MiniLM-L6-v2",
"update": "2023-05-16T13:38:32Z"
}
In [44]:
# Create embeddings instance
embeddings = Embeddings({"path": "sentence-transformers/all-MiniLM-L6-v2", "content": "sqlite", "backend": "numpy"})
# Index data
embeddings.index((x, text, None) for x, text in enumerate(dataset))In [45]:
embeddings.search("red sox")Out [45]:
[{'id': '19831',
'text': 'Boston Red Sox Team Report - September 6 (Sports Network) - Two of the top teams in the American League tangle in a possible American League Division Series preview tonight, as the West-leading Oakland Athletics host the wild card-leading Boston Red Sox for the first of a three-game set at the ',
'score': 0.6780003309249878},
{'id': '18302',
'text': 'BASEBALL: RED-HOT SOX CLIP THE ANGELS #39; WINGS BOSTON RED SOX fans are enjoying their best week of the season. While their beloved team swept wild-card rivals Anaheim in a three-game series to establish a nine-game winning streak, the hated New York Yankees endured the heaviest loss in their history.',
'score': 0.6639199256896973},
{'id': '16370',
'text': 'Boston Red Sox Team Report - September 1 (Sports Network) - The red-hot Boston Red Sox hope to continue rolling as they continue their three-game set with the Anaheim Angels this evening at Fenway Park.',
'score': 0.6617192029953003}]In [46]:
embeddings.info(){
"backend": "numpy",
"build": {
"create": "2023-05-16T13:38:52Z",
"python": "3.10.11",
"settings": {
"numpy": "1.22.4"
},
"system": "Linux (x86_64)",
"txtai": "5.6.0"
},
"content": "sqlite",
"dimensions": 384,
"offset": 25000,
"path": "sentence-transformers/all-MiniLM-L6-v2",
"update": "2023-05-16T13:38:52Z"
}
In [47]:
print(embeddings.ann.backend.shape)
print(type(embeddings.ann.backend))(25000, 384) <class 'numpy.memmap'>
In [48]:
timer(embeddings)0.028768999292000445 seconds per query
In [49]:
# Create embeddings instance
embeddings = Embeddings({"path": "sentence-transformers/all-MiniLM-L6-v2", "content": "sqlite", "backend": "torch"})
# Index data
embeddings.index((x, text, None) for x, text in enumerate(dataset))In [50]:
embeddings.search("red sox")Out [50]:
[{'id': '19831',
'text': 'Boston Red Sox Team Report - September 6 (Sports Network) - Two of the top teams in the American League tangle in a possible American League Division Series preview tonight, as the West-leading Oakland Athletics host the wild card-leading Boston Red Sox for the first of a three-game set at the ',
'score': 0.678000271320343},
{'id': '18302',
'text': 'BASEBALL: RED-HOT SOX CLIP THE ANGELS #39; WINGS BOSTON RED SOX fans are enjoying their best week of the season. While their beloved team swept wild-card rivals Anaheim in a three-game series to establish a nine-game winning streak, the hated New York Yankees endured the heaviest loss in their history.',
'score': 0.6639199256896973},
{'id': '16370',
'text': 'Boston Red Sox Team Report - September 1 (Sports Network) - The red-hot Boston Red Sox hope to continue rolling as they continue their three-game set with the Anaheim Angels this evening at Fenway Park.',
'score': 0.6617191433906555}]In [51]:
embeddings.info(){
"backend": "torch",
"build": {
"create": "2023-05-16T13:39:19Z",
"python": "3.10.11",
"settings": {
"torch": "2.0.0+cu118"
},
"system": "Linux (x86_64)",
"txtai": "5.6.0"
},
"content": "sqlite",
"dimensions": 384,
"offset": 25000,
"path": "sentence-transformers/all-MiniLM-L6-v2",
"update": "2023-05-16T13:39:19Z"
}
In [52]:
print(embeddings.ann.backend.shape)
print(type(embeddings.ann.backend))torch.Size([25000, 384]) <class 'torch.Tensor'>
In [53]:
timer(embeddings)0.0198183048359997 seconds per query
In [54]:
# Create embeddings instance
embeddings = Embeddings({"path": "sentence-transformers/all-MiniLM-L6-v2", "content": True})
# Index data
embeddings.index((x, text, None) for x, text in enumerate(dataset))In [55]:
embeddings.search("red sox")Out [55]:
[{'id': '19831',
'text': 'Boston Red Sox Team Report - September 6 (Sports Network) - Two of the top teams in the American League tangle in a possible American League Division Series preview tonight, as the West-leading Oakland Athletics host the wild card-leading Boston Red Sox for the first of a three-game set at the ',
'score': 0.6780003309249878},
{'id': '18302',
'text': 'BASEBALL: RED-HOT SOX CLIP THE ANGELS #39; WINGS BOSTON RED SOX fans are enjoying their best week of the season. While their beloved team swept wild-card rivals Anaheim in a three-game series to establish a nine-game winning streak, the hated New York Yankees endured the heaviest loss in their history.',
'score': 0.6639199256896973},
{'id': '16370',
'text': 'Boston Red Sox Team Report - September 1 (Sports Network) - The red-hot Boston Red Sox hope to continue rolling as they continue their three-game set with the Anaheim Angels this evening at Fenway Park.',
'score': 0.6617192029953003}]In [56]:
embeddings.info(){
"backend": "faiss",
"build": {
"create": "2023-05-16T13:39:47Z",
"python": "3.10.11",
"settings": {
"components": "IVF632,Flat"
},
"system": "Linux (x86_64)",
"txtai": "5.6.0"
},
"content": true,
"dimensions": 384,
"offset": 25000,
"path": "sentence-transformers/all-MiniLM-L6-v2",
"update": "2023-05-16T13:39:47Z"
}
In [57]:
timer(embeddings)0.00825659705599992 seconds per query
In [58]:
# Create embeddings instance
embeddings = Embeddings({"path": "sentence-transformers/all-MiniLM-L6-v2", "content": True, "backend": "hnsw", "hnsw": {"m": 32}})
# Index data
embeddings.index((x, text, None) for x, text in enumerate(dataset))In [59]:
embeddings.search("red sox")Out [59]:
[{'id': '19831',
'text': 'Boston Red Sox Team Report - September 6 (Sports Network) - Two of the top teams in the American League tangle in a possible American League Division Series preview tonight, as the West-leading Oakland Athletics host the wild card-leading Boston Red Sox for the first of a three-game set at the ',
'score': 0.678000271320343},
{'id': '18302',
'text': 'BASEBALL: RED-HOT SOX CLIP THE ANGELS #39; WINGS BOSTON RED SOX fans are enjoying their best week of the season. While their beloved team swept wild-card rivals Anaheim in a three-game series to establish a nine-game winning streak, the hated New York Yankees endured the heaviest loss in their history.',
'score': 0.6639199256896973},
{'id': '16370',
'text': 'Boston Red Sox Team Report - September 1 (Sports Network) - The red-hot Boston Red Sox hope to continue rolling as they continue their three-game set with the Anaheim Angels this evening at Fenway Park.',
'score': 0.6617191433906555}]In [60]:
embeddings.info(){
"backend": "hnsw",
"build": {
"create": "2023-05-16T13:40:21Z",
"python": "3.10.11",
"settings": {
"efconstruction": 200,
"m": 32,
"seed": 100
},
"system": "Linux (x86_64)",
"txtai": "5.6.0"
},
"content": true,
"deletes": 0,
"dimensions": 384,
"hnsw": {
"m": 32
},
"metric": "ip",
"offset": 25000,
"path": "sentence-transformers/all-MiniLM-L6-v2",
"update": "2023-05-16T13:40:21Z"
}
In [61]:
timer(embeddings)0.006280380824000531 seconds per query
In [62]:
import numpy as np
import requests
BASE = "https://api-inference.huggingface.co/pipeline/feature-extraction"
def transform(inputs):
# Your API provider of choice
response = requests.post(f"{BASE}/sentence-transformers/all-MiniLM-L6-v2", json={"inputs": inputs})
return np.array(response.json(), dtype=np.float32)
embeddings = Embeddings({"transform": transform, "backend": "numpy", "content": True})
embeddings.index([(0, "sunny", None), (1, "rainy", None)])
embeddings.search("nice day") Out [62]:
[{'id': '0', 'text': 'sunny', 'score': 0.28077083826065063},
{'id': '1', 'text': 'rainy', 'score': 0.18051263689994812}]In [63]:
# Create embeddings instance
embeddings = Embeddings({"path": "sentence-transformers/all-MiniLM-L6-v2", "content": True, "format": "json"})
# Index data
embeddings.index((x, text, None) for x, text in enumerate(dataset))
# Save embeddings
embeddings.save("index")
!cat index/config.json{
"path": "sentence-transformers/all-MiniLM-L6-v2",
"content": true,
"format": "json",
"dimensions": 384,
"backend": "faiss",
"offset": 25000,
"build": {
"create": "2023-05-16T13:40:49Z",
"python": "3.10.11",
"settings": {
"components": "IVF632,Flat"
},
"system": "Linux (x86_64)",
"txtai": "5.6.0"
},
"update": "2023-05-16T13:40:49Z"
}In [64]:
# Create embeddings instance
embeddings = Embeddings({"path": "sentence-transformers/all-MiniLM-L6-v2", "content": "sqlite"})
# Index data
embeddings.index((x, text, None) for x, text in enumerate(dataset))In [65]:
timer(embeddings, "SELECT text FROM txtai where id = 3980")0.00012401376399975562 seconds per query
In [66]:
timer(embeddings, "SELECT count(*), text FROM txtai group by text order by count(*) desc")0.03863514600000053 seconds per query
In [67]:
# Create embeddings instance
embeddings = Embeddings({"path": "sentence-transformers/all-MiniLM-L6-v2", "content": "duckdb"})
# Index data
embeddings.index((x, text, None) for x, text in enumerate(dataset))In [68]:
timer(embeddings, "SELECT text FROM txtai where id = 3980")0.0038918176440001844 seconds per query
In [69]:
timer(embeddings, "SELECT count(*), text FROM txtai group by text order by count(*) desc")0.0198518766039997 seconds per query