mirror of
https://github.com/neuml/txtai.git
synced 2026-10-11 21:39:55 +00:00
16 KiB
16 KiB
In [ ]:
%%capture
!pip install git+https://github.com/neuml/txtai#egg=txtai[api]In [ ]:
import os
os.chdir("/content")In [ ]:
%%writefile index.yml
writable: true
# Embeddings settings
embeddings:
path: sentence-transformers/nli-mpnet-base-v2
content: trueWriting index.yml
In [ ]:
%%writefile cluster.yml
# Embeddings cluster
cluster:
shards:
- http://127.0.0.1:8001
- http://127.0.0.1:8002Writing cluster.yml
In [ ]:
# Start embeddings shards
!CONFIG=index.yml nohup uvicorn --port 8001 "txtai.api:app" &> shard-1.log &
!CONFIG=index.yml nohup uvicorn --port 8002 "txtai.api:app" &> shard-2.log &
# Start main instance
!CONFIG=cluster.yml nohup uvicorn --port 8000 "txtai.api:app" &> main.log &
# Wait for startup
!sleep 90In [ ]:
%%writefile run.py
from txtai.api import Cluster
cluster = Cluster({"shards": ["http://127.0.0.1:8001", "http://127.0.0.1:8002"]})
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 data
cluster.add([{"id": x, "text": row} for x, row in enumerate(data)])
cluster.index()
# Test search
result = cluster.search("feel good story", 1)[0]
print("Query: feel good story\nResult:", result["text"])Writing run.py
In [ ]:
!python run.pyQuery: feel good story Result: Maine man wins $1M from $25 lottery ticket
In [ ]:
%%capture
!git clone https://github.com/neuml/txtai.jsIn [ ]:
%%writefile txtai.js/examples/node/src/cluster.js
import {Embeddings} from "txtai";
import {sprintf} from "sprintf-js";
const run = async () => {
try {
let embeddings = new Embeddings(process.argv[2]);
let 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"];
console.log();
console.log("Querying an Embeddings cluster");
console.log(sprintf("%-20s %s", "Query", "Best Match"));
console.log("-".repeat(50));
for (let query of ["feel good story", "climate change", "public health story", "war", "wildlife", "asia", "lucky", "dishonest junk"]) {
let results = await embeddings.search(query, 1);
if (results && results.length > 0) {
let result = results[0].text;
console.log(sprintf("%-20s %s", query, result));
}
}
}
catch (e) {
console.trace(e);
}
};
run();Writing txtai.js/examples/node/src/cluster.js
In [ ]:
%%capture
os.chdir("txtai.js/examples/node")
!npm install
!npm run buildIn [ ]:
!node dist/cluster.js http://127.0.0.1:8000Querying an Embeddings cluster Query Best Match -------------------------------------------------- feel good story Maine man wins $1M from $25 lottery ticket climate change Canada's last fully intact ice shelf has suddenly collapsed, forming a Manhattan-sized iceberg public health story US tops 5 million confirmed virus cases war Beijing mobilises invasion craft along coast as Taiwan tensions escalate wildlife The National Park Service warns against sacrificing slower friends in a bear attack asia Beijing mobilises invasion craft along coast as Taiwan tensions escalate lucky Maine man wins $1M from $25 lottery ticket dishonest junk Make huge profits without work, earn up to $100,000 a day
In [ ]:
!node dist/cluster.js http://127.0.0.1:8001Querying an Embeddings cluster Query Best Match -------------------------------------------------- feel good story Maine man wins $1M from $25 lottery ticket climate change Beijing mobilises invasion craft along coast as Taiwan tensions escalate public health story US tops 5 million confirmed virus cases war Beijing mobilises invasion craft along coast as Taiwan tensions escalate wildlife Beijing mobilises invasion craft along coast as Taiwan tensions escalate asia Beijing mobilises invasion craft along coast as Taiwan tensions escalate lucky Maine man wins $1M from $25 lottery ticket
In [ ]:
!node dist/cluster.js http://127.0.0.1:8002Querying an Embeddings cluster Query Best Match -------------------------------------------------- feel good story Make huge profits without work, earn up to $100,000 a day climate change Canada's last fully intact ice shelf has suddenly collapsed, forming a Manhattan-sized iceberg public health story The National Park Service warns against sacrificing slower friends in a bear attack war The National Park Service warns against sacrificing slower friends in a bear attack wildlife The National Park Service warns against sacrificing slower friends in a bear attack asia The National Park Service warns against sacrificing slower friends in a bear attack lucky The National Park Service warns against sacrificing slower friends in a bear attack dishonest junk Make huge profits without work, earn up to $100,000 a day
In [ ]:
!curl http://127.0.0.1:8000/count
!printf "\n"
!curl http://127.0.0.1:8001/count
!printf "\n"
!curl http://127.0.0.1:8002/count6 3 3