Use LLM embeddings to create mappings between the Brick ontology and external vocabularies
Project description
Brick-vocab-mapper
pip install brickmapper
This package is a simple wrapper around the OpenAI embeddings API to assist in producing vocabulary mappings between external vocabs (like Haystack protos, gbXML equipment names, or internal naming schems) and Brick classes.
You will need an OpenAI API key to compute your own embeddings. This costs money,
but is very cheap (much less than $1) to do for hundreds of terms.
We cache embeddings in .index
files to avoid hitting the API again.
The brick.index
file in this repo will allow you to run the example below without any OpenAI API calls.
You must provide your external vocabulary to the Brick mapper tool as a list of dictonaries.
Each dictionary defines key-value pairs of a single concept; this dictionary must contain a key called name
which contains
the unique name of the term to be mapped.
There are two kinds of mappings that can be produced by the package:
stable
: a stable mapping performs a 1-1 matching of external terms to Brick classes; it does not permit overlapsunstable
: an unstable mapping allows 1 external term to match multiple Brick classes and vice-versa
Haystack Protos Example
from brickmapper import Mapper
from rdflib import BRICK
import urllib.request
import csv
defns = []
# read haystack proto definitions off their website
request = urllib.request.Request("https://project-haystack.org/download/protos.csv", headers={'User-Agent': "Python script"})
response = urllib.request.urlopen(request)
rdr = csv.DictReader(response.read().decode('utf8').splitlines())
for row in rdr:
# save each proto (e.g. 'air temp sensor point') as a dictionary
defns.append({'name': row['proto']})
# create a new Mapper object which knows about the definitions. This will
# access OpenAI to compute embeddings if necessary
m = Mapper(defns, "haystack_proto.index")
# define which set of Brick classes we want to match against
# all points which aren't parameters (these have similar enough names it can confuse the mapping)
points = m.get_brick_classes(BRICK.Point, [BRICK.Parameter])
# all equipment in Brick
equips = m.get_brick_classes(BRICK.Equipment)
# concatenate these two lists of Brick classes
bcs = points + equips
# compute a 'stable' mapping (1:1 mapping)
stable = m.get_mapping(bcs, allow_collisions=False)
# compute an 'unstable' mapping that allows 1:n and n:1 matches
unstable = m.get_mapping(bcs, allow_collisions=True)
# loop through and print the stable/unstable matches
for k,v in unstable.items():
print(f"{k}:\n\tunstable: {v}\n\tstable: {stable.get(k)}")
Gives output like:
flue hot water heat valve cmd point:
unstable: https://brickschema.org/schema/Brick#Domestic_Hot_Water_Valve
stable: https://brickschema.org/schema/Brick#Hot_Water_Valve
steam bypass pump point:
unstable: https://brickschema.org/schema/Brick#Bypass_Valve
stable: https://brickschema.org/schema/Brick#Bypass_Valve
makeup water entering valve actuator equip:
unstable: https://brickschema.org/schema/Brick#Makeup_Water_Valve
stable: https://brickschema.org/schema/Brick#Makeup_Water_Valve
makeup water entering pipe equip:
unstable: https://brickschema.org/schema/Brick#Eye_Wash_Station
stable: https://brickschema.org/schema/Brick#Eye_Wash_Station
steam leaving pump point:
unstable: https://brickschema.org/schema/Brick#Emergency_Wash_Station
stable: https://brickschema.org/schema/Brick#Steam_Baseboard_Radiator
infiltration well equip:
unstable: https://brickschema.org/schema/Brick#Intrusion_Detection_Equipment
stable: https://brickschema.org/schema/Brick#Intrusion_Detection_Equipment
economizer damper actuator equip:
unstable: https://brickschema.org/schema/Brick#Economizer_Damper
stable: https://brickschema.org/schema/Brick#Economizer_Damper
steam leaving point:
unstable: https://brickschema.org/schema/Brick#Steam_Distribution
stable: https://brickschema.org/schema/Brick#Steam_Distribution
flue equip:
unstable: https://brickschema.org/schema/Brick#Fire_Safety_Equipment
stable: https://brickschema.org/schema/Brick#Fire_Safety_Equipment
condenser water entering pump equip:
unstable: https://brickschema.org/schema/Brick#Condenser_Water_Pump
stable: https://brickschema.org/schema/Brick#Condenser_Water_Pump
TODOs
- incorporate common abbreviations & support embedding of them
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file brickmapper-0.1.3.tar.gz
.
File metadata
- Download URL: brickmapper-0.1.3.tar.gz
- Upload date:
- Size: 6.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.7.1 CPython/3.12.1 Darwin/23.2.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 11618e3a9bed66dd5f94f09d7cb406469edb62eb505344c90181c254b0e0c5d6 |
|
MD5 | 222964e81171f4f4aed313ef566fd220 |
|
BLAKE2b-256 | 6da657628a34bec81c6aeb2c364faad584b9d93c706def7fa1387a0b26935288 |
File details
Details for the file brickmapper-0.1.3-py3-none-any.whl
.
File metadata
- Download URL: brickmapper-0.1.3-py3-none-any.whl
- Upload date:
- Size: 6.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.7.1 CPython/3.12.1 Darwin/23.2.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 155a8322771bef966d9d7f6616ac1f517311e34eb4385ee362e923073dfd581d |
|
MD5 | 7eb0887fe5f7e737f9ebb2a508f479df |
|
BLAKE2b-256 | 659ccd233a5294116ade15d4f0c25178afad22d7061e6c2bddddccf611610508 |