mirror of
https://github.com/neuml/txtai.git
synced 2026-10-12 02:19:52 +00:00
13 KiB
13 KiB
In [ ]:
%%capture
!pip install git+https://github.com/neuml/txtai datasetsIn [1]:
%%capture
from txtai import Embeddings, RAG
# Create embeddings model with content support
embeddings = Embeddings(path="sentence-transformers/all-MiniLM-L6-v2", content=True)
# Create the RAG pipeline
rag = RAG(embeddings, "Qwen/Qwen3-4B-Instruct-2507", template="""
Answer the following question using the provided context.
Question:
{question}
Context:
{context}
""")In [2]:
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"]
questions = ["What team won the game?", "What was score?"]
for query in ["Red Sox - Blue Jays", "Phillies - Braves", "Dodgers - Giants", "Flyers - Lightning"]:
print("----", query, "----")
for answer in rag([f"{query} {x}" for x in questions], data):
print(answer)
print()---- Red Sox - Blue Jays ----
{'answer': 'The Blue Jays won the game.'}
{'answer': 'The score was 2-1 in favor of the Blue Jays.'}
---- Phillies - Braves ----
{'answer': 'The Phillies won the game.'}
{'answer': 'The score was 5-0 in favor of the Phillies.'}
---- Dodgers - Giants ----
{'answer': 'The Giants won the game.'}
{'answer': 'The score was Giants 5, Dodgers 4.'}
---- Flyers - Lightning ----
{'answer': 'The Flyers won the game.'}
{'answer': 'The score was Flyers 4, Lightning 1.'}
In [ ]:
from datasets import load_dataset
dataset = load_dataset("ag_news", split="train")
# Create an embeddings index over the dataset
embeddings = Embeddings(path="sentence-transformers/all-MiniLM-L6-v2", content=True)
embeddings.index(dataset["text"])
# Create RAG instance
rag = RAG(embeddings, "Qwen/Qwen3-4B-Instruct-2507", template="""
Answer the following question using the provided context.
Question:
{question}
Context:
{context}
""", output="flatten")In [22]:
question = "Who won the 2004 presidential election?"
answer = rag(question)
print(question, answer)
nquestion = "Who did the candidate beat?"
print(nquestion, rag(f"{question} {answer}. {nquestion}"))Who won the 2004 presidential election? George W. Bush won the 2004 presidential election. Who did the candidate beat? George W. Bush beat John F. Kerry in the 2004 presidential election.
In [23]:
question = "Who won the World Series in 2004?"
answer = rag(question)
print(question, answer)
nquestion = "What team did the Red Sox beat in the World Series?"
print(nquestion, rag(f"{question} {answer}. {nquestion}"))Who won the World Series in 2004? The Boston Red Sox won the World Series in 2004. What team did the Red Sox beat in the World Series? The Boston Red Sox beat the St. Louis Cardinals in the World Series.
In [29]:
rag("Tell me something interesting")Out [29]:
'An interesting fact is that herrings communicate by farting—a quirky and unusual discovery that was honored with an Ig Nobel Prize for its oddball research.'
In [28]:
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