-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: refactor backend node code * feat: add db_utils and now get model info from database * feat: refactor basenode code with a better init model according to provider and model name * fix: change tool node data tool to tools
- Loading branch information
1 parent
64c40ed
commit 52eaddd
Showing
6 changed files
with
86 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
from contextlib import contextmanager | ||
from typing import Callable, TypeVar, Any | ||
from sqlmodel import Session | ||
from app.core.db import engine | ||
|
||
T = TypeVar('T') | ||
|
||
@contextmanager | ||
def get_db_session(): | ||
session = Session(engine) | ||
try: | ||
yield session | ||
session.commit() | ||
except Exception: | ||
session.rollback() | ||
raise | ||
finally: | ||
session.close() | ||
|
||
def db_operation(operation: Callable[[Session], T]) -> T: | ||
""" | ||
执行数据库操作的辅助函数。 | ||
:param operation: 一个接受 Session 作为参数并返回结果的函数。 | ||
:return: 操作的结果。 | ||
""" | ||
with get_db_session() as session: | ||
return operation(session) | ||
|
||
# 示例用法 | ||
def get_all_models_helper(): | ||
from app.curd.models import get_all_models | ||
return db_operation(get_all_models) | ||
|
||
def get_models_by_provider_helper(provider_id: int): | ||
from app.curd.models import get_models_by_provider | ||
return db_operation(lambda session: get_models_by_provider(session, provider_id)) | ||
|
||
# 可以根据需要添加更多辅助函数 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters