mirror of
https://github.com/neuml/txtai.git
synced 2026-10-11 21:39:55 +00:00
12 KiB
12 KiB
In [ ]:
%%capture
!pip install git+https://github.com/neuml/txtai#egg=txtai[pipeline]In [ ]:
from txtai.embeddings 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([(uid, text, None) for uid, text in enumerate(data)])
# Run a search
embeddings.search("feel good story", 1)[{'id': '4',
'score': 0.08329004049301147,
'text': 'Maine man wins $1M from $25 lottery ticket'}]In [ ]:
def clength(text):
return len(text) if text else 0
# 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, "functions": [clength]})
# Create an index for the list of text
embeddings.index([(uid, text, None) for uid, text in enumerate(data)])
# Run a search using a custom SQL function
embeddings.search("select clength(text) clength, length(text) length, text from txtai where similar('feel good story')", 1)[{'clength': 42,
'length': 42,
'text': 'Maine man wins $1M from $25 lottery ticket'}]In [ ]:
from txtai.pipeline import Translation
# Translation pipeline
translate = 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, "functions": [translate]})
# Create an index for the list of text
embeddings.index([(uid, text, None) for uid, text in enumerate(data)])
query = """
select
text,
translation(text, 'de', null) 'text (DE)',
translation(text, 'es', null) 'text (ES)',
translation(text, 'fr', null) 'text (FR)'
from txtai where similar('feel good story')
limit 1
"""
# Run a search using a custom SQL function
embeddings.search(query)[{'text': 'Maine man wins $1M from $25 lottery ticket',
'text (DE)': 'Maine Mann gewinnt $1M von $25 Lotterie-Ticket',
'text (ES)': 'Maine hombre gana $1M de billete de lotería de $25',
'text (FR)': 'Maine homme gagne $1M à partir de $25 billet de loterie'}]In [ ]:
def translation(text, lang):
return translate(text, lang)
# 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, "functions": [translation]})
# Create an index for the list of text
embeddings.index([(uid, text, None) for uid, text in enumerate(data)])
query = """
select
text,
translation(text, 'de') 'text (DE)',
translation(text, 'es') 'text (ES)',
translation(text, 'fr') 'text (FR)'
from txtai where similar('feel good story')
limit 1
"""
# Run a search using a custom SQL function
embeddings.search(query)[{'text': 'Maine man wins $1M from $25 lottery ticket',
'text (DE)': 'Maine Mann gewinnt $1M von $25 Lotterie-Ticket',
'text (ES)': 'Maine hombre gana $1M de billete de lotería de $25',
'text (FR)': 'Maine homme gagne $1M à partir de $25 billet de loterie'}]In [ ]:
config = """
translation:
writable: true
embeddings:
path: sentence-transformers/nli-mpnet-base-v2
content: true
functions:
- {name: translation, 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 with custom SQL
app.search(query)[{'text': 'Maine man wins $1M from $25 lottery ticket',
'text (DE)': 'Maine Mann gewinnt $1M von $25 Lotterie-Ticket',
'text (ES)': 'Maine hombre gana $1M de billete de lotería de $25',
'text (FR)': 'Maine homme gagne $1M à partir de $25 billet de loterie'}]