Spaces:
Runtime error
Runtime error
Update openai_integration.py
Browse files- openai_integration.py +6 -6
openai_integration.py
CHANGED
|
@@ -48,18 +48,18 @@ def reformulate_query(nl_query, schema_description):
|
|
| 48 |
examples = """
|
| 49 |
Example 1:
|
| 50 |
Input: "I'm trying to figure out which agents are the busiest. Can you show me like the top few agents who have a lot on their plate?"
|
| 51 |
-
Reformulated: "Show me the top 5 agents with the highest workload, including their names, emails, and specializations."
|
| 52 |
|
| 53 |
Example 2:
|
| 54 |
Input: "I need to get an overview of our classes. Could you pull up a list that shows what each class is about and which client it's for?"
|
| 55 |
-
Reformulated: "List all classes with their subjects, client names, and the number of learners in each class, sorted by the number of learners in descending order."
|
| 56 |
"""
|
| 57 |
|
| 58 |
prompt = (
|
| 59 |
f"Database Schema:\n{schema_description}\n\n"
|
| 60 |
-
f"Using the database schema, reformulate the following natural language query to be more precise and
|
| 61 |
f"Here are some examples of how to reformulate the natural language query:\n{examples}\n\n"
|
| 62 |
-
f"Output
|
| 63 |
)
|
| 64 |
|
| 65 |
logging.info("Sending reformulation request to OpenAI...")
|
|
@@ -133,14 +133,14 @@ def generate_sql_from_reformulated(reformulated_query, schema_description):
|
|
| 133 |
f"Examples:\n{examples}\n\n"
|
| 134 |
f"Convert the natural language query into an SQL query that matches the schema.\n\n"
|
| 135 |
f"Use the Examples as a guide on what I expect you to do.\n\n"
|
| 136 |
-
f"Only return the RAW SQL that can be directly executed, don't prefix it with sql
|
| 137 |
)
|
| 138 |
|
| 139 |
logging.info("Sending SQL generation request to OpenAI...")
|
| 140 |
response = client.chat.completions.create(
|
| 141 |
model=GPT_MODEL,
|
| 142 |
messages=[
|
| 143 |
-
{"role": "system", "content": "You are an assistant that converts natural language to SQL using a databse shema."},
|
| 144 |
{"role": "user", "content": prompt}
|
| 145 |
],
|
| 146 |
max_tokens=300,
|
|
|
|
| 48 |
examples = """
|
| 49 |
Example 1:
|
| 50 |
Input: "I'm trying to figure out which agents are the busiest. Can you show me like the top few agents who have a lot on their plate?"
|
| 51 |
+
Reformulated: "Show me the top 5 agents with the highest workload, including their names, emails, and specializations. Use Tables: agents"
|
| 52 |
|
| 53 |
Example 2:
|
| 54 |
Input: "I need to get an overview of our classes. Could you pull up a list that shows what each class is about and which client it's for?"
|
| 55 |
+
Reformulated: "List all classes with their subjects, client names, and the number of learners in each class, sorted by the number of learners in descending order. Use Tables: clients, learners, classes"
|
| 56 |
"""
|
| 57 |
|
| 58 |
prompt = (
|
| 59 |
f"Database Schema:\n{schema_description}\n\n"
|
| 60 |
+
f"Using the database schema, reformulate the following natural language query to be more precise and in line with the database schema:\n{nl_query}\n\n"
|
| 61 |
f"Here are some examples of how to reformulate the natural language query:\n{examples}\n\n"
|
| 62 |
+
f"Output the query string, list the DB tables that will be used seperated by commas, don't prefix with anything and don't use markdown or quotation marks."
|
| 63 |
)
|
| 64 |
|
| 65 |
logging.info("Sending reformulation request to OpenAI...")
|
|
|
|
| 133 |
f"Examples:\n{examples}\n\n"
|
| 134 |
f"Convert the natural language query into an SQL query that matches the schema.\n\n"
|
| 135 |
f"Use the Examples as a guide on what I expect you to do.\n\n"
|
| 136 |
+
f"Only return the RAW SQL that can be directly executed, don't prefix it with sql, don't use quotation marks and don't use markdown."
|
| 137 |
)
|
| 138 |
|
| 139 |
logging.info("Sending SQL generation request to OpenAI...")
|
| 140 |
response = client.chat.completions.create(
|
| 141 |
model=GPT_MODEL,
|
| 142 |
messages=[
|
| 143 |
+
{"role": "system", "content": "You are an assistant that converts natural language to SQL using a provided databse shema."},
|
| 144 |
{"role": "user", "content": prompt}
|
| 145 |
],
|
| 146 |
max_tokens=300,
|