Skip to content

init commit rework #70

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 26 commits into
base: dev
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 30 additions & 1 deletion business_objects/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,9 @@ def get_zero_shot_project_config(project_id: str, payload_id: str) -> Any:


def get_or_create_queue_project(
org_id: str, user_id: str, with_commit: bool = False
org_id: str,
user_id: str,
with_commit: bool = False,
) -> Project:
## user_id is a "last used by" indicator

Expand Down Expand Up @@ -244,12 +246,37 @@ def get_or_create_queue_project(
return prj


def create_dataset_project(
org_id: str,
user_id: str,
name: str,
description: str,
tokenizer: str,
with_commit: bool = False,
) -> Project:
## user_id is a "last used by" indicator

prj = create(
organization_id=org_id,
name=name,
description=description,
created_by=user_id,
tokenizer=tokenizer,
tokenizer_blank=tokenizer[:2],
with_commit=with_commit,
)

return prj


def create(
organization_id: str,
name: str,
description: str,
created_by: str,
created_at: Optional[str] = None,
tokenizer: Optional[str] = None,
tokenizer_blank: Optional[str] = None,
with_commit: bool = False,
status: enums.ProjectStatus = enums.ProjectStatus.INIT_UPLOAD,
) -> Project:
Expand All @@ -260,6 +287,8 @@ def create(
created_by=created_by,
created_at=created_at,
status=status.value,
tokenizer=tokenizer,
tokenizer_blank=tokenizer_blank,
)
general.add(project, with_commit)
return project
Expand Down
8 changes: 8 additions & 0 deletions business_objects/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,14 @@ def update_organization(
general.flush_or_commit(with_commit)


def update_language_display(
user_id: str, language_display: str, with_commit: bool = False
) -> None:
user = get(user_id)
user.language_display = language_display
general.flush_or_commit(with_commit)


def __create_migration_user() -> str:
organization_item = organization.get_by_name("migration")
if not organization_item:
Expand Down
90 changes: 90 additions & 0 deletions cognition_objects/consumption_log.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
from typing import Dict, List, Optional, Tuple, Any

from datetime import datetime

from submodules.model import enums

from ..cognition_objects import message
from ..business_objects import general
from ..session import session
from ..models import CognitionConsumptionLog


def get(project_id: str, log_id: str) -> CognitionConsumptionLog:
return (
session.query(CognitionConsumptionLog)
.filter(
CognitionConsumptionLog.project_id == project_id,
CognitionConsumptionLog.id == log_id,
)
.first()
)


def get_all_by_project_id(
project_id: str,
) -> List[CognitionConsumptionLog]:
return (
session.query(CognitionConsumptionLog)
.filter(
CognitionConsumptionLog.project_id == project_id,
)
.order_by(CognitionConsumptionLog.created_at.asc())
.all()
)


def get_all_by_project_id_for_year(
project_id: str,
year: int,
) -> List[CognitionConsumptionLog]:
return (
session.query(CognitionConsumptionLog)
.filter(
CognitionConsumptionLog.project_id == project_id,
CognitionConsumptionLog.created_at >= datetime(year, 1, 1),
CognitionConsumptionLog.created_at < datetime(year + 1, 1, 1),
)
.order_by(CognitionConsumptionLog.created_at.asc())
.all()
)


def create(
project_id: str,
user_id: str,
conversation_id: str,
message_id: str,
state: str,
with_commit: bool = True,
created_at: Optional[datetime] = None,
) -> CognitionConsumptionLog:
conversation: CognitionConsumptionLog = CognitionConsumptionLog(
project_id=project_id,
created_by=user_id,
conversation_id=conversation_id,
message_id=message_id,
created_at=created_at,
state=state,
)
general.add(conversation, with_commit)
return conversation


def update(
project_id: str,
log_id: str,
strategy_id: Optional[str] = None,
complexity: Optional[str] = None,
state: Optional[str] = None,
with_commit: bool = True,
) -> CognitionConsumptionLog:
log = get(project_id=project_id, log_id=log_id)
if strategy_id is not None:
log.strategy_id = strategy_id
if complexity is not None:
log.complexity = complexity
if state is not None:
log.state = state
general.flush_or_commit(with_commit)
return log
45 changes: 44 additions & 1 deletion cognition_objects/conversation.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,19 @@ def get(project_id: str, conversation_id: str) -> CognitionConversation:
)


def get_all_by_spreadsheet_row_id(
spreadsheet_row_id: str,
) -> List[CognitionConversation]:
return (
session.query(CognitionConversation)
.filter(
CognitionConversation.synopsis_spreadsheet_row_id == spreadsheet_row_id,
)
.order_by(CognitionConversation.created_at.asc())
.all()
)


def get_all_paginated_by_project_id(
project_id: str, page: int, limit: int
) -> Tuple[int, int, List[CognitionConversation]]:
Expand All @@ -42,7 +55,7 @@ def get_all_paginated_by_project_id(
paginated_result = (
session.query(CognitionConversation)
.filter(CognitionConversation.project_id == project_id)
.order_by(CognitionConversation.created_at.asc())
.order_by(CognitionConversation.created_at.desc())
.limit(limit)
.offset((page - 1) * limit)
.all()
Expand All @@ -56,13 +69,17 @@ def create(
project_id: str,
user_id: str,
with_commit: bool = True,
synopsis_column: Optional[str] = None,
spreadsheet_row_id: Optional[str] = None,
created_at: Optional[datetime] = None,
) -> CognitionConversation:
conversation: CognitionConversation = CognitionConversation(
project_id=project_id,
created_by=user_id,
created_at=created_at,
scope_dict={},
synopsis_column=synopsis_column,
synopsis_spreadsheet_row_id=spreadsheet_row_id,
)
general.add(conversation, with_commit)
return conversation
Expand Down Expand Up @@ -90,11 +107,28 @@ def update(
project_id: str,
conversation_id: str,
scope_dict: Optional[Dict[str, Any]] = None,
header: Optional[str] = None,
error: Optional[str] = None,
with_commit: bool = True,
) -> CognitionConversation:
conversation_entity = get(project_id, conversation_id)
if scope_dict is not None:
conversation_entity.scope_dict = scope_dict
if header is not None:
conversation_entity.header = header
if error is not None:
conversation_entity.error = error
general.flush_or_commit(with_commit)
return conversation_entity


def clear_error(
project_id: str,
conversation_id: str,
with_commit: bool = True,
) -> CognitionConversation:
conversation_entity = get(project_id, conversation_id)
conversation_entity.error = None
general.flush_or_commit(with_commit)
return conversation_entity

Expand All @@ -104,6 +138,9 @@ def update_message(
conversation_id: str,
message_id: str,
answer: Optional[str] = None,
feedback_value: Optional[str] = None,
feedback_category: Optional[str] = None,
feedback_message: Optional[str] = None,
strategy_id: Optional[str] = None,
scope_dict_diff_previous_conversation: Optional[Dict[str, Any]] = None,
with_commit: bool = True,
Expand All @@ -113,6 +150,12 @@ def update_message(
message_entity.strategy_id = strategy_id
if answer is not None:
message_entity.answer = answer
if feedback_value is not None:
message_entity.feedback_value = feedback_value
if feedback_category is not None:
message_entity.feedback_category = feedback_category
if feedback_message is not None:
message_entity.feedback_message = feedback_message
if scope_dict_diff_previous_conversation is not None:
message_entity.scope_dict_diff_previous_conversation = (
scope_dict_diff_previous_conversation
Expand Down
16 changes: 10 additions & 6 deletions cognition_objects/markdown_dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,17 +52,21 @@ def get_enriched(org_id: str, id: str) -> Dict[str, Any]:

def get_all_paginated_for_category_origin(
org_id: str,
category_origin: str,
page: int,
limit: int,
category_origin: Optional[str] = None,
) -> Tuple[int, int, List[CognitionMarkdownDataset]]:
total_count = (
session.query(CognitionMarkdownDataset.id)
.filter(CognitionMarkdownDataset.organization_id == org_id)
.filter(CognitionMarkdownDataset.category_origin == category_origin)
.count()
query = session.query(CognitionMarkdownDataset.id).filter(
CognitionMarkdownDataset.organization_id == org_id
)

if category_origin is not None:
query = query.filter(
CognitionMarkdownDataset.category_origin == category_origin
)

total_count = query.count()

num_pages = int(total_count / limit)
if total_count % limit > 0:
num_pages += 1
Expand Down
32 changes: 31 additions & 1 deletion cognition_objects/message.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import List, Optional
from typing import Any, Dict, List, Optional
from datetime import datetime
from ..business_objects import general
from ..session import session
Expand Down Expand Up @@ -92,6 +92,36 @@ def create(
return message


def update(
project_id: str,
message_id: str,
answer: Optional[str] = None,
facts: Optional[List[Dict[str, Any]]] = None,
selection_widget: Optional[List[Dict[str, Any]]] = None,
feedback_value: Optional[str] = None,
feedback_category: Optional[str] = None,
feedback_message: Optional[str] = None,
with_commit: bool = True,
) -> CognitionMessage:
message = get(project_id, message_id)
if answer is not None:
message.answer = answer
if facts is not None:
message.facts = facts
if selection_widget is not None:
message.selection_widget = selection_widget
if feedback_value is not None:
message.feedback_value = feedback_value
if feedback_category is not None:
message.feedback_category = feedback_category
if feedback_message is not None:
message.feedback_message = feedback_message

general.flush_or_commit(with_commit)

return message


def delete(project_id: str, message_id: str, with_commit: bool = True) -> None:
session.query(CognitionMessage).filter(
CognitionMessage.project_id == project_id,
Expand Down
2 changes: 2 additions & 0 deletions cognition_objects/pipeline_log.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ def create(
time_elapsed: float,
record_dict_diff_previous: Dict[str, Any],
scope_dict_diff_previous: Dict[str, Any],
skipped_step: Optional[bool] = None,
with_commit: bool = True,
created_at: Optional[datetime] = None,
) -> CognitionPipelineLogs:
Expand All @@ -80,6 +81,7 @@ def create(
time_elapsed=time_elapsed,
record_dict_diff_previous_message=record_dict_diff_previous,
scope_dict_diff_previous_message=scope_dict_diff_previous,
skipped_step=skipped_step,
)

general.add(log, with_commit)
Expand Down
Loading