From 49a86c622a8ef7605cfe956702e0acf6fee69f94 Mon Sep 17 00:00:00 2001 From: _Yusaki Date: Fri, 13 Mar 2026 19:34:51 +0700 Subject: [PATCH] Adjust relationship prompt to infer connections from character traits --- backend/app/services/llm_graph_builder.py | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/backend/app/services/llm_graph_builder.py b/backend/app/services/llm_graph_builder.py index d331175..5675513 100644 --- a/backend/app/services/llm_graph_builder.py +++ b/backend/app/services/llm_graph_builder.py @@ -36,18 +36,21 @@ ENTITY_EXTRACT_PROMPT = ( ) RELATIONSHIP_EXTRACT_PROMPT = ( - "You are a knowledge graph relationship extraction engine. Given a text section, a list of known entities, " - "and an ontology schema, extract all relationships between the entities.\n\n" - "ONTOLOGY SCHEMA (edge types):\n%s\n\n" + "You are a knowledge graph relationship inference engine. Given a text section describing characters/entities " + "in a social simulation, a list of known entities, and relationship types, infer all plausible relationships.\n\n" + "RELATIONSHIP TYPES:\n%s\n\n" "KNOWN ENTITIES:\n%s\n\n" "RULES:\n" - "1. Find relationships between the known entities that match the edge_types defined above.\n" - "2. Each relationship needs: name (must match an edge_type name), source (entity name), target (entity name), " - "fact (1 sentence describing the specific relationship found in the text).\n" + "1. Infer relationships between the known entities based on their described traits, roles, goals, and behaviors.\n" + "2. Each relationship needs: name (must match a relationship type name above), source (entity name), target (entity name), " + "fact (1 sentence explaining WHY this relationship is likely based on the text).\n" "3. Both source and target MUST be from the known entities list.\n" - "4. Only extract relationships explicitly stated or strongly implied in the text.\n" - "5. Extract ALL relationships you can find — be thorough.\n" - "6. If no relationships are found, return an empty array.\n\n" + "4. Include both explicit relationships AND strongly implied ones based on complementary/conflicting traits.\n" + " Examples of inference: if player A 'trades supplies' and player B 'buys tools', infer TRADE_WITH.\n" + " If player A 'mediates conflicts' and player B 'causes chaos', infer OPPOSES.\n" + " If two players share goals or would naturally work together, infer COLLABORATES_WITH.\n" + "5. Be thorough — extract as many plausible relationships as the text supports.\n" + "6. Do NOT invent relationships with no textual basis. Each fact must reference something from the text.\n\n" 'Return JSON: a single key "relationships" with an array of objects, each having keys: name, source, target, fact.' )