-
Notifications
You must be signed in to change notification settings - Fork 121
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #121 from alipay/dev_weizj
add sql tool
- Loading branch information
Showing
14 changed files
with
171 additions
and
47 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
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
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
13 changes: 13 additions & 0 deletions
13
sample_standard_app/app/core/tool/langchain_tool/info_sql_database_tool.yaml
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,13 @@ | ||
name: 'info_sql_database_tool' | ||
description: '' | ||
tool_type: 'api' | ||
input_keys: ['input'] | ||
langchain: | ||
module: langchain_community.tools | ||
class_name: InfoSQLDatabaseTool | ||
init_params: | ||
db_wrapper: demo_sqldb_wrapper | ||
metadata: | ||
type: 'TOOL' | ||
module: 'sample_standard_app.app.core.tool.langchain_tool.sql_langchain_tool' | ||
class: 'SqlLangchainTool' |
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
13 changes: 13 additions & 0 deletions
13
sample_standard_app/app/core/tool/langchain_tool/list_sql_database_tool.yaml
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,13 @@ | ||
name: 'list_sql_database_tool' | ||
description: '' | ||
tool_type: 'api' | ||
input_keys: ['input'] | ||
langchain: | ||
module: langchain_community.tools | ||
class_name: ListSQLDatabaseTool | ||
init_params: | ||
db_wrapper: demo_sqldb_wrapper | ||
metadata: | ||
type: 'TOOL' | ||
module: 'sample_standard_app.app.core.tool.langchain_tool.sql_langchain_tool' | ||
class: 'SqlLangchainTool' |
13 changes: 13 additions & 0 deletions
13
sample_standard_app/app/core/tool/langchain_tool/query_sql_database_tool.yaml
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,13 @@ | ||
name: 'query_sql_database_tool' | ||
description: '' | ||
tool_type: 'api' | ||
input_keys: ['input'] | ||
langchain: | ||
module: langchain_community.tools | ||
class_name: QuerySQLDataBaseTool | ||
init_params: | ||
db_wrapper: demo_sqldb_wrapper | ||
metadata: | ||
type: 'TOOL' | ||
module: 'sample_standard_app.app.core.tool.langchain_tool.sql_langchain_tool' | ||
class: 'SqlLangchainTool' |
39 changes: 39 additions & 0 deletions
39
sample_standard_app/app/core/tool/langchain_tool/sql_langchain_tool.py
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 @@ | ||
# !/usr/bin/env python3 | ||
# -*- coding:utf-8 -*- | ||
|
||
# @Time : 2024/7/9 20:04 | ||
# @Author : weizjajj | ||
# @Email : weizhongjie.wzj@antgroup.com | ||
# @FileName: sql_langchain_tool.py | ||
|
||
from typing import Type, Optional | ||
|
||
from langchain_core.tools import BaseTool, Tool as LangchainTool | ||
|
||
from agentuniverse.agent.action.tool.tool import ToolInput | ||
from agentuniverse.database.sqldb_wrapper_manager import SQLDBWrapperManager | ||
from sample_standard_app.app.core.tool.langchain_tool.langchain_tool import LangChainTool | ||
|
||
|
||
class SqlLangchainTool(LangChainTool): | ||
db_wrapper_name: Optional[str] = "" | ||
clz: Type[BaseTool] = BaseTool | ||
|
||
def execute(self, tool_input: ToolInput): | ||
if self.tool is None: | ||
self.get_sql_database() | ||
return super().execute(tool_input) | ||
|
||
def get_sql_database(self): | ||
db_wrapper = SQLDBWrapperManager().get_instance_obj(self.db_wrapper_name) | ||
self.tool = self.clz(db=db_wrapper.sql_database) | ||
self.description = self.tool.description | ||
|
||
def as_langchain(self) -> LangchainTool: | ||
if self.tool is None: | ||
self.get_sql_database() | ||
return super().as_langchain() | ||
|
||
def get_langchain_tool(self, init_params: dict, clz: Type[BaseTool]): | ||
self.db_wrapper_name = init_params.get("db_wrapper") | ||
self.clz = clz |