mirror of
https://github.com/neuml/txtai.git
synced 2026-10-11 21:39:55 +00:00
22 KiB
22 KiB
In [ ]:
%%capture
!pip install git+https://github.com/neuml/txtai#egg=txtai[pipeline]
# Get test data
!wget -N https://github.com/neuml/txtai/releases/download/v6.2.0/tests.tar.gz
!tar -xvzf tests.tar.gz
# Install NLTK
import nltk
nltk.download(['punkt', 'punkt_tab'])In [1]:
from txtai import LLM
# Create LLM
llm = LLM("Qwen/Qwen3-4B-Instruct-2507")In [ ]:
from txtai import Textractor
# Create Textractor
textractor = Textractor()
text = textractor("txtai/document.docx")
print(text)# txtai – the all-in-one embeddings database txtai is an all-in-one embeddings database for semantic search, LLM orchestration and language model workflows. Summary of txtai features: · *Vector search* with SQL, object storage, topic modeling · Create *embeddings* for text, documents, audio, images and video · *Pipelines* powered by language models that run LLM prompts · *Workflows* to join pipelines together and aggregate business logic · Build with *Python* or *YAML* . API bindings available for JavaScript, Java, Rust and Go. · *Run local or scale out with container orchestration* ## Examples List of example notebooks. |Notebook|Description| |---|---| |Introducing txtai |Overview of the functionality provided by txtai| |Similarity search with images|Embed images and text into the same space for search| |Build a QA database|Question matching with semantic search| |Semantic Graphs|Explore topics, data connectivity and run network analysis| ## Install The easiest way to install is via pip and PyPI pip install txtai Python 3.11+ is supported. Using a Python virtual environment is **recommended** . See the detailed install instructions for more information covering optional dependencies, environment specific prerequisites, installing from source, conda support and how to run with containers. ## Model guide The following shows a list of suggested models. |Component|Model(s)| |---|---| |Embeddings|all-MiniLM-L6-v2| ||E5-base-v2| |Image Captions|BLIP| |Labels - Zero Shot|BART-Large-MNLI| |Labels - Fixed|Fine-tune with training pipeline| |Large Language Model (LLM)|Flan T5 XL| ||Mistral 7B OpenOrca| |Summarization|DistilBART| |Text-to-Speech|ESPnet JETS| |Transcription|Whisper| |Translation|OPUS Model Series|
In [18]:
def execute(question, text):
return llm([
{"role": "system", "content": "You are a friendly assistant. You answer questions from users."},
{"role": "user", "content": f"""
Answer the following question using only the context below. Only include information specifically discussed.
question: {question}
context: {text}
"""}
], maxlength=4096)
execute("Tell me about txtai in one sentence", text)Out [18]:
'txtai is an all-in-one embeddings database that supports semantic search, LLM orchestration, and language model workflows with features like vector search, embeddings for text, audio, images, and video, pipelines powered by language models, and scalable workflows available via Python or YAML with API bindings for multiple languages.'
In [19]:
execute("What model does txtai recommend for transcription?", text)Out [19]:
'txtai recommends using Whisper for transcription.'
In [20]:
execute("I don't know anything about txtai, what would be the best thing to read?", text)Out [20]:
'The best thing to read if you don\'t know anything about txtai would be the "Introducing txtai" notebook, as it provides an overview of the functionality offered by txtai.'
In [ ]:
import os
from txtai import Embeddings
def stream(path):
for f in sorted(os.listdir(path)):
fpath = os.path.join(path, f)
# Only accept documents
if f.endswith(("docx", "xlsx", "pdf")):
print(f"Indexing {fpath}")
for paragraph in textractor(fpath):
yield paragraph
# Document text extraction, split into paragraphs
textractor = Textractor(paragraphs=True)
# Vector Database
embeddings = Embeddings(content=True)
embeddings.index(stream("txtai"))Indexing txtai/article.pdf Indexing txtai/document.docx Indexing txtai/document.pdf Indexing txtai/spreadsheet.xlsx
In [23]:
def context(question):
context = "\n".join(x["text"] for x in embeddings.search(question))
return context
def rag(question):
return execute(question, context(question))
rag("What model does txtai recommend for image captioning?")Out [23]:
'txtai recommends using BLIP for image captioning.'
In [24]:
result = rag("When was the BLIP model added for image captioning?")
print(result)The BLIP model was added for image captioning on 2022-03-17.
In [25]:
for x in embeddings.search(result):
print(x["text"])E5-base-v2 Image Captions BLIP Labels - Zero Shot BART-Large-MNLI # Model Guide |Component |Model(s)|Date Added| |---|---|---| |Embeddings |all-MiniLM-L6-v2|2022-04-15| |Image Captions |BLIP|2022-03-17| |Labels - Zero Shot |BART-Large-MNLI|2022-01-01| |Large Language Model (LLM) |Mistral 7B OpenOrca|2023-10-01| |Summarization |DistilBART|2021-02-22| |Text-to-Speech |ESPnet JETS|2022-08-01| |Transcription |Whisper|2022-08-01| |Translation |OPUS Model Series|2021-04-06| &"Times New Roman,Regular"&12&A ## Model guide The following shows a list of suggested models. |Component|Model(s)| |---|---| |Embeddings|all-MiniLM-L6-v2| ||E5-base-v2| |Image Captions|BLIP| |Labels - Zero Shot|BART-Large-MNLI| |Labels - Fixed|Fine-tune with training pipeline| |Large Language Model (LLM)|Flan T5 XL| ||Mistral 7B OpenOrca| |Summarization|DistilBART| |Text-to-Speech|ESPnet JETS| |Transcription|Whisper| |Translation|OPUS Model Series|
In [26]:
from txtai import RAG
# 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}
""", output="reference")In [27]:
result = rag("What version of Python is supported?", maxlength=4096)
print("ANSWER:", result["answer"])
print("CITATION:", embeddings.search("select id, text from txtai where id = :id", limit=1, parameters={"id": result["reference"]}))ANSWER: Python 3.11 and later versions (Python 3.11+) are supported.
CITATION: [{'id': '24', 'text': 'Python 3.11+ is supported. Using a Python virtual environment is recommended.'}]