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[agent]In [ ]:
%%writefile skill.md
---
name: txtai
description: Examples on how to build txtai embeddings databases, txtai RAG pipelines, txtai reranker pipelines and txtai translation pipelines
---
# Build an embeddings database
```python
from txtai import Embeddings
# Create embeddings model, backed by sentence-transformers & transformers
embeddings = Embeddings(path="sentence-transformers/nli-mpnet-base-v2")
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"
]
# Index the list of text
embeddings.index(data)
```
# Search an embeddings database
```python
embeddings.search("Search query")
```
# Build a RAG pipeline
```python
from txtai import Embeddings, RAG
# Input data
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"
]
# Build embeddings index
embeddings = Embeddings(content=True)
embeddings.index(data)
# Create the RAG pipeline
rag = RAG(embeddings, "Qwen/Qwen3-0.6B", template="""
Answer the following question using the provided context.
Question:
{question}
Context:
{context}
""")
# Run RAG pipeline
rag("What was won?")
```
# Translate text from English into French
```python
from txtai.pipeline import Translation
# Create and run pipeline
translate = Translation()
translate("This is a test translation", "fr")
```
# Re-ranker pipeline
```python
from txtai import Embeddings
from txtai.pipeline import Reranker, Similarity
# Embeddings instance
embeddings = Embeddings()
embeddings.load(provider="huggingface-hub", container="neuml/txtai-wikipedia")
# Similarity instance
similarity = Similarity(path="colbert-ir/colbertv2.0", lateencode=True)
# Reranking pipeline
reranker = Reranker(embeddings, similarity)
reranker("Tell me about AI")
```In [ ]:
from txtai import Agent
from IPython.display import display, Markdown
def run(query, reset=False):
answer = agent(query, maxlength=50000, reset=reset)
display(Markdown(answer))
agent = Agent(
model="Qwen/Qwen3-4B-Instruct-2507",
tools=["websearch", "webview", "skill.md"],
memory=2,
verbosity_level=0
)In [2]:
run("Write a txtai embeddings program that indexes data")<IPython.core.display.Markdown object>
In [3]:
run("Write a txtai re-ranker pipeline")<IPython.core.display.Markdown object>
In [4]:
run("Update the similarity path to use another reranker model. Disable lateencode.")<IPython.core.display.Markdown object>
In [5]:
run("Write a txtai program that translate text from English to Spanish", reset=True)<IPython.core.display.Markdown object>