Skip to content

SourceCodeVerbalizer

relationalai.agent.cortex.verbalize
SourceCodeVerbalizer(model: relationalai.semantics.Model, *modules: Any)

Verbalizer that uses relationship readings for model-level explanations and returns Python source code for concept-level explanations.

Extends ModelVerbalizer. The explain_model method is inherited and returns sorted relationship readings. The explain_concept method filters the provided modules to those that reference the requested concept and returns their source code.

Leading indentation is normalized and excessive newlines are collapsed for cleaner presentation.

  • model

    (Model) - RAI Model to verbalize.
  • *modules

    (Any, default: ()) - Variable number of Python callables (functions, classes) or modules containing model definitions. Each must have retrievable source code via inspect.getsource(). Built-ins without source are silently skipped.
>>> from myproject.model import define_customer, define_order
>>> verbalizer = SourceCodeVerbalizer(model, define_customer, define_order)
>>> print(verbalizer.explain_concept("Customer"))
def define_customer(model):
...
SourceCodeVerbalizer.explain_concept(concept: str) -> str

Extract source code from modules that reference the given concept.

Filters to only include modules where the concept name appears in source code.

Parameters:

  • concept

    (str) - Concept name to search for in the source code.

Returns:

  • str - Concatenated source code from matching modules with normalized formatting. Returns "Could not explain {concept}" if no modules contain the concept.
SourceCodeVerbalizerModelVerbalizerVerbalizerabc.ABC