11import json
22import time
33import traceback
4+ from concurrent .futures import ThreadPoolExecutor
45from typing import List
56
67from sqlalchemy import and_ , select , update
8+ from sqlalchemy .orm import sessionmaker
79from sqlalchemy .orm .session import Session
810
911from apps .ai_model .embedding import EmbeddingModelCache
1214from common .utils .utils import SQLBotLogUtil
1315from ..models .datasource import CoreTable , CoreField
1416
17+ executor = ThreadPoolExecutor (max_workers = 200 )
18+
19+ from common .core .db import engine
20+
21+ session_maker = sessionmaker (bind = engine )
22+ session = session_maker ()
23+
1524
1625def delete_table_by_ds_id (session : SessionDep , id : int ):
1726 session .query (CoreTable ).filter (CoreTable .ds_id == id ).delete (synchronize_session = False )
@@ -32,14 +41,18 @@ def update_table(session: SessionDep, item: CoreTable):
3241
3342
3443def run_fill_empty_table_embedding (session : Session ):
35- if not settings .EMBEDDING_ENABLED :
36- return
44+ try :
45+ if not settings .EMBEDDING_ENABLED :
46+ return
3747
38- SQLBotLogUtil .info ('get tables' )
39- stmt = select (CoreTable .id ).where (and_ (CoreTable .embedding .is_ (None )))
40- results = session .execute (stmt ).scalars ().all ()
48+ SQLBotLogUtil .info ('get tables' )
49+ stmt = select (CoreTable .id ).where (and_ (CoreTable .embedding .is_ (None )))
50+ results = session .execute (stmt ).scalars ().all ()
51+ SQLBotLogUtil .info ('result:' + str (len (results )))
4152
42- save_table_embedding (session , results )
53+ save_table_embedding (session , results )
54+ except Exception :
55+ traceback .print_exc ()
4356
4457
4558def save_table_embedding (session : Session , ids : List [int ]):
@@ -89,3 +102,14 @@ def save_table_embedding(session: Session, ids: List[int]):
89102 SQLBotLogUtil .info ('table embedding finished in:' + str (end_time - start_time ) + 'seconds' )
90103 except Exception :
91104 traceback .print_exc ()
105+
106+
107+ def run_save_table_embeddings (ids : List [int ]):
108+ executor .submit (save_table_embedding , session , ids )
109+
110+
111+ def fill_empty_table_embeddings ():
112+ try :
113+ executor .submit (run_fill_empty_table_embedding , session )
114+ except Exception :
115+ traceback .print_exc ()
0 commit comments