2025-09-23 14:49:00 +08:00
|
|
|
from email.policy import default
|
2025-09-24 14:39:42 +08:00
|
|
|
import dmPython
|
|
|
|
|
import logging
|
2025-09-23 14:49:00 +08:00
|
|
|
|
2025-09-24 14:39:42 +08:00
|
|
|
from logging_config import LOGGING_CONFIG
|
2025-09-23 14:49:00 +08:00
|
|
|
from service.cus_vanna_srevice import CustomVanna, QdrantClient
|
|
|
|
|
from decouple import config
|
|
|
|
|
import flask
|
2025-09-23 16:29:56 +08:00
|
|
|
from util import load_ddl_doc
|
2025-09-23 14:49:00 +08:00
|
|
|
|
|
|
|
|
from flask import Flask, Response, jsonify, request, send_from_directory
|
|
|
|
|
|
2025-09-24 14:39:42 +08:00
|
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
2025-09-23 14:49:00 +08:00
|
|
|
def connect_database(vn):
|
|
|
|
|
db_type = config('DATA_SOURCE_TYPE', default='sqlite')
|
|
|
|
|
if db_type == 'sqlite':
|
|
|
|
|
vn.connect_to_sqlite(config('SQLITE_DATABASE_URL', default=''))
|
|
|
|
|
elif db_type == 'mysql':
|
|
|
|
|
vn.connect_to_mysql(host=config('MYSQL_DATABASE_HOST', default=''),
|
2025-09-23 16:29:56 +08:00
|
|
|
port=int(config('MYSQL_DATABASE_PORT', default=3306)),
|
2025-09-23 14:49:00 +08:00
|
|
|
user=config('MYSQL_DATABASE_USER', default=''),
|
|
|
|
|
password=config('MYSQL_DATABASE_PASSWORD', default=''),
|
2025-09-23 16:29:56 +08:00
|
|
|
dbname=config('MYSQL_DATABASE_DBNAME', default=''))
|
2025-09-24 14:39:42 +08:00
|
|
|
elif db_type == 'dameng':
|
|
|
|
|
vn.connect_to_dameng( )
|
2025-09-23 14:49:00 +08:00
|
|
|
elif db_type == 'postgresql':
|
|
|
|
|
# 待补充
|
|
|
|
|
pass
|
|
|
|
|
else:
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def load_train_data_ddl(vn: CustomVanna):
|
2025-09-23 16:29:56 +08:00
|
|
|
vn.train()
|
2025-09-23 14:49:00 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def create_vana():
|
|
|
|
|
print("----------------create---------")
|
|
|
|
|
vn = CustomVanna(
|
|
|
|
|
vector_store_config={"client": QdrantClient(":memory:")},
|
|
|
|
|
llm_config={
|
|
|
|
|
"api_key": config('CHAT_MODEL_API_KEY', default=''),
|
|
|
|
|
"api_base": config('CHAT_MODEL_BASE_URL', default=''),
|
|
|
|
|
"model": config('CHAT_MODEL_NAME', default=''),
|
|
|
|
|
},
|
|
|
|
|
)
|
|
|
|
|
return vn
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def init_vn(vn):
|
|
|
|
|
print("--------------init vn-----connect----")
|
|
|
|
|
connect_database(vn)
|
2025-09-23 16:29:56 +08:00
|
|
|
load_ddl_doc.add_ddl(vn)
|
|
|
|
|
load_ddl_doc.add_documentation(vn)
|
2025-09-23 14:49:00 +08:00
|
|
|
if config('IS_FIRST_LOAD', default=False, cast=bool):
|
|
|
|
|
load_train_data_ddl(vn)
|
|
|
|
|
return vn
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
from vanna.flask import VannaFlaskApp
|
|
|
|
|
vn = create_vana()
|
|
|
|
|
app = VannaFlaskApp(vn,chart=False)
|
|
|
|
|
init_vn(vn)
|
2025-09-23 20:06:32 +08:00
|
|
|
cache = app.cache
|
2025-09-23 14:49:00 +08:00
|
|
|
@app.flask_app.route("/api/v0/generate_sql_2", methods=["GET"])
|
|
|
|
|
def generate_sql_2():
|
|
|
|
|
"""
|
|
|
|
|
Generate SQL from a question
|
|
|
|
|
---
|
|
|
|
|
parameters:
|
|
|
|
|
- name: user
|
|
|
|
|
in: query
|
|
|
|
|
- name: question
|
|
|
|
|
in: query
|
|
|
|
|
type: string
|
|
|
|
|
required: true
|
|
|
|
|
responses:
|
|
|
|
|
200:
|
|
|
|
|
schema:
|
|
|
|
|
type: object
|
|
|
|
|
properties:
|
|
|
|
|
type:
|
|
|
|
|
type: string
|
|
|
|
|
default: sql
|
|
|
|
|
id:
|
|
|
|
|
type: string
|
|
|
|
|
text:
|
|
|
|
|
type: string
|
|
|
|
|
"""
|
2025-09-24 14:39:42 +08:00
|
|
|
logger.info("Start to generate sql in main")
|
2025-09-23 14:49:00 +08:00
|
|
|
question = flask.request.args.get("question")
|
|
|
|
|
|
|
|
|
|
if question is None:
|
|
|
|
|
return jsonify({"type": "error", "error": "No question provided"})
|
2025-09-24 14:39:42 +08:00
|
|
|
try:
|
|
|
|
|
id = cache.generate_id(question=question)
|
|
|
|
|
data = vn.generate_sql_2(question=question)
|
|
|
|
|
data['id'] = id
|
|
|
|
|
sql = data["resp"]["sql"]
|
|
|
|
|
print("sql:", sql)
|
|
|
|
|
cache.set(id=id, field="question", value=question)
|
|
|
|
|
cache.set(id=id, field="sql", value=sql)
|
|
|
|
|
print("data---------------------------", data)
|
|
|
|
|
return jsonify(data)
|
|
|
|
|
except Exception as e:
|
|
|
|
|
return jsonify({"type": "error", "error": str(e)})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@app.flask_app.route("/api/v0/run_sql_2", methods=["GET"])
|
|
|
|
|
@app.requires_cache(["sql"])
|
|
|
|
|
def run_sql_2(id: str, sql: str):
|
|
|
|
|
"""
|
|
|
|
|
Run SQL
|
|
|
|
|
---
|
|
|
|
|
parameters:
|
|
|
|
|
- name: user
|
|
|
|
|
in: query
|
|
|
|
|
- name: id
|
|
|
|
|
in: query|body
|
|
|
|
|
type: string
|
|
|
|
|
required: true
|
|
|
|
|
responses:
|
|
|
|
|
200:
|
|
|
|
|
schema:
|
|
|
|
|
type: object
|
|
|
|
|
properties:
|
|
|
|
|
type:
|
|
|
|
|
type: string
|
|
|
|
|
default: df
|
|
|
|
|
id:
|
|
|
|
|
type: string
|
|
|
|
|
df:
|
|
|
|
|
type: object
|
|
|
|
|
should_generate_chart:
|
|
|
|
|
type: boolean
|
|
|
|
|
"""
|
|
|
|
|
logger.info("Start to run sql in main")
|
|
|
|
|
try:
|
|
|
|
|
if not vn.run_sql_is_set:
|
|
|
|
|
return jsonify(
|
|
|
|
|
{
|
|
|
|
|
"type": "error",
|
|
|
|
|
"error": "Please connect to a database using vn.connect_to_... in order to run SQL queries.",
|
|
|
|
|
}
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
df = vn.run_sql(sql=sql)
|
|
|
|
|
logger.info("")
|
|
|
|
|
app.cache.set(id=id, field="df", value=df)
|
|
|
|
|
x = df.head(10).to_dict(orient='records')
|
|
|
|
|
logger.info("df ---------------{0} {1}".format(x,type(x)))
|
|
|
|
|
return jsonify(
|
|
|
|
|
{
|
|
|
|
|
"type": "df",
|
|
|
|
|
"id": id,
|
|
|
|
|
"df": df.head(10).to_dict(orient='records'),
|
|
|
|
|
}
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
except Exception as e:
|
|
|
|
|
return jsonify({"type": "sql_error", "error": str(e)})
|
2025-09-23 14:49:00 +08:00
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
|
|
|
|
|
|
app.run(host='0.0.0.0', port=8084, debug=False)
|