Skip to content
Merged
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
36 changes: 36 additions & 0 deletions backend/alembic/versions/062_update_chat_log_dll.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
"""062_update_chat_log_dll

Revision ID: c9ab05247503
Revises: 547df942eb90
Create Date: 2026-01-27 14:20:35.069255

"""
from alembic import op
import sqlalchemy as sa
import sqlmodel.sql.sqltypes
from sqlalchemy.dialects import postgresql

# revision identifiers, used by Alembic.
revision = 'c9ab05247503'
down_revision = '547df942eb90'
branch_labels = None
depends_on = None


def upgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.add_column('chat_log', sa.Column('local_operation', sa.Boolean(), nullable=True))
sql = '''
UPDATE chat_log SET local_operation = false
'''
op.execute(sql)
op.alter_column('chat_log', 'local_operation',
existing_type=sa.BOOLEAN(),
nullable=False)
# ### end Alembic commands ###


def downgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.drop_column('chat_log', 'local_operation')
# ### end Alembic commands ###
24 changes: 18 additions & 6 deletions backend/apps/chat/curd/chat.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import datetime
from typing import List, Optional
from typing import List, Optional, Union

import orjson
import sqlparse
Expand Down Expand Up @@ -62,6 +62,7 @@ def list_recent_questions(session: SessionDep, current_user: CurrentUser, dataso
)
return [record[0] for record in chat_records] if chat_records else []


def rename_chat_with_user(session: SessionDep, current_user: CurrentUser, rename_object: RenameChat) -> str:
chat = session.get(Chat, rename_object.id)
if not chat:
Expand All @@ -78,6 +79,7 @@ def rename_chat_with_user(session: SessionDep, current_user: CurrentUser, rename
session.commit()
return brief


def rename_chat(session: SessionDep, rename_object: RenameChat) -> str:
chat = session.get(Chat, rename_object.id)
if not chat:
Expand All @@ -104,6 +106,7 @@ def delete_chat(session, chart_id) -> str:

return f'Chat with id {chart_id} has been deleted'


def delete_chat_with_user(session, current_user: CurrentUser, chart_id) -> str:
chat = session.query(Chat).filter(Chat.id == chart_id).first()
if not chat:
Expand Down Expand Up @@ -220,6 +223,7 @@ def get_chat_chart_config(session: SessionDep, chat_record_id: int):
pass
return {}


def get_chart_data_with_user(session: SessionDep, current_user: CurrentUser, chat_record_id: int):
stmt = select(ChatRecord.data).where(and_(ChatRecord.id == chat_record_id, ChatRecord.create_by == current_user.id))
res = session.execute(stmt)
Expand All @@ -230,6 +234,7 @@ def get_chart_data_with_user(session: SessionDep, current_user: CurrentUser, cha
pass
return {}


def get_chat_chart_data(session: SessionDep, chat_record_id: int):
stmt = select(ChatRecord.data).where(and_(ChatRecord.id == chat_record_id))
res = session.execute(stmt)
Expand All @@ -240,8 +245,10 @@ def get_chat_chart_data(session: SessionDep, chat_record_id: int):
pass
return {}


def get_chat_predict_data_with_user(session: SessionDep, current_user: CurrentUser, chat_record_id: int):
stmt = select(ChatRecord.predict_data).where(and_(ChatRecord.id == chat_record_id, ChatRecord.create_by == current_user.id))
stmt = select(ChatRecord.predict_data).where(
and_(ChatRecord.id == chat_record_id, ChatRecord.create_by == current_user.id))
res = session.execute(stmt)
for row in res:
try:
Expand All @@ -250,6 +257,7 @@ def get_chat_predict_data_with_user(session: SessionDep, current_user: CurrentUs
pass
return {}


def get_chat_predict_data(session: SessionDep, chat_record_id: int):
stmt = select(ChatRecord.predict_data).where(and_(ChatRecord.id == chat_record_id))
res = session.execute(stmt)
Expand Down Expand Up @@ -607,10 +615,11 @@ def save_analysis_predict_record(session: SessionDep, base_record: ChatRecord, a
return result


def start_log(session: SessionDep, ai_modal_id: int, ai_modal_name: str, operate: OperationEnum, record_id: int,
full_message: list[dict]) -> ChatLog:
def start_log(session: SessionDep, ai_modal_id: int = None, ai_modal_name: str = None, operate: OperationEnum = None,
record_id: int = None, full_message: Union[list[dict], dict] = None,
local_operation: bool = False) -> ChatLog:
log = ChatLog(type=TypeEnum.CHAT, operate=operate, pid=record_id, ai_modal_id=ai_modal_id, base_modal=ai_modal_name,
messages=full_message, start_time=datetime.datetime.now())
messages=full_message, start_time=datetime.datetime.now(), local_operation=local_operation)

result = ChatLog(**log.model_dump())

Expand All @@ -623,7 +632,8 @@ def start_log(session: SessionDep, ai_modal_id: int, ai_modal_name: str, operate
return result


def end_log(session: SessionDep, log: ChatLog, full_message: list[dict], reasoning_content: str = None,
def end_log(session: SessionDep, log: ChatLog, full_message: Union[list[dict], dict, str],
reasoning_content: str = None,
token_usage=None) -> ChatLog:
if token_usage is None:
token_usage = {}
Expand Down Expand Up @@ -867,6 +877,8 @@ def save_error_message(session: SessionDep, record_id: int, message: str) -> Cha

session.commit()

# todo log error finish

return result


Expand Down
8 changes: 7 additions & 1 deletion backend/apps/chat/models/chat_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,12 @@ class OperationEnum(Enum):
GENERATE_SQL_WITH_PERMISSIONS = '5'
CHOOSE_DATASOURCE = '6'
GENERATE_DYNAMIC_SQL = '7'

CHOOSE_TABLE = '8'
FILTER_TERMS = '9'
FILTER_SQL_EXAMPLE = '10'
FILTER_CUSTOM_PROMPT = '11'
EXECUTE_SQL = '12'
GENERATE_PICTURE = '13'

class ChatFinishStep(Enum):
GENERATE_SQL = 1
Expand Down Expand Up @@ -71,6 +76,7 @@ class ChatLog(SQLModel, table=True):
start_time: datetime = Field(sa_column=Column(DateTime(timezone=False), nullable=True))
finish_time: datetime = Field(sa_column=Column(DateTime(timezone=False), nullable=True))
token_usage: Optional[dict | None | int] = Field(sa_column=Column(JSONB))
local_operation: bool = Field(default=False)


class Chat(SQLModel, table=True):
Expand Down
Loading