Created
March 29, 2025 10:46
-
-
Save vndee/a2f547f63083c9ed67b7a0ce3724a17f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class KnowledgeGraph: | |
def __init__(self, model=None): | |
self.entities = {} | |
self.relations = {} | |
self.triples = [] | |
self.documents = {} | |
self.model = model | |
self.llm = None | |
def get_relation_candidates(self, entity_id: str) -> List[Dict]: | |
"""Get all possible relations for an entity""" | |
candidates = [] | |
# Find all triples where entity is either head or tail | |
for triple in self.triples: | |
head_id, relation_id, tail_id = triple | |
if head_id == entity_id: | |
# Entity is head | |
candidates.append({ | |
'entity_id': entity_id, | |
'relation': relation_id, | |
'relation_name': self.relations[relation_id]['name'], | |
'target_id': tail_id, | |
'target_name': self.entities[tail_id]['name'], | |
'head': True | |
}) | |
if tail_id == entity_id: | |
# Entity is tail | |
candidates.append({ | |
'entity_id': entity_id, | |
'relation': relation_id, | |
'relation_name': self.relations[relation_id]['name'], | |
'target_id': head_id, | |
'target_name': self.entities[head_id]['name'], | |
'head': False | |
}) | |
return candidates |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment