mirror of
https://github.com/qdrant/landing_page.git
synced 2026-10-06 11:28:31 +02:00
Test Edge code snippets (#2179)
* Add Edge and FastEmbed as Python dependencies * Make Edge code snippets testable
This commit is contained in:
+17
@@ -0,0 +1,17 @@
|
||||
```python
|
||||
from fastembed import ImageEmbedding, TextEmbedding
|
||||
|
||||
TEXT_MODEL_NAME='Qdrant/clip-ViT-B-32-text'
|
||||
VISION_MODEL_NAME='Qdrant/clip-ViT-B-32-vision'
|
||||
MODELS_DIR="./qdrant-edge-directory/models"
|
||||
|
||||
ImageEmbedding(
|
||||
model_name=VISION_MODEL_NAME,
|
||||
cache_dir=MODELS_DIR
|
||||
)
|
||||
|
||||
TextEmbedding(
|
||||
model_name=TEXT_MODEL_NAME,
|
||||
cache_dir=MODELS_DIR
|
||||
)
|
||||
```
|
||||
+22
@@ -0,0 +1,22 @@
|
||||
```python
|
||||
from pathlib import Path
|
||||
from qdrant_edge import Point, UpdateOperation
|
||||
import uuid
|
||||
|
||||
IMAGES_DIR = "images"
|
||||
|
||||
image_model = ImageEmbedding(
|
||||
model_name=VISION_MODEL_NAME,
|
||||
cache_dir=MODELS_DIR,
|
||||
local_files_only=True
|
||||
)
|
||||
|
||||
embeddings = list(image_model.embed([Path(IMAGES_DIR) / "temp.jpg"]))[0]
|
||||
|
||||
point = Point(
|
||||
id=str(uuid.uuid4()),
|
||||
vector={VECTOR_NAME: embeddings.tolist()}
|
||||
)
|
||||
|
||||
edge_shard.update(UpdateOperation.upsert_points([point]))
|
||||
```
|
||||
+25
@@ -0,0 +1,25 @@
|
||||
```python
|
||||
from pathlib import Path
|
||||
from qdrant_edge import (
|
||||
Distance,
|
||||
EdgeConfig,
|
||||
EdgeShard,
|
||||
VectorDataConfig,
|
||||
)
|
||||
|
||||
SHARD_DIRECTORY = "./qdrant-edge-directory"
|
||||
VECTOR_DIMENSION = 512
|
||||
VECTOR_NAME="my-vector"
|
||||
|
||||
Path(SHARD_DIRECTORY).mkdir(parents=True, exist_ok=True)
|
||||
config = EdgeConfig(
|
||||
vector_data={
|
||||
VECTOR_NAME: VectorDataConfig(
|
||||
size=VECTOR_DIMENSION,
|
||||
distance=Distance.Cosine,
|
||||
)
|
||||
}
|
||||
)
|
||||
|
||||
edge_shard = EdgeShard(SHARD_DIRECTORY, config)
|
||||
```
|
||||
+81
@@ -0,0 +1,81 @@
|
||||
```python
|
||||
from fastembed import ImageEmbedding, TextEmbedding
|
||||
|
||||
TEXT_MODEL_NAME='Qdrant/clip-ViT-B-32-text'
|
||||
VISION_MODEL_NAME='Qdrant/clip-ViT-B-32-vision'
|
||||
MODELS_DIR="./qdrant-edge-directory/models"
|
||||
|
||||
ImageEmbedding(
|
||||
model_name=VISION_MODEL_NAME,
|
||||
cache_dir=MODELS_DIR
|
||||
)
|
||||
|
||||
TextEmbedding(
|
||||
model_name=TEXT_MODEL_NAME,
|
||||
cache_dir=MODELS_DIR
|
||||
)
|
||||
|
||||
from pathlib import Path
|
||||
from qdrant_edge import (
|
||||
Distance,
|
||||
EdgeConfig,
|
||||
EdgeShard,
|
||||
VectorDataConfig,
|
||||
)
|
||||
|
||||
SHARD_DIRECTORY = "./qdrant-edge-directory"
|
||||
VECTOR_DIMENSION = 512
|
||||
VECTOR_NAME="my-vector"
|
||||
|
||||
Path(SHARD_DIRECTORY).mkdir(parents=True, exist_ok=True)
|
||||
config = EdgeConfig(
|
||||
vector_data={
|
||||
VECTOR_NAME: VectorDataConfig(
|
||||
size=VECTOR_DIMENSION,
|
||||
distance=Distance.Cosine,
|
||||
)
|
||||
}
|
||||
)
|
||||
|
||||
edge_shard = EdgeShard(SHARD_DIRECTORY, config)
|
||||
|
||||
from pathlib import Path
|
||||
from qdrant_edge import Point, UpdateOperation
|
||||
import uuid
|
||||
|
||||
IMAGES_DIR = "images"
|
||||
|
||||
image_model = ImageEmbedding(
|
||||
model_name=VISION_MODEL_NAME,
|
||||
cache_dir=MODELS_DIR,
|
||||
local_files_only=True
|
||||
)
|
||||
|
||||
embeddings = list(image_model.embed([Path(IMAGES_DIR) / "temp.jpg"]))[0]
|
||||
|
||||
point = Point(
|
||||
id=str(uuid.uuid4()),
|
||||
vector={VECTOR_NAME: embeddings.tolist()}
|
||||
)
|
||||
|
||||
edge_shard.update(UpdateOperation.upsert_points([point]))
|
||||
|
||||
from qdrant_edge import Query, QueryRequest
|
||||
|
||||
text_model = TextEmbedding(
|
||||
model_name=TEXT_MODEL_NAME,
|
||||
cache_dir=MODELS_DIR,
|
||||
local_files_only=True
|
||||
)
|
||||
|
||||
embeddings = list(text_model.embed(["<search terms>"]))[0]
|
||||
|
||||
results = edge_shard.query(
|
||||
QueryRequest(
|
||||
query=Query.Nearest(embeddings.tolist(),using=VECTOR_NAME),
|
||||
limit=10,
|
||||
with_vector=False,
|
||||
with_payload=True
|
||||
)
|
||||
)
|
||||
```
|
||||
+20
@@ -0,0 +1,20 @@
|
||||
```python
|
||||
from qdrant_edge import Query, QueryRequest
|
||||
|
||||
text_model = TextEmbedding(
|
||||
model_name=TEXT_MODEL_NAME,
|
||||
cache_dir=MODELS_DIR,
|
||||
local_files_only=True
|
||||
)
|
||||
|
||||
embeddings = list(text_model.embed(["<search terms>"]))[0]
|
||||
|
||||
results = edge_shard.query(
|
||||
QueryRequest(
|
||||
query=Query.Nearest(embeddings.tolist(),using=VECTOR_NAME),
|
||||
limit=10,
|
||||
with_vector=False,
|
||||
with_payload=True
|
||||
)
|
||||
)
|
||||
```
|
||||
@@ -0,0 +1,87 @@
|
||||
# @block-start download-models
|
||||
from fastembed import ImageEmbedding, TextEmbedding
|
||||
|
||||
TEXT_MODEL_NAME='Qdrant/clip-ViT-B-32-text'
|
||||
VISION_MODEL_NAME='Qdrant/clip-ViT-B-32-vision'
|
||||
MODELS_DIR="./qdrant-edge-directory/models"
|
||||
|
||||
ImageEmbedding(
|
||||
model_name=VISION_MODEL_NAME,
|
||||
cache_dir=MODELS_DIR
|
||||
)
|
||||
|
||||
TextEmbedding(
|
||||
model_name=TEXT_MODEL_NAME,
|
||||
cache_dir=MODELS_DIR
|
||||
)
|
||||
# @block-end download-models
|
||||
|
||||
# @block-start initialize-edge-shard
|
||||
from pathlib import Path
|
||||
from qdrant_edge import (
|
||||
Distance,
|
||||
EdgeConfig,
|
||||
EdgeShard,
|
||||
VectorDataConfig,
|
||||
)
|
||||
|
||||
SHARD_DIRECTORY = "./qdrant-edge-directory"
|
||||
VECTOR_DIMENSION = 512
|
||||
VECTOR_NAME="my-vector"
|
||||
|
||||
Path(SHARD_DIRECTORY).mkdir(parents=True, exist_ok=True)
|
||||
config = EdgeConfig(
|
||||
vector_data={
|
||||
VECTOR_NAME: VectorDataConfig(
|
||||
size=VECTOR_DIMENSION,
|
||||
distance=Distance.Cosine,
|
||||
)
|
||||
}
|
||||
)
|
||||
|
||||
edge_shard = EdgeShard(SHARD_DIRECTORY, config)
|
||||
# @block-end initialize-edge-shard
|
||||
|
||||
# @block-start embed-and-store-image
|
||||
from pathlib import Path
|
||||
from qdrant_edge import Point, UpdateOperation
|
||||
import uuid
|
||||
|
||||
IMAGES_DIR = "images"
|
||||
|
||||
image_model = ImageEmbedding(
|
||||
model_name=VISION_MODEL_NAME,
|
||||
cache_dir=MODELS_DIR,
|
||||
local_files_only=True
|
||||
)
|
||||
|
||||
embeddings = list(image_model.embed([Path(IMAGES_DIR) / "temp.jpg"]))[0]
|
||||
|
||||
point = Point(
|
||||
id=str(uuid.uuid4()),
|
||||
vector={VECTOR_NAME: embeddings.tolist()}
|
||||
)
|
||||
|
||||
edge_shard.update(UpdateOperation.upsert_points([point]))
|
||||
# @block-end embed-and-store-image
|
||||
|
||||
# @block-start query-with-text-embedding
|
||||
from qdrant_edge import Query, QueryRequest
|
||||
|
||||
text_model = TextEmbedding(
|
||||
model_name=TEXT_MODEL_NAME,
|
||||
cache_dir=MODELS_DIR,
|
||||
local_files_only=True
|
||||
)
|
||||
|
||||
embeddings = list(text_model.embed(["<search terms>"]))[0]
|
||||
|
||||
results = edge_shard.query(
|
||||
QueryRequest(
|
||||
query=Query.Nearest(embeddings.tolist(),using=VECTOR_NAME),
|
||||
limit=10,
|
||||
with_vector=False,
|
||||
with_payload=True
|
||||
)
|
||||
)
|
||||
# @block-end query-with-text-embedding
|
||||
+3
@@ -0,0 +1,3 @@
|
||||
```python
|
||||
edge_shard.close()
|
||||
```
|
||||
+19
@@ -0,0 +1,19 @@
|
||||
```python
|
||||
from qdrant_edge import (
|
||||
Distance,
|
||||
EdgeConfig,
|
||||
VectorDataConfig,
|
||||
)
|
||||
|
||||
VECTOR_NAME="my-vector"
|
||||
VECTOR_DIMENSION=4
|
||||
|
||||
config = EdgeConfig(
|
||||
vector_data={
|
||||
VECTOR_NAME: VectorDataConfig(
|
||||
size=VECTOR_DIMENSION,
|
||||
distance=Distance.Cosine,
|
||||
)
|
||||
}
|
||||
)
|
||||
```
|
||||
+7
@@ -0,0 +1,7 @@
|
||||
```python
|
||||
from pathlib import Path
|
||||
|
||||
SHARD_DIRECTORY = "./qdrant-edge-directory"
|
||||
|
||||
Path(SHARD_DIRECTORY).mkdir(parents=True, exist_ok=True)
|
||||
```
|
||||
+5
@@ -0,0 +1,5 @@
|
||||
```python
|
||||
from qdrant_edge import EdgeShard
|
||||
|
||||
edge_shard = EdgeShard(SHARD_DIRECTORY, config)
|
||||
```
|
||||
+3
@@ -0,0 +1,3 @@
|
||||
```python
|
||||
edge_shard = EdgeShard(SHARD_DIRECTORY)
|
||||
```
|
||||
+60
@@ -0,0 +1,60 @@
|
||||
```python
|
||||
from pathlib import Path
|
||||
|
||||
SHARD_DIRECTORY = "./qdrant-edge-directory"
|
||||
|
||||
Path(SHARD_DIRECTORY).mkdir(parents=True, exist_ok=True)
|
||||
|
||||
from qdrant_edge import (
|
||||
Distance,
|
||||
EdgeConfig,
|
||||
VectorDataConfig,
|
||||
)
|
||||
|
||||
VECTOR_NAME="my-vector"
|
||||
VECTOR_DIMENSION=4
|
||||
|
||||
config = EdgeConfig(
|
||||
vector_data={
|
||||
VECTOR_NAME: VectorDataConfig(
|
||||
size=VECTOR_DIMENSION,
|
||||
distance=Distance.Cosine,
|
||||
)
|
||||
}
|
||||
)
|
||||
|
||||
from qdrant_edge import EdgeShard
|
||||
|
||||
edge_shard = EdgeShard(SHARD_DIRECTORY, config)
|
||||
|
||||
from qdrant_edge import ( Point, UpdateOperation )
|
||||
|
||||
point = Point(
|
||||
id=1,
|
||||
vector={VECTOR_NAME: [0.1, 0.2, 0.3, 0.4]},
|
||||
payload={"color": "red"}
|
||||
)
|
||||
|
||||
edge_shard.update(UpdateOperation.upsert_points([point]))
|
||||
|
||||
point = edge_shard.retrieve(
|
||||
point_ids=[1],
|
||||
with_payload=True,
|
||||
with_vector=False
|
||||
)
|
||||
|
||||
from qdrant_edge import Query, QueryRequest
|
||||
|
||||
results = edge_shard.query(
|
||||
QueryRequest(
|
||||
query=Query.Nearest([0.2, 0.1, 0.9, 0.7], using=VECTOR_NAME),
|
||||
limit=10,
|
||||
with_vector=False,
|
||||
with_payload=True
|
||||
)
|
||||
)
|
||||
|
||||
edge_shard.close()
|
||||
|
||||
edge_shard = EdgeShard(SHARD_DIRECTORY)
|
||||
```
|
||||
+12
@@ -0,0 +1,12 @@
|
||||
```python
|
||||
from qdrant_edge import Query, QueryRequest
|
||||
|
||||
results = edge_shard.query(
|
||||
QueryRequest(
|
||||
query=Query.Nearest([0.2, 0.1, 0.9, 0.7], using=VECTOR_NAME),
|
||||
limit=10,
|
||||
with_vector=False,
|
||||
with_payload=True
|
||||
)
|
||||
)
|
||||
```
|
||||
+7
@@ -0,0 +1,7 @@
|
||||
```python
|
||||
point = edge_shard.retrieve(
|
||||
point_ids=[1],
|
||||
with_payload=True,
|
||||
with_vector=False
|
||||
)
|
||||
```
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
```python
|
||||
from qdrant_edge import ( Point, UpdateOperation )
|
||||
|
||||
point = Point(
|
||||
id=1,
|
||||
vector={VECTOR_NAME: [0.1, 0.2, 0.3, 0.4]},
|
||||
payload={"color": "red"}
|
||||
)
|
||||
|
||||
edge_shard.update(UpdateOperation.upsert_points([point]))
|
||||
```
|
||||
@@ -0,0 +1,74 @@
|
||||
# @block-start create-storage-directory
|
||||
from pathlib import Path
|
||||
|
||||
SHARD_DIRECTORY = "./qdrant-edge-directory"
|
||||
|
||||
Path(SHARD_DIRECTORY).mkdir(parents=True, exist_ok=True)
|
||||
# @block-end create-storage-directory
|
||||
|
||||
# @block-start configure-edge-shard
|
||||
from qdrant_edge import (
|
||||
Distance,
|
||||
EdgeConfig,
|
||||
VectorDataConfig,
|
||||
)
|
||||
|
||||
VECTOR_NAME="my-vector"
|
||||
VECTOR_DIMENSION=4
|
||||
|
||||
config = EdgeConfig(
|
||||
vector_data={
|
||||
VECTOR_NAME: VectorDataConfig(
|
||||
size=VECTOR_DIMENSION,
|
||||
distance=Distance.Cosine,
|
||||
)
|
||||
}
|
||||
)
|
||||
# @block-end configure-edge-shard
|
||||
|
||||
# @block-start initialize-edge-shard
|
||||
from qdrant_edge import EdgeShard
|
||||
|
||||
edge_shard = EdgeShard(SHARD_DIRECTORY, config)
|
||||
# @block-end initialize-edge-shard
|
||||
|
||||
# @block-start upsert-points
|
||||
from qdrant_edge import ( Point, UpdateOperation )
|
||||
|
||||
point = Point(
|
||||
id=1,
|
||||
vector={VECTOR_NAME: [0.1, 0.2, 0.3, 0.4]},
|
||||
payload={"color": "red"}
|
||||
)
|
||||
|
||||
edge_shard.update(UpdateOperation.upsert_points([point]))
|
||||
# @block-end upsert-points
|
||||
|
||||
# @block-start retrieve-point
|
||||
point = edge_shard.retrieve(
|
||||
point_ids=[1],
|
||||
with_payload=True,
|
||||
with_vector=False
|
||||
)
|
||||
# @block-end retrieve-point
|
||||
|
||||
# @block-start query-points
|
||||
from qdrant_edge import Query, QueryRequest
|
||||
|
||||
results = edge_shard.query(
|
||||
QueryRequest(
|
||||
query=Query.Nearest([0.2, 0.1, 0.9, 0.7], using=VECTOR_NAME),
|
||||
limit=10,
|
||||
with_vector=False,
|
||||
with_payload=True
|
||||
)
|
||||
)
|
||||
# @block-end query-points
|
||||
|
||||
# @block-start close-edge-shard
|
||||
edge_shard.close()
|
||||
# @block-end close-edge-shard
|
||||
|
||||
# @block-start load-edge-shard
|
||||
edge_shard = EdgeShard(SHARD_DIRECTORY)
|
||||
# @block-end load-edge-shard
|
||||
+17
@@ -0,0 +1,17 @@
|
||||
```python
|
||||
from qdrant_edge import (
|
||||
Filter,
|
||||
FieldCondition,
|
||||
RangeFloat
|
||||
)
|
||||
|
||||
mutable_shard.update(
|
||||
UpdateOperation.delete_points_by_filter(Filter(
|
||||
must=[
|
||||
FieldCondition(
|
||||
key=SYNC_TIMESTAMP_KEY, range=RangeFloat(lte=sync_timestamp)
|
||||
)
|
||||
])
|
||||
)
|
||||
)
|
||||
```
|
||||
+26
@@ -0,0 +1,26 @@
|
||||
```python
|
||||
from qdrant_edge import ( Point, UpdateOperation )
|
||||
from qdrant_client import models
|
||||
import time
|
||||
|
||||
SYNC_TIMESTAMP_KEY="timestamp"
|
||||
|
||||
id=2
|
||||
vector=[0.4, 0.3, 0.2, 0.1]
|
||||
payload={
|
||||
"color": "green",
|
||||
SYNC_TIMESTAMP_KEY: time.time()
|
||||
}
|
||||
|
||||
point = Point(
|
||||
id=id,
|
||||
vector={VECTOR_NAME: vector},
|
||||
payload=payload
|
||||
)
|
||||
|
||||
mutable_shard.update(UpdateOperation.upsert_points([point]))
|
||||
|
||||
rest_point = models.PointStruct(id=id, vector={VECTOR_NAME: vector}, payload=payload)
|
||||
|
||||
upload_queue.put(rest_point)
|
||||
```
|
||||
+29
@@ -0,0 +1,29 @@
|
||||
```python
|
||||
import requests
|
||||
import tempfile
|
||||
import shutil
|
||||
|
||||
COLLECTION_NAME="edge-collection"
|
||||
snapshot_url = f"{QDRANT_URL}/collections/{COLLECTION_NAME}/shards/0/snapshot"
|
||||
|
||||
IMMUTABLE_SHARD_DIR = "./qdrant-edge-directory/mutable"
|
||||
data_dir = Path(IMMUTABLE_SHARD_DIR)
|
||||
|
||||
with tempfile.TemporaryDirectory(dir=data_dir.parent) as restore_dir:
|
||||
snapshot_path = Path(restore_dir) / "shard.snapshot"
|
||||
|
||||
with requests.get(snapshot_url, headers={"api-key": QDRANT_API_KEY}, stream=True) as r:
|
||||
r.raise_for_status()
|
||||
with open(snapshot_path, "wb") as f:
|
||||
for chunk in r.iter_content(chunk_size=8192):
|
||||
f.write(chunk)
|
||||
|
||||
immutable_shard = None
|
||||
if data_dir.exists():
|
||||
shutil.rmtree(data_dir)
|
||||
data_dir.mkdir(parents=True, exist_ok=True)
|
||||
|
||||
EdgeShard.unpack_snapshot(str(snapshot_path), str(data_dir))
|
||||
|
||||
immutable_shard = EdgeShard(IMMUTABLE_SHARD_DIR)
|
||||
```
|
||||
+27
@@ -0,0 +1,27 @@
|
||||
```python
|
||||
from pathlib import Path
|
||||
from qdrant_edge import (
|
||||
Distance,
|
||||
EdgeConfig,
|
||||
EdgeShard,
|
||||
VectorDataConfig,
|
||||
)
|
||||
|
||||
MUTABLE_SHARD_DIR = "./qdrant-edge-directory/mutable"
|
||||
|
||||
Path(MUTABLE_SHARD_DIR).mkdir(parents=True, exist_ok=True)
|
||||
|
||||
VECTOR_NAME="my-vector"
|
||||
VECTOR_DIMENSION=4
|
||||
|
||||
config = EdgeConfig(
|
||||
vector_data={
|
||||
VECTOR_NAME: VectorDataConfig(
|
||||
size=VECTOR_DIMENSION,
|
||||
distance=Distance.Cosine,
|
||||
)
|
||||
}
|
||||
)
|
||||
|
||||
mutable_shard = EdgeShard(MUTABLE_SHARD_DIR, config)
|
||||
```
|
||||
+146
@@ -0,0 +1,146 @@
|
||||
```python
|
||||
from pathlib import Path
|
||||
from qdrant_edge import (
|
||||
Distance,
|
||||
EdgeConfig,
|
||||
EdgeShard,
|
||||
VectorDataConfig,
|
||||
)
|
||||
|
||||
MUTABLE_SHARD_DIR = "./qdrant-edge-directory/mutable"
|
||||
|
||||
Path(MUTABLE_SHARD_DIR).mkdir(parents=True, exist_ok=True)
|
||||
|
||||
VECTOR_NAME="my-vector"
|
||||
VECTOR_DIMENSION=4
|
||||
|
||||
config = EdgeConfig(
|
||||
vector_data={
|
||||
VECTOR_NAME: VectorDataConfig(
|
||||
size=VECTOR_DIMENSION,
|
||||
distance=Distance.Cosine,
|
||||
)
|
||||
}
|
||||
)
|
||||
|
||||
mutable_shard = EdgeShard(MUTABLE_SHARD_DIR, config)
|
||||
|
||||
import requests
|
||||
import tempfile
|
||||
import shutil
|
||||
|
||||
COLLECTION_NAME="edge-collection"
|
||||
snapshot_url = f"{QDRANT_URL}/collections/{COLLECTION_NAME}/shards/0/snapshot"
|
||||
|
||||
IMMUTABLE_SHARD_DIR = "./qdrant-edge-directory/mutable"
|
||||
data_dir = Path(IMMUTABLE_SHARD_DIR)
|
||||
|
||||
with tempfile.TemporaryDirectory(dir=data_dir.parent) as restore_dir:
|
||||
snapshot_path = Path(restore_dir) / "shard.snapshot"
|
||||
|
||||
with requests.get(snapshot_url, headers={"api-key": QDRANT_API_KEY}, stream=True) as r:
|
||||
r.raise_for_status()
|
||||
with open(snapshot_path, "wb") as f:
|
||||
for chunk in r.iter_content(chunk_size=8192):
|
||||
f.write(chunk)
|
||||
|
||||
immutable_shard = None
|
||||
if data_dir.exists():
|
||||
shutil.rmtree(data_dir)
|
||||
data_dir.mkdir(parents=True, exist_ok=True)
|
||||
|
||||
EdgeShard.unpack_snapshot(str(snapshot_path), str(data_dir))
|
||||
|
||||
immutable_shard = EdgeShard(IMMUTABLE_SHARD_DIR)
|
||||
|
||||
from qdrant_edge import ( Point, UpdateOperation )
|
||||
from qdrant_client import models
|
||||
import time
|
||||
|
||||
SYNC_TIMESTAMP_KEY="timestamp"
|
||||
|
||||
id=2
|
||||
vector=[0.4, 0.3, 0.2, 0.1]
|
||||
payload={
|
||||
"color": "green",
|
||||
SYNC_TIMESTAMP_KEY: time.time()
|
||||
}
|
||||
|
||||
point = Point(
|
||||
id=id,
|
||||
vector={VECTOR_NAME: vector},
|
||||
payload=payload
|
||||
)
|
||||
|
||||
mutable_shard.update(UpdateOperation.upsert_points([point]))
|
||||
|
||||
rest_point = models.PointStruct(id=id, vector={VECTOR_NAME: vector}, payload=payload)
|
||||
|
||||
upload_queue.put(rest_point)
|
||||
|
||||
import time
|
||||
|
||||
manifest = immutable_shard.snapshot_manifest()
|
||||
|
||||
url = f"{QDRANT_URL}/collections/{COLLECTION_NAME}/shards/0/snapshot/partial/create"
|
||||
|
||||
sync_timestamp = time.time()
|
||||
|
||||
with tempfile.TemporaryDirectory(dir=data_dir) as temp_dir:
|
||||
partial_snapshot_path = Path(temp_dir) / "partial.snapshot"
|
||||
response = requests.post(url, headers={"api-key": QDRANT_API_KEY}, json=manifest, stream=True)
|
||||
response.raise_for_status()
|
||||
|
||||
with open(partial_snapshot_path, "wb") as f:
|
||||
for chunk in response.iter_content(chunk_size=8192):
|
||||
f.write(chunk)
|
||||
|
||||
immutable_shard.update_from_snapshot(str(partial_snapshot_path))
|
||||
|
||||
from qdrant_edge import (
|
||||
Filter,
|
||||
FieldCondition,
|
||||
RangeFloat
|
||||
)
|
||||
|
||||
mutable_shard.update(
|
||||
UpdateOperation.delete_points_by_filter(Filter(
|
||||
must=[
|
||||
FieldCondition(
|
||||
key=SYNC_TIMESTAMP_KEY, range=RangeFloat(lte=sync_timestamp)
|
||||
)
|
||||
])
|
||||
)
|
||||
)
|
||||
|
||||
from qdrant_edge import Query, QueryRequest
|
||||
|
||||
query_request = QueryRequest(
|
||||
query=Query.Nearest([0.2, 0.1, 0.9, 0.7], using=VECTOR_NAME),
|
||||
limit=10,
|
||||
with_vector=False,
|
||||
with_payload=True
|
||||
)
|
||||
|
||||
mutable_results = mutable_shard.query(query_request)
|
||||
immutable_results = immutable_shard.query(query_request)
|
||||
|
||||
all_results = list(mutable_results) + list(immutable_results)
|
||||
all_results.sort(key=lambda x: x.score, reverse=True)
|
||||
|
||||
seen_ids = set()
|
||||
unique_results = []
|
||||
for result in all_results:
|
||||
if result.id not in seen_ids:
|
||||
seen_ids.add(result.id)
|
||||
unique_results.append(result)
|
||||
|
||||
results= [
|
||||
{
|
||||
"id": result.id,
|
||||
"score": result.score,
|
||||
"payload": result.payload
|
||||
}
|
||||
for result in unique_results[:10]
|
||||
]
|
||||
```
|
||||
+32
@@ -0,0 +1,32 @@
|
||||
```python
|
||||
from qdrant_edge import Query, QueryRequest
|
||||
|
||||
query_request = QueryRequest(
|
||||
query=Query.Nearest([0.2, 0.1, 0.9, 0.7], using=VECTOR_NAME),
|
||||
limit=10,
|
||||
with_vector=False,
|
||||
with_payload=True
|
||||
)
|
||||
|
||||
mutable_results = mutable_shard.query(query_request)
|
||||
immutable_results = immutable_shard.query(query_request)
|
||||
|
||||
all_results = list(mutable_results) + list(immutable_results)
|
||||
all_results.sort(key=lambda x: x.score, reverse=True)
|
||||
|
||||
seen_ids = set()
|
||||
unique_results = []
|
||||
for result in all_results:
|
||||
if result.id not in seen_ids:
|
||||
seen_ids.add(result.id)
|
||||
unique_results.append(result)
|
||||
|
||||
results= [
|
||||
{
|
||||
"id": result.id,
|
||||
"score": result.score,
|
||||
"payload": result.payload
|
||||
}
|
||||
for result in unique_results[:10]
|
||||
]
|
||||
```
|
||||
+20
@@ -0,0 +1,20 @@
|
||||
```python
|
||||
import time
|
||||
|
||||
manifest = immutable_shard.snapshot_manifest()
|
||||
|
||||
url = f"{QDRANT_URL}/collections/{COLLECTION_NAME}/shards/0/snapshot/partial/create"
|
||||
|
||||
sync_timestamp = time.time()
|
||||
|
||||
with tempfile.TemporaryDirectory(dir=data_dir) as temp_dir:
|
||||
partial_snapshot_path = Path(temp_dir) / "partial.snapshot"
|
||||
response = requests.post(url, headers={"api-key": QDRANT_API_KEY}, json=manifest, stream=True)
|
||||
response.raise_for_status()
|
||||
|
||||
with open(partial_snapshot_path, "wb") as f:
|
||||
for chunk in response.iter_content(chunk_size=8192):
|
||||
f.write(chunk)
|
||||
|
||||
immutable_shard.update_from_snapshot(str(partial_snapshot_path))
|
||||
```
|
||||
+165
@@ -0,0 +1,165 @@
|
||||
# @hide-start
|
||||
# mypy: disable-error-code="import-untyped"
|
||||
from queue import Queue
|
||||
from qdrant_client import models
|
||||
QDRANT_URL=""
|
||||
QDRANT_API_KEY=""
|
||||
upload_queue: Queue[models.PointStruct] = Queue()
|
||||
# @hide-end
|
||||
|
||||
# @block-start initialize-mutable-shard
|
||||
from pathlib import Path
|
||||
from qdrant_edge import (
|
||||
Distance,
|
||||
EdgeConfig,
|
||||
EdgeShard,
|
||||
VectorDataConfig,
|
||||
)
|
||||
|
||||
MUTABLE_SHARD_DIR = "./qdrant-edge-directory/mutable"
|
||||
|
||||
Path(MUTABLE_SHARD_DIR).mkdir(parents=True, exist_ok=True)
|
||||
|
||||
VECTOR_NAME="my-vector"
|
||||
VECTOR_DIMENSION=4
|
||||
|
||||
config = EdgeConfig(
|
||||
vector_data={
|
||||
VECTOR_NAME: VectorDataConfig(
|
||||
size=VECTOR_DIMENSION,
|
||||
distance=Distance.Cosine,
|
||||
)
|
||||
}
|
||||
)
|
||||
|
||||
mutable_shard = EdgeShard(MUTABLE_SHARD_DIR, config)
|
||||
# @block-end initialize-mutable-shard
|
||||
|
||||
# @block-start initialize-immutable-shard
|
||||
import requests
|
||||
import tempfile
|
||||
import shutil
|
||||
|
||||
COLLECTION_NAME="edge-collection"
|
||||
snapshot_url = f"{QDRANT_URL}/collections/{COLLECTION_NAME}/shards/0/snapshot"
|
||||
|
||||
IMMUTABLE_SHARD_DIR = "./qdrant-edge-directory/mutable"
|
||||
data_dir = Path(IMMUTABLE_SHARD_DIR)
|
||||
|
||||
with tempfile.TemporaryDirectory(dir=data_dir.parent) as restore_dir:
|
||||
snapshot_path = Path(restore_dir) / "shard.snapshot"
|
||||
|
||||
with requests.get(snapshot_url, headers={"api-key": QDRANT_API_KEY}, stream=True) as r:
|
||||
r.raise_for_status()
|
||||
with open(snapshot_path, "wb") as f:
|
||||
for chunk in r.iter_content(chunk_size=8192):
|
||||
f.write(chunk)
|
||||
|
||||
immutable_shard = None
|
||||
if data_dir.exists():
|
||||
shutil.rmtree(data_dir)
|
||||
data_dir.mkdir(parents=True, exist_ok=True)
|
||||
|
||||
EdgeShard.unpack_snapshot(str(snapshot_path), str(data_dir))
|
||||
|
||||
immutable_shard = EdgeShard(IMMUTABLE_SHARD_DIR)
|
||||
# @block-end initialize-immutable-shard
|
||||
|
||||
# @block-start dual-write
|
||||
from qdrant_edge import ( Point, UpdateOperation )
|
||||
from qdrant_client import models
|
||||
import time
|
||||
|
||||
SYNC_TIMESTAMP_KEY="timestamp"
|
||||
|
||||
id=2
|
||||
vector=[0.4, 0.3, 0.2, 0.1]
|
||||
payload={
|
||||
"color": "green",
|
||||
SYNC_TIMESTAMP_KEY: time.time()
|
||||
}
|
||||
|
||||
point = Point(
|
||||
id=id,
|
||||
vector={VECTOR_NAME: vector},
|
||||
payload=payload
|
||||
)
|
||||
|
||||
mutable_shard.update(UpdateOperation.upsert_points([point]))
|
||||
|
||||
rest_point = models.PointStruct(id=id, vector={VECTOR_NAME: vector}, payload=payload)
|
||||
|
||||
upload_queue.put(rest_point)
|
||||
# @block-end dual-write
|
||||
|
||||
# @block-start update-immutable-shard
|
||||
import time
|
||||
|
||||
manifest = immutable_shard.snapshot_manifest()
|
||||
|
||||
url = f"{QDRANT_URL}/collections/{COLLECTION_NAME}/shards/0/snapshot/partial/create"
|
||||
|
||||
sync_timestamp = time.time()
|
||||
|
||||
with tempfile.TemporaryDirectory(dir=data_dir) as temp_dir:
|
||||
partial_snapshot_path = Path(temp_dir) / "partial.snapshot"
|
||||
response = requests.post(url, headers={"api-key": QDRANT_API_KEY}, json=manifest, stream=True)
|
||||
response.raise_for_status()
|
||||
|
||||
with open(partial_snapshot_path, "wb") as f:
|
||||
for chunk in response.iter_content(chunk_size=8192):
|
||||
f.write(chunk)
|
||||
|
||||
immutable_shard.update_from_snapshot(str(partial_snapshot_path))
|
||||
# @block-end update-immutable-shard
|
||||
|
||||
# @block-start delete-synced-points
|
||||
from qdrant_edge import (
|
||||
Filter,
|
||||
FieldCondition,
|
||||
RangeFloat
|
||||
)
|
||||
|
||||
mutable_shard.update(
|
||||
UpdateOperation.delete_points_by_filter(Filter(
|
||||
must=[
|
||||
FieldCondition(
|
||||
key=SYNC_TIMESTAMP_KEY, range=RangeFloat(lte=sync_timestamp)
|
||||
)
|
||||
])
|
||||
)
|
||||
)
|
||||
# @block-end delete-synced-points
|
||||
|
||||
# @block-start query-both-shards
|
||||
from qdrant_edge import Query, QueryRequest
|
||||
|
||||
query_request = QueryRequest(
|
||||
query=Query.Nearest([0.2, 0.1, 0.9, 0.7], using=VECTOR_NAME),
|
||||
limit=10,
|
||||
with_vector=False,
|
||||
with_payload=True
|
||||
)
|
||||
|
||||
mutable_results = mutable_shard.query(query_request)
|
||||
immutable_results = immutable_shard.query(query_request)
|
||||
|
||||
all_results = list(mutable_results) + list(immutable_results)
|
||||
all_results.sort(key=lambda x: x.score, reverse=True)
|
||||
|
||||
seen_ids = set()
|
||||
unique_results = []
|
||||
for result in all_results:
|
||||
if result.id not in seen_ids:
|
||||
seen_ids.add(result.id)
|
||||
unique_results.append(result)
|
||||
|
||||
results= [
|
||||
{
|
||||
"id": result.id,
|
||||
"score": result.score,
|
||||
"payload": result.payload
|
||||
}
|
||||
for result in unique_results[:10]
|
||||
]
|
||||
# @block-end query-both-shards
|
||||
+7
@@ -0,0 +1,7 @@
|
||||
```python
|
||||
from queue import Empty, Queue
|
||||
|
||||
# This is in-memory queue
|
||||
# For production use cases consider persisting changes
|
||||
upload_queue: Queue[models.PointStruct] = Queue()
|
||||
```
|
||||
+24
@@ -0,0 +1,24 @@
|
||||
```python
|
||||
from pathlib import Path
|
||||
from qdrant_edge import (
|
||||
Distance,
|
||||
EdgeConfig,
|
||||
VectorDataConfig,
|
||||
)
|
||||
|
||||
SHARD_DIRECTORY = "./qdrant-edge-directory"
|
||||
VECTOR_NAME="my-vector"
|
||||
VECTOR_DIMENSION=4
|
||||
|
||||
Path(SHARD_DIRECTORY).mkdir(parents=True, exist_ok=True)
|
||||
config = EdgeConfig(
|
||||
vector_data={
|
||||
VECTOR_NAME: VectorDataConfig(
|
||||
size=VECTOR_DIMENSION,
|
||||
distance=Distance.Cosine,
|
||||
)
|
||||
}
|
||||
)
|
||||
|
||||
edge_shard = EdgeShard(SHARD_DIRECTORY, config)
|
||||
```
|
||||
+14
@@ -0,0 +1,14 @@
|
||||
```python
|
||||
from qdrant_client import QdrantClient, models
|
||||
|
||||
server_client = QdrantClient(url=QDRANT_URL, api_key=QDRANT_API_KEY)
|
||||
|
||||
COLLECTION_NAME="edge-collection"
|
||||
|
||||
if not server_client.collection_exists(collection_name=COLLECTION_NAME):
|
||||
|
||||
server_client.create_collection(
|
||||
collection_name=COLLECTION_NAME,
|
||||
vectors_config={VECTOR_NAME: models.VectorParams(size=VECTOR_DIMENSION, distance=models.Distance.COSINE)}
|
||||
)
|
||||
```
|
||||
+15
@@ -0,0 +1,15 @@
|
||||
```python
|
||||
BATCH_SIZE = 10
|
||||
points_to_upload: list[models.PointStruct] = []
|
||||
|
||||
while len(points_to_upload) < BATCH_SIZE:
|
||||
try:
|
||||
points_to_upload.append(upload_queue.get_nowait())
|
||||
except Empty:
|
||||
break
|
||||
|
||||
if points_to_upload:
|
||||
server_client.upsert(
|
||||
collection_name=COLLECTION_NAME, points=points_to_upload
|
||||
)
|
||||
```
|
||||
+121
@@ -0,0 +1,121 @@
|
||||
```python
|
||||
COLLECTION_NAME="edge-collection"
|
||||
|
||||
snapshot_url = f"{QDRANT_URL}/collections/{COLLECTION_NAME}/shards/0/snapshot"
|
||||
|
||||
from pathlib import Path
|
||||
from qdrant_edge import EdgeShard
|
||||
import requests
|
||||
import shutil
|
||||
import tempfile
|
||||
|
||||
SHARD_DIRECTORY = "./qdrant-edge-directory"
|
||||
data_dir = Path(SHARD_DIRECTORY)
|
||||
|
||||
with tempfile.TemporaryDirectory(dir=data_dir.parent) as restore_dir:
|
||||
snapshot_path = Path(restore_dir) / "shard.snapshot"
|
||||
|
||||
with requests.get(snapshot_url, headers={"api-key": QDRANT_API_KEY}, stream=True) as r:
|
||||
r.raise_for_status()
|
||||
with open(snapshot_path, "wb") as f:
|
||||
for chunk in r.iter_content(chunk_size=8192):
|
||||
f.write(chunk)
|
||||
|
||||
if data_dir.exists():
|
||||
shutil.rmtree(data_dir)
|
||||
data_dir.mkdir(parents=True, exist_ok=True)
|
||||
|
||||
EdgeShard.unpack_snapshot(str(snapshot_path), str(data_dir))
|
||||
|
||||
edge_shard = EdgeShard(SHARD_DIRECTORY)
|
||||
|
||||
manifest = edge_shard.snapshot_manifest()
|
||||
|
||||
url = f"{QDRANT_URL}/collections/{COLLECTION_NAME}/shards/0/snapshot/partial/create"
|
||||
|
||||
with tempfile.TemporaryDirectory(dir=data_dir) as temp_dir:
|
||||
partial_snapshot_path = Path(temp_dir) / "partial.snapshot"
|
||||
response = requests.post(url, headers={"api-key": QDRANT_API_KEY}, json=manifest, stream=True)
|
||||
response.raise_for_status()
|
||||
|
||||
with open(partial_snapshot_path, "wb") as f:
|
||||
for chunk in response.iter_content(chunk_size=8192):
|
||||
f.write(chunk)
|
||||
|
||||
edge_shard.update_from_snapshot(str(partial_snapshot_path))
|
||||
|
||||
from pathlib import Path
|
||||
from qdrant_edge import (
|
||||
Distance,
|
||||
EdgeConfig,
|
||||
VectorDataConfig,
|
||||
)
|
||||
|
||||
SHARD_DIRECTORY = "./qdrant-edge-directory"
|
||||
VECTOR_NAME="my-vector"
|
||||
VECTOR_DIMENSION=4
|
||||
|
||||
Path(SHARD_DIRECTORY).mkdir(parents=True, exist_ok=True)
|
||||
config = EdgeConfig(
|
||||
vector_data={
|
||||
VECTOR_NAME: VectorDataConfig(
|
||||
size=VECTOR_DIMENSION,
|
||||
distance=Distance.Cosine,
|
||||
)
|
||||
}
|
||||
)
|
||||
|
||||
edge_shard = EdgeShard(SHARD_DIRECTORY, config)
|
||||
|
||||
from qdrant_client import QdrantClient, models
|
||||
|
||||
server_client = QdrantClient(url=QDRANT_URL, api_key=QDRANT_API_KEY)
|
||||
|
||||
COLLECTION_NAME="edge-collection"
|
||||
|
||||
if not server_client.collection_exists(collection_name=COLLECTION_NAME):
|
||||
|
||||
server_client.create_collection(
|
||||
collection_name=COLLECTION_NAME,
|
||||
vectors_config={VECTOR_NAME: models.VectorParams(size=VECTOR_DIMENSION, distance=models.Distance.COSINE)}
|
||||
)
|
||||
|
||||
from queue import Empty, Queue
|
||||
|
||||
# This is in-memory queue
|
||||
# For production use cases consider persisting changes
|
||||
upload_queue: Queue[models.PointStruct] = Queue()
|
||||
|
||||
from qdrant_edge import ( Point, UpdateOperation )
|
||||
from qdrant_client import models
|
||||
|
||||
id=1
|
||||
vector=[0.1, 0.2, 0.3, 0.4]
|
||||
payload={"color": "red"}
|
||||
|
||||
point = Point(
|
||||
id=id,
|
||||
vector={VECTOR_NAME: vector},
|
||||
payload=payload
|
||||
)
|
||||
|
||||
edge_shard.update(UpdateOperation.upsert_points([point]))
|
||||
|
||||
rest_point = models.PointStruct(id=id, vector={VECTOR_NAME: vector}, payload=payload)
|
||||
|
||||
upload_queue.put(rest_point)
|
||||
|
||||
BATCH_SIZE = 10
|
||||
points_to_upload: list[models.PointStruct] = []
|
||||
|
||||
while len(points_to_upload) < BATCH_SIZE:
|
||||
try:
|
||||
points_to_upload.append(upload_queue.get_nowait())
|
||||
except Empty:
|
||||
break
|
||||
|
||||
if points_to_upload:
|
||||
server_client.upsert(
|
||||
collection_name=COLLECTION_NAME, points=points_to_upload
|
||||
)
|
||||
```
|
||||
+27
@@ -0,0 +1,27 @@
|
||||
```python
|
||||
from pathlib import Path
|
||||
from qdrant_edge import EdgeShard
|
||||
import requests
|
||||
import shutil
|
||||
import tempfile
|
||||
|
||||
SHARD_DIRECTORY = "./qdrant-edge-directory"
|
||||
data_dir = Path(SHARD_DIRECTORY)
|
||||
|
||||
with tempfile.TemporaryDirectory(dir=data_dir.parent) as restore_dir:
|
||||
snapshot_path = Path(restore_dir) / "shard.snapshot"
|
||||
|
||||
with requests.get(snapshot_url, headers={"api-key": QDRANT_API_KEY}, stream=True) as r:
|
||||
r.raise_for_status()
|
||||
with open(snapshot_path, "wb") as f:
|
||||
for chunk in r.iter_content(chunk_size=8192):
|
||||
f.write(chunk)
|
||||
|
||||
if data_dir.exists():
|
||||
shutil.rmtree(data_dir)
|
||||
data_dir.mkdir(parents=True, exist_ok=True)
|
||||
|
||||
EdgeShard.unpack_snapshot(str(snapshot_path), str(data_dir))
|
||||
|
||||
edge_shard = EdgeShard(SHARD_DIRECTORY)
|
||||
```
|
||||
+5
@@ -0,0 +1,5 @@
|
||||
```python
|
||||
COLLECTION_NAME="edge-collection"
|
||||
|
||||
snapshot_url = f"{QDRANT_URL}/collections/{COLLECTION_NAME}/shards/0/snapshot"
|
||||
```
|
||||
+16
@@ -0,0 +1,16 @@
|
||||
```python
|
||||
manifest = edge_shard.snapshot_manifest()
|
||||
|
||||
url = f"{QDRANT_URL}/collections/{COLLECTION_NAME}/shards/0/snapshot/partial/create"
|
||||
|
||||
with tempfile.TemporaryDirectory(dir=data_dir) as temp_dir:
|
||||
partial_snapshot_path = Path(temp_dir) / "partial.snapshot"
|
||||
response = requests.post(url, headers={"api-key": QDRANT_API_KEY}, json=manifest, stream=True)
|
||||
response.raise_for_status()
|
||||
|
||||
with open(partial_snapshot_path, "wb") as f:
|
||||
for chunk in response.iter_content(chunk_size=8192):
|
||||
f.write(chunk)
|
||||
|
||||
edge_shard.update_from_snapshot(str(partial_snapshot_path))
|
||||
```
|
||||
+20
@@ -0,0 +1,20 @@
|
||||
```python
|
||||
from qdrant_edge import ( Point, UpdateOperation )
|
||||
from qdrant_client import models
|
||||
|
||||
id=1
|
||||
vector=[0.1, 0.2, 0.3, 0.4]
|
||||
payload={"color": "red"}
|
||||
|
||||
point = Point(
|
||||
id=id,
|
||||
vector={VECTOR_NAME: vector},
|
||||
payload=payload
|
||||
)
|
||||
|
||||
edge_shard.update(UpdateOperation.upsert_points([point]))
|
||||
|
||||
rest_point = models.PointStruct(id=id, vector={VECTOR_NAME: vector}, payload=payload)
|
||||
|
||||
upload_queue.put(rest_point)
|
||||
```
|
||||
+141
@@ -0,0 +1,141 @@
|
||||
# @hide-start
|
||||
# mypy: disable-error-code="import-untyped"
|
||||
QDRANT_URL=""
|
||||
QDRANT_API_KEY=""
|
||||
# @hide-end
|
||||
|
||||
# @block-start snapshot-url
|
||||
COLLECTION_NAME="edge-collection"
|
||||
|
||||
snapshot_url = f"{QDRANT_URL}/collections/{COLLECTION_NAME}/shards/0/snapshot"
|
||||
# @block-end snapshot-url
|
||||
|
||||
# @block-start restore-snapshot
|
||||
from pathlib import Path
|
||||
from qdrant_edge import EdgeShard
|
||||
import requests
|
||||
import shutil
|
||||
import tempfile
|
||||
|
||||
SHARD_DIRECTORY = "./qdrant-edge-directory"
|
||||
data_dir = Path(SHARD_DIRECTORY)
|
||||
|
||||
with tempfile.TemporaryDirectory(dir=data_dir.parent) as restore_dir:
|
||||
snapshot_path = Path(restore_dir) / "shard.snapshot"
|
||||
|
||||
with requests.get(snapshot_url, headers={"api-key": QDRANT_API_KEY}, stream=True) as r:
|
||||
r.raise_for_status()
|
||||
with open(snapshot_path, "wb") as f:
|
||||
for chunk in r.iter_content(chunk_size=8192):
|
||||
f.write(chunk)
|
||||
|
||||
if data_dir.exists():
|
||||
shutil.rmtree(data_dir)
|
||||
data_dir.mkdir(parents=True, exist_ok=True)
|
||||
|
||||
EdgeShard.unpack_snapshot(str(snapshot_path), str(data_dir))
|
||||
|
||||
edge_shard = EdgeShard(SHARD_DIRECTORY)
|
||||
# @block-end restore-snapshot
|
||||
|
||||
# @block-start update-from-snapshot
|
||||
manifest = edge_shard.snapshot_manifest()
|
||||
|
||||
url = f"{QDRANT_URL}/collections/{COLLECTION_NAME}/shards/0/snapshot/partial/create"
|
||||
|
||||
with tempfile.TemporaryDirectory(dir=data_dir) as temp_dir:
|
||||
partial_snapshot_path = Path(temp_dir) / "partial.snapshot"
|
||||
response = requests.post(url, headers={"api-key": QDRANT_API_KEY}, json=manifest, stream=True)
|
||||
response.raise_for_status()
|
||||
|
||||
with open(partial_snapshot_path, "wb") as f:
|
||||
for chunk in response.iter_content(chunk_size=8192):
|
||||
f.write(chunk)
|
||||
|
||||
edge_shard.update_from_snapshot(str(partial_snapshot_path))
|
||||
# @block-end update-from-snapshot
|
||||
|
||||
# @block-start initialize-edge-shard
|
||||
from pathlib import Path
|
||||
from qdrant_edge import (
|
||||
Distance,
|
||||
EdgeConfig,
|
||||
VectorDataConfig,
|
||||
)
|
||||
|
||||
SHARD_DIRECTORY = "./qdrant-edge-directory"
|
||||
VECTOR_NAME="my-vector"
|
||||
VECTOR_DIMENSION=4
|
||||
|
||||
Path(SHARD_DIRECTORY).mkdir(parents=True, exist_ok=True)
|
||||
config = EdgeConfig(
|
||||
vector_data={
|
||||
VECTOR_NAME: VectorDataConfig(
|
||||
size=VECTOR_DIMENSION,
|
||||
distance=Distance.Cosine,
|
||||
)
|
||||
}
|
||||
)
|
||||
|
||||
edge_shard = EdgeShard(SHARD_DIRECTORY, config)
|
||||
# @block-end initialize-edge-shard
|
||||
|
||||
# @block-start initialize-server-client
|
||||
from qdrant_client import QdrantClient, models
|
||||
|
||||
server_client = QdrantClient(url=QDRANT_URL, api_key=QDRANT_API_KEY)
|
||||
|
||||
COLLECTION_NAME="edge-collection"
|
||||
|
||||
if not server_client.collection_exists(collection_name=COLLECTION_NAME):
|
||||
|
||||
server_client.create_collection(
|
||||
collection_name=COLLECTION_NAME,
|
||||
vectors_config={VECTOR_NAME: models.VectorParams(size=VECTOR_DIMENSION, distance=models.Distance.COSINE)}
|
||||
)
|
||||
# @block-end initialize-server-client
|
||||
|
||||
# @block-start create-upload-queue
|
||||
from queue import Empty, Queue
|
||||
|
||||
# This is in-memory queue
|
||||
# For production use cases consider persisting changes
|
||||
upload_queue: Queue[models.PointStruct] = Queue()
|
||||
# @block-end create-upload-queue
|
||||
|
||||
# @block-start upsert-point
|
||||
from qdrant_edge import ( Point, UpdateOperation )
|
||||
from qdrant_client import models
|
||||
|
||||
id=1
|
||||
vector=[0.1, 0.2, 0.3, 0.4]
|
||||
payload={"color": "red"}
|
||||
|
||||
point = Point(
|
||||
id=id,
|
||||
vector={VECTOR_NAME: vector},
|
||||
payload=payload
|
||||
)
|
||||
|
||||
edge_shard.update(UpdateOperation.upsert_points([point]))
|
||||
|
||||
rest_point = models.PointStruct(id=id, vector={VECTOR_NAME: vector}, payload=payload)
|
||||
|
||||
upload_queue.put(rest_point)
|
||||
# @block-end upsert-point
|
||||
|
||||
# @block-start process-upload-queue
|
||||
BATCH_SIZE = 10
|
||||
points_to_upload: list[models.PointStruct] = []
|
||||
|
||||
while len(points_to_upload) < BATCH_SIZE:
|
||||
try:
|
||||
points_to_upload.append(upload_queue.get_nowait())
|
||||
except Empty:
|
||||
break
|
||||
|
||||
if points_to_upload:
|
||||
server_client.upsert(
|
||||
collection_name=COLLECTION_NAME, points=points_to_upload
|
||||
)
|
||||
# @block-end process-upload-queue
|
||||
Reference in New Issue
Block a user