Arcana.Maintenance (Arcana v2.0.1)

Copy Markdown View Source

Maintenance functions for Arcana.

These functions are designed to be callable from production environments where mix tasks are not available (e.g., releases).

Usage in Production

# Remote IEx
iex> Arcana.Maintenance.reembed(MyApp.Repo)

# Release command
bin/my_app eval "Arcana.Maintenance.reembed(MyApp.Repo)"

Summary

Functions

Detects communities in the knowledge graph using the Leiden algorithm.

Embeds entity descriptions for entities that lack embeddings.

Returns the current embedding dimensions.

Returns info about the current embedding configuration.

Returns info about the current graph configuration.

Rebuilds the knowledge graph for documents.

Re-embeds all chunks and rechunks documents that have no chunks.

Generates summaries for communities that need them.

Functions

detect_communities(repo, opts \\ [])

Detects communities in the knowledge graph using the Leiden algorithm.

This runs community detection on entities and relationships, producing hierarchical community clusters. Existing communities for the collection(s) are cleared before detection.

Options

  • :collection - Filter to a specific collection by name (default: all collections)
  • :resolution - Community detection resolution (default: 1.0)
  • :max_level - Maximum hierarchy levels (default: 3)
  • :progress - Function to call with progress updates fn current, total -> :ok end

Examples

# Basic usage - all collections
Arcana.Maintenance.detect_communities(MyApp.Repo)

# Single collection
Arcana.Maintenance.detect_communities(MyApp.Repo, collection: "my-docs")

# With custom resolution
Arcana.Maintenance.detect_communities(MyApp.Repo, resolution: 0.5)

embed_entities(repo, opts \\ [])

Embeds entity descriptions for entities that lack embeddings.

Uses the configured embedder to generate vector embeddings from entity descriptions, enabling GraphRAG-style entity similarity search.

Options

  • :collection - Only embed entities in this collection
  • :batch_size - Entities per batch (default: 100)
  • :progress - Progress callback fn current, total -> :ok end
  • :force - Re-embed all entities, not just those without embeddings (default: false)

embedding_dimensions()

Returns the current embedding dimensions.

Useful for verifying the configured embedder before running migrations.

Examples

iex> Arcana.Maintenance.embedding_dimensions()
{:ok, 1536}

embedding_info()

Returns info about the current embedding configuration.

Examples

iex> Arcana.Maintenance.embedding_info()
%{type: :openai, model: "text-embedding-3-small", dimensions: 1536}

graph_info()

Returns info about the current graph configuration.

Examples

iex> Arcana.Maintenance.graph_info()
%{enabled: true, extractor: :llm}

rebuild_graph(repo, opts \\ [])

Rebuilds the knowledge graph for documents.

See module docs for full options.

reembed(repo, opts \\ [])

Re-embeds all chunks and rechunks documents that have no chunks.

This is useful when switching embedding models or after a migration that cleared chunks.

Options

  • :batch_size - Number of items to process at once (default: 50)
  • :concurrency - Number of parallel embedding requests (default: 5)
  • :skip - Number of chunks to skip (for resuming interrupted runs)
  • :progress - Function to call with progress updates fn current, total -> :ok end

Examples

# Basic usage
Arcana.Maintenance.reembed(MyApp.Repo)

# With progress callback and concurrency
Arcana.Maintenance.reembed(MyApp.Repo,
  batch_size: 100,
  concurrency: 10,
  progress: fn current, total ->
    IO.puts("Progress: #{current}/#{total}")
  end
)

# Resume from chunk 500
Arcana.Maintenance.reembed(MyApp.Repo, skip: 500)

summarize_communities(repo, opts \\ [])

Generates summaries for communities that need them.

This function iterates through communities and generates LLM summaries for those that are dirty, have no summary, or have accumulated changes.

Options

  • :collection - Only summarize communities in this collection (default: all)
  • :progress - Progress callback function
  • :force - Regenerate all summaries even if not dirty (default: false)
  • :concurrency - Number of parallel summarization tasks (default: 1)
  • :llm - LLM function for summarization (uses config if not provided)

Returns

{:ok, %{communities: count, summaries: count}} on success.

Examples

# Summarize all dirty communities
Maintenance.summarize_communities(repo)

# Force regenerate all summaries
Maintenance.summarize_communities(repo, force: true)

# Summarize a specific collection
Maintenance.summarize_communities(repo, collection: "my-docs")