Ragex.RAG.ContextBuilder (Ragex v0.20.0)

View Source

Formats retrieved code for AI consumption.

Supports multiple output formats:

  • :text - Markdown-formatted context (default)
  • :json - Structured JSON with code metadata
  • :ast - JSON with MetaAST data, purity, and complexity

Context assembly strategy

The default assembly uses greedy packing: results are scored against the query, sorted by relevance, and added one-by-one until max_context_length would be exceeded. Before adding each result, overlapping code ranges in already-selected results are checked; if the candidate's file+line range substantially overlaps an already-included result, it is skipped to avoid sending the same code twice (common when chunk embeddings and entity embeddings both match).

To use the old truncation behaviour, pass smart_assembly: false.

Options

  • :format - Output format: :text, :json, or :ast (default: :text)
  • :include_code - Include full code snippets (default: true)
  • :max_context_length - Max context size in characters (default: 8000)
  • :smart_assembly - Enable greedy packing + deduplication (default: true)
  • :query - Original query string used to score candidates (optional). When provided, candidates are scored against the query before packing.
  • :overlap_threshold - Line-overlap ratio above which a candidate is considered a duplicate of an already-included result (default: 0.6)

Summary

Functions

Build context from retrieval results in the specified format.

Functions

build_context(results, opts \\ [])

@spec build_context(
  [map()],
  keyword()
) :: {:ok, String.t()}

Build context from retrieval results in the specified format.

Parameters

  • results - List of retrieval result maps
  • opts - Keyword list of options

Returns

  • {:ok, context} where context is a string (text/json/ast encoded)