Update README with LanceDB examples
Browse files
README.md
CHANGED
|
@@ -58,6 +58,18 @@ ds = lance.dataset("hf://datasets/lance-format/trivia-qa-lance/data/train.lance"
|
|
| 58 |
print(ds.count_rows(), ds.schema.names, ds.list_indices())
|
| 59 |
```
|
| 60 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
## Semantic search over questions
|
| 62 |
|
| 63 |
```python
|
|
@@ -76,6 +88,43 @@ hits = ds.scanner(
|
|
| 76 |
).to_table().to_pylist()
|
| 77 |
```
|
| 78 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 79 |
## Filter by answer type
|
| 80 |
|
| 81 |
```python
|
|
@@ -83,6 +132,22 @@ ds = lance.dataset("hf://datasets/lance-format/trivia-qa-lance/data/train.lance"
|
|
| 83 |
wiki = ds.scanner(filter="answer_type = 'WikipediaEntity'", columns=["question"], limit=5).to_table()
|
| 84 |
```
|
| 85 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 86 |
## Why Lance?
|
| 87 |
|
| 88 |
- One dataset carries questions + answers + aliases + embeddings + indices — no sidecar files.
|
|
|
|
| 58 |
print(ds.count_rows(), ds.schema.names, ds.list_indices())
|
| 59 |
```
|
| 60 |
|
| 61 |
+
## Load with LanceDB
|
| 62 |
+
|
| 63 |
+
These tables can also be consumed by [LanceDB](https://lancedb.github.io/lancedb/), the serverless vector database built on Lance, for simplified vector search and other queries.
|
| 64 |
+
|
| 65 |
+
```python
|
| 66 |
+
import lancedb
|
| 67 |
+
|
| 68 |
+
db = lancedb.connect("hf://datasets/lance-format/trivia-qa-lance/data")
|
| 69 |
+
tbl = db.open_table("train")
|
| 70 |
+
print(f"LanceDB table opened with {len(tbl)} trivia questions")
|
| 71 |
+
```
|
| 72 |
+
|
| 73 |
## Semantic search over questions
|
| 74 |
|
| 75 |
```python
|
|
|
|
| 88 |
).to_table().to_pylist()
|
| 89 |
```
|
| 90 |
|
| 91 |
+
### LanceDB semantic search
|
| 92 |
+
|
| 93 |
+
```python
|
| 94 |
+
import lancedb
|
| 95 |
+
from sentence_transformers import SentenceTransformer
|
| 96 |
+
|
| 97 |
+
encoder = SentenceTransformer("sentence-transformers/all-MiniLM-L6-v2", device="cuda")
|
| 98 |
+
q = encoder.encode(["who painted the sistine chapel ceiling"], normalize_embeddings=True)[0]
|
| 99 |
+
|
| 100 |
+
db = lancedb.connect("hf://datasets/lance-format/trivia-qa-lance/data")
|
| 101 |
+
tbl = db.open_table("train")
|
| 102 |
+
|
| 103 |
+
results = (
|
| 104 |
+
tbl.search(q.tolist(), vector_column_name="question_emb")
|
| 105 |
+
.metric("cosine")
|
| 106 |
+
.select(["question", "answer_value", "answer_aliases"])
|
| 107 |
+
.limit(5)
|
| 108 |
+
.to_list()
|
| 109 |
+
)
|
| 110 |
+
```
|
| 111 |
+
|
| 112 |
+
### LanceDB full-text search
|
| 113 |
+
|
| 114 |
+
```python
|
| 115 |
+
import lancedb
|
| 116 |
+
|
| 117 |
+
db = lancedb.connect("hf://datasets/lance-format/trivia-qa-lance/data")
|
| 118 |
+
tbl = db.open_table("train")
|
| 119 |
+
|
| 120 |
+
results = (
|
| 121 |
+
tbl.search("sistine chapel")
|
| 122 |
+
.select(["question", "answer_value"])
|
| 123 |
+
.limit(10)
|
| 124 |
+
.to_list()
|
| 125 |
+
)
|
| 126 |
+
```
|
| 127 |
+
|
| 128 |
## Filter by answer type
|
| 129 |
|
| 130 |
```python
|
|
|
|
| 132 |
wiki = ds.scanner(filter="answer_type = 'WikipediaEntity'", columns=["question"], limit=5).to_table()
|
| 133 |
```
|
| 134 |
|
| 135 |
+
### Filter with LanceDB
|
| 136 |
+
|
| 137 |
+
```python
|
| 138 |
+
import lancedb
|
| 139 |
+
|
| 140 |
+
db = lancedb.connect("hf://datasets/lance-format/trivia-qa-lance/data")
|
| 141 |
+
tbl = db.open_table("train")
|
| 142 |
+
wiki = (
|
| 143 |
+
tbl.search()
|
| 144 |
+
.where("answer_type = 'WikipediaEntity'")
|
| 145 |
+
.select(["question"])
|
| 146 |
+
.limit(5)
|
| 147 |
+
.to_list()
|
| 148 |
+
)
|
| 149 |
+
```
|
| 150 |
+
|
| 151 |
## Why Lance?
|
| 152 |
|
| 153 |
- One dataset carries questions + answers + aliases + embeddings + indices — no sidecar files.
|