Skip to content

Commit 4f5f54c

Browse files
committed
refactor: dictionary instead of model for better type safety
1 parent da34548 commit 4f5f54c

File tree

1 file changed

+50
-73
lines changed

1 file changed

+50
-73
lines changed

backend/onyx/kg/setup/kg_default_entity_definitions.py

Lines changed: 50 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
from pydantic import BaseModel
2-
31
from onyx.configs.constants import DocumentSource
42
from onyx.db.engine import get_session_with_current_tenant
53
from onyx.db.entity_type import KGEntityType
@@ -9,9 +7,8 @@
97
from onyx.kg.models import KGGroundingType
108

119

12-
class KGDefaultPrimaryGroundedEntityDefinitions(BaseModel):
13-
14-
LINEAR: KGDefaultEntityDefinition = KGDefaultEntityDefinition(
10+
KG_DEFAULT_PRIMARY_GROUNDED_ENTITIES = {
11+
"LINEAR": KGDefaultEntityDefinition(
1512
description="A formal Linear ticket about a product issue or improvement request.",
1613
attributes={
1714
"metadata_attributes": {
@@ -26,9 +23,8 @@ class KGDefaultPrimaryGroundedEntityDefinitions(BaseModel):
2623
},
2724
grounding=KGGroundingType.GROUNDED,
2825
grounded_source_name=DocumentSource.LINEAR,
29-
)
30-
31-
JIRA__EPIC: KGDefaultEntityDefinition = KGDefaultEntityDefinition(
26+
),
27+
"JIRA-EPIC": KGDefaultEntityDefinition(
3228
description=(
3329
"A formal Jira ticket describing large bodies of work that can be broken down into "
3430
"a number of smaller Jira Tasks, Stories, or Bugs."
@@ -47,9 +43,8 @@ class KGDefaultPrimaryGroundedEntityDefinitions(BaseModel):
4743
},
4844
grounding=KGGroundingType.GROUNDED,
4945
grounded_source_name=DocumentSource.JIRA,
50-
)
51-
52-
JIRA__STORY: KGDefaultEntityDefinition = KGDefaultEntityDefinition(
46+
),
47+
"JIRA-STORY": KGDefaultEntityDefinition(
5348
description=(
5449
"Also called 'user stories', these are Jira tickets describing short requirements or requests "
5550
"written from the perspective of the end user."
@@ -68,9 +63,8 @@ class KGDefaultPrimaryGroundedEntityDefinitions(BaseModel):
6863
},
6964
grounding=KGGroundingType.GROUNDED,
7065
grounded_source_name=DocumentSource.JIRA,
71-
)
72-
73-
JIRA__BUG: KGDefaultEntityDefinition = KGDefaultEntityDefinition(
66+
),
67+
"JIRA-BUG": KGDefaultEntityDefinition(
7468
description=("A Jira ticket describing a bug."),
7569
attributes={
7670
"metadata_attributes": {
@@ -86,9 +80,8 @@ class KGDefaultPrimaryGroundedEntityDefinitions(BaseModel):
8680
},
8781
grounding=KGGroundingType.GROUNDED,
8882
grounded_source_name=DocumentSource.JIRA,
89-
)
90-
91-
JIRA__TASK: KGDefaultEntityDefinition = KGDefaultEntityDefinition(
83+
),
84+
"JIRA-TASK": KGDefaultEntityDefinition(
9285
description=("A Jira ticket describing a unit of work."),
9386
attributes={
9487
"metadata_attributes": {
@@ -104,9 +97,8 @@ class KGDefaultPrimaryGroundedEntityDefinitions(BaseModel):
10497
},
10598
grounding=KGGroundingType.GROUNDED,
10699
grounded_source_name=DocumentSource.JIRA,
107-
)
108-
109-
JIRA__SUBTASK: KGDefaultEntityDefinition = KGDefaultEntityDefinition(
100+
),
101+
"JIRA-SUBTASK": KGDefaultEntityDefinition(
110102
description=("A Jira ticket describing a sub-unit of work."),
111103
attributes={
112104
"metadata_attributes": {
@@ -122,9 +114,8 @@ class KGDefaultPrimaryGroundedEntityDefinitions(BaseModel):
122114
},
123115
grounding=KGGroundingType.GROUNDED,
124116
grounded_source_name=DocumentSource.JIRA,
125-
)
126-
127-
GITHUB__PR: KGDefaultEntityDefinition = KGDefaultEntityDefinition(
117+
),
118+
"GITHUB-PR": KGDefaultEntityDefinition(
128119
description="Our (---vendor_name---) Engineering PRs describing what was actually implemented.",
129120
attributes={
130121
"metadata_attributes": {
@@ -144,9 +135,8 @@ class KGDefaultPrimaryGroundedEntityDefinitions(BaseModel):
144135
},
145136
grounding=KGGroundingType.GROUNDED,
146137
grounded_source_name=DocumentSource.GITHUB,
147-
)
148-
149-
GITHUB__ISSUE: KGDefaultEntityDefinition = KGDefaultEntityDefinition(
138+
),
139+
"GITHUB-ISSUE": KGDefaultEntityDefinition(
150140
description="Our (---vendor_name---) Engineering issues describing what needs to be implemented.",
151141
attributes={
152142
"metadata_attributes": {
@@ -162,9 +152,8 @@ class KGDefaultPrimaryGroundedEntityDefinitions(BaseModel):
162152
},
163153
grounding=KGGroundingType.GROUNDED,
164154
grounded_source_name=DocumentSource.GITHUB,
165-
)
166-
167-
FIREFLIES: KGDefaultEntityDefinition = KGDefaultEntityDefinition(
155+
),
156+
"FIREFLIES": KGDefaultEntityDefinition(
168157
description=(
169158
"A phone call transcript between us (---vendor_name---) "
170159
"and another account or individuals, or an internal meeting."
@@ -198,9 +187,8 @@ class KGDefaultPrimaryGroundedEntityDefinitions(BaseModel):
198187
},
199188
grounding=KGGroundingType.GROUNDED,
200189
grounded_source_name=DocumentSource.FIREFLIES,
201-
)
202-
203-
GONG: KGDefaultEntityDefinition = KGDefaultEntityDefinition(
190+
),
191+
"GONG": KGDefaultEntityDefinition(
204192
description=(
205193
"A phone call transcript between us (---vendor_name---) "
206194
"and another account or individuals, or an internal meeting."
@@ -212,9 +200,8 @@ class KGDefaultPrimaryGroundedEntityDefinitions(BaseModel):
212200
},
213201
grounding=KGGroundingType.GROUNDED,
214202
grounded_source_name=DocumentSource.GONG,
215-
)
216-
217-
GOOGLE_DRIVE: KGDefaultEntityDefinition = KGDefaultEntityDefinition(
203+
),
204+
"GOOGLE_DRIVE": KGDefaultEntityDefinition(
218205
description="A Google Drive document.",
219206
attributes={
220207
"metadata_attributes": {},
@@ -223,9 +210,8 @@ class KGDefaultPrimaryGroundedEntityDefinitions(BaseModel):
223210
},
224211
grounding=KGGroundingType.GROUNDED,
225212
grounded_source_name=DocumentSource.GOOGLE_DRIVE,
226-
)
227-
228-
GMAIL: KGDefaultEntityDefinition = KGDefaultEntityDefinition(
213+
),
214+
"GMAIL": KGDefaultEntityDefinition(
229215
description="An email.",
230216
attributes={
231217
"metadata_attributes": {},
@@ -234,9 +220,8 @@ class KGDefaultPrimaryGroundedEntityDefinitions(BaseModel):
234220
},
235221
grounding=KGGroundingType.GROUNDED,
236222
grounded_source_name=DocumentSource.GMAIL,
237-
)
238-
239-
ACCOUNT: KGDefaultEntityDefinition = KGDefaultEntityDefinition(
223+
),
224+
"ACCOUNT": KGDefaultEntityDefinition(
240225
description=(
241226
"A company that was, is, or potentially could be a customer of the vendor "
242227
"('us, ---vendor_name---'). Note that ---vendor_name--- can never be an ACCOUNT."
@@ -248,9 +233,8 @@ class KGDefaultPrimaryGroundedEntityDefinitions(BaseModel):
248233
},
249234
grounding=KGGroundingType.GROUNDED,
250235
grounded_source_name=DocumentSource.SALESFORCE,
251-
)
252-
253-
OPPORTUNITY: KGDefaultEntityDefinition = KGDefaultEntityDefinition(
236+
),
237+
"OPPORTUNITY": KGDefaultEntityDefinition(
254238
description="A sales opportunity.",
255239
attributes={
256240
"metadata_attributes": {
@@ -271,9 +255,8 @@ class KGDefaultPrimaryGroundedEntityDefinitions(BaseModel):
271255
},
272256
grounding=KGGroundingType.GROUNDED,
273257
grounded_source_name=DocumentSource.SALESFORCE,
274-
)
275-
276-
SLACK: KGDefaultEntityDefinition = KGDefaultEntityDefinition(
258+
),
259+
"SLACK": KGDefaultEntityDefinition(
277260
description="A Slack conversation.",
278261
attributes={
279262
"metadata_attributes": {},
@@ -282,9 +265,8 @@ class KGDefaultPrimaryGroundedEntityDefinitions(BaseModel):
282265
},
283266
grounding=KGGroundingType.GROUNDED,
284267
grounded_source_name=DocumentSource.SLACK,
285-
)
286-
287-
WEB: KGDefaultEntityDefinition = KGDefaultEntityDefinition(
268+
),
269+
"WEB": KGDefaultEntityDefinition(
288270
description="A web page.",
289271
attributes={
290272
"metadata_attributes": {},
@@ -293,29 +275,26 @@ class KGDefaultPrimaryGroundedEntityDefinitions(BaseModel):
293275
},
294276
grounding=KGGroundingType.GROUNDED,
295277
grounded_source_name=DocumentSource.WEB,
296-
)
278+
),
279+
}
297280

298-
299-
class KGDefaultAccountEmployeeDefinitions(BaseModel):
300-
301-
VENDOR: KGDefaultEntityDefinition = KGDefaultEntityDefinition(
281+
KG_DEFAULT_ACCOUNT_EMPLOYEE_ENTITIES = {
282+
"VENDOR": KGDefaultEntityDefinition(
302283
description="The Vendor ---vendor_name---, 'us'",
303284
grounding=KGGroundingType.GROUNDED,
304285
active=False,
305286
grounded_source_name=None,
306-
)
307-
308-
ACCOUNT: KGDefaultEntityDefinition = KGDefaultEntityDefinition(
287+
),
288+
"ACCOUNT": KGDefaultEntityDefinition(
309289
description=(
310290
"A company that was, is, or potentially could be a customer of the vendor "
311291
"('us, ---vendor_name---'). Note that ---vendor_name--- can never be an ACCOUNT."
312292
),
313293
grounding=KGGroundingType.GROUNDED,
314294
active=False,
315295
grounded_source_name=None,
316-
)
317-
318-
EMPLOYEE: KGDefaultEntityDefinition = KGDefaultEntityDefinition(
296+
),
297+
"EMPLOYEE": KGDefaultEntityDefinition(
319298
description=(
320299
"A person who speaks on behalf of 'our' company (the VENDOR ---vendor_name---), "
321300
"NOT of another account. Therefore, employees of other companies "
@@ -324,7 +303,8 @@ class KGDefaultAccountEmployeeDefinitions(BaseModel):
324303
grounding=KGGroundingType.GROUNDED,
325304
active=False,
326305
grounded_source_name=None,
327-
)
306+
),
307+
}
328308

329309

330310
def populate_default_entity_types() -> None:
@@ -339,34 +319,31 @@ def populate_default_entity_types() -> None:
339319

340320
# Create an instance of the default definitions
341321
default_definitions = [
342-
KGDefaultPrimaryGroundedEntityDefinitions(),
343-
KGDefaultAccountEmployeeDefinitions(),
322+
KG_DEFAULT_PRIMARY_GROUNDED_ENTITIES,
323+
KG_DEFAULT_ACCOUNT_EMPLOYEE_ENTITIES,
344324
]
345325

346326
# Iterate over all attributes in the default definitions
347327
for default_definition in default_definitions:
348-
for id_name, definition in default_definition.model_dump().items():
349-
# Replace "__" with "-" for subtypes
350-
id_name = id_name.replace("__", "-")
351-
328+
for id_name, definition in default_definition.items():
352329
# Skip if this entity type already exists
353330
if id_name in existing_entity_types:
354331
continue
355332

356333
# Create new entity type
357-
description = definition["description"].replace(
334+
description = definition.description.replace(
358335
"---vendor_name---", kg_config_settings.KG_VENDOR
359336
)
360337
grounded_source_name = (
361-
definition["grounded_source_name"].value
362-
if definition["grounded_source_name"]
338+
definition.grounded_source_name.value
339+
if definition.grounded_source_name
363340
else None
364341
)
365342
new_entity_type = KGEntityType(
366343
id_name=id_name,
367344
description=description,
368-
attributes=definition["attributes"],
369-
grounding=definition["grounding"],
345+
attributes=definition.attributes,
346+
grounding=definition.grounding,
370347
grounded_source_name=grounded_source_name,
371348
active=False,
372349
)

0 commit comments

Comments
 (0)