mirror of
https://github.com/neuml/txtai.git
synced 2026-10-12 02:19:52 +00:00
15 KiB
15 KiB
In [ ]:
%%capture
!pip install git+https://github.com/neuml/txtai#egg=txtai[pipeline]In [1]:
from txtai import Embeddings
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"]
# 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)
# Create an index for the list of text
embeddings.index(data)
# Run a search
embeddings.search("feel good story", 1)Out [1]:
[{'id': '4',
'text': 'Maine man wins $1M from $25 lottery ticket',
'score': 0.08329025655984879}]In [2]:
from txtai import LLM
llm = LLM("NeuML/t5-small-txtsql", template="translate English to SQL: {text}")
queries = [
"feel good story",
"feel good story since yesterday",
"feel good story with 'lottery' in text",
"how many feel good story",
"feel good story translated to fr",
"feel good story summarized"
]
for query in queries:
print(f"Input: {query}")
print(f"SQL: {llm(query)}")
print()
Input: feel good story
SQL: select id, text, score from txtai where similar('feel good story')
Input: feel good story since yesterday
SQL: select id, text, score from txtai where similar('feel good story') and entry >= date('now', '-1 day')
Input: feel good story with 'lottery' in text
SQL: select id, text, score from txtai where similar('feel good story') and text like '%lottery%'
Input: how many feel good story
SQL: select count(*) from txtai where similar('feel good story')
Input: feel good story translated to fr
SQL: select id, translate(text, 'fr') text, score from txtai where similar('feel good story')
Input: feel good story summarized
SQL: select id, summary(text) text, score from txtai where similar('feel good story')
In [2]:
from txtai.pipeline import Translation
def translate(text, lang):
return translation(text, lang)
translation = Translation()
# 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,
query={"path": "NeuML/t5-small-txtsql"},
functions=[translate]
)
# Create an index for the list of text
embeddings.index(data)
query = "select id, score, translate(text, 'de') 'text' from txtai where similar('feel good story')"
# Run a search using a custom SQL function
embeddings.search(query)[0]Out [2]:
{'id': '4',
'score': 0.08329025655984879,
'text': 'Maine Mann gewinnt $1M von $25 Lotterie-Ticket'}In [4]:
embeddings.search("feel good story translated to de")[0]Out [4]:
{'id': '4',
'text': 'Maine Mann gewinnt $1M von $25 Lotterie-Ticket',
'score': 0.08329025655984879}In [5]:
embeddings.search("feel good story since yesterday")[0]Out [5]:
{'id': '4',
'text': 'Maine man wins $1M from $25 lottery ticket',
'score': 0.08329025655984879}In [6]:
embeddings.search("feel good story with 'lottery' in text")Out [6]:
[{'id': '4',
'text': 'Maine man wins $1M from $25 lottery ticket',
'score': 0.08329025655984879}]In [7]:
embeddings.search("feel good story with missing in text")Out [7]:
[]
In [8]:
embeddings.search("feel good story with field equal 14")Out [8]:
[]
In [9]:
config = """
translation:
writable: true
embeddings:
path: sentence-transformers/nli-mpnet-base-v2
content: true
query:
path: NeuML/t5-small-txtsql
functions:
- {name: translate, argcount: 2, function: translation}
"""
from txtai.app import Application
# Build application and index data
app = Application(config)
app.add([{"id": x, "text": row} for x, row in enumerate(data)])
app.index()
# Run search query
app.search("feel good story translated to de")[0]Out [9]:
{'id': '4',
'text': 'Maine Mann gewinnt $1M von $25 Lotterie-Ticket',
'score': 0.08329025655984879}