Skip to content

schema_to_ttl.py§

schema_to_ttl(state, config) §

Generate a TTL (Turtle) script from the building description and component hierarchy.

Parameters:

Name Type Description Default
state State

The current state containing the user prompt, sensors, and element hierarchy.

required
config dict

Configuration dictionary containing the language model.

required

Returns:

Name Type Description
dict Dict[str, Any]

A dictionary containing the generated TTL output.

Source code in brickllm/nodes/schema_to_ttl.py
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
def schema_to_ttl(state: State, config: Dict[str, Any]) -> Dict[str, Any]:
    """
    Generate a TTL (Turtle) script from the building description and component hierarchy.

    Args:
        state (State): The current state containing the user prompt, sensors, and element hierarchy.
        config (dict): Configuration dictionary containing the language model.

    Returns:
        dict: A dictionary containing the generated TTL output.
    """
    custom_logger.eurac("📝 Generating TTL from schema")

    user_prompt = state["user_prompt"]
    sensors_dict = state["sensors_dict"]
    elem_hierarchy = state["elem_hierarchy"]

    sensors_dict_json = json.dumps(sensors_dict, indent=2)
    elem_hierarchy_json = json.dumps(elem_hierarchy, indent=2)

    # Get the model name from the config
    llm = config.get("configurable", {}).get("llm_model")

    # Enforce structured output
    structured_llm = llm.with_structured_output(TTLSchema)

    # System message
    system_message = schema_to_ttl_instructions.format(
        prompt=user_prompt,
        sensors_dict=sensors_dict_json,
        elem_hierarchy=elem_hierarchy_json,
        ttl_example=ttl_example,
    )

    # Generate question
    answer = structured_llm.invoke(
        [SystemMessage(content=system_message)]
        + [HumanMessage(content="Generate the TTL.")]
    )

    return {"ttl_output": answer.ttl_output}