Ragex.Embeddings.Helper (Ragex v0.21.0)

View Source

Helper functions to generate and store embeddings for analyzed code entities.

This module bridges the gap between code analyzers and the embedding system, automatically generating embeddings for modules, functions, and other entities.

Chunking

Large entities (modules or functions whose embeddable text exceeds Chunker.defaults()[:min_lines] lines) are also stored as per-chunk embeddings under node type :chunk. Each chunk key is {parent_type, parent_id, chunk_index}.

The entity-level embedding is always stored as well for backward compatibility with graph_first retrieval, which looks up embeddings by exact entity key. Chunk embeddings are used by semantic and hybrid search via the :include_chunks option on VectorStore.search/2.

Summary

Functions

Generates and stores embeddings for all entities in an analysis result.

Generate chunk embeddings for every entity in a batch.

Generates embeddings for a batch of entities efficiently.

Generate and store chunk embeddings for a single entity text.

Generates and stores an embedding for a single function.

Generates and stores an embedding for a single module.

Checks if the embedding system is available and ready.

Functions

generate_and_store_embeddings(analysis_result, opts \\ [])

Generates and stores embeddings for all entities in an analysis result.

Takes the output from an analyzer and generates embeddings for:

  • Modules
  • Functions

Returns :ok if embeddings were generated, or {:error, reason} if the model is not ready or embedding generation fails.

Options

  • :only - MapSet of entity IDs whose embeddings should be (re)generated. When nil (default) all entities are embedded. Set this to the result of FileTracker.stale_entities_for_file/2 to skip unchanged functions and avoid redundant re-embeddings on incremental re-analysis.

generate_batch_chunk_embeddings(entities, entity_type, opts \\ [])

@spec generate_batch_chunk_embeddings([map()], atom(), keyword()) :: :ok

Generate chunk embeddings for every entity in a batch.

Called automatically after generate_batch_embeddings/2. Can also be called independently.

Options

  • :only - MapSet of entity IDs to embed. Entities not in the set are skipped. Pass nil (default) to process all entities.

generate_batch_embeddings(entities, entity_type, opts \\ [])

Generates embeddings for a batch of entities efficiently.

Uses batch embedding to process multiple entities at once for better performance.

Options

  • :only - MapSet of entity IDs to embed. When nil (default) all entities in the batch are embedded. Pass the result of FileTracker.stale_entities_for_file/2 to skip unchanged functions.

generate_chunk_embeddings(entity_type, entity_id, text, opts \\ [])

@spec generate_chunk_embeddings(atom(), term(), String.t(), keyword()) ::
  {:ok, non_neg_integer()} | :skip | {:error, term()}

Generate and store chunk embeddings for a single entity text.

Splits text into overlapping windows via Chunker.split/2, embeds each window in one batch call, and stores them under node type :chunk with compound keys {parent_type, parent_id, chunk_index}.

Returns {:ok, chunk_count} when chunks are stored, or :skip when the text is short enough that a single entity embedding already suffices.

generate_function_embedding(function_data)

Generates and stores an embedding for a single function.

generate_module_embedding(module_data)

Generates and stores an embedding for a single module.

ready?()

Checks if the embedding system is available and ready.