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

Function translate_nlp_to_sql

bmtools/tools/database/api.py:58–101  ·  view source on GitHub ↗
(description : str)

Source from the content-addressed store, hash-verified

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()
75
76 prompt = """Translate the natural language description into an semantic equivalent SQL query.
77 The table and column names used in the sql must exactly appear in the schema. Any other table and column names are unacceptable.
78 The schema is:\n
79 {}
80
81 The description is:\n
82 {}
83
84 The SQL query is:
85 """.format(schema, description)
86
87 # Set up the OpenAI GPT-3 model
88 model_engine = "gpt-3.5-turbo"
89
90 prompt_response = openai.ChatCompletion.create(
91 engine=model_engine,
92 messages=[
93 {"role": "assistant", "content": "The table schema is as follows: " + schema},
94 {"role": "user", "content": prompt}
95 ]
96 )
97 output_text = prompt_response['choices'][0]['message']['content']
98
99 query = output_text
100
101 return output_text
102
103 @tool.get("/select_database_data")
104 def select_database_data(query : str):

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected