SYSTEM_PROMPT = "You are an expert AI assistant specializing in converting natural language business queries into PostgreSQL-compliant SQL and corresponding JSON chart specifications. You must follow all instructions precisely and output only a single, valid JSON object."
HUMAN_PROMPT_TEMPLATE = """
TASK
Your task is to analyze the provided database schema and the user's natural language query (NLQ). Based on this analysis, you will generate a PostgreSQL query to retrieve the necessary data and a JSON specification for a suitable chart to visualize the results.
DATABASE SCHEMA
Here are the tables and columns you must use. Adhere strictly to these names.
{schema}
DOMAIN-SPECIFIC KNOWLEDGE
Pay close attention to these business definitions when constructing your query:
Status Values: The 'status' column can only be 'Stopped' or 'Running'.
No-Metal Time: This is a key metric calculated as the total time (in minutes or hours) a machine was stopped due to the material level being too low.
Calculation Logic: Sum the duration where machine_working_condition = 'Stopped' AND current_level < low_level_limit.
Sensor Needs Cleaning: This is an anomaly detection pattern. A sensor likely needs cleaning if you observe the following pattern at least twice:
The material level remains constant (change = 0) for 5-10 consecutive minutes.
This period of no change is immediately followed by a sudden, large drop in the level reading (e.g., > 20mm in one minute).
Level Change Formula: Level at (T-1) - Level at (T). Use window functions like LAG() to calculate this.
OUTPUT REQUIREMENTS
You must generate a single JSON object containing two keys: "sql" and "chart". Do not add any explanatory text before or after the JSON object.
sql (string):
The query must be PostgreSQL-compliant.
Use LOWER(column) LIKE LOWER('%value%') for case-insensitive text matching.
Use DATE_TRUNC('period', timestamp_column) for time-based grouping (e.g., 'day', 'month').
Alias all derived columns (e.g., AS total_revenue).
Use window functions like SUM(...) OVER (...) or LAG(...) where appropriate, especially for complex calculations like "sensor needs cleaning".
chart (object):
type: Must be one of: bar, line, pie, scatter, area. Choose the most appropriate type.
line for trends over time.
bar for comparisons between categories.
pie for part-to-whole distributions.
x_axis: The column name for the x-axis.
y_axis: A list of column names for the y-axis.
labels: An object with x and y keys for axis labels.
title: A clear, descriptive title for the chart.
EXAMPLE
NLQ: "Show monthly revenue vs cost per product line."
Output:
json
{{
"sql": "SELECT DATE_TRUNC('month', sale_date) AS month, product_line, SUM(revenue) AS total_revenue, SUM(cost) AS total_cost FROM Sales GROUP BY month, product_line ORDER BY month;",
"chart": {{
"type": "line",
"x_axis": "month",
"y_axis": ["total_revenue", "total_cost"],
"labels": {{"x": "Month", "y": "Amount"}},
"title": "Monthly Revenue vs Cost by Product Line"
}}
}}
USER'S QUERY
NLQ: "{question}"
YOUR RESPONSE (JSON only)
"""
# Initialize the ChatOpenAI client
client = ChatOpenAI(model="gpt-4.1", openai_api_key=openapi)
# Format the HUMAN_PROMPT_TEMPLATE with the schema and the user's query
prompt = HUMAN_PROMPT_TEMPLATE.format(schema=schema_str, question=nlq_query)
messages = [
SystemMessage(content="You are an AI that generates SQL queries which will accepted by PostgreSQL and chart details from business-related natural language queries."),
HumanMessage(content=prompt)
]
response = client.invoke(messages)
# Optionally, validate that the response is valid JSON:
try:
result = json.loads(response.content)
print(result)
except json.JSONDecodeError as e:
raise ValueError("The response is not a valid JSON object.") from e
return result
throws error
JSONDecodeError Traceback (most recent call last)
<ipython-input-28-3460407561> in <cell line: 0>()
89 try:
---> 90 result = json.loads(response.content)
91 print(result)
3 frames
JSONDecodeError: Expecting value: line 1 column 1 (char 0)
The above exception was the direct cause of the following exception:
ValueError Traceback (most recent call last)
<ipython-input-28-3460407561> in <cell line: 0>()
91 print(result)
92 except json.JSONDecodeError as e:
---> 93 raise ValueError("The response is not a valid JSON object.") from e
94
95 return result
ValueError: The response is not a valid JSON object.
Assistant
Looking at your code, I can see a few issues that are likely causing the JSON parsing error:
Incorrect model name: You're using "gpt-4.1" which isn't a valid OpenAI model name. It should be something like "gpt-4" or "gpt-4-turbo".
Variable name typo: In the client initialization, you have openapi instead of openai_api_key.
Duplicate client initialization: You're initializing the ChatOpenAI client twice.
Missing backticks in the example: The JSON example in your prompt template is missing the closing backticks.
Here's the corrected version:
python
User
Optionally, validate that the response is valid JSON:
try:
result = json.loads(response.content)
print("Parsed result:", result)
except json.JSONDecodeError as e:
print(f"Failed to parse JSON. Response was: {response.content}")
raise ValueError("The response is not a valid JSON object.") from e
Raw response: ```json
{
"sql": "SELECT machine_no, machine_name, day, 1440 - data_minutes AS data_not_available_minutes FROM ( SELECT machine_no, machine_name, DATE_TRUNC('day', time_stamp)::date AS day, COUNT(DISTINCT DATE_TRUNC('minute', time_stamp)) AS data_minutes FROM otif_apr25 WHERE time_stamp >= '2024-04-01' AND time_stamp < '2024-05-01' GROUP BY machine_no, machine_name, day ) AS daily_data_minutes ORDER BY machine_no, day;",
"chart": {
"type": "bar",
"x_axis": "day",
"y_axis": ["data_not_available_minutes"],
"labels": {"x": "Day", "y": "Minutes Data Not Available"},
"title": "Daywise Data Not Available (Minutes) per Machine in April"
}
}
code
File "<ipython-input-30-1524144638>", line 88
return result
^
SyntaxError: 'return' outside function
Assistant
The issue is that the response from the model includes markdown code block markers (json and ) around the JSON, which is causing the JSON parsing to fail. You need to extract the JSON content from between these markers.
Here's the corrected code with proper JSON extraction:
import json
from langchain.chat_models import ChatOpenAI
from langchain.schema import SystemMessage, HumanMessage
def generate_sql_and_chart_details(schema_str, nlq_query, openai_api_key):
# Initialize the ChatOpenAI client with correct model name
client = ChatOpenAI(model="gpt-4", openai_api_key=openai_api_key)
# This will be our new prompt template
SYSTEM_PROMPT = "You are an expert AI assistant specializing in converting natural language business queries into PostgreSQL-compliant SQL and corresponding JSON chart specifications. You must follow all instructions precisely and output only a single, valid JSON object."
HUMAN_PROMPT_TEMPLATE = """
### TASK
Your task is to analyze the provided database schema and the user's natural language query (NLQ). Based on this analysis, you will generate a PostgreSQL query to retrieve the necessary data and a JSON specification for a suitable chart to visualize the results.
### DATABASE SCHEMA
Here are the tables and columns you must use. Adhere strictly to these names.
{schema}
### DOMAIN-SPECIFIC KNOWLEDGE
Pay close attention to these business definitions when constructing your query:
- **Status Values**: The 'status' column can only be 'Stopped' or 'Running'.
- **No-Metal Time**: This is a key metric calculated as the total time (in minutes or hours) a machine was stopped due to the material level being too low.
- **Calculation Logic**: Sum the duration where `machine_working_condition = 'Stopped'` AND `current_level < low_level_limit`.
- **Sensor Needs Cleaning**: This is an anomaly detection pattern. A sensor likely needs cleaning if you observe the following pattern at least twice:
- The material level remains constant (change = 0) for 5-10 consecutive minutes.
- This period of no change is immediately followed by a sudden, large drop in the level reading (e.g., > 20mm in one minute).
- **Level Change Formula**: `Level at (T-1) - Level at (T)`. Use window functions like `LAG()` to calculate this.
### OUTPUT REQUIREMENTS
You must generate a single JSON object containing two keys: "sql" and "chart". Do not add any explanatory text before or after the JSON object.
1. **`sql` (string)**:
- The query must be PostgreSQL-compliant.
- Use `LOWER(column) LIKE LOWER('%value%')` for case-insensitive text matching.
- Use `DATE_TRUNC('period', timestamp_column)` for time-based grouping (e.g., 'day', 'month').
- Alias all derived columns (e.g., `AS total_revenue`).
- Use window functions like `SUM(...) OVER (...)` or `LAG(...)` where appropriate, especially for complex calculations like "sensor needs cleaning".
2. **`chart` (object)**:
- **`type`**: Must be one of: `bar`, `line`, `pie`, `scatter`, `area`. Choose the most appropriate type.
- `line` for trends over time.
- `bar` for comparisons between categories.
- `pie` for part-to-whole distributions.
- **`x_axis`**: The column name for the x-axis.
- **`y_axis`**: A list of column names for the y-axis.
- **`labels`**: An object with `x` and `y` keys for axis labels.
- **`title`**: A clear, descriptive title for the chart.
### EXAMPLE
**NLQ**: "Show monthly revenue vs cost per product line."
**Output**:
```json
{{
"sql": "SELECT DATE_TRUNC('month', sale_date) AS month, product_line, SUM(revenue) AS total_revenue, SUM(cost) AS total_cost FROM Sales GROUP BY month, product_line ORDER BY month;",
"chart": {{
"type": "line",
"x_axis": "month",
"y_axis": ["total_revenue", "total_cost"],
"labels": {{"x": "Month", "y": "Amount"}},
"title": "Monthly Revenue vs Cost by Product Line"
}}
}}
USER'S QUERY
NLQ: "{question}"
YOUR RESPONSE (JSON only):
"""
code
# Format the HUMAN_PROMPT_TEMPLATE with the schema and the user's query
prompt = HUMAN_PROMPT_TEMPLATE.format(schema=schema_str, question=nlq_query)
messages = [
SystemMessage(content=SYSTEM_PROMPT),
HumanMessage(content=prompt)
]
response = client.invoke(messages)
# Debug: print the raw response to see what's being returned
print("Raw response:", response.content)
# Optionally, validate that the response is valid JSON:
try:
result = json.loads(response.content)
print("Parsed result:", result)
except json.JSONDecodeError as e:
print(f"Failed to parse JSON. Response was: {response.content}")
raise ValueError("The response is not a valid JSON object.") from e
return result
code
Key changes made:
1. Fixed the model name to `"gpt-4"` (you can also use `"gpt-4-turbo"` or `"gpt-3.5-turbo"`)
2. Fixed the variable name from `openapi` to `openai_api_key`
3. Removed the duplicate client initialization
4. Added the closing backticks for the JSON example
5. Used the `SYSTEM_PROMPT` variable in the SystemMessage
6. Added debug print statements to help diagnose issues
If you're still getting errors, the debug print statements will help you see what the model is actually returning, which will help identify if it's returning explanatory text before the JSON or if there's another formatting issue.
import json
import re
from langchain.chat_models import ChatOpenAI
from langchain.schema import SystemMessage, HumanMessage
def generate_sql_and_chart_details(schema_str, nlq_query, openai_api_key):
# Initialize the ChatOpenAI client with correct model name
client = ChatOpenAI(model="gpt-4", openai_api_key=openai_api_key)
# This will be our new prompt template
SYSTEM_PROMPT = "You are an expert AI assistant specializing in converting natural language business queries into PostgreSQL-compliant SQL and corresponding JSON chart specifications. You must follow all instructions precisely and output only a single, valid JSON object."
HUMAN_PROMPT_TEMPLATE = """
### TASK
Your task is to analyze the provided database schema and the user's natural language query (NLQ). Based on this analysis, you will generate a PostgreSQL query to retrieve the necessary data and a JSON specification for a suitable chart to visualize the results.
### DATABASE SCHEMA
Here are the tables and columns you must use. Adhere strictly to these names.
{schema}
### DOMAIN-SPECIFIC KNOWLEDGE
Pay close attention to these business definitions when constructing your query:
- **Status Values**: The 'status' column can only be 'Stopped' or 'Running'.
- **No-Metal Time**: This is a key metric calculated as the total time (in minutes or hours) a machine was stopped due to the material level being too low.
- **Calculation Logic**: Sum the duration where `machine_working_condition = 'Stopped'` AND `current_level < low_level_limit`.
- **Sensor Needs Cleaning**: This is an anomaly detection pattern. A sensor likely needs cleaning if you observe the following pattern at least twice:
- The material level remains constant (change = 0) for 5-10 consecutive minutes.
- This period of no change is immediately followed by a sudden, large drop in the level reading (e.g., > 20mm in one minute).
- **Level Change Formula**: `Level at (T-1) - Level at (T)`. Use window functions like `LAG()` to calculate this.
### OUTPUT REQUIREMENTS
You must generate a single JSON object containing two keys: "sql" and "chart". Do not add any explanatory text before or after the JSON object.
1. **`sql` (string)**:
- The query must be PostgreSQL-compliant.
- Use `LOWER(column) LIKE LOWER('%value%')` for case-insensitive text matching.
- Use `DATE_TRUNC('period', timestamp_column)` for time-based grouping (e.g., 'day', 'month').
- Alias all derived columns (e.g., `AS total_revenue`).
- Use window functions like `SUM(...) OVER (...)` or `LAG(...)` where appropriate, especially for complex calculations like "sensor needs cleaning".
2. **`chart` (object)**:
- **`type`**: Must be one of: `bar`, `line`, `pie`, `scatter`, `area`. Choose the most appropriate type.
- `line` for trends over time.
- `bar` for comparisons between categories.
- `pie` for part-to-whole distributions.
- **`x_axis`**: The column name for the x-axis.
- **`y_axis`**: A list of column names for the y-axis.
- **`labels`**: An object with `x` and `y` keys for axis labels.
- **`title`**: A clear, descriptive title for the chart.
### EXAMPLE
**NLQ**: "Show monthly revenue vs cost per product line."
**Output**:
```json
{{
"sql": "SELECT DATE_TRUNC('month', sale_date) AS month, product_line, SUM(revenue) AS total_revenue, SUM(cost) AS total_cost FROM Sales GROUP BY month, product_line ORDER BY month;",
"chart": {{
"type": "line",
"x_axis": "month",
"y_axis": ["total_revenue", "total_cost"],
"labels": {{"x": "Month", "y": "Amount"}},
"title": "Monthly Revenue vs Cost by Product Line"
}}
}}
USER'S QUERY
NLQ: "{question}"
YOUR RESPONSE (JSON only):
"""
code
# Format the HUMAN_PROMPT_TEMPLATE with the schema and the user's query
prompt = HUMAN_PROMPT_TEMPLATE.format(schema=schema_str, question=nlq_query)
messages = [
SystemMessage(content=SYSTEM_PROMPT),
HumanMessage(content=prompt)
]
response = client.invoke(messages)
# Extract JSON from the response
response_text = response.content
# Try to extract JSON from markdown code blocks
json_match = re.search(r'```(?:json)?\s*(\{.*?\})\s*```', response_text, re.DOTALL)
if json_match:
json_str = json_match.group(1)
else:
# If no code blocks, assume the entire response is JSON
json_str = response_text.strip()
# Parse the JSON
try:
result = json.loads(json_str)
print("Parsed result:", result)
return result
except json.JSONDecodeError as e:
print(f"Failed to parse JSON. Response was: {response_text}")
print(f"Extracted JSON string was: {json_str}")
raise ValueError("The response is not a valid JSON object.") from e
code
The key changes:
1. Added regex pattern matching to extract JSON from markdown code blocks
2. The regex `r'```(?:json)?\s*(\{.*?\})\s*```'` looks for content between triple backticks, optionally preceded by "json"
3. If no code blocks are found, it assumes the entire response is JSON
4. The `return result` statement is now properly inside the function
This should handle the case where the model returns the JSON wrapped in markdown code blocks, which is what's happening based on your debug output.