Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rishabh/clarify questions system prompt #181

Merged
merged 14 commits into from
Jun 25, 2024

Conversation

rishsriv
Copy link
Member

@rishsriv rishsriv commented Jun 24, 2024

  • made phrasing clearer for 3 questions where we wanted all months/years - including those which did not occur in the data. This does not change results for any of the models, but figured we might as well clean it up
  • updated the golden query for a question (Return the highest sale price for each make and model of cars that have been sold and are no longer in inventory, ordered by the sale price from highest to lowest.)
  • added a system prompt to prompt_cot.md
  • fixed anthropic and bedrock runners
  • fixed an answer for an advanced question where it was unnecessarily coalescing a value to 0

@rishsriv
Copy link
Member Author

JP/Wendy - for whenever you review this, feel free to make any changes needed and merge!

else:
# raise Exception("Replace this with your private data import")
from defog_data_private.metadata import dbs

with open(self.prompt_file) as file:
model_prompt = file.read()
question_instructions = question + " " + instructions
if table_metadata_string == "":
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oooh good catch that we missed out on this logic for the anthropic runner

Copy link
Collaborator

@wongjingping wongjingping left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for updating, left a small comment on the advanced question.

@@ -41,7 +41,7 @@ SPM (Selling Profit Margin) = (Total Amount from Sells - (Tax + Commission)) / T
TAC = Total Active Customers who joined within a specified timeframe
CR = Rank customers by their total transaction volume, identifying the customer with the highest transaction volume as rank 1. This involves joining price data with ticker identifiers and filtering for a specified date range."
car_dealership,instructions_cte_join,"What is the average number of days between the sale date and payment received date, rounded to 2 decimal places?","When getting duration between sale and payment date for each sale, get the latest payment for sale by aggregating over the payments_received table first.","WITH sale_payments AS (SELECT s.id AS sale_id, s.sale_date, MAX(p.payment_date) AS latest_payment_date FROM sales s JOIN payments_received p ON s.id = p.sale_id GROUP BY 1,2) SELECT ROUND(AVG(latest_payment_date - sale_date), 2) AS avg_days_to_payment FROM sale_payments","When getting duration between sale and payment date for each sale, get the latest payment for sale by aggregating over the payments_received table first. ASP = Calculate the average price of sales within a specific timeframe Last 30 days = Use a range from the current date minus a certain interval to the current date, always ensure to make the necessary joins before utilizing the sales data. TSC = Count of sales within a specified period"
car_dealership,instructions_cte_join,"Return the highest sale price for each make and model of cars that have been sold and are no longer in inventory, ordered by the sale price from highest to lowest.","When getting a car's inventory status, always take the latest status from the inventory_snapshots table","WITH latest_inventory_status AS (SELECT car_id, is_in_inventory, ROW_NUMBER() OVER(PARTITION BY car_id ORDER BY snapshot_date DESC, crtd_ts DESC) AS rn FROM inventory_snapshots) SELECT c.make, c.model, MAX(s.sale_price) AS highest_sale_price FROM cars c JOIN sales s ON c.id = s.car_id JOIN latest_inventory_status lis ON c.id = lis.car_id WHERE lis.is_in_inventory = FALSE AND lis.rn = 1 GROUP BY c.make, c.model ORDER BY highest_sale_price DESC","TSC = Count of sales within a specified period
car_dealership,instructions_cte_join,"Return the highest sale price for each make and model of cars that have been sold and are no longer in inventory, ordered by the sale price from highest to lowest.","When getting a car's inventory status, always take the latest status from the inventory_snapshots table","WITH latest_inventory_status AS (SELECT car_id, is_in_inventory FROM inventory_snapshots) SELECT c.make, c.model, MAX(s.sale_price) AS highest_sale_price FROM cars c JOIN sales s ON c.id = s.car_id JOIN latest_inventory_status lis ON c.id = lis.car_id WHERE lis.is_in_inventory = FALSE GROUP BY c.make, c.model ORDER BY highest_sale_price DESC","TSC = Count of sales within a specified period
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a tricky one and it took me awhile to figure out what's going on here. I think the golden query seems right, and the key lies in the instruction:

"When getting a car's inventory status, always take the latest status from the inventory_snapshots table"

ie to get the inventory status, we'll need to first get the latest row for each car in inventory_snapshot, hence the window function that partitions by car id and orders by snapshot_date (crtd_ts can be safely ignored). That said, I think this might still be ambiguous because this could mean either:

  • get the latest snapshot date, and only consider cars whose is_in_inventory on that specific date. in our data, the latest snapshot date is 2023-04-15, and there is only 1 entry on 2023-04-15 (car 9), and that means only that car is considered.
  • for each car, get its respective latest date. eg car 1's latest date is 2023-03-28, while car 9's latest date is 2023-04-15, and then use the is_in_inventory for that latest date. in our data this means that we have 10 cars that we can consider, out of which 8 is_in_inventory is false.

I think this is hard to clarify much further without an extensive glossary about how the tables work, and I think we might need to adapt the data, such that we get full snapshots of all cars on each snapshot date so that either of both interpretations return the same result. wdyt?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok i'm slightly confused. I'm not getting how the instructions could mean the first point. From "When getting a car's inventory status" I just thought it's pretty straightforward for it to mean an individual car, the same way your second pointer implies. Which means the row_number() is still important to the question.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Aye! From looking at the data underneath,the same car can have multiple entries in the inventory_snapshots table. For example, if this was (say) a second hand car store that can have the same car floating in and out of the system. M

Just clarifying this in either the instructions or questions should work (without updating the underlying data). From the question + instructions, it’s unclear that this would be the case!

I'll do this in a bit :D Thanks JP and Wendy!

Copy link
Collaborator

@wongjingping wongjingping Jun 25, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the inputs all! On a second read, I agree that the second point is the more natural interpretation and am for clarifying the question/instructions, possibly something along the lines of for each car, get its latest entry in the snapshots table and use that entry's inventory status

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in latest push!

generated_query = (
generated_query.split("```sql")[-1].split("```")[0].split(";")[0].strip() + ";"
)
print(generated_query)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: should we remove the print statement?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will do!

shuffle,
)
pruned_metadata_str = pruned_metadata_ddl + join_str
elif columns_to_keep == 0:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks for adding this! sorry I missed this while updating the columns_to_keep earlier.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All good!

Copy link
Contributor

@wendy-aw wendy-aw left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the improvements!

@rishsriv
Copy link
Member Author

rishsriv commented Jun 25, 2024

Made a final change (couldn't find this yesterday because it was a Monday)!

For this question in v1: show me the daily total amount of payments received in the whole of last week, split by the payment_method

Our previous golden query was:

SELECT
  payment_date,
  payment_method,
  SUM(payment_amount) AS total_amount
FROM
  payments_received
WHERE
  payment_date BETWEEN
    DATE_TRUNC('WEEK', CURRENT_DATE) - INTERVAL '1 week'
    AND DATE_TRUNC('WEEK', CURRENT_DATE)
GROUP BY
  payment_date, payment_method
ORDER BY payment_date DESC, payment_method ASC;

However, the question was ambiguous. Last week can mean both the last 7 days, and the last ISO week. So I have changed this to the harder and intended variant show me the daily total amount of payments received in the whole of the last ISO week, split by the payment_method

Additionally, the BETWEEN clause in inclusive. So this query would give answers for a 8 day period instead of a 7 day one. So the golden query is now updated to:

SELECT
  payment_date,
  payment_method,
  SUM(payment_amount) AS total_amount
FROM
  payments_received
WHERE
  payment_date >= DATE_TRUNC('WEEK', CURRENT_DATE) - INTERVAL '1 week'
  AND payment_date < DATE_TRUNC('WEEK', CURRENT_DATE)
GROUP BY
  payment_date, payment_method
ORDER BY payment_date DESC, payment_method ASC;

@wendy-aw
Copy link
Contributor

ooh good find. thanks for the change!

Copy link
Collaborator

@wongjingping wongjingping left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm thanks for the detailed fixes and explanation as well!

@wongjingping wongjingping merged commit 062638c into main Jun 25, 2024
2 checks passed
@wongjingping wongjingping deleted the rishabh/clarify-questions-system-prompt branch June 25, 2024 04:50
wendy-aw added a commit that referenced this pull request Jun 26, 2024
wendy-aw added a commit that referenced this pull request Jun 26, 2024
- Remove 'MoM will always be zero for the first month that appears in your answer.'
rishsriv pushed a commit that referenced this pull request Jun 26, 2024
- Remove 'MoM will always be zero for the first month that appears in your answer.'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants