Skip to content

BaseContextBuilder

BaseContextBuilder

Bases: ABC

Abstract base class for building context using graph data and a search engine.

Attributes:

  • graph

    The graph object.

  • single_batch

    Whether to process data in a single batch.

  • search_engine

    The search engine for retrieving top-k objects.

  • token_encoder

    Token encoder for text tokenization.

__init__(graph, single_batch=True, search_engine=None, token_encoder=None)

Initialize the BaseContextBuilder.

Parameters:

  • graph (Graph) –

    The graph object.

  • single_batch (bool, default: True ) –

    Whether to process data in a single batch.

  • search_engine (Optional[BaseSearchEngine], default: None ) –

    The search engine for similarity searches.

  • token_encoder (Optional[Encoding], default: None ) –

    Token encoder for text tokenization. Defaults to "cl100k_base".

build_context(*args, **kwargs) abstractmethod async

Abstract method to build context.

Returns:

  • str | List[str]

    The generated context as a string or list of strings.

batch_and_convert_to_text(graph_data, section_name, single_batch=False, max_tokens=12000)

Convert graph data to a formatted string or list of strings in batches based on token count.

Parameters:

  • graph_data (DataFrame) –

    The graph data to convert.

  • section_name (str) –

    The section name for the header.

  • single_batch (bool, default: False ) –

    Whether to process data in a single batch. Defaults to False.

  • max_tokens (int, default: 12000 ) –

    Maximum number of tokens per batch. Defaults to 12000.

Returns:

  • str | List[str]

    The formatted graph data as a string or list of strings.

retrieve_top_k_objects(query, k=10, oversample_scaler=2, **kwargs) async

Retrieve the top-k objects most similar to the query.

Parameters:

  • query (str) –

    The query string.

  • k (int, default: 10 ) –

    The number of top results to retrieve. Defaults to 10.

  • **kwargs (Dict[str, Any], default: {} ) –

    Additional parameters for the search engine.

Returns:

  • List[str]

    A list of the top-k results.

Raises:

  • ValueError

    If k is less than or equal to 0 or if the search engine is not initialized.