Skip to content

Commit 0e2ed07

Browse files
committed
Merge branch 'main' of https://github.com/dataease/SQLBot
2 parents 955f2d0 + 0a3432f commit 0e2ed07

File tree

25 files changed

+49
-126
lines changed

25 files changed

+49
-126
lines changed

.env.example

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,7 @@
1-
2-
# Before running, please copy .env.example to .env
3-
DOMAIN=localhost
4-
FRONTEND_HOST=http://localhost:5173
5-
ENVIRONMENT=local
61
PROJECT_NAME="SQLBot"
7-
STACK_NAME=SQLBot
8-
92
# Backend
103
BACKEND_CORS_ORIGINS="http://localhost,http://localhost:5173,https://localhost,https://localhost:5173"
114
SECRET_KEY=y5txe1mRmS_JpOrUzFzHEu-kIQn3lf7ll0AOv9DQh0s
12-
FIRST_SUPERUSER=[email protected]
13-
FIRST_SUPERUSER_PASSWORD=123456 # Change this to your pwd
145

156
TOKEN_KEY="X-SQLBOT-TOKEN"
167
DEFAULT_PWD="SQLBot@123456"
@@ -22,7 +13,6 @@ LOG_FORMAT="%(asctime)s - %(name)s - %(levelname)s:%(lineno)d - %(message)s"
2213
SQL_DEBUG=False
2314

2415
CACHE_TYPE="memory"
25-
CACHE_REDIS_URL="redis://127.0.0.1:6379"
2616

2717
# Postgres
2818
POSTGRES_SERVER=localhost
@@ -31,12 +21,6 @@ POSTGRES_DB=sqlbot
3121
POSTGRES_USER=root
3222
POSTGRES_PASSWORD=132456 # Change this to your pwd
3323

34-
SENTRY_DSN=
35-
36-
# Configure these with your own Docker registry images
37-
DOCKER_IMAGE_BACKEND=backend
38-
DOCKER_IMAGE_FRONTEND=frontend
39-
4024
MCP_IMAGE_PATH=/opt/sqlbot/images
4125
MCP_IMAGE_HOST=http://localhost:3000
4226
SERVER_IMAGE_HOST=https://sqlbot.fit2cloud.cn/images/

backend/apps/chat/task/llm.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -439,6 +439,7 @@ def select_datasource(self):
439439
_ds = self.out_ds_instance.get_ds(data['id'])
440440
self.ds = _ds
441441
self.chat_question.engine = _ds.type
442+
self.chat_question.db_schema = self.out_ds_instance.get_db_schema(self.ds.id)
442443
_engine_type = self.chat_question.engine
443444
_chat.engine_type = _ds.type
444445
else:
@@ -448,6 +449,7 @@ def select_datasource(self):
448449
raise Exception(f"Datasource configuration with id {_datasource} not found")
449450
self.ds = CoreDatasource(**_ds.model_dump())
450451
self.chat_question.engine = _ds.type_name if _ds.type != 'excel' else 'PostgreSQL'
452+
self.chat_question.db_schema = get_table_schema(session=self.session, current_user=self.current_user, ds=self.ds)
451453
_engine_type = self.chat_question.engine
452454
_chat.engine_type = _ds.type_name
453455
# save chat
@@ -834,7 +836,8 @@ def run_task(self, in_chat: bool = True):
834836
self.ds.id) if self.out_ds_instance else get_table_schema(session=self.session,
835837
current_user=self.current_user,
836838
ds=self.ds)
837-
839+
else:
840+
self.validate_history_ds()
838841
# generate sql
839842
sql_res = self.generate_sql()
840843
full_sql_text = ''
@@ -1023,7 +1026,13 @@ def run_analysis_or_predict_task(self, action_type: str):
10231026
finally:
10241027
# end
10251028
pass
1026-
1029+
1030+
def validate_history_ds(self):
1031+
_invalid_ds = False
1032+
_ds = self.ds
1033+
_assistant = self.current_assistant
1034+
if _invalid_ds:
1035+
yield orjson.dumps({'content': 'ds is invalid', 'type': 'error'}).decode() + '\n\n'
10271036

10281037
def execute_sql_with_db(db: SQLDatabase, sql: str) -> str:
10291038
"""Execute SQL query using SQLDatabase

backend/common/core/config.py

Lines changed: 1 addition & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,14 @@
11
import secrets
2-
import warnings
32
from typing import Annotated, Any, Literal
43

54
from pydantic import (
65
AnyUrl,
76
BeforeValidator,
8-
EmailStr,
9-
HttpUrl,
107
PostgresDsn,
118
computed_field,
12-
model_validator,
139
)
1410
from pydantic_core import MultiHostUrl
1511
from pydantic_settings import BaseSettings, SettingsConfigDict
16-
from typing_extensions import Self
1712

1813

1914
def parse_cors(v: Any) -> list[str] | str:
@@ -31,12 +26,12 @@ class Settings(BaseSettings):
3126
env_ignore_empty=True,
3227
extra="ignore",
3328
)
29+
PROJECT_NAME: str = "SQLBot"
3430
API_V1_STR: str = "/api/v1"
3531
SECRET_KEY: str = secrets.token_urlsafe(32)
3632
# 60 minutes * 24 hours * 8 days = 8 days
3733
ACCESS_TOKEN_EXPIRE_MINUTES: int = 60 * 24 * 8
3834
FRONTEND_HOST: str = "http://localhost:5173"
39-
ENVIRONMENT: Literal["local", "staging", "production"] = "local"
4035

4136
BACKEND_CORS_ORIGINS: Annotated[
4237
list[AnyUrl] | str, BeforeValidator(parse_cors)
@@ -49,8 +44,6 @@ def all_cors_origins(self) -> list[str]:
4944
self.FRONTEND_HOST
5045
]
5146

52-
PROJECT_NAME: str
53-
SENTRY_DSN: HttpUrl | None = None
5447
POSTGRES_SERVER: str
5548
POSTGRES_PORT: int = 5432
5649
POSTGRES_USER: str
@@ -81,55 +74,8 @@ def SQLALCHEMY_DATABASE_URI(self) -> PostgresDsn:
8174
path=self.POSTGRES_DB,
8275
)
8376

84-
SMTP_TLS: bool = True
85-
SMTP_SSL: bool = False
86-
SMTP_PORT: int = 587
87-
SMTP_HOST: str | None = None
88-
SMTP_USER: str | None = None
89-
SMTP_PASSWORD: str | None = None
90-
EMAILS_FROM_EMAIL: EmailStr | None = None
91-
EMAILS_FROM_NAME: EmailStr | None = None
9277
MCP_IMAGE_PATH: str
9378
MCP_IMAGE_HOST: str
9479
SERVER_IMAGE_HOST: str
9580

96-
@model_validator(mode="after")
97-
def _set_default_emails_from(self) -> Self:
98-
if not self.EMAILS_FROM_NAME:
99-
self.EMAILS_FROM_NAME = self.PROJECT_NAME
100-
return self
101-
102-
EMAIL_RESET_TOKEN_EXPIRE_HOURS: int = 48
103-
104-
@computed_field # type: ignore[prop-decorator]
105-
@property
106-
def emails_enabled(self) -> bool:
107-
return bool(self.SMTP_HOST and self.EMAILS_FROM_EMAIL)
108-
109-
EMAIL_TEST_USER: EmailStr = "[email protected]"
110-
FIRST_SUPERUSER: EmailStr
111-
FIRST_SUPERUSER_PASSWORD: str
112-
113-
def _check_default_secret(self, var_name: str, value: str | None) -> None:
114-
if value == "changethis":
115-
message = (
116-
f'The value of {var_name} is "changethis", '
117-
"for security, please change it, at least for deployments."
118-
)
119-
if self.ENVIRONMENT == "local":
120-
warnings.warn(message, stacklevel=1)
121-
else:
122-
raise ValueError(message)
123-
124-
@model_validator(mode="after")
125-
def _enforce_non_default_secrets(self) -> Self:
126-
self._check_default_secret("SECRET_KEY", self.SECRET_KEY)
127-
self._check_default_secret("POSTGRES_PASSWORD", self.POSTGRES_PASSWORD)
128-
self._check_default_secret(
129-
"FIRST_SUPERUSER_PASSWORD", self.FIRST_SUPERUSER_PASSWORD
130-
)
131-
132-
return self
133-
134-
13581
settings = Settings() # type: ignore

backend/main.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
from fastapi.concurrency import asynccontextmanager
2-
import sentry_sdk
32
from fastapi import FastAPI
43
from fastapi.routing import APIRoute
54
from starlette.middleware.cors import CORSMiddleware
@@ -38,9 +37,6 @@ def custom_generate_unique_id(route: APIRoute) -> str:
3837
return f"{tag}-{route.name}"
3938

4039

41-
if settings.SENTRY_DSN and settings.ENVIRONMENT != "local":
42-
sentry_sdk.init(dsn=str(settings.SENTRY_DSN), enable_tracing=True)
43-
4440
app = FastAPI(
4541
title=settings.PROJECT_NAME,
4642
openapi_url=f"{settings.API_V1_STR}/openapi.json",

backend/template.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ template:
66
任务:
77
根据给定的表结构(M-Schema)和用户问题生成符合{engine}数据库引擎规范的sql语句,以及sql中所用到的表名(不要包含schema和database,用数组返回)。
88
你必须遵守以下规则:
9+
- 不要编造没有提供给你的表结构
910
- 生成的SQL必须符合{engine}的规范。
1011
- 你的回答必须使用如下JSON格式返回:
1112
{{"success":true,"sql":"生成的SQL语句","tables":["表名1","表名2",...]}}

frontend/public/assistant.js

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
<path d="M9.95317 8.73169L15.5511 3.13376C15.7138 2.97104 15.9776 2.97104 16.1403 3.13376L16.7296 3.72301C16.8923 3.88573 16.8923 4.14955 16.7296 4.31227L11.1317 9.9102L16.7296 15.5081C16.8923 15.6708 16.8923 15.9347 16.7296 16.0974L16.1403 16.6866C15.9776 16.8494 15.7138 16.8494 15.5511 16.6866L9.95317 11.0887L4.35524 16.6866C4.19252 16.8494 3.9287 16.8494 3.76598 16.6866L3.17673 16.0974C3.01401 15.9347 3.01401 15.6708 3.17673 15.5081L8.77465 9.9102L3.17673 4.31227C3.01401 4.14955 3.01401 3.88573 3.17673 3.72301L3.76598 3.13376C3.9287 2.97104 4.19252 2.97104 4.35524 3.13376L9.95317 8.73169Z" fill="#ffffff"></path>
2323
</svg>
2424
</div>
25-
25+
2626
<div class="sqlbot-assistant-title"> 🌟 遇见问题,不再有障碍!</div>
2727
<p>你好,我是你的智能小助手。<br/>
2828
点我,开启高效解答模式,让问题变成过去式。</p>
@@ -217,9 +217,9 @@
217217
bottom: 0!important;
218218
}
219219
}
220-
220+
221221
/* 引导 */
222-
222+
223223
#sqlbot-assistant .sqlbot-assistant-mask {
224224
position: fixed;
225225
z-index: 10001;
@@ -289,7 +289,7 @@
289289
right: 20px;
290290
top: 20px;
291291
cursor: pointer;
292-
292+
293293
}
294294
#sqlbot-assistant-chat-container {
295295
width: 460px;
@@ -303,7 +303,7 @@
303303
right: 0 !important;
304304
}
305305
}
306-
306+
307307
#sqlbot-assistant .sqlbot-assistant-chat-button{
308308
position: fixed;
309309
${data.x_type}: ${data.x_value}px;
@@ -362,7 +362,7 @@
362362
from {
363363
height: 0;;
364364
}
365-
365+
366366
to {
367367
height: 600px;
368368
}
@@ -433,7 +433,6 @@
433433
}
434434
if (event.data?.busi == 'ready' && event.data?.ready) {
435435
const certificate = parsrCertificate(data)
436-
console.log(certificate)
437436
params = {
438437
busi: 'certificate',
439438
certificate,

frontend/src/components/filter-text/index.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,6 @@ const valueText = (field: string, val: string, options: any[]) => {
5858
return val
5959
}
6060
export const convertFilterText = (conditions: any[], options: any[]) => {
61-
console.log('conditions', conditions, options)
62-
6361
const result: any[] = []
6462
conditions.forEach((condition) => {
6563
const field = condition.field

frontend/src/i18n/zh-CN.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
"add_dashboard_name_tips": "请输入仪表板名称",
4545
"existing_dashboard": "存量仪表板",
4646
"add_success": "添加成功",
47-
"no_data": "没找到相关内容",
47+
"no_data": "没有找到相关内容",
4848
"new_tab": "新建Tab",
4949
"length_limit64": "字段长度需要在1-64之间",
5050
"sort_column": "排序字段",

frontend/src/router/watch.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ export const watchRouter = (router: any) => {
3131
return
3232
}
3333
if (to.path === '/login') {
34-
console.log(from)
34+
console.info(from)
3535
next('/chat')
3636
} else {
3737
next()

frontend/src/views/chat/answer/AnalysisAnswer.vue

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -133,9 +133,6 @@ const sendMessage = async () => {
133133
_list.push(line)
134134
}
135135
}
136-
137-
// console.log(_list)
138-
139136
for (const str of _list) {
140137
let data
141138
try {
@@ -161,7 +158,7 @@ const sendMessage = async () => {
161158
_currentChat.value.records[index.value].id = data.id
162159
break
163160
case 'info':
164-
console.log(data.msg)
161+
console.info(data.msg)
165162
break
166163
case 'error':
167164
currentRecord.error = data.content

0 commit comments

Comments
 (0)