mirror of
https://github.com/neuml/txtai.git
synced 2026-10-12 02:19:52 +00:00
14 KiB
14 KiB
In [ ]:
%%capture
!pip install git+https://github.com/neuml/txtaiIn [ ]:
%%capture
from txtai.embeddings import Embeddings
# Create embeddings model, backed by sentence-transformers & transformers
embeddings = Embeddings({"method": "transformers", "path": "sentence-transformers/bert-base-nli-mean-tokens"})In [ ]:
data = ["US tops 5 million confirmed virus cases",
"Canada's last fully intact ice shelf has suddenly collapsed, forming a Manhattan-sized iceberg",
"Beijing mobilises invasion craft along coast as Taiwan tensions escalate",
"The National Park Service warns against sacrificing slower friends in a bear attack",
"Maine man wins $1M from $25 lottery ticket",
"Make huge profits without work, earn up to $100,000 a day"]
print("%-20s %s" % ("Query", "Best Match"))
print("-" * 50)
for query in ("feel good story", "climate change", "health", "war", "wildlife", "asia", "north america", "dishonest junk"):
# Get index of best section that best matches query
uid = embeddings.similarity(query, data)[0][0]
print("%-20s %s" % (query, data[uid]))Query Best Match -------------------------------------------------- feel good story Maine man wins $1M from $25 lottery ticket climate change Canada's last fully intact ice shelf has suddenly collapsed, forming a Manhattan-sized iceberg health US tops 5 million confirmed virus cases war Beijing mobilises invasion craft along coast as Taiwan tensions escalate wildlife The National Park Service warns against sacrificing slower friends in a bear attack asia Beijing mobilises invasion craft along coast as Taiwan tensions escalate north america US tops 5 million confirmed virus cases dishonest junk Make huge profits without work, earn up to $100,000 a day
In [ ]:
# Create an index for the list of text
embeddings.index([(uid, text, None) for uid, text in enumerate(data)])
print("%-20s %s" % ("Query", "Best Match"))
print("-" * 50)
# Run an embeddings search for each query
for query in ("feel good story", "climate change", "health", "war", "wildlife", "asia", "north america", "dishonest junk"):
# Extract uid of first result
# search result format: (uid, score)
uid = embeddings.search(query, 1)[0][0]
# Print text
print("%-20s %s" % (query, data[uid]))Query Best Match -------------------------------------------------- feel good story Maine man wins $1M from $25 lottery ticket climate change Canada's last fully intact ice shelf has suddenly collapsed, forming a Manhattan-sized iceberg health US tops 5 million confirmed virus cases war Beijing mobilises invasion craft along coast as Taiwan tensions escalate wildlife The National Park Service warns against sacrificing slower friends in a bear attack asia Beijing mobilises invasion craft along coast as Taiwan tensions escalate north america US tops 5 million confirmed virus cases dishonest junk Make huge profits without work, earn up to $100,000 a day
In [ ]:
embeddings.save("index")
embeddings = Embeddings()
embeddings.load("index")
uid = embeddings.search("climate change", 1)[0][0]
print(data[uid])Canada's last fully intact ice shelf has suddenly collapsed, forming a Manhattan-sized iceberg
In [ ]:
!ls indexconfig embeddings