mirror of
https://github.com/neuml/txtai.git
synced 2026-10-12 02:19:52 +00:00
19 KiB
19 KiB
In [ ]:
%%capture
!pip install git+https://github.com/neuml/txtai datasetsIn [ ]:
%%capture
from txtai.embeddings import Embeddings
from txtai.pipeline import Extractor
# Create embeddings model with content support
embeddings = Embeddings({"path": "sentence-transformers/all-MiniLM-L6-v2", "content": True})
# Create extractor instance
extractor = Extractor(embeddings, "google/flan-t5-base")In [ ]:
data = ["Giants hit 3 HRs to down Dodgers",
"Giants 5 Dodgers 4 final",
"Dodgers drop Game 2 against the Giants, 5-4",
"Blue Jays beat Red Sox final score 2-1",
"Red Sox lost to the Blue Jays, 2-1",
"Blue Jays at Red Sox is over. Score: 2-1",
"Phillies win over the Braves, 5-0",
"Phillies 5 Braves 0 final",
"Final: Braves lose to the Phillies in the series opener, 5-0",
"Lightning goaltender pulled, lose to Flyers 4-1",
"Flyers 4 Lightning 1 final",
"Flyers win 4-1"]
def prompt(question):
return f"""
Answer the following question using the context below.
Question: {question}
Context:
"""
questions = ["What team won the game?", "What was score?"]
execute = lambda query: extractor([(question, query, prompt(question), False) for question in questions], data)
for query in ["Red Sox - Blue Jays", "Phillies - Braves", "Dodgers - Giants", "Flyers - Lightning"]:
print("----", query, "----")
for answer in execute(query):
print(answer)
print()---- Red Sox - Blue Jays ----
('What team won the game?', 'Blue Jays')
('What was score?', '2-1')
---- Phillies - Braves ----
('What team won the game?', 'Phillies')
('What was score?', '5-0')
---- Dodgers - Giants ----
('What team won the game?', 'Giants')
('What was score?', '5-4')
---- Flyers - Lightning ----
('What team won the game?', 'Flyers')
('What was score?', '4-1')
In [ ]:
from datasets import load_dataset
dataset = load_dataset("ag_news", split="train")
# List of all text elements
texts = dataset["text"]
# Create an embeddings index over the dataset
embeddings = Embeddings({"path": "sentence-transformers/all-MiniLM-L6-v2", "content": True})
embeddings.index((x, text, None) for x, text in enumerate(texts))
# Create extractor instance
extractor = Extractor(embeddings, "google/flan-t5-large")WARNING:datasets.builder:Found cached dataset ag_news (/root/.cache/huggingface/datasets/ag_news/default/0.0.0/bc2bcb40336ace1a0374767fc29bb0296cdaf8a6da7298436239c54d79180548)
In [ ]:
def prompt(question):
return f"""Answer the following question using only the context below. Say 'no answer' when the question can't be answered.
Question: {question}
Context: """
def search(query, question=None):
# Default question to query if empty
if not question:
question = query
return extractor([("answer", query, prompt(question), False)])[0][1]
question = "Who won the 2004 presidential election?"
answer = search(question)
print(question, answer)
nquestion = "Who did the candidate beat?"
print(nquestion, search(f"{question} {answer}. {nquestion}"))Who won the 2004 presidential election? George W. Bush Who did the candidate beat? John F. Kerry
In [ ]:
text = prompt(question)
text += "\n" + "\n".join(x["text"]for x in embeddings.search(question))
print(text)Answer the following question using only the context below. Say 'no answer' when the question can't be answered. Question: Who won the 2004 presidential election? Context: Right- and left-click politics The 2004 presidential race ended last week in a stunning defeat for Massachusetts Senator John F. Kerry, as incumbent President George W. Bush cruised to an easy victory. 2004 Presidential Endorsements (AP) AP - Newspaper endorsements in the 2004 presidential campaign between President Bush, a Republican, and Sen. John Kerry, a Democrat. Presidential Campaign to Nov. 2, 2004 (Reuters) Reuters - The following diary of events\leading up to the presidential election on Nov. 2.
In [ ]:
question = "Who won the World Series in 2004?"
answer = search(question)
print(question, answer)
nquestion = "Who did they beat?"
print(nquestion, search(f"{question} {answer}. {nquestion}"))Who won the World Series in 2004? Boston Who did they beat? St Louis
In [ ]:
search("Tell me something interesting?")'herrings communicate by farting'
In [ ]:
answer = "herrings communicate by farting"
for x in embeddings.search("Tell me something interesting?"):
if answer in x["text"]:
start = x["text"].find(answer)
print(x["text"][start:start + len(answer)])herrings communicate by farting
In [ ]:
import requests
data = ["Giants hit 3 HRs to down Dodgers",
"Giants 5 Dodgers 4 final",
"Dodgers drop Game 2 against the Giants, 5-4",
"Blue Jays beat Red Sox final score 2-1",
"Red Sox lost to the Blue Jays, 2-1",
"Blue Jays at Red Sox is over. Score: 2-1",
"Phillies win over the Braves, 5-0",
"Phillies 5 Braves 0 final",
"Final: Braves lose to the Phillies in the series opener, 5-0",
"Lightning goaltender pulled, lose to Flyers 4-1",
"Flyers 4 Lightning 1 final",
"Flyers win 4-1"]
def prompt(question):
return f"""
Answer the following question using the context below.
Question: {question}
Context:
"""
# Submits a series of prompts to the Hugging Face API.
# This call can easily be switched to use the OpenAI API (GPT-3), Cohere API or a library like langchain.
def api(prompts):
response = requests.post("https://api-inference.huggingface.co/models/google/flan-t5-base",
json={"inputs": prompts})
return [x["generated_text"] for x in response.json()]
# Create embeddings model with content support
embeddings = Embeddings({"path": "sentence-transformers/all-MiniLM-L6-v2", "content": True})
# Create extractor instance, submit prompts to the Hugging Face inference API
extractor = Extractor(embeddings, api)
questions = ["What team won the game?", "What was score?"]
execute = lambda query: extractor([(question, query, prompt(question), False) for question in questions], data)
for query in ["Red Sox - Blue Jays", "Phillies - Braves", "Dodgers - Giants", "Flyers - Lightning"]:
print("----", query, "----")
for answer in execute(query):
print(answer)
print()
---- Red Sox - Blue Jays ----
('What team won the game?', 'Blue Jays')
('What was score?', '2-1')
---- Phillies - Braves ----
('What team won the game?', 'Phillies')
('What was score?', '5-0')
---- Dodgers - Giants ----
('What team won the game?', 'Giants')
('What was score?', '5-4')
---- Flyers - Lightning ----
('What team won the game?', 'Flyers')
('What was score?', '4-1')