Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add sqlalchemy support and mongodb support #321

Draft
wants to merge 11 commits into
base: main
Choose a base branch
from

Conversation

SuperEver
Copy link
Contributor

No description provided.

@SuperEver SuperEver force-pushed the sql/support_sqlalchemy branch from 6e1f29a to 74f63cb Compare October 24, 2024 10:44
@@ -365,6 +365,8 @@ def forward(self, __input: Union[Tuple[Union[str, Dict], str], str, Dict] = pack
headers = {'Content-Type': 'application/json'}
text_input_for_token_usage = __input

usage = {"prompt_tokens": self._estimate_token_usage(__input), "completion_tokens": 0}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个是什么作用?TrainableModule会覆盖这个变量,非TrainableModule则用不上

if db_type not in self.DB_TYPE_SUPPORTED:
return DBResult(status=DBStatus.FAIL, detail=f"{db_type} not supported")
if db_type in self.DB_DRIVER_MAP:
conn_url = f"{db_type}+{self.DB_DRIVER_MAP[db_type]}://{user}:{password}@{host}:{port}/{db_name}"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个url拼完会是mysql+pymysql://{user}:{password}@{host}:{port}/{db_name},确认一下是合法的?


@property
def desc(self):
return self._desc
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

基类的方法不能使用子类定义的变量


@property
def db_type(self):
return self._db_type
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

基类的方法不能使用子类定义的变量,可以直接在34行赋值给self

return False

def _validate_desc(self, d):
return isinstance(d, dict) and all(self._is_str_or_nested_dict(v) for v in d.values())
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

return isinstance(d, dict) and self._is_str_or_nested_dict(d)

lazyllm/tools/sql/sql_manager.py Show resolved Hide resolved

__all__ = ["SqlCall", "SQLiteManger", "SqlManager"]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

旧的SqlCall代码有没有删掉呀

if result.status != DBStatus.SUCCESS:
return result
"""
if db_name not in self.client.list_database_names():
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

不要把代码注释起来,这里是什么情况呀

raise ValueError(db_result.detail)

@overload
def reset_engine(self, db_type, user, password, host, port, db_name, options_str) -> DBResult:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

为什么要提供一个reset_engine的对外接口?什么场景下会对一个已经创建的SqlManager做重置呀

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

看起来只有sql这一支使用了这个函数,是不是应该放到sqlmanagerbase中定义

if db_result.status != DBStatus.SUCCESS:
raise ValueError(db_result.detail)

@overload
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

abstractmethod?

if self.status != DBStatus.SUCCESS:
raise ValueError(self.detail)

def reset_client(self, user, password, host, port, db_name, collection_name, options_str="") -> DBResult:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

同,感觉这个函数没有暴露出去的必要性

result: Union[List, None] = None


class DBManager(ABC):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

图中是有forward的,但我没看到基类和MongoDB有实现forward

super().__init__(db_type, user, password, host, port, db_name, options_str)

def reset_engine(
self,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

参数统一都放紧凑一点吧,即尽量减少参数的行数,与前后保持风格一致

self._db_type = "mongodb"
self.status = DBStatus.SUCCESS
self.detail = ""
conn_url = f"{self._db_type}://{user}:{password}@{host}:{port}/"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

直接赋值给self即可,无需先搞个临时变量


def check_connection(self) -> DBResult:
try:
# check connection status
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个注释删掉吧,函数很简单,注释和函数名重复

self._collection.delete_one(filter)

def select(self, query, projection: dict[str, bool] = None, limit: int = None):
if limit is None:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

result = self._collection.find(query, projection)
if limit: result = result.limit(limit)

def execute_to_json(self, statement) -> str:
dbresult = self.execute(statement)
if dbresult.status != DBStatus.SUCCESS:
self.status, self.detail = dbresult.status, dbresult.detail
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

状态不能写进self,可以抛异常


self._engine = sqlalchemy.create_engine(self._conn_url)
self._desc = ""
extra_fields = {}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个临时变量也可以不要,直接赋值给self.

return DBResult()

@property
def desc(self) -> str:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

重复定义

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants