Skip to content

Commit 952dc14

Browse files
authored
Adds new templates (#182)
* Adds new templates * PR comments * PR comments
1 parent f9ef28a commit 952dc14

File tree

4 files changed

+212
-1
lines changed

4 files changed

+212
-1
lines changed

enums.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,7 @@ class Tablenames(Enum):
172172
CONVERSATION_TAG_ASSOCIATION = (
173173
"conversation_tag_association" # association between conversation and tags
174174
)
175+
SUMS_TABLE = "sums_table"
175176

176177
def snake_case_to_pascal_case(self):
177178
# the type name (written in PascalCase) of a table is needed to create backrefs
@@ -926,6 +927,8 @@ class AdminQueries(Enum):
926927
)
927928
CONVERSATIONS_PER_TAG = "CONVERSATIONS_PER_TAG" # parameter options: organization_id, without_kern_email, distinct_conversations
928929
MULTITAGGED_CONVERSATIONS = "MULTITAGGED_CONVERSATIONS" # parameter options: organization_id, without_kern_email
930+
TEMPLATE_USAGE = "TEMPLATE_USAGE" # parameter options: organization_id
931+
PRIVATEMODE_USE_OVER_TIME = "PRIVATEMODE_USE_OVER_TIME" # parameter options: organization_id, without_kern_email
929932

930933

931934
class CognitionIntegrationType(Enum):

global_objects/admin_queries.py

Lines changed: 79 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88

99
from .. import enums
10+
from ..global_objects import sums_table as sums_table_db_go
1011

1112
ENGINEERING_TEAM_INDICATOR = "ENGINEERING_TEAM"
1213
PERIOD_OPTIONS = {"days", "weeks", "months"}
@@ -47,10 +48,87 @@ def get_result_admin_query(
4748
return __get_conversations_per_tag(**parameters, as_query=as_query)
4849
elif query == enums.AdminQueries.MULTITAGGED_CONVERSATIONS:
4950
return __get_multitagged_conversations(**parameters, as_query=as_query)
50-
51+
elif query == enums.AdminQueries.TEMPLATE_USAGE:
52+
return __get_template_usage(**parameters, as_query=as_query)
53+
elif query == enums.AdminQueries.PRIVATEMODE_USE_OVER_TIME:
54+
return __get_privatemode_use_over_time(**parameters, as_query=as_query)
5155
return []
5256

5357

58+
def __get_template_usage(organization_id: str = "", as_query: bool = False):
59+
60+
org_where = ""
61+
if organization_id:
62+
org_where = f"WHERE o.id = '{organization_id}'"
63+
64+
query = f"""
65+
SELECT o.name organization_name, COUNT(st.id) created_templates, COUNT(c.template_id) templates_in_use, COALESCE(SUM(c.uses),0) template_uses
66+
FROM cognition.step_templates st
67+
LEFT JOIN (
68+
SELECT (ss.config->>'templateId')::UUID template_id, COUNT(*) uses
69+
FROM cognition.strategy_step ss
70+
WHERE ss.step_type = 'TEMPLATED'
71+
group BY 1
72+
)C
73+
ON st.id = c.template_id
74+
INNER JOIN organization o
75+
ON st.organization_id = o.id
76+
{org_where}
77+
group BY 1"""
78+
if as_query:
79+
return query
80+
return general.execute_all(query)
81+
82+
83+
def __get_privatemode_use_over_time(
84+
organization_id: str = "", without_kern_email: bool = False, as_query: bool = False
85+
):
86+
org_where = ""
87+
if organization_id:
88+
org_where = f"AND t.organization_id = '{organization_id}'"
89+
90+
kern_where = ""
91+
if without_kern_email:
92+
kern_where = "AND t.is_kern_user = FALSE"
93+
94+
query = f"""
95+
WITH full_sums AS (
96+
SELECT st.data
97+
FROM GLOBAL.sums_table st
98+
WHERE st.sum_key = '{enums.AdminQueries.PRIVATEMODE_USE_OVER_TIME.value}'
99+
UNION ALL
100+
{sums_table_db_go.get_privatemode_sum_snapshot(as_query=True).replace(" - INTERVAL '1 day'", "")}
101+
)
102+
103+
SELECT *
104+
FROM (
105+
SELECT
106+
st.data->>'counted_for' date,
107+
t.organization_name,
108+
t.project_name,
109+
SUM(t.count)
110+
FROM full_sums st,
111+
json_to_recordset( (st.data->'values') )
112+
AS t(organization_id uuid, organization_name TEXT, project_id uuid, project_name TEXT, is_kern_user BOOLEAN, COUNT int)
113+
WHERE st.data->>'values' IS NOT NULL
114+
{org_where} {kern_where}
115+
GROUP BY 1,2,3
116+
UNION ALL
117+
SELECT
118+
st.data->>'counted_for' date,
119+
'<no values>',
120+
'<no values>',
121+
0
122+
FROM full_sums st
123+
WHERE st.data->>'values' IS NULL
124+
)x
125+
ORDER BY 1 DESC
126+
"""
127+
if as_query:
128+
return query
129+
return general.execute_all(query)
130+
131+
54132
def __get_multitagged_conversations(
55133
organization_id: str = "",
56134
without_kern_email: bool = False,

global_objects/sums_table.py

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
from typing import Any, Dict, List, Union
2+
from datetime import datetime, timedelta
3+
4+
from ..business_objects import general
5+
from ..session import session
6+
from ..models import SumsTable
7+
from ..enums import LLMProvider, StrategyStepType
8+
9+
10+
def get(id: str) -> SumsTable:
11+
return (
12+
session.query(SumsTable)
13+
.filter(
14+
SumsTable.id == id,
15+
)
16+
.first()
17+
)
18+
19+
20+
def get_all_by_key(sum_key: str) -> List[SumsTable]:
21+
return (
22+
session.query(SumsTable)
23+
.filter(
24+
SumsTable.sum_key == sum_key,
25+
)
26+
.all()
27+
)
28+
29+
30+
def get_last_execution_by_key(sum_key: str) -> datetime:
31+
entry = (
32+
session.query(SumsTable)
33+
.filter(
34+
SumsTable.sum_key == sum_key,
35+
)
36+
.order_by(SumsTable.created_at.desc())
37+
.first()
38+
)
39+
if entry:
40+
return entry.created_at
41+
return None
42+
43+
44+
def get_privatemode_sum_snapshot(as_query: bool = False) -> Dict[str, Any]:
45+
## counts messages created with something related to privatemode
46+
## this means either tmp_doc, llm or templated values
47+
## the messages are count distinct for the previous day to understand when and how much it's used
48+
## deleted messages on the fly are not included, for this we would need to track them on creation
49+
query = f"""
50+
SELECT json_build_object('counted_for',(CURRENT_DATE - INTERVAL '1 day')::date,'values',array_agg(row_to_json(y)))
51+
FROM (
52+
SELECT o.id organization_id, o.name organization_name, p.id project_id, p.name project_name, is_kern_user, COUNT(*)
53+
FROM (
54+
SELECT pl.project_id, pl.message_id, CASE WHEN u.email LIKE '%@kern.ai' THEN TRUE ELSE FALSE END is_kern_user
55+
FROM cognition.pipeline_logs pl
56+
INNER JOIN cognition.strategy_step ss
57+
ON pl.project_id = ss.project_id AND pl.strategy_step_id = ss.id
58+
INNER JOIN PUBLIC.user u
59+
ON pl.created_by = u.id
60+
WHERE pl.created_at::date = CURRENT_DATE - INTERVAL '1 day'
61+
AND ss.step_type IN ('{StrategyStepType.LLM.value}' ,'{StrategyStepType.QUERY_REPHRASING.value}','{StrategyStepType.HEADER.value}')
62+
AND ss.config->>'llmIdentifier' = '{LLMProvider.PRIVATEMODE_AI.value}'
63+
UNION
64+
SELECT pl.project_id, pl.message_id, CASE WHEN u.email LIKE '%@kern.ai' THEN TRUE ELSE FALSE END is_kern_user
65+
FROM cognition.pipeline_logs pl
66+
INNER JOIN cognition.strategy_step ss
67+
ON pl.project_id = ss.project_id AND pl.strategy_step_id = ss.id AND ss.step_type = '{StrategyStepType.TEMPLATED.value}'
68+
INNER JOIN cognition.step_templates st
69+
ON (ss.config->>'templateId')::UUID = st.id
70+
INNER JOIN PUBLIC.user u
71+
ON pl.created_by = u.id
72+
WHERE pl.created_at::date = CURRENT_DATE - INTERVAL '1 day'
73+
AND pl.strategy_step_type = '{StrategyStepType.TEMPLATED.value}'
74+
AND st.config::jsonb -> 'steps' @> '[{{"config": {{"llmIdentifier": "{LLMProvider.PRIVATEMODE_AI.value}"}}}}]'
75+
UNION
76+
SELECT pl.project_id, pl.message_id, CASE WHEN u.email LIKE '%@kern.ai' THEN TRUE ELSE FALSE END is_kern_user
77+
FROM cognition.pipeline_logs pl
78+
INNER JOIN cognition.project p
79+
ON pl.project_id = p.id
80+
INNER JOIN PUBLIC.user u
81+
ON pl.created_by = u.id
82+
WHERE pl.created_at::date = CURRENT_DATE - INTERVAL '1 day'
83+
AND pl.strategy_step_type = '{StrategyStepType.TMP_DOC_RETRIEVAL.value}'
84+
AND (p.llm_config::jsonb -> 'extraction' ->> 'llmIdentifier' = 'PRIVATEMODE_AI' --written like the enum here so not interpolated
85+
OR p.llm_config::jsonb -> 'transformation' ->> 'llmIdentifier' = 'PRIVATEMODE_AI') --written like the enum here so not interpolated
86+
) x
87+
INNER JOIN cognition.project p
88+
ON x.project_id = p.id
89+
INNER JOIN organization o
90+
ON p.organization_id = o.id
91+
group BY o.id, p.id,is_kern_user
92+
) y """
93+
if as_query:
94+
return query
95+
result = general.execute_first(query)
96+
if result and result[0]:
97+
return result[0]
98+
return None
99+
100+
101+
def create(
102+
sum_key: str,
103+
data: Union[List[Any], Dict[str, Any]],
104+
with_commit: bool = True,
105+
) -> SumsTable:
106+
obj = SumsTable(
107+
sum_key=sum_key,
108+
data=data,
109+
)
110+
general.add(obj, with_commit)
111+
112+
return obj
113+
114+
115+
def clean_old_entries(sum_key: str, delta: timedelta, with_commit: bool = True) -> None:
116+
117+
session.query(SumsTable).filter(
118+
SumsTable.sum_key == sum_key,
119+
SumsTable.created_at < datetime.now() - delta,
120+
).delete()
121+
general.flush_or_commit(with_commit)

models.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2422,3 +2422,12 @@ class CognitionConversationTagAssociation(Base):
24222422
index=True,
24232423
)
24242424
created_at = Column(DateTime, default=sql.func.now())
2425+
2426+
2427+
class SumsTable(Base):
2428+
__tablename__ = Tablenames.SUMS_TABLE.value
2429+
__table_args__ = {"schema": "global"}
2430+
id = Column(UUID(as_uuid=True), primary_key=True, default=uuid.uuid4)
2431+
sum_key = Column(String, index=True) # e.g. enums.AdminQueries
2432+
created_at = Column(DateTime, default=sql.func.now())
2433+
data = Column(JSON)

0 commit comments

Comments
 (0)