Skip to content

Commit 3a64c87

Browse files
committed
Merge branch 'main' of https://github.com/dataease/SQLBot
2 parents c254f46 + 3139b62 commit 3a64c87

File tree

4 files changed

+57
-37
lines changed

4 files changed

+57
-37
lines changed

Dockerfile

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,12 @@ ENV SQLBOT_HOME=/opt/sqlbot
5454
ENV PYTHONPATH=${SQLBOT_HOME}/app
5555
ENV PATH="${SQLBOT_HOME}/app/.venv/bin:$PATH"
5656

57+
ENV SQLBOT_DB_HOST=localhost
58+
ENV SQLBOT_DB_PORT=5432
59+
ENV SQLBOT_DB_DB=sqlbot
60+
ENV SQLBOT_DB_USER=root
61+
ENV SQLBOT_DB_PASSWORD=Password123@pg
62+
5763
# Copy necessary files from builder
5864
COPY start.sh /opt/sqlbot/app/start.sh
5965
COPY g2-ssr/*.ttf /usr/share/fonts/truetype/liberation/

backend/apps/chat/api/chat.py

Lines changed: 33 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import traceback
44

55
import numpy as np
6+
import orjson
67
import pandas as pd
78
from fastapi import APIRouter, HTTPException
89
from fastapi.responses import StreamingResponse
@@ -147,56 +148,52 @@ async def stream_sql(session: SessionDep, current_user: CurrentUser, request_que
147148
llm_service.run_task_async()
148149
except Exception as e:
149150
traceback.print_exc()
150-
raise HTTPException(
151-
status_code=500,
152-
detail=str(e)
153-
)
151+
152+
def _err(_e: Exception):
153+
yield 'data:' + orjson.dumps({'content': str(_e), 'type': 'error'}).decode() + '\n\n'
154+
155+
return StreamingResponse(_err(e), media_type="text/event-stream")
154156

155157
return StreamingResponse(llm_service.await_result(), media_type="text/event-stream")
156158

157159

158160
@router.post("/record/{chat_record_id}/{action_type}")
159161
async def analysis_or_predict(session: SessionDep, current_user: CurrentUser, chat_record_id: int, action_type: str,
160162
current_assistant: CurrentAssistant):
161-
if action_type != 'analysis' and action_type != 'predict':
162-
raise HTTPException(
163-
status_code=404,
164-
detail="Not Found"
165-
)
166-
record: ChatRecord | None = None
167-
168-
stmt = select(ChatRecord.id, ChatRecord.question, ChatRecord.chat_id, ChatRecord.datasource, ChatRecord.engine_type,
169-
ChatRecord.ai_modal_id, ChatRecord.create_by, ChatRecord.chart, ChatRecord.data).where(
170-
and_(ChatRecord.id == chat_record_id))
171-
result = session.execute(stmt)
172-
for r in result:
173-
record = ChatRecord(id=r.id, question=r.question, chat_id=r.chat_id, datasource=r.datasource,
174-
engine_type=r.engine_type, ai_modal_id=r.ai_modal_id, create_by=r.create_by, chart=r.chart,
175-
data=r.data)
176-
177-
if not record:
178-
raise HTTPException(
179-
status_code=400,
180-
detail=f"Chat record with id {chat_record_id} not found"
181-
)
163+
try:
164+
if action_type != 'analysis' and action_type != 'predict':
165+
raise Exception(f"Type {action_type} Not Found")
166+
record: ChatRecord | None = None
167+
168+
stmt = select(ChatRecord.id, ChatRecord.question, ChatRecord.chat_id, ChatRecord.datasource,
169+
ChatRecord.engine_type,
170+
ChatRecord.ai_modal_id, ChatRecord.create_by, ChatRecord.chart, ChatRecord.data).where(
171+
and_(ChatRecord.id == chat_record_id))
172+
result = session.execute(stmt)
173+
for r in result:
174+
record = ChatRecord(id=r.id, question=r.question, chat_id=r.chat_id, datasource=r.datasource,
175+
engine_type=r.engine_type, ai_modal_id=r.ai_modal_id, create_by=r.create_by,
176+
chart=r.chart,
177+
data=r.data)
182178

183-
if not record.chart:
184-
raise HTTPException(
185-
status_code=500,
186-
detail=f"Chat record with id {chat_record_id} has not generated chart, do not support to analyze it"
187-
)
179+
if not record:
180+
raise Exception(f"Chat record with id {chat_record_id} not found")
188181

189-
request_question = ChatQuestion(chat_id=record.chat_id, question=record.question)
182+
if not record.chart:
183+
raise Exception(
184+
f"Chat record with id {chat_record_id} has not generated chart, do not support to analyze it")
185+
186+
request_question = ChatQuestion(chat_id=record.chat_id, question=record.question)
190187

191-
try:
192188
llm_service = await LLMService.create(current_user, request_question, current_assistant)
193189
llm_service.run_analysis_or_predict_task_async(action_type, record)
194190
except Exception as e:
195191
traceback.print_exc()
196-
raise HTTPException(
197-
status_code=500,
198-
detail=str(e)
199-
)
192+
193+
def _err(_e: Exception):
194+
yield 'data:' + orjson.dumps({'content': str(_e), 'type': 'error'}).decode() + '\n\n'
195+
196+
return StreamingResponse(_err(e), media_type="text/event-stream")
200197

201198
return StreamingResponse(llm_service.await_result(), media_type="text/event-stream")
202199

backend/apps/system/api/assistant.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,15 @@ async def validator(session: SessionDep, id: int, virtual: Optional[int] = Query
6464
if not db_model:
6565
return AssistantValidator()
6666
db_model = AssistantModel.model_validate(db_model)
67+
assistant_oid = 1
68+
if(db_model.type == 0):
69+
configuration = db_model.configuration
70+
config_obj = json.loads(configuration) if configuration else {}
71+
assistant_oid = config_obj.get('oid', 1)
72+
6773
access_token_expires = timedelta(minutes=settings.ACCESS_TOKEN_EXPIRE_MINUTES)
6874
assistantDict = {
69-
"id": virtual, "account": 'sqlbot-inner-assistant', "oid": 1, "assistant_id": id
75+
"id": virtual, "account": 'sqlbot-inner-assistant', "oid": assistant_oid, "assistant_id": id
7076
}
7177
access_token = create_access_token(
7278
assistantDict, expires_delta=access_token_expires

backend/apps/system/middleware/auth.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11

22
import base64
3+
import json
34
from typing import Optional
45
from fastapi import Request
56
from fastapi.responses import JSONResponse
@@ -112,6 +113,16 @@ async def validateAssistant(self, assistantToken: Optional[str], trans: I18n) ->
112113
assistant_info = await get_assistant_info(session=session, assistant_id=payload['assistant_id'])
113114
assistant_info = AssistantModel.model_validate(assistant_info)
114115
assistant_info = AssistantHeader.model_validate(assistant_info.model_dump(exclude_unset=True))
116+
if assistant_info and assistant_info.type == 0:
117+
if payload['oid']:
118+
session_user.oid = payload['oid']
119+
else:
120+
assistant_oid = 1
121+
configuration = assistant_info.configuration
122+
config_obj = json.loads(configuration) if configuration else {}
123+
assistant_oid = config_obj.get('oid', 1)
124+
session_user.oid = assistant_oid
125+
115126
return True, session_user, assistant_info
116127
except Exception as e:
117128
SQLBotLogUtil.exception(f"Assistant validation error: {str(e)}")

0 commit comments

Comments
 (0)