2024-07-10 23:20:06 +01:00
|
|
|
|
|
|
|
|
"""
|
2024-07-12 15:12:40 +01:00
|
|
|
Accepts entity/vector pairs and writes them to a Milvus store.
|
2024-07-10 23:20:06 +01:00
|
|
|
"""
|
|
|
|
|
|
2025-09-18 15:57:52 +01:00
|
|
|
import logging
|
|
|
|
|
|
2024-08-26 23:45:23 +01:00
|
|
|
from .... direct.milvus_graph_embeddings import EntityVectors
|
2025-12-05 21:45:30 +00:00
|
|
|
from .... base import GraphEmbeddingsStoreService, CollectionConfigHandler
|
2025-09-18 15:57:52 +01:00
|
|
|
from .... base import AsyncProcessor, Consumer, Producer
|
|
|
|
|
from .... base import ConsumerMetrics, ProducerMetrics
|
2026-01-27 13:48:08 +00:00
|
|
|
from .... schema import IRI, LITERAL
|
2025-09-18 15:57:52 +01:00
|
|
|
|
|
|
|
|
# Module logger
|
|
|
|
|
logger = logging.getLogger(__name__)
|
2024-07-10 23:20:06 +01:00
|
|
|
|
2026-01-27 13:48:08 +00:00
|
|
|
|
|
|
|
|
def get_term_value(term):
|
|
|
|
|
"""Extract the string value from a Term"""
|
|
|
|
|
if term is None:
|
|
|
|
|
return None
|
|
|
|
|
if term.type == IRI:
|
|
|
|
|
return term.iri
|
|
|
|
|
elif term.type == LITERAL:
|
|
|
|
|
return term.value
|
|
|
|
|
else:
|
|
|
|
|
# For blank nodes or other types, use id or value
|
|
|
|
|
return term.id or term.value
|
|
|
|
|
|
2026-02-23 15:56:29 +00:00
|
|
|
default_ident = "graph-embeddings-write"
|
2024-07-15 17:17:04 +01:00
|
|
|
default_store_uri = 'http://localhost:19530'
|
|
|
|
|
|
2025-12-05 21:45:30 +00:00
|
|
|
class Processor(CollectionConfigHandler, GraphEmbeddingsStoreService):
|
2024-07-10 23:20:06 +01:00
|
|
|
|
2024-07-18 17:20:42 +01:00
|
|
|
def __init__(self, **params):
|
|
|
|
|
|
|
|
|
|
store_uri = params.get("store_uri", default_store_uri)
|
2024-07-10 23:20:06 +01:00
|
|
|
|
2024-07-17 16:56:47 +01:00
|
|
|
super(Processor, self).__init__(
|
2024-07-18 17:20:42 +01:00
|
|
|
**params | {
|
|
|
|
|
"store_uri": store_uri,
|
|
|
|
|
}
|
2024-07-10 23:20:06 +01:00
|
|
|
)
|
|
|
|
|
|
2024-08-26 23:45:23 +01:00
|
|
|
self.vecstore = EntityVectors(store_uri)
|
2024-07-10 23:20:06 +01:00
|
|
|
|
2025-12-05 21:45:30 +00:00
|
|
|
# Register for config push notifications
|
|
|
|
|
self.register_config_handler(self.on_collection_config)
|
2025-09-30 16:02:33 +01:00
|
|
|
|
2025-07-15 09:33:35 +01:00
|
|
|
async def store_graph_embeddings(self, message):
|
2024-07-10 23:20:06 +01:00
|
|
|
|
2025-07-15 09:33:35 +01:00
|
|
|
for entity in message.entities:
|
2026-01-27 13:48:08 +00:00
|
|
|
entity_value = get_term_value(entity.entity)
|
2024-12-30 12:53:19 +00:00
|
|
|
|
2026-01-27 13:48:08 +00:00
|
|
|
if entity_value != "" and entity_value is not None:
|
2024-12-30 12:53:19 +00:00
|
|
|
for vec in entity.vectors:
|
2025-09-09 21:44:55 +01:00
|
|
|
self.vecstore.insert(
|
2026-01-27 13:48:08 +00:00
|
|
|
vec, entity_value,
|
2025-09-09 21:44:55 +01:00
|
|
|
message.metadata.user,
|
2026-03-06 12:23:58 +00:00
|
|
|
message.metadata.collection,
|
|
|
|
|
chunk_id=entity.chunk_id or "",
|
2025-09-09 21:44:55 +01:00
|
|
|
)
|
2024-07-18 17:20:42 +01:00
|
|
|
|
2024-07-17 16:56:47 +01:00
|
|
|
@staticmethod
|
|
|
|
|
def add_args(parser):
|
2024-07-10 23:20:06 +01:00
|
|
|
|
2025-07-15 09:33:35 +01:00
|
|
|
GraphEmbeddingsStoreService.add_args(parser)
|
2024-07-15 17:17:04 +01:00
|
|
|
|
2024-07-17 16:56:47 +01:00
|
|
|
parser.add_argument(
|
|
|
|
|
'-t', '--store-uri',
|
2024-08-13 17:30:59 +01:00
|
|
|
default=default_store_uri,
|
|
|
|
|
help=f'Milvus store URI (default: {default_store_uri})'
|
2024-07-17 16:56:47 +01:00
|
|
|
)
|
2024-07-10 23:20:06 +01:00
|
|
|
|
2025-12-05 21:45:30 +00:00
|
|
|
async def create_collection(self, user: str, collection: str, metadata: dict):
|
2025-11-10 16:56:51 +00:00
|
|
|
"""
|
2025-12-05 21:45:30 +00:00
|
|
|
Create collection via config push - collections are created lazily on first write
|
2025-11-10 16:56:51 +00:00
|
|
|
with the correct dimension determined from the actual embeddings.
|
|
|
|
|
"""
|
2025-09-30 16:02:33 +01:00
|
|
|
try:
|
2025-12-05 21:45:30 +00:00
|
|
|
logger.info(f"Collection create request for {user}/{collection} - will be created lazily on first write")
|
|
|
|
|
self.vecstore.create_collection(user, collection)
|
2025-09-30 16:02:33 +01:00
|
|
|
|
|
|
|
|
except Exception as e:
|
2025-12-05 21:45:30 +00:00
|
|
|
logger.error(f"Failed to create collection {user}/{collection}: {e}", exc_info=True)
|
|
|
|
|
raise
|
2025-09-18 15:57:52 +01:00
|
|
|
|
2025-12-05 21:45:30 +00:00
|
|
|
async def delete_collection(self, user: str, collection: str):
|
|
|
|
|
"""Delete the collection for graph embeddings via config push"""
|
|
|
|
|
try:
|
|
|
|
|
self.vecstore.delete_collection(user, collection)
|
|
|
|
|
logger.info(f"Successfully deleted collection {user}/{collection}")
|
2025-09-18 15:57:52 +01:00
|
|
|
|
|
|
|
|
except Exception as e:
|
2025-12-05 21:45:30 +00:00
|
|
|
logger.error(f"Failed to delete collection {user}/{collection}: {e}", exc_info=True)
|
2025-09-18 15:57:52 +01:00
|
|
|
raise
|
|
|
|
|
|
2024-07-10 23:20:06 +01:00
|
|
|
def run():
|
|
|
|
|
|
2025-07-15 09:33:35 +01:00
|
|
|
Processor.launch(default_ident, __doc__)
|
2024-07-10 23:20:06 +01:00
|
|
|
|