|
11 | 11 | - Internal operations automatically handle Entity/Relation creation and linking |
12 | 12 | """ |
13 | 13 |
|
14 | | -from typing import List, Optional, Dict, Any |
| 14 | +from typing import Any, Dict, List, Optional |
15 | 15 |
|
16 | 16 | from vector_graph_rag.config import Settings, get_settings |
17 | | -from vector_graph_rag.storage.milvus import MilvusStore, generate_id |
18 | | -from vector_graph_rag.storage.embeddings import EmbeddingModel |
19 | | -from vector_graph_rag.models import Entity, Relation, Passage, Triplet |
20 | 17 | from vector_graph_rag.graph.knowledge_graph import SubGraph |
21 | 18 | from vector_graph_rag.llm.extractor import processing_phrases |
| 19 | +from vector_graph_rag.models import Entity, Passage, Relation, Triplet |
| 20 | +from vector_graph_rag.storage.embeddings import EmbeddingModel |
| 21 | +from vector_graph_rag.storage.milvus import MilvusStore, generate_id |
22 | 22 |
|
23 | 23 |
|
24 | 24 | class Graph: |
@@ -128,8 +128,12 @@ def _create_entity( |
128 | 128 | existing = self._store._get_entities_by_ids([existing_id]) |
129 | 129 | if existing: |
130 | 130 | current = existing[0] |
131 | | - new_relation_ids = list(set(current.get("relation_ids", []) + (relation_ids or []))) |
132 | | - new_passage_ids = list(set(current.get("passage_ids", []) + (passage_ids or []))) |
| 131 | + new_relation_ids = list( |
| 132 | + set(current.get("relation_ids", []) + (relation_ids or [])) |
| 133 | + ) |
| 134 | + new_passage_ids = list( |
| 135 | + set(current.get("passage_ids", []) + (passage_ids or [])) |
| 136 | + ) |
133 | 137 | self._store._update_entity( |
134 | 138 | existing_id, |
135 | 139 | relation_ids=new_relation_ids, |
@@ -326,8 +330,12 @@ def _create_relation( |
326 | 330 | relation_id = id or generate_id() |
327 | 331 |
|
328 | 332 | # Create entities (if not exist) and get their IDs |
329 | | - subject_id = self._create_entity(subject, relation_ids=[relation_id], passage_ids=passage_ids) |
330 | | - object_id = self._create_entity(object_, relation_ids=[relation_id], passage_ids=passage_ids) |
| 333 | + subject_id = self._create_entity( |
| 334 | + subject, relation_ids=[relation_id], passage_ids=passage_ids |
| 335 | + ) |
| 336 | + object_id = self._create_entity( |
| 337 | + object_, relation_ids=[relation_id], passage_ids=passage_ids |
| 338 | + ) |
331 | 339 |
|
332 | 340 | # Generate embedding |
333 | 341 | embedding = self._embedding_model.embed(relation_text) |
@@ -413,8 +421,12 @@ def _update_relation( |
413 | 421 | # Build new text if any triplet field changed |
414 | 422 | new_text = None |
415 | 423 | if subject or predicate or object_: |
416 | | - new_subject = self._normalize_entity_name(subject) if subject else data.get("subject", "") |
417 | | - new_predicate = processing_phrases(predicate) if predicate else data.get("predicate", "") |
| 424 | + new_subject = ( |
| 425 | + self._normalize_entity_name(subject) if subject else data.get("subject", "") |
| 426 | + ) |
| 427 | + new_predicate = ( |
| 428 | + processing_phrases(predicate) if predicate else data.get("predicate", "") |
| 429 | + ) |
418 | 430 | new_object = self._normalize_entity_name(object_) if object_ else data.get("object", "") |
419 | 431 | new_text = f"{new_subject} {new_predicate} {new_object}" |
420 | 432 |
|
@@ -453,15 +465,19 @@ def _delete_relation(self, relation_id: str) -> bool: |
453 | 465 | entities = self._store._get_entities_by_ids([eid]) |
454 | 466 | if entities: |
455 | 467 | e_data = entities[0] |
456 | | - new_relation_ids = [rid for rid in e_data.get("relation_ids", []) if rid != relation_id] |
| 468 | + new_relation_ids = [ |
| 469 | + rid for rid in e_data.get("relation_ids", []) if rid != relation_id |
| 470 | + ] |
457 | 471 | self._store._update_entity(eid, relation_ids=new_relation_ids) |
458 | 472 |
|
459 | 473 | # Update related passages (remove this relation from relation_ids) |
460 | 474 | for pid in passage_ids: |
461 | 475 | passages = self._store.get_passages_by_ids([pid]) |
462 | 476 | if passages: |
463 | 477 | p_data = passages[0] |
464 | | - new_relation_ids = [rid for rid in p_data.get("relation_ids", []) if rid != relation_id] |
| 478 | + new_relation_ids = [ |
| 479 | + rid for rid in p_data.get("relation_ids", []) if rid != relation_id |
| 480 | + ] |
465 | 481 | self._store.update_passage(pid, relation_ids=new_relation_ids) |
466 | 482 |
|
467 | 483 | # Delete the relation |
@@ -528,7 +544,9 @@ def create_passage( |
528 | 544 | relation_ids.append(relation_id) |
529 | 545 |
|
530 | 546 | # Get entity IDs for this triplet |
531 | | - subject_id = self._entity_name_to_id.get(self._normalize_entity_name(triplet.subject)) |
| 547 | + subject_id = self._entity_name_to_id.get( |
| 548 | + self._normalize_entity_name(triplet.subject) |
| 549 | + ) |
532 | 550 | object_id = self._entity_name_to_id.get(self._normalize_entity_name(triplet.object)) |
533 | 551 |
|
534 | 552 | if subject_id and subject_id not in entity_ids: |
@@ -596,12 +614,14 @@ def search_passages( |
596 | 614 | passages = [] |
597 | 615 | for r in results: |
598 | 616 | data = r["entity"] |
599 | | - passages.append(Passage( |
600 | | - id=data["id"], |
601 | | - text=data["text"], |
602 | | - entity_ids=data.get("entity_ids", []), |
603 | | - relation_ids=data.get("relation_ids", []), |
604 | | - )) |
| 617 | + passages.append( |
| 618 | + Passage( |
| 619 | + id=data["id"], |
| 620 | + text=data["text"], |
| 621 | + entity_ids=data.get("entity_ids", []), |
| 622 | + relation_ids=data.get("relation_ids", []), |
| 623 | + ) |
| 624 | + ) |
605 | 625 |
|
606 | 626 | return passages |
607 | 627 |
|
@@ -657,15 +677,19 @@ def delete_passage(self, passage_id: str) -> bool: |
657 | 677 | entities = self._store._get_entities_by_ids([eid]) |
658 | 678 | if entities: |
659 | 679 | e_data = entities[0] |
660 | | - new_passage_ids = [pid for pid in e_data.get("passage_ids", []) if pid != passage_id] |
| 680 | + new_passage_ids = [ |
| 681 | + pid for pid in e_data.get("passage_ids", []) if pid != passage_id |
| 682 | + ] |
661 | 683 | self._store._update_entity(eid, passage_ids=new_passage_ids) |
662 | 684 |
|
663 | 685 | # Update related relations (remove this passage from passage_ids) |
664 | 686 | for rid in relation_ids: |
665 | 687 | relations = self._store._get_relations_by_ids([rid]) |
666 | 688 | if relations: |
667 | 689 | r_data = relations[0] |
668 | | - new_passage_ids = [pid for pid in r_data.get("passage_ids", []) if pid != passage_id] |
| 690 | + new_passage_ids = [ |
| 691 | + pid for pid in r_data.get("passage_ids", []) if pid != passage_id |
| 692 | + ] |
669 | 693 | self._store._update_relation(rid, passage_ids=new_passage_ids) |
670 | 694 |
|
671 | 695 | # Delete the passage |
|
0 commit comments