Skip to content

Commit 256734f

Browse files
authored
Visual changes for the privatemode ai dispaly (#190)
* Adds enum * Adds reranker step type * Add new message field * Adds data for messages * Adds score mult for requests * Submodule ref fix
1 parent 9a8b5bb commit 256734f

File tree

5 files changed

+97
-21
lines changed

5 files changed

+97
-21
lines changed

cognition_objects/conversation.py

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -358,27 +358,6 @@ def create(
358358
return conversation
359359

360360

361-
# TODO: replace usage with create from message.py and delete this function
362-
def add_message(
363-
project_id: str,
364-
conversation_id: str,
365-
user_id: str,
366-
question: str,
367-
initiated_via: str,
368-
with_commit: bool = True,
369-
) -> CognitionConversation:
370-
message_entity = message.create(
371-
conversation_id=conversation_id,
372-
project_id=project_id,
373-
user_id=user_id,
374-
question=question,
375-
initiated_via=initiated_via,
376-
with_commit=with_commit,
377-
)
378-
379-
return message_entity
380-
381-
382361
def update(
383362
project_id: str,
384363
conversation_id: str,

cognition_objects/message.py

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from ..models import CognitionMessage
66
from ..util import prevent_sql_injection
77
from .pipeline_version import get_current_version
8+
from sqlalchemy.orm.attributes import flag_modified
89

910

1011
DEFAULT_TIME_ELAPSED = {
@@ -268,6 +269,51 @@ def get_message_feedback_overview(
268269
return general.execute_all(query)
269270

270271

272+
def get_show_shield_dict_by_conversation_ids(
273+
project_id: str, conversation_ids: List[str]
274+
) -> Dict[str, bool]:
275+
if not conversation_ids or not project_id:
276+
return {}
277+
project_id = prevent_sql_injection(project_id, isinstance(project_id, str))
278+
conversation_ids = [
279+
prevent_sql_injection(conversation_id, isinstance(conversation_id, str))
280+
for conversation_id in conversation_ids
281+
]
282+
conversation_id_filter = "('" + "','".join(conversation_ids) + "')"
283+
query = f"""
284+
SELECT jsonb_object_agg(t.conversation_id::text, t.show_shield_icon) AS convo_shields
285+
FROM (
286+
SELECT
287+
c.id AS conversation_id,
288+
COALESCE(
289+
bool_and(
290+
COALESCE(
291+
(m.additional_data->'privacy_report'->>'is_private') IN ('A+','A','A-'),
292+
false
293+
)
294+
)
295+
AND
296+
bool_and(
297+
COALESCE(
298+
(m.additional_data->'privacy_report'->>'any_privatemode_ai')::boolean,
299+
false
300+
)
301+
),
302+
false
303+
) AS show_shield_icon
304+
FROM cognition.conversation c
305+
LEFT JOIN cognition.message m
306+
ON c.project_id = m.project_id AND m.conversation_id = c.id
307+
WHERE c.project_id = '{project_id}' AND c.id IN {conversation_id_filter}
308+
GROUP BY c.id
309+
) t;"""
310+
311+
show_shield_dict = general.execute_first(query)
312+
if show_shield_dict and show_shield_dict[0]:
313+
return show_shield_dict[0]
314+
return {}
315+
316+
271317
def create(
272318
conversation_id: str,
273319
project_id: str,
@@ -276,6 +322,7 @@ def create(
276322
initiated_via: str,
277323
with_commit: bool = True,
278324
created_at: Optional[datetime] = None,
325+
additional_data: Optional[Dict[str, Any]] = None,
279326
) -> CognitionMessage:
280327
version_id = None
281328
current_version = get_current_version(project_id)
@@ -289,6 +336,7 @@ def create(
289336
question=question,
290337
facts=[],
291338
version_id=version_id,
339+
additional_data=additional_data or {},
292340
initiated_via=initiated_via,
293341
)
294342

@@ -306,6 +354,7 @@ def update(
306354
feedback_value: Optional[str] = None,
307355
feedback_category: Optional[str] = None,
308356
feedback_message: Optional[str] = None,
357+
additional_data: Optional[Union[Dict[str, Any], str]] = None,
309358
with_commit: bool = True,
310359
) -> CognitionMessage:
311360
message = get(project_id, message_id)
@@ -321,6 +370,14 @@ def update(
321370
message.feedback_category = feedback_category
322371
if feedback_message is not None:
323372
message.feedback_message = feedback_message
373+
if additional_data is not None:
374+
if additional_data == "NULL":
375+
message.additional_data = {}
376+
if message.additional_data is None:
377+
message.additional_data = {}
378+
for key, value in additional_data.items():
379+
message.additional_data[key] = value
380+
flag_modified(message, "additional_data")
324381

325382
general.flush_or_commit(with_commit)
326383

enums.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -566,6 +566,7 @@ class StrategyStepType(Enum):
566566
WEBHOOK = "WEBHOOK"
567567
GRAPHRAG_SEARCH = "GRAPHRAG_SEARCH"
568568
TEMPLATED = "TEMPLATED"
569+
RERANKER = "RERANKER"
569570

570571
def get_description(self):
571572
return STEP_DESCRIPTIONS.get(self, "No description available")
@@ -594,6 +595,7 @@ def get_progress_text(self):
594595
StrategyStepType.WEBHOOK: "Webhook",
595596
StrategyStepType.GRAPHRAG_SEARCH: "Query GraphRAG index",
596597
StrategyStepType.TEMPLATED: "Templated step",
598+
StrategyStepType.RERANKER: "Reranker",
597599
}
598600

599601
STEP_WHEN_TO_USE = {
@@ -612,6 +614,7 @@ def get_progress_text(self):
612614
StrategyStepType.WEBHOOK: "When you want to run a webhook",
613615
StrategyStepType.GRAPHRAG_SEARCH: "When you want to query a knowledge graph",
614616
StrategyStepType.TEMPLATED: "When you want to reuse existing templates",
617+
StrategyStepType.RERANKER: "When you want to rerank results",
615618
}
616619

617620
STEP_PROGRESS_TEXTS = {
@@ -631,6 +634,7 @@ def get_progress_text(self):
631634
StrategyStepType.WEBHOOK: "Running webhook",
632635
StrategyStepType.GRAPHRAG_SEARCH: "Querying knowledge graph",
633636
StrategyStepType.TEMPLATED: "Running templated step",
637+
StrategyStepType.RERANKER: "Running reranker",
634638
}
635639

636640
STEP_ERRORS = {
@@ -958,6 +962,37 @@ class SharepointPropertySyncState(Enum):
958962
FAILED = "FAILED"
959963

960964

965+
class CognitionPrivateUsage(Enum):
966+
KERN_INTERNAL = "KERN_INTERNAL" # same server no connection outside VM & DB
967+
KERN_EXTERNAL_RESOURCE = (
968+
"KERN_EXTERNAL_RESOURCE" # e.g. reranker, distinct from external
969+
)
970+
WEBHOOK_NO_DATA = "WEBHOOK_NO_DATA" # webhook only sending conversation id
971+
DATA_SEND_EXTERNALLY = (
972+
"DATA_SEND_EXTERNALLY" # e.g. webhook with data or reranker outside kern
973+
)
974+
AZURE = "AZURE" # e.g. BYOK from customers
975+
AZURE_KERN = "AZURE_KERN" # e.g. sponsorship azure resources or e.g. mistral with key specific key
976+
OPEN_AI = "OPEN_AI" # hosted by openai
977+
PRIVATEMODE_AI = "PRIVATEMODE_AI" # encrypted and hosted by privatemode.ai
978+
REQUESTS_USED = "REQUESTS_USED" # special case, is set handled separately => only for final result set
979+
__SCORES = {
980+
"KERN_INTERNAL": 1.0,
981+
"KERN_EXTERNAL_RESOURCE": 0.95,
982+
"WEBHOOK_NO_DATA": 0.9,
983+
"DATA_SEND_EXTERNALLY": 0.3,
984+
"AZURE": 0.5,
985+
"AZURE_KERN": 0.85,
986+
"OPEN_AI": 0.3,
987+
"PRIVATEMODE_AI": 0.95,
988+
"REQUESTS_USED": 0.9, # final project multiplier
989+
}
990+
991+
@property
992+
def score(self) -> float:
993+
return self.__SCORES[self.value]
994+
995+
961996
class MessageInitiationType(Enum):
962997
UI = "UI"
963998
API = "API"

models.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1286,6 +1286,8 @@ class CognitionMessage(Base):
12861286
),
12871287
index=True,
12881288
)
1289+
# holds e.g. the project privacy report rating (high level) at message creation
1290+
additional_data = Column(JSON)
12891291
initiated_via = Column(String) # of type enums.MessageInitiationType.*.value
12901292

12911293

util.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import decimal
88
from uuid import UUID
99
from datetime import datetime, date
10+
from enum import Enum
1011

1112

1213
from sqlalchemy.sql import text as sql_text
@@ -225,6 +226,8 @@ def to_json_serializable(x: Any):
225226
return float(x)
226227
elif isinstance(x, UUID):
227228
return str(x)
229+
elif isinstance(x, Enum):
230+
return x.value
228231
else:
229232
return x
230233

0 commit comments

Comments
 (0)