Skip to content

Instantly share code, notes, and snippets.

@vndee
Created March 29, 2025 10:49
Show Gist options
  • Save vndee/4895835888923f8d2f02f324b4f57522 to your computer and use it in GitHub Desktop.
Save vndee/4895835888923f8d2f02f324b4f57522 to your computer and use it in GitHub Desktop.
def rank_relations(self, query: str, entity_name: str, relations: List[Dict]) -> List[Dict]:
"""Rank relations by their relevance to the query"""
relation_info = '\n'.join([
f"- {rel['relation_name']} (connects {entity_name} to {rel['target_name']})"
for rel in relations
])
# Craft a prompt that asks the LLM to evaluate relations
system_prompt = """
You are an expert knowledge graph reasoner. Your task is to:
1. Analyze how relations contribute to answering queries
2. Consider multi-step reasoning paths
3. Evaluate both direct and indirect relationships
"""
prompt = f"""
Query: {query}
Entity: {entity_name}
Available relations:
{relation_info}
Analyze these relations for answering the query.
Return a JSON array of objects with relation_name and score (0-10) fields,
sorted by score in descending order.
"""
# Get LLM response and parse results
result = self.generate(prompt, system_prompt)
ranked_relations = self._parse_relation_rankings(result)
# Enrich and sort relations
# ...
return enriched_relations
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment