Skip to content

Commit 0b7bf18

Browse files
committed
feat: add get_chat_with_data
1 parent 4a82702 commit 0b7bf18

File tree

2 files changed

+31
-3
lines changed

2 files changed

+31
-3
lines changed

backend/apps/chat/api/chat.py

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
from fastapi.responses import StreamingResponse
99

1010
from apps.chat.curd.chat import list_chats, get_chat_with_records, create_chat, rename_chat, \
11-
delete_chat, get_chat_chart_data, get_chat_predict_data
11+
delete_chat, get_chat_chart_data, get_chat_predict_data, get_chat_with_records_with_data
1212
from apps.chat.models.chat_model import CreateChat, ChatRecord, RenameChat, ChatQuestion, ExcelData
1313
from apps.chat.task.llm import LLMService
1414
from common.core.deps import CurrentAssistant, SessionDep, CurrentUser
@@ -33,6 +33,18 @@ async def get_chat(session: SessionDep, current_user: CurrentUser, chart_id: int
3333
)
3434

3535

36+
@router.get("/get/with_data/{chart_id}")
37+
async def get_chat_with_data(session: SessionDep, current_user: CurrentUser, chart_id: int, current_assistant: CurrentAssistant):
38+
try:
39+
return get_chat_with_records_with_data(chart_id=chart_id, session=session, current_user=current_user,
40+
current_assistant=current_assistant)
41+
except Exception as e:
42+
raise HTTPException(
43+
status_code=500,
44+
detail=str(e)
45+
)
46+
47+
3648
@router.get("/record/get/{chart_record_id}/data")
3749
async def chat_record_data(session: SessionDep, chart_record_id: int):
3850
try:
@@ -205,7 +217,8 @@ def inner():
205217

206218
buffer = io.BytesIO()
207219

208-
with pd.ExcelWriter(buffer, engine='xlsxwriter', engine_kwargs={'options': {'strings_to_numbers': True}}) as writer:
220+
with pd.ExcelWriter(buffer, engine='xlsxwriter',
221+
engine_kwargs={'options': {'strings_to_numbers': True}}) as writer:
209222
df.to_excel(writer, sheet_name='Sheet1', index=False)
210223

211224
buffer.seek(0)

backend/apps/chat/curd/chat.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,13 @@ def get_chat_predict_data(session: SessionDep, chart_record_id: int):
6767
return {}
6868

6969

70+
def get_chat_with_records_with_data(session: SessionDep, chart_id: int, current_user: CurrentUser,
71+
current_assistant: CurrentAssistant) -> ChatInfo:
72+
return get_chat_with_records(session, chart_id, current_user, current_assistant, True)
73+
74+
7075
def get_chat_with_records(session: SessionDep, chart_id: int, current_user: CurrentUser,
71-
current_assistant: CurrentAssistant) -> ChatInfo:
76+
current_assistant: CurrentAssistant, with_data: bool = False) -> ChatInfo:
7277
chat = session.get(Chat, chart_id)
7378
if not chat:
7479
raise Exception(f"Chat with id {chart_id} not found")
@@ -96,6 +101,16 @@ def get_chat_with_records(session: SessionDep, chart_id: int, current_user: Curr
96101
ChatRecord.recommended_question, ChatRecord.first_chat,
97102
ChatRecord.finish, ChatRecord.error).where(
98103
and_(ChatRecord.create_by == current_user.id, ChatRecord.chat_id == chart_id)).order_by(ChatRecord.create_time)
104+
if with_data:
105+
stmt = select(ChatRecord.id, ChatRecord.chat_id, ChatRecord.create_time, ChatRecord.finish_time,
106+
ChatRecord.question, ChatRecord.sql_answer, ChatRecord.sql,
107+
ChatRecord.chart_answer, ChatRecord.chart, ChatRecord.analysis, ChatRecord.predict,
108+
ChatRecord.datasource_select_answer, ChatRecord.analysis_record_id, ChatRecord.predict_record_id,
109+
ChatRecord.recommended_question, ChatRecord.first_chat,
110+
ChatRecord.finish, ChatRecord.error, ChatRecord.data, ChatRecord.predict_data).where(
111+
and_(ChatRecord.create_by == current_user.id, ChatRecord.chat_id == chart_id)).order_by(
112+
ChatRecord.create_time)
113+
99114
result = session.execute(stmt).all()
100115
record_list: list[ChatRecord] = []
101116
for row in result:

0 commit comments

Comments
 (0)