-
Notifications
You must be signed in to change notification settings - Fork 68
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
base: main
Are you sure you want to change the base?
Conversation
6e1f29a
to
74f63cb
Compare
…url 3. estimate token uasge in local llm model
… ProgrammingError), use Exception when Table exists in _create_by_api function 2. Fix regrex error in get_db_init_keywords function when password has special character
@@ -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} |
There was a problem hiding this comment.
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}" |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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()) |
There was a problem hiding this comment.
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)
|
||
__all__ = ["SqlCall", "SQLiteManger", "SqlManager"] |
There was a problem hiding this comment.
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(): |
There was a problem hiding this comment.
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: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
为什么要提供一个reset_engine的对外接口?什么场景下会对一个已经创建的SqlManager做重置呀
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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: |
There was a problem hiding this comment.
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): |
There was a problem hiding this comment.
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, |
There was a problem hiding this comment.
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}/" |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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: |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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 = {} |
There was a problem hiding this comment.
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: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
重复定义
No description provided.