Skip to content

Commit 27bd84d

Browse files
committed
Merge branch 'main' of https://github.com/dataease/SQLBot
2 parents 64d8220 + 592a5ef commit 27bd84d

File tree

17 files changed

+851
-479
lines changed

17 files changed

+851
-479
lines changed
Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
"""035_create_chat_log
2+
3+
Revision ID: 68a06302cf70
4+
Revises: 29559ee607af
5+
Create Date: 2025-08-18 16:02:43.353110
6+
7+
"""
8+
from alembic import op
9+
import sqlalchemy as sa
10+
import sqlmodel.sql.sqltypes
11+
from sqlalchemy.dialects import postgresql
12+
13+
# revision identifiers, used by Alembic.
14+
revision = '68a06302cf70'
15+
down_revision = '646e7ca28e0e'
16+
branch_labels = None
17+
depends_on = None
18+
19+
20+
sql='''
21+
CREATE OR REPLACE FUNCTION safe_jsonb_cast(text) RETURNS jsonb AS
22+
$$
23+
BEGIN
24+
RETURN $1::jsonb;
25+
EXCEPTION
26+
WHEN others THEN
27+
RETURN to_json($1::text)::jsonb;
28+
END;
29+
$$ LANGUAGE plpgsql;
30+
31+
INSERT INTO chat_log(type, operate, pid, ai_modal_id, messages, start_time, finish_time, token_usage, reasoning_content)
32+
SELECT '0',
33+
'0',
34+
id,
35+
ai_modal_id,
36+
safe_jsonb_cast(full_sql_message),
37+
create_time,
38+
finish_time,
39+
safe_jsonb_cast(token_sql),
40+
safe_jsonb_cast(sql_answer)->>'reasoning_content'
41+
FROM chat_record
42+
WHERE full_sql_message IS NOT NULL;
43+
INSERT INTO chat_log(type, operate, pid, ai_modal_id, messages, start_time, finish_time, token_usage, reasoning_content)
44+
SELECT '0',
45+
'1',
46+
id,
47+
ai_modal_id,
48+
safe_jsonb_cast(full_chart_message),
49+
create_time,
50+
finish_time,
51+
safe_jsonb_cast(token_chart),
52+
safe_jsonb_cast(chart_answer)->>'reasoning_content'
53+
FROM chat_record
54+
WHERE full_chart_message IS NOT NULL;
55+
INSERT INTO chat_log(type, operate, pid, ai_modal_id, messages, start_time, finish_time, token_usage, reasoning_content)
56+
SELECT '0',
57+
'2',
58+
id,
59+
ai_modal_id,
60+
safe_jsonb_cast(full_analysis_message),
61+
create_time,
62+
finish_time,
63+
safe_jsonb_cast(token_analysis),
64+
safe_jsonb_cast(analysis)->>'reasoning_content'
65+
FROM chat_record
66+
WHERE full_analysis_message IS NOT NULL;
67+
INSERT INTO chat_log(type, operate, pid, ai_modal_id, messages, start_time, finish_time, token_usage, reasoning_content)
68+
SELECT '0',
69+
'3',
70+
id,
71+
ai_modal_id,
72+
safe_jsonb_cast(full_predict_message),
73+
create_time,
74+
finish_time,
75+
safe_jsonb_cast(token_predict),
76+
safe_jsonb_cast(predict)->>'reasoning_content'
77+
FROM chat_record
78+
WHERE full_predict_message IS NOT NULL;
79+
INSERT INTO chat_log(type, operate, pid, ai_modal_id, messages, start_time, finish_time, token_usage, reasoning_content)
80+
SELECT '0',
81+
'4',
82+
id,
83+
ai_modal_id,
84+
safe_jsonb_cast(full_recommended_question_message),
85+
create_time,
86+
finish_time,
87+
safe_jsonb_cast(token_recommended_question),
88+
safe_jsonb_cast(recommended_question_answer)->>'reasoning_content'
89+
FROM chat_record
90+
WHERE full_recommended_question_message IS NOT NULL;
91+
INSERT INTO chat_log(type, operate, pid, ai_modal_id, messages, start_time, finish_time, token_usage, reasoning_content)
92+
SELECT '0',
93+
'6',
94+
id,
95+
ai_modal_id,
96+
safe_jsonb_cast(full_select_datasource_message),
97+
create_time,
98+
finish_time,
99+
safe_jsonb_cast(token_select_datasource_question),
100+
safe_jsonb_cast(datasource_select_answer)->>'reasoning_content'
101+
FROM chat_record
102+
WHERE full_select_datasource_message IS NOT NULL;
103+
104+
'''
105+
106+
def upgrade():
107+
# ### commands auto generated by Alembic - please adjust! ###
108+
op.create_table('chat_log',
109+
sa.Column('id', sa.BigInteger(), sa.Identity(always=True), nullable=False),
110+
sa.Column('type', sa.Enum('0', name='typeenum', native_enum=False, length=3), nullable=True),
111+
sa.Column('operate', sa.Enum('0', '1', '2', '3', '4', '5', '6', name='operationenum', native_enum=False, length=3), nullable=True),
112+
sa.Column('pid', sa.BigInteger(), nullable=True),
113+
sa.Column('ai_modal_id', sa.BigInteger(), nullable=True),
114+
sa.Column('base_modal', sqlmodel.sql.sqltypes.AutoString(length=255), nullable=True),
115+
sa.Column('messages', postgresql.JSONB(astext_type=sa.Text()), nullable=True),
116+
sa.Column('reasoning_content', sa.Text(), nullable=True),
117+
sa.Column('start_time', sa.DateTime(), nullable=True),
118+
sa.Column('finish_time', sa.DateTime(), nullable=True),
119+
sa.Column('token_usage', postgresql.JSONB(astext_type=sa.Text()), nullable=True),
120+
sa.PrimaryKeyConstraint('id')
121+
)
122+
123+
op.execute(sql)
124+
# ### end Alembic commands ###
125+
126+
127+
def downgrade():
128+
# ### commands auto generated by Alembic - please adjust! ###
129+
130+
op.drop_table('chat_log')
131+
# ### end Alembic commands ###
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
"""038_remove_chat_record_cloumns
2+
3+
Revision ID: fc23c4f3e755
4+
Revises: 68a06302cf70
5+
Create Date: 2025-08-21 14:34:59.149410
6+
7+
"""
8+
from alembic import op
9+
import sqlalchemy as sa
10+
import sqlmodel.sql.sqltypes
11+
from sqlalchemy.dialects import postgresql
12+
13+
# revision identifiers, used by Alembic.
14+
revision = 'fc23c4f3e755'
15+
down_revision = '68a06302cf70'
16+
branch_labels = None
17+
depends_on = None
18+
19+
20+
def upgrade():
21+
# ### commands auto generated by Alembic - please adjust! ###
22+
op.drop_column('chat_record', 'token_predict')
23+
op.drop_column('chat_record', 'token_select_datasource_question')
24+
op.drop_column('chat_record', 'token_sql')
25+
op.drop_column('chat_record', 'full_analysis_message')
26+
op.drop_column('chat_record', 'full_recommended_question_message')
27+
op.drop_column('chat_record', 'token_chart')
28+
op.drop_column('chat_record', 'full_predict_message')
29+
op.drop_column('chat_record', 'full_chart_message')
30+
op.drop_column('chat_record', 'full_sql_message')
31+
op.drop_column('chat_record', 'full_select_datasource_message')
32+
op.drop_column('chat_record', 'token_recommended_question')
33+
op.drop_column('chat_record', 'token_analysis')
34+
# ### end Alembic commands ###
35+
36+
37+
def downgrade():
38+
# ### commands auto generated by Alembic - please adjust! ###
39+
op.add_column('chat_record', sa.Column('token_analysis', sa.VARCHAR(length=256), autoincrement=False, nullable=True))
40+
op.add_column('chat_record', sa.Column('token_recommended_question', sa.VARCHAR(length=256), autoincrement=False, nullable=True))
41+
op.add_column('chat_record', sa.Column('full_select_datasource_message', sa.TEXT(), autoincrement=False, nullable=True))
42+
op.add_column('chat_record', sa.Column('full_sql_message', sa.TEXT(), autoincrement=False, nullable=True))
43+
op.add_column('chat_record', sa.Column('full_chart_message', sa.TEXT(), autoincrement=False, nullable=True))
44+
op.add_column('chat_record', sa.Column('full_predict_message', sa.TEXT(), autoincrement=False, nullable=True))
45+
op.add_column('chat_record', sa.Column('token_chart', sa.VARCHAR(length=256), autoincrement=False, nullable=True))
46+
op.add_column('chat_record', sa.Column('full_recommended_question_message', sa.TEXT(), autoincrement=False, nullable=True))
47+
op.add_column('chat_record', sa.Column('full_analysis_message', sa.TEXT(), autoincrement=False, nullable=True))
48+
op.add_column('chat_record', sa.Column('token_sql', sa.VARCHAR(length=256), autoincrement=False, nullable=True))
49+
op.add_column('chat_record', sa.Column('token_select_datasource_question', sa.VARCHAR(length=256), autoincrement=False, nullable=True))
50+
op.add_column('chat_record', sa.Column('token_predict', sa.VARCHAR(length=256), autoincrement=False, nullable=True))
51+
# ### end Alembic commands ###

0 commit comments

Comments
 (0)