build_kg
Standalone script to build knowledge graph entries from channel messages.
Fetches the last N messages (default 1000) from a channel via Redis cache first, falling back to the platform API. Sends ALL messages plus the entire existing knowledge graph to gemini-3-flash-preview in a single call, then presents the proposed entities/relationships for human approval before committing to FalkorDB.
- Usage:
python build_kg.py –platform discord –channel 123456789 python build_kg.py –platform discord –channel 123456789 –guild 987
- async build_kg.fetch_messages_redis(cache, platform, channel_id, count)[source]
Pull up to count messages from the Redis sorted-set cache.
- Return type:
- Parameters:
cache (MessageCache)
platform (str)
channel_id (str)
count (int)
- async build_kg.fetch_messages_discord(token, channel_id, limit)[source]
Fetch messages directly from the Discord API using discord.py.
Returns dicts with keys: user_id, user_name, text, timestamp (float).
- async build_kg.gather_messages(cache, platform, channel_id, count, cfg)[source]
Collect up to count messages, Redis-first with API fallback.
Returns a chronologically-ordered list of message dicts with keys: user_id, user_name, text, timestamp.
- async build_kg.dump_full_graph(kg)[source]
Serialize the entire knowledge graph into a text block for LLM context.
- Return type:
- Parameters:
- build_kg.build_extraction_prompt(conversation_text, graph_context)[source]
Build the messages list for the extraction LLM call.
- async build_kg.run_extraction(openrouter, conversation_text, graph_context)[source]
Call the LLM to extract entities and relationships.
Returns {“entities”: […], “relationships”: […]}.
- build_kg.prompt_approval(entities, relationships, num_messages)[source]
Display proposed entries and return approved indices.
Returns (entity_indices, relationship_indices) or None to quit. Entity/relationship indices are 0-based.
- async build_kg.commit_entities(kg, entities, approved_indices, channel_id, entity_uuid_lookup)[source]
Resolve-or-create approved entities. Returns count committed.
Populates entity_uuid_lookup with name->uuid mappings.
- async build_kg.commit_relationships(kg, relationships, approved_indices, entity_uuid_lookup)[source]
Create/reinforce approved relationships. Returns count committed.
- build_kg.format_conversation(messages)[source]
Format all messages into conversation text for the LLM.