mirror of
https://github.com/RYDE-WORK/Langchain-Chatchat.git
synced 2026-01-19 21:37:20 +08:00
* 知识库支持子目录(不包括temp和tmp开头的目录),文件相对路径总长度不可超过255 * init_database.py 增加 --import-db 参数,在版本升级时,如果 info.db 表结构发生变化,但向量库无需重建,可以在重建数据库后,使用本参数从旧的数据库中导入信息
47 lines
962 B
Python
47 lines
962 B
Python
from functools import wraps
|
|
from contextlib import contextmanager
|
|
from server.db.base import SessionLocal
|
|
from sqlalchemy.orm import Session
|
|
|
|
|
|
@contextmanager
|
|
def session_scope() -> Session:
|
|
"""上下文管理器用于自动获取 Session, 避免错误"""
|
|
session = SessionLocal()
|
|
try:
|
|
yield session
|
|
session.commit()
|
|
except:
|
|
session.rollback()
|
|
raise
|
|
finally:
|
|
session.close()
|
|
|
|
|
|
def with_session(f):
|
|
@wraps(f)
|
|
def wrapper(*args, **kwargs):
|
|
with session_scope() as session:
|
|
try:
|
|
result = f(session, *args, **kwargs)
|
|
session.commit()
|
|
return result
|
|
except:
|
|
session.rollback()
|
|
raise
|
|
|
|
return wrapper
|
|
|
|
|
|
def get_db() -> SessionLocal:
|
|
db = SessionLocal()
|
|
try:
|
|
yield db
|
|
finally:
|
|
db.close()
|
|
|
|
|
|
def get_db0() -> SessionLocal:
|
|
db = SessionLocal()
|
|
return db
|