hbllmutils.meta.datamodel.prompt

Module for inspecting Python data model classes and generating meta prompts.

This module provides utilities for extracting source code information from Python classes, particularly data model classes, and generating prompts based on their structure. It includes functionality to:

  • Inspect class source code, file locations, and line numbers

  • Group related classes by their source files

  • Generate meta prompts for data models including their related classes

The main components are:

  • DataModelInspect: A dataclass storing inspection metadata for a single class

  • RelatedReferencedFile: A dataclass grouping classes from the same source file

  • Helper functions for class inspection and prompt generation

DataModelInspect

class hbllmutils.meta.datamodel.prompt.DataModelInspect(class_obj: type, source_file: str, start_line: int, end_line: int, source_lines: List[str])[source]

Data class for storing inspection information about a Python class.

This class holds metadata about a class’s source code, including the file location, line numbers, and the actual source code lines.

Variables:
  • class_obj – The class object being inspected.

  • source_file – The absolute path to the source file containing the class.

  • start_line – The line number where the class definition starts.

  • end_line – The line number where the class definition ends.

  • source_lines – List of source code lines for the class definition.

__post_init__()[source]

Post-initialization processing to normalize the source file path.

Converts the source file path to an absolute, normalized, and case-normalized path for consistent comparison across different platforms.

property class_name: str

Get the name of the inspected class.

Returns:

The class name.

Return type:

str

Example::
>>> class MyClass:
...     pass
>>> info = get_class_info(MyClass)
>>> info.class_name
'MyClass'
property source_code: str

Get the complete source code of the inspected class.

Returns:

The concatenated source code lines of the class.

Return type:

str

Example::
>>> inspect_info = get_class_info(MyClass)
>>> print(inspect_info.source_code)
class MyClass:
    def __init__(self):
        pass
property source_file_code: str

Get the complete source code of the file containing the inspected class.

Returns:

The entire content of the source file.

Return type:

str

Example::
>>> inspect_info = get_class_info(MyClass)
>>> file_content = inspect_info.source_file_code
>>> print(len(file_content))
1234

RelatedReferencedFile

class hbllmutils.meta.datamodel.prompt.RelatedReferencedFile(source_file: str, inspects: List[DataModelInspect])[source]

Data class for grouping related class inspections from the same source file.

This class organizes multiple DataModelInspect objects that belong to the same source file, providing convenient access to their names and the file’s content.

Variables:
  • source_file – The absolute path to the source file.

  • inspects – List of DataModelInspect objects for classes in this file.

property class_names: List[str]

Get the names of all classes in this file.

Returns:

List of class names from all inspected classes in this file.

Return type:

List[str]

Example::
>>> ref_file = RelatedReferencedFile(
...     source_file='/path/to/file.py',
...     inspects=[info1, info2]
... )
>>> ref_file.class_names
['ClassA', 'ClassB']
property source_file_code: str

Get the complete source code of the referenced file.

Returns:

The entire content of the source file.

Return type:

str

Example::
>>> ref_file = RelatedReferencedFile(
...     source_file='/path/to/file.py',
...     inspects=[info1]
... )
>>> code = ref_file.source_file_code
>>> print(len(code))
5678

get_class_info

hbllmutils.meta.datamodel.prompt.get_class_info(cls: type) DataModelInspect[source]

Get inspection information for a given class.

This function retrieves metadata about a class including its source file location, line numbers, and source code. It uses Python’s inspect module to extract this information.

Parameters:

cls (type) – The class to inspect.

Returns:

An object containing the class’s source file, line numbers, and source code.

Return type:

DataModelInspect

Raises:
  • OSError – If the source file cannot be found or read.

  • TypeError – If the provided object is not a class or doesn’t have source code.

Example::
>>> class MyClass:
...     def method(self):
...         pass
>>> info = get_class_info(MyClass)
>>> print(info.start_line)
1
>>> print(info.source_file)
/path/to/file.py
>>> print(info.source_code)
class MyClass:
    def method(self):
        pass

create_meta_prompt_for_datamodel

hbllmutils.meta.datamodel.prompt.create_meta_prompt_for_datamodel(datamodel_class: type, related_datamodel_classes: List[type] | None = None) str[source]

Create a meta prompt for a data model class and its related classes.

This function generates a prompt by inspecting a primary data model class and optionally related classes. It groups classes by their source files and renders them using a Jinja2 template.

Parameters:
  • datamodel_class (type) – The primary data model class to generate the prompt for.

  • related_datamodel_classes (Optional[List[type]]) – Optional list of related data model classes to include in the prompt. Defaults to None.

Returns:

The rendered prompt string containing information about the data model and its related classes.

Return type:

str

Raises:
  • FileNotFoundError – If the prompt template file cannot be found.

  • OSError – If source files cannot be read.

Example::
>>> class UserModel:
...     name: str
...     age: int
>>> class AddressModel:
...     street: str
...     city: str
>>> prompt = create_meta_prompt_for_datamodel(
...     UserModel,
...     related_datamodel_classes=[AddressModel]
... )
>>> print(prompt)
# Generated prompt with class information