mirror of
https://github.com/qdrant/landing_page.git
synced 2026-10-09 21:08: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:
+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