Skip to content

Instantly share code, notes, and snippets.

@vndee
Created March 29, 2025 10:47
Show Gist options
  • Save vndee/80029ff972fadaf13c9be59c7e1ec620 to your computer and use it in GitHub Desktop.
Save vndee/80029ff972fadaf13c9be59c7e1ec620 to your computer and use it in GitHub Desktop.
def iterative_reasoning(self, query: str) -> Tuple[List[Dict], str]:
# Initialize tracking variables
reasoning_steps = []
visited_entities = set()
# Step 1: Initial Topic Entities
topic_entities = self.identify_topic_entities(query)
visited_entities.update(topic_entities)
# Initial document retrieval
initial_contexts = self.knowledge_graph.search_entity_documents(topic_entities, query)
# Iterative exploration loop
for depth in range(self.max_depth):
# Step 2: Knowledge-guided Graph Search
for entity_id in topic_entities:
# Rank relations by relevance to query
ranked_relations = self.knowledge_graph.prune_relations(entity_id, query)
# Explore connected entities through top relations
for relation in ranked_relations[:self.max_width]:
# Find entities connected through this relation
related_entity_ids = self.knowledge_graph.get_entity_candidates(
relation['entity_id'], relation['relation'], relation['head']
)
# Track these new entities and relationships
# ...
# Step 3: Knowledge-guided Context Retrieval
# Retrieve document contexts for newly discovered entities
# ...
# Step 4: Context-based Entity Pruning
# Rank entities based on context relevance
# ...
# Should we continue exploring or generate an answer?
# ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment