Skip to content

Commit 1e11382

Browse files
committed
refactor: Optimize question connection pool
1 parent 6e24ea9 commit 1e11382

File tree

1 file changed

+10
-5
lines changed

1 file changed

+10
-5
lines changed

backend/apps/chat/task/llm.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
from langchain_core.messages import BaseMessage, SystemMessage, HumanMessage, AIMessage, BaseMessageChunk
1919
from sqlalchemy import select
2020
from sqlalchemy.orm import sessionmaker
21-
from sqlmodel import create_engine, Session
21+
from sqlmodel import Session
2222

2323
from apps.ai_model.model_factory import LLMConfig, LLMFactory, get_default_config
2424
from apps.chat.curd.chat import save_question, save_sql_answer, save_sql, \
@@ -37,6 +37,7 @@
3737
from apps.system.schemas.system_schema import AssistantOutDsSchema
3838
from apps.terminology.curd.terminology import get_terminology_template
3939
from common.core.config import settings
40+
from common.core.db import engine
4041
from common.core.deps import CurrentAssistant, CurrentUser
4142
from common.error import SingleMessageError, SQLBotDBError, ParseSQLResultError, SQLBotDBConnectionError
4243
from common.utils.utils import SQLBotLogUtil, extract_nested_json, prepare_for_orjson
@@ -50,6 +51,10 @@
5051
dynamic_ds_types = [1, 3]
5152
dynamic_subsql_prefix = 'select * from sqlbot_dynamic_temp_table_'
5253

54+
session_maker = sessionmaker(bind=engine)
55+
db_session = session_maker()
56+
57+
5358
class LLMService:
5459
ds: CoreDatasource
5560
chat_question: ChatQuestion
@@ -59,7 +64,7 @@ class LLMService:
5964
sql_message: List[Union[BaseMessage, dict[str, Any]]] = []
6065
chart_message: List[Union[BaseMessage, dict[str, Any]]] = []
6166

62-
session: Session
67+
session: Session = db_session
6368
current_user: CurrentUser
6469
current_assistant: Optional[CurrentAssistant] = None
6570
out_ds_instance: Optional[AssistantOutDs] = None
@@ -79,9 +84,9 @@ def __init__(self, current_user: CurrentUser, chat_question: ChatQuestion,
7984
current_assistant: Optional[CurrentAssistant] = None, no_reasoning: bool = False,
8085
config: LLMConfig = None):
8186
self.chunk_list = []
82-
engine = create_engine(str(settings.SQLALCHEMY_DATABASE_URI))
83-
session_maker = sessionmaker(bind=engine)
84-
self.session = session_maker()
87+
# engine = create_engine(str(settings.SQLALCHEMY_DATABASE_URI))
88+
# session_maker = sessionmaker(bind=engine)
89+
# self.session = session_maker()
8590
self.session.exec = self.session.exec if hasattr(self.session, "exec") else self.session.execute
8691
self.current_user = current_user
8792
self.current_assistant = current_assistant

0 commit comments

Comments
 (0)