MCPcopy Create free account
hub / github.com/OpenBMB/BMTools / build_database_tool

Function build_database_tool

bmtools/tools/database/api.py:17–154  ·  view source on GitHub ↗
(config)

Source from the content-addressed store, hash-verified

15
16
17def build_database_tool(config) -> Tool:
18 tool = Tool(
19 "Data in a database",
20 "Look up user data",
21 name_for_model="Database",
22 description_for_model="Plugin for querying the data in a database",
23 logo_url="https://commons.wikimedia.org/wiki/File:Postgresql_elephant.svg",
24 contact_email="hello@contact.com",
25 legal_info_url="hello@legal.com"
26 )
27
28 URL_REWRITE= "http://8.131.229.55:5114/rewrite"
29
30 # load db settings
31 script_path = os.path.abspath(__file__)
32 script_dir = os.path.dirname(script_path)
33 config = get_conf(script_dir + '/my_config.ini', 'postgresql')
34 dbargs = DBArgs("postgresql", config=config) # todo assign database name
35
36 # send request to database
37 db = Database(dbargs, timeout=-1)
38 schema = ""
39 query = ""
40
41 @tool.get("/get_database_schema")
42 #def get_database_schema(query : str='select * from customer limit 2;', db_name : str='tpch10x'):
43 def get_database_schema(db_name : str='tpch10x'):
44 global schema
45
46 #todo simplify the schema based on the query
47 print("=========== database name:", db_name)
48 schema = db.compute_table_schema()
49
50 print("========schema:", schema)
51
52 text_output = f"The database schema is:\n" + "".join(str(schema))
53
54 return text_output
55
56
57 @tool.get("/translate_nlp_to_sql")
58 def translate_nlp_to_sql(description : str):
59 global schema, query
60 """translate_nlp_to_sql(description: str) translates the input nlp string into sql query based on the database schema, and the sql query is the input of rewrite_sql and select_database_data API.
61 description is a string that represents the description of the result data.
62 schema is a string that represents the database schema.
63 Final answer should be complete.
64
65 This is an example:
66 Thoughts: Now that I have the database schema, I will use the \\\'translate_nlp_to_sql\\\' command to generate the SQL query based on the given description and schema, and take the SQL query as the input of the \\\'rewrite_sql\\\' and \\\'select_database_data\\\' commands.
67 Reasoning: I need to generate the SQL query accurately based on the given description. I will use the \\\'translate_nlp_to_sql\\\' command to obtain the SQL query based on the given description and schema, and take the SQL query as the input of the \\\'select_database_data\\\' command.
68 Plan: - Use the \\\'translate_nlp_to_sql\\\' command to generate the SQL query. \\\\n- Use the \\\'finish\\\' command to signal that I have completed all my objectives.
69 Command: {"name": "translate_nlp_to_sql", "args": {"description": "Retrieve the comments of suppliers . The results should be sorted in descending order based on the comments of the suppliers."}}
70 Result: Command translate_nlp_to_sql returned: "SELECT s_comment FROM supplier BY s_comment DESC"
71 """
72
73 openai.api_key = os.environ["OPENAI_API_KEY"]
74 # schema = db.compute_table_schema()

Callers

nothing calls this directly

Calls 4

get_confFunction · 0.90
DBArgsClass · 0.90
DatabaseClass · 0.90
ToolClass · 0.50

Tested by

no test coverage detected