Update milvus_multitenancy.py (#25857)
This commit is contained in:
1 parent
819ad1d904
commit
d501e3d6b5
1 file changed
+33
-11
@@ -31,10 +31,15 @@ from pymilvus import (
|
|||||||
connections,
|
connections,
|
||||||
utility,
|
utility,
|
||||||
)
|
)
|
||||||
|
from pymilvus.exceptions import MilvusException
|
||||||
|
|
||||||
log = logging.getLogger(__name__)
|
log = logging.getLogger(__name__)
|
||||||
|
|
||||||
RESOURCE_ID_FIELD = 'resource_id'
|
RESOURCE_ID_FIELD = 'resource_id'
|
||||||
|
# Milvus VARCHAR hard cap for the `text` field (see _create_shared_collection).
|
||||||
|
# Chunks longer than this are truncated before insert so one oversized chunk
|
||||||
|
# can't fail the whole batch (and leave the file with zero embeddings).
|
||||||
|
MILVUS_TEXT_MAX_LENGTH = 65535
|
||||||
|
|
||||||
# Milvus expressions are SQL-like strings with no parameterized-query API;
|
# Milvus expressions are SQL-like strings with no parameterized-query API;
|
||||||
# values get interpolated into single-quoted literals. Reject anything that
|
# values get interpolated into single-quoted literals. Reject anything that
|
||||||
@@ -169,17 +174,34 @@ class MilvusClient(VectorDBBase):
|
|||||||
self._ensure_collection(mt_collection, dimension)
|
self._ensure_collection(mt_collection, dimension)
|
||||||
collection = Collection(mt_collection)
|
collection = Collection(mt_collection)
|
||||||
|
|
||||||
entities = [
|
entities = []
|
||||||
{
|
for item in items:
|
||||||
'id': item['id'],
|
text = item['text'] or ''
|
||||||
'vector': item['vector'],
|
if len(text) > MILVUS_TEXT_MAX_LENGTH:
|
||||||
'text': item['text'],
|
log.warning(
|
||||||
'metadata': item['metadata'],
|
f'Milvus: truncating text id={item["id"]} '
|
||||||
RESOURCE_ID_FIELD: resource_id,
|
f'{len(text)}->{MILVUS_TEXT_MAX_LENGTH} chars '
|
||||||
}
|
f'(collection={mt_collection}, resource_id={resource_id})'
|
||||||
for item in items
|
)
|
||||||
]
|
text = text[:MILVUS_TEXT_MAX_LENGTH]
|
||||||
collection.insert(entities)
|
entities.append(
|
||||||
|
{
|
||||||
|
'id': item['id'],
|
||||||
|
'vector': item['vector'],
|
||||||
|
'text': text,
|
||||||
|
'metadata': item['metadata'],
|
||||||
|
RESOURCE_ID_FIELD: resource_id,
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
try:
|
||||||
|
collection.insert(entities)
|
||||||
|
except MilvusException as e:
|
||||||
|
log.error(
|
||||||
|
f'Milvus insert failed (collection={mt_collection}, '
|
||||||
|
f'resource_id={resource_id}, items={len(entities)}): {e}'
|
||||||
|
)
|
||||||
|
raise
|
||||||
|
|
||||||
def search(
|
def search(
|
||||||
self,
|
self,
|
||||||
|
|||||||
Reference in new issue
Block a user