mirror of
https://github.com/neuml/txtai.git
synced 2026-10-12 02:19:52 +00:00
14 KiB
14 KiB
In [75]:
%%capture
!pip install git+https://github.com/neuml/txtai datasetsIn [76]:
from datasets import load_dataset
ds = load_dataset("web_questions", split="train")
for row in ds.select(range(5)):
print(row["question"], row["answers"])what is the name of justin bieber brother? ['Jazmyn Bieber', 'Jaxon Bieber'] what character did natalie portman play in star wars? ['Padmé Amidala'] what state does selena gomez? ['New York City'] what country is the grand bahama island in? ['Bahamas'] what kind of money to take to bahamas? ['Bahamian dollar']
In [77]:
from txtai.embeddings import Embeddings
# Create embeddings index with content enabled. The default behavior is to only store indexed vectors.
embeddings = Embeddings({"path": "sentence-transformers/nli-mpnet-base-v2", "content": True})
# Map question to text and store content
embeddings.index([(uid, {"url": row["url"], "text": row["question"], "answer": ", ".join(row["answers"])}, None) for uid, row in enumerate(ds)])In [78]:
def question(text):
return embeddings.search(f"select text, answer, score from txtai where similar('{text}') limit 1")
question("What is the timezone of NYC?")Out [78]:
[{'answer': 'North American Eastern Time Zone',
'score': 0.8904051184654236,
'text': 'what time zone is new york under?'}]In [79]:
question("Things to do in New York")Out [79]:
[{'answer': "Chelsea Art Museum, Brooklyn Bridge, Empire State Building, The Broadway Theatre, American Museum of Natural History, Central Park, St. Patrick's Cathedral, Japan Society of New York, FusionArts Museum, American Folk Art Museum",
'score': 0.8308358192443848,
'text': 'what are some places to visit in new york?'}]In [80]:
question("Microsoft founder")Out [80]:
[{'answer': 'Bill Gates',
'score': 0.6617322564125061,
'text': 'who created microsoft windows?'}]In [81]:
question("Apple founder university")Out [81]:
[{'answer': 'Reed College',
'score': 0.5137897729873657,
'text': 'what college did steve jobs attend?'}]In [82]:
question("What country uses the Yen?")Out [82]:
[{'answer': 'Japanese yen',
'score': 0.6663530468940735,
'text': 'what money do japanese use?'}]In [83]:
question("Show me a list of Pixar movies")Out [83]:
[{'answer': "A Bug's Life, Toy Story 2, Ratatouille, Cars, Up, Toy Story, Monsters, Inc., The Incredibles, Finding Nemo, WALL-E",
'score': 0.653051495552063,
'text': 'what does pixar produce?'}]In [84]:
question("What is the timezone of Florida?")Out [84]:
[{'answer': 'North American Eastern Time Zone',
'score': 0.9672279357910156,
'text': 'where is the time zone in florida?'}]In [85]:
question("Tell me an animal found offshore in Florida")Out [85]:
[{'answer': 'Largemouth bass',
'score': 0.6526554822921753,
'text': 'what kind of fish do you catch in florida?'}]In [86]:
from txtai.app import Application
# Save index
embeddings.save("questions.tar.gz")
# Build application and index data
app = Application("path: questions.tar.gz")
# Run search query
app.search("select text, answer, score from txtai where similar('Tell me an animal found offshore in Florida') limit 1")[0]Out [86]:
{'answer': 'Largemouth bass',
'score': 0.6526554822921753,
'text': 'what kind of fish do you catch in florida?'}