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:

list[CachedMessage]

Parameters:
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).

Return type:

list[dict[str, Any]]

Parameters:
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.

Return type:

list[dict[str, Any]]

Parameters:
async build_kg.dump_full_graph(kg)[source]

Serialize the entire knowledge graph into a text block for LLM context.

Return type:

str

Parameters:

kg (KnowledgeGraphManager)

build_kg.build_extraction_prompt(conversation_text, graph_context)[source]

Build the messages list for the extraction LLM call.

Return type:

list[dict[str, str]]

Parameters:
  • conversation_text (str)

  • graph_context (str)

async build_kg.run_extraction(openrouter, conversation_text, graph_context)[source]

Call the LLM to extract entities and relationships.

Returns {“entities”: […], “relationships”: […]}.

Return type:

dict[str, list[dict]]

Parameters:
build_kg.format_entity(idx, ent)[source]

Format entity for output.

Parameters:
  • idx (int) – The idx value.

  • ent (dict) – The ent value.

Returns:

Result string.

Return type:

str

build_kg.format_relationship(idx, rel)[source]

Format relationship for output.

Parameters:
  • idx (int) – The idx value.

  • rel (dict) – The rel value.

Returns:

Result string.

Return type:

str

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.

Return type:

tuple[list[int], list[int]] | None

Parameters:
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.

Return type:

int

Parameters:
async build_kg.commit_relationships(kg, relationships, approved_indices, entity_uuid_lookup)[source]

Create/reinforce approved relationships. Returns count committed.

Return type:

int

Parameters:
build_kg.format_conversation(messages)[source]

Format all messages into conversation text for the LLM.

Return type:

str

Parameters:

messages (list[dict[str, Any]])

async build_kg.run(args)[source]

Execute this tool and return the result.

Parameters:

args (Namespace) – The args value.

Return type:

None

build_kg.main()[source]

Main.

Return type:

None