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

Function select_database_data

bmtools/tools/database/api.py:104–132  ·  view source on GitHub ↗

select_database_data(query : str) Read the data stored in database based on the SQL query from the translate_nlp_to_sql API. query : str is a string that represents the SQL query outputted by the translate_nlp_to_sql API. Final answer should be complete. This is

(query : str)

Source from the content-addressed store, hash-verified

102
103 @tool.get("/select_database_data")
104 def select_database_data(query : str):
105 """select_database_data(query : str) Read the data stored in database based on the SQL query from the translate_nlp_to_sql API.
106 query : str is a string that represents the SQL query outputted by the translate_nlp_to_sql API.
107 Final answer should be complete.
108
109 This is an example:
110 Thoughts: Now that I have the database schema and SQL query, I will use the \\\'select_database_data\\\' command to retrieve the data from the database based on the SQL query
111 Reasoning: I will use the \\\'select_database_data\\\' command to retrieve the data from the database based on the SQL query
112 Plan: - Use the \\\'select_database_data\\\' command to retrieve the data from the database based on the SQL query.\\\\n- Use the \\\'finish\\\' command to signal that I have completed all my objectives.
113 Command: {"name": "select_database_data", "args": {query: "SELECT s_comment FROM supplier BY s_comment DESC"}}
114 Result: Command select_database_data returned: "The number of result rows is: 394"
115 """
116
117 if query == "":
118 raise RuntimeError("SQL query is empty")
119
120 print("=========== database query:", query)
121 res_completion = db.pgsql_results(query) # list format
122
123 if res_completion == "<fail>":
124 raise RuntimeError("Database query failed")
125
126 #data = json.loads(str(res_completion).strip())
127 if isinstance(res_completion, list):
128 text_output = f"The number of result rows is: "+"".join(str(len(res_completion)))
129 else:
130 text_output = f"The number of result rows is: "+"".join(str(res_completion))
131
132 return text_output
133
134 @tool.get("/rewrite_sql")
135 def rewrite_sql(sql: str="select distinct l_orderkey, sum(l_extendedprice + 3 + (1 - l_discount)) as revenue, o_orderkey, o_shippriority from customer, orders, lineitem where c_mktsegment = 'BUILDING' and c_custkey = o_custkey and l_orderkey = o_orderkey and o_orderdate < date '1995-03-15' and l_shipdate > date '1995-03-15' group by l_orderkey, o_orderkey, o_shippriority order by revenue desc, o_orderkey;"):

Callers

nothing calls this directly

Calls 1

pgsql_resultsMethod · 0.45

Tested by

no test coverage detected