Skip to content

Commit

Permalink
fix: improved ttl description generation
Browse files Browse the repository at this point in the history
  • Loading branch information
PeriniM committed Nov 11, 2024
1 parent d83e66f commit bf4bdae
Show file tree
Hide file tree
Showing 4 changed files with 143 additions and 3 deletions.
5 changes: 5 additions & 0 deletions brickllm/helpers/prompts.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,11 @@
- Only include information explicitly stated in the TTL script
- If no TTL content is provided, return an empty string
Eventually, the user can provide additional instructions to help you generate the building description.
<additional_instructions>
{additional_instructions}
</additional_instructions>
TTL script to analyze:
<ttl_script>
{ttl_script}
Expand Down
14 changes: 11 additions & 3 deletions brickllm/utils/ttl_to_prompt.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from typing import List, Tuple, Union
import logging
from typing import List, Optional, Tuple, Union

from langchain.chat_models.base import BaseChatModel
from langchain_community.llms import Ollama
Expand All @@ -7,17 +8,24 @@
from ..helpers import ttl_to_user_prompt
from ..schemas import TTLToBuildingPromptSchema

logger = logging.getLogger(__name__)


def ttl_to_building_prompt(
ttl_file: str, llm: Union[Ollama, BaseChatModel]
ttl_file: str,
llm: Union[Ollama, BaseChatModel],
additional_instructions: Optional[str] = None,
) -> Tuple[str, List[str]]:

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

# System message
system_message = ttl_to_user_prompt.format(ttl_script=ttl_file)
system_message = ttl_to_user_prompt.format(
ttl_script=ttl_file, additional_instructions=additional_instructions
)

logger.info("Generating building description and key elements from the TTL file.")
# Generate question
answer = structured_llm.invoke(
[SystemMessage(content=system_message)]
Expand Down
32 changes: 32 additions & 0 deletions examples/example_ttl_to_description.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import os

from dotenv import load_dotenv
from langchain_openai import ChatOpenAI

from brickllm.utils import ttl_to_building_prompt

# Load environment variables
load_dotenv()

# Create a custom model
custom_model = ChatOpenAI(temperature=0.8, model="gpt-4o")

current_dir = os.path.dirname(os.path.abspath(__file__))
file_name = "my_building.ttl"

# Open the ttl file
with open(os.path.join(current_dir, file_name), "r") as file:
ttl_file = file.read()

# Generate the building description from the ttl file
building_description, key_elements = ttl_to_building_prompt(
ttl_file,
custom_model,
additional_instructions="Keep a professional and structured tone.",
)

print("Generated building description:")
print(building_description)
print("--------------------------------")
print("Generated key elements:")
print(key_elements)
95 changes: 95 additions & 0 deletions examples/my_building.ttl
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
@prefix bldg: <urn:Building#> .
@prefix brick: <https://brickschema.org/schema/Brick#> .

bldg:Bolzano_Building a brick:Building ;
brick:hasLocation [ brick:value "Bolzano" ] .

bldg:Floor_1 a brick:Floor ;
brick:isPartOf bldg:Bolzano_Building .

bldg:Floor_2 a brick:Floor ;
brick:isPartOf bldg:Bolzano_Building .

bldg:Floor_3 a brick:Floor ;
brick:isPartOf bldg:Bolzano_Building .

bldg:Office_1 a brick:Office ;
brick:isPartOf bldg:Floor_1 .

bldg:Office_2 a brick:Office ;
brick:isPartOf bldg:Floor_2 .

bldg:Office_3 a brick:Office ;
brick:isPartOf bldg:Floor_3 .

bldg:Room_1 a brick:Room ;
brick:isPartOf bldg:Office_1 .

bldg:Room_2 a brick:Room ;
brick:isPartOf bldg:Office_1 .

bldg:Room_3 a brick:Room ;
brick:isPartOf bldg:Office_2 .

bldg:Room_4 a brick:Room ;
brick:isPartOf bldg:Office_2 .

bldg:Room_5 a brick:Room ;
brick:isPartOf bldg:Office_3 .

bldg:Room_6 a brick:Room ;
brick:isPartOf bldg:Office_3 .

bldg:Temperature_Sensor_1 a brick:Temperature_Sensor ;
brick:isPointOf bldg:Room_1 .

bldg:Humidity_Sensor_1 a brick:Humidity_Sensor ;
brick:isPointOf bldg:Room_1 .

bldg:CO_Sensor_1 a brick:CO_Sensor ;
brick:isPointOf bldg:Room_1 .

bldg:Temperature_Sensor_2 a brick:Temperature_Sensor ;
brick:isPointOf bldg:Room_2 .

bldg:Humidity_Sensor_2 a brick:Humidity_Sensor ;
brick:isPointOf bldg:Room_2 .

bldg:CO_Sensor_2 a brick:CO_Sensor ;
brick:isPointOf bldg:Room_2 .

bldg:Temperature_Sensor_3 a brick:Temperature_Sensor ;
brick:isPointOf bldg:Room_3 .

bldg:Humidity_Sensor_3 a brick:Humidity_Sensor ;
brick:isPointOf bldg:Room_3 .

bldg:CO_Sensor_3 a brick:CO_Sensor ;
brick:isPointOf bldg:Room_3 .

bldg:Temperature_Sensor_4 a brick:Temperature_Sensor ;
brick:isPointOf bldg:Room_4 .

bldg:Humidity_Sensor_4 a brick:Humidity_Sensor ;
brick:isPointOf bldg:Room_4 .

bldg:CO_Sensor_4 a brick:CO_Sensor ;
brick:isPointOf bldg:Room_4 .

bldg:Temperature_Sensor_5 a brick:Temperature_Sensor ;
brick:isPointOf bldg:Room_5 .

bldg:Humidity_Sensor_5 a brick:Humidity_Sensor ;
brick:isPointOf bldg:Room_5 .

bldg:CO_Sensor_5 a brick:CO_Sensor ;
brick:isPointOf bldg:Room_5 .

bldg:Temperature_Sensor_6 a brick:Temperature_Sensor ;
brick:isPointOf bldg:Room_6 .

bldg:Humidity_Sensor_6 a brick:Humidity_Sensor ;
brick:isPointOf bldg:Room_6 .

bldg:CO_Sensor_6 a brick:CO_Sensor ;
brick:isPointOf bldg:Room_6 .

0 comments on commit bf4bdae

Please sign in to comment.