Commit e1529c0b authored by vuong's avatar vuong

up code

parents
from datetime import datetime
from openai import OpenAI
client = OpenAI(
api_key="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",
)
current_date = datetime.now().strftime("%d/%m/%Y")
# role_prompt = f"You are a data analysis chatbot. You will analyze the data sent in JSON format and provide the result, which is the quantity needed to restock for each product for the next month and provide a brief explanation (reason). The meanings of the data are as follows: 1.order_qty: the quantity needed to restock. 2.qty_expected_avrage_Q1: the expected quantity to be sold in quarter 1. 3.qty_sold_avrage_Q1: the average quantity sold in quarter 1. 4.days_in_stock_Q1: the number of days the product remained in stock in quarter 1. 5.qty_expected_avrage_Q2: the expected quantity to be sold in quarter 2. 6.qty_sold_avrage_Q2: the average quantity sold in quarter 2. 7.days_in_stock_Q2: the number of days the product remained in stock in quarter 2. 8.qty_expected_avrage_Q3: the expected quantity to be sold in quarter 3. 9.qty_sold_avrage_Q3: the average quantity sold in quarter 3. 10.days_in_stock_Q3: the number of days the product remained in stock in quarter 3. 11.qty_expected_avrage_Q4: the expected quantity to be sold in quarter 4. 12.qty_sold_avrage_Q4: the average quantity sold in quarter 4. 13.days_in_stock_Q4: the number of days the product remained in stock in quarter 4. 14.qty_last_halloween: the quantity sold during last year's Halloween. 15.qty_last_xmas: the quantity sold during last year's Christmas. 16.qtysold_30: the quantity sold in the last 30 days. 17.qtysold_60: the quantity sold in the last 60 days. 18.qtysold_90: the quantity sold in the last 90 days. 19.qtysold_180: the quantity sold in the last 180 days. 20.qtysold_720: the quantity sold in the last 2 years. 21.days_in_stock_12: the total number of days the product remained in stock in the last 12 months. 22.days_in_stock_24: the total number of days the product remained in stock in the last 24 months. 23.qty: the quantity of the product in stock. 24.created_at: the date the product was created. 25.b_fill_up_date: the date of the most recent restock. 26.qty_ordered: the total quantity sold. 27.qty_per_month: the average quantity sold per month. 28.last_time_sold: the most recent date the product was sold. 29.vendor_last_fillup_qty: the quantity restocked most recently. 30.qtysold_2024: the quantity sold in the year 2024. 31.qtysold_2023: the quantity sold in the year 2023. 32.qtysold_2022: the quantity sold in the year 2022. 33.qtysold_2021: the quantity sold in the year 2021. 34.qtysold_2020: the quantity sold in the year 2020. Currently, it's {current_date}. The output is in JSON format with the following data: product_id, sku, order_qty, and reason."
role_prompt = f"""
You are a data analysis chatbot.
You need to carefully and clearly analyze the data sent in JSON format and provide the result, which is the quantity needed to restock for each product for the next month (Only one month) and provide an explanation (reason).
The meanings of the data are as follows:
1.order_qty: the quantity needed to restock.
2.qty_expected_avrage_Q1: the expected quantity to be sold in quarter 1.
3.qty_sold_avrage_Q1: the average quantity sold in quarter 1.
4.days_in_stock_Q1: the number of days the product remained in stock in quarter 1.
5.qty_expected_avrage_Q2: the expected quantity to be sold in quarter 2.
6.qty_sold_avrage_Q2: the average quantity sold in quarter 2.
7.days_in_stock_Q2: the number of days the product remained in stock in quarter 2.
8.qty_expected_avrage_Q3: the expected quantity to be sold in quarter 3.
9.qty_sold_avrage_Q3: the average quantity sold in quarter 3.
10.days_in_stock_Q3: the number of days the product remained in stock in quarter 3.
11.qty_expected_avrage_Q4: the expected quantity to be sold in quarter 4.
12.qty_sold_avrage_Q4: the average quantity sold in quarter 4.
13.days_in_stock_Q4: the number of days the product remained in stock in quarter 4.
14.qty_last_halloween: the quantity sold during last year's Halloween.
15.qty_last_xmas: the quantity sold during last year's Christmas.
16.qtysold_30: the quantity sold in the last 30 days.
17.qtysold_60: the quantity sold in the last 60 days.
18.qtysold_90: the quantity sold in the last 90 days.
19.qtysold_180: the quantity sold in the last 180 days.
20.qtysold_720: the quantity sold in the last 2 years.
21.days_in_stock_12: the total number of days the product remained in stock in the last 12 months.
22.days_in_stock_24: the total number of days the product remained in stock in the last 24 months.
23.qty: the quantity of the product in stock.
24.created_at: the date the product was created.
25.b_fill_up_date: the date of the most recent restock.
26.qty_ordered: the total quantity sold.
27.qty_per_month: the average quantity sold per month.
28.last_time_sold: the most recent date the product was sold.
29.vendor_last_fillup_qty: the quantity restocked most recently.
30.qtysold_2024: the quantity sold in the year 2024.
31.qtysold_2023: the quantity sold in the year 2023.
32.qtysold_2022: the quantity sold in the year 2022.
33.qtysold_2021: the quantity sold in the year 2021.
34.qtysold_2020: the quantity sold in the year 2020.
These products are candies, pastries, and children's toys sold in Norway.
Currently, it's {current_date}.
Please take note of the holidays and major festivals in Norway, as well as analyze based on the quantity of items sold and the number of days in stock.
The reason and the provided result need to include clear information about the product as well as the factors influencing that quantity.
The output is in JSON format with the following structure('result': []) and containing the data: product_id, sku, order_qty, and reason.
"""
system = [{"role": "system", "content": role_prompt}]
chat_history = [] # past user and assistant turns for AI memory
def estimate_restock_quantity(data):
user = [{"role": "user", "content": data}]
chat_completion = client.chat.completions.create(
messages = system + chat_history + user,
model="gpt-3.5-turbo",
top_p=0.9,
)
return chat_completion.choices[0].message.content
data = '[{"product_id":126543,"sku":141589,"qty_expected_avrage_Q1":null,"qty_sold_avrage_Q1":5,"days_in_stock_Q1":46,"qty_expected_avrage_Q2":null,"qty_sold_avrage_Q2":0,"days_in_stock_Q2":0,"qty_expected_avrage_Q3":null,"qty_sold_avrage_Q3":0,"days_in_stock_Q3":0,"qty_expected_avrage_Q4":null,"qty_sold_avrage_Q4":0,"days_in_stock_Q4":0,"qty_last_halloween":0,"qty_last_xmas":0,"qtysold_30":4,"qtysold_60":4,"qtysold_90":4,"qtysold_180":4,"qtysold_720":4,"days_in_stock_12":46,"days_in_stock_24":46,"qty":2,"created_at":"2011-09-23","b_fill_up_date":"2016-12-14","qty_ordered":66,"qty_per_month":0.4,"last_time_sold":"2024-02-20","vendor_last_fillup_qty":null,"qtysold_2024":4,"qtysold_2023":0,"qtysold_2022":0,"qtysold_2021":0,"qtysold_2020":0},{"product_id":217688,"sku":191498,"qty_expected_avrage_Q1":null,"qty_sold_avrage_Q1":1,"days_in_stock_Q1":90,"qty_expected_avrage_Q2":null,"qty_sold_avrage_Q2":2,"days_in_stock_Q2":91,"qty_expected_avrage_Q3":null,"qty_sold_avrage_Q3":6,"days_in_stock_Q3":92,"qty_expected_avrage_Q4":null,"qty_sold_avrage_Q4":9,"days_in_stock_Q4":92,"qty_last_halloween":1,"qty_last_xmas":8,"qtysold_30":0,"qtysold_60":0,"qtysold_90":5,"qtysold_180":16,"qtysold_720":31,"days_in_stock_12":365,"days_in_stock_24":681,"qty":1,"created_at":"2022-01-10","b_fill_up_date":"2023-10-30","qty_ordered":34,"qty_per_month":1.3,"last_time_sold":"2023-12-26","vendor_last_fillup_qty":null,"qtysold_2024":0,"qtysold_2023":19,"qtysold_2022":15,"qtysold_2021":null,"qtysold_2020":null},{"product_id":221220,"sku":122199,"qty_expected_avrage_Q1":null,"qty_sold_avrage_Q1":2,"days_in_stock_Q1":88,"qty_expected_avrage_Q2":null,"qty_sold_avrage_Q2":1,"days_in_stock_Q2":91,"qty_expected_avrage_Q3":null,"qty_sold_avrage_Q3":1,"days_in_stock_Q3":92,"qty_expected_avrage_Q4":null,"qty_sold_avrage_Q4":4,"days_in_stock_Q4":79,"qty_last_halloween":2,"qty_last_xmas":2,"qtysold_30":1,"qtysold_60":2,"qtysold_90":3,"qtysold_180":8,"qtysold_720":12,"days_in_stock_12":350,"days_in_stock_24":465,"qty":1,"created_at":"2022-09-14","b_fill_up_date":"2024-01-03","qty_ordered":12,"qty_per_month":0.7,"last_time_sold":"2024-02-29","vendor_last_fillup_qty":3,"qtysold_2024":2,"qtysold_2023":6,"qtysold_2022":4,"qtysold_2021":null,"qtysold_2020":null},{"product_id":217643,"sku":42511,"qty_expected_avrage_Q1":null,"qty_sold_avrage_Q1":3,"days_in_stock_Q1":90,"qty_expected_avrage_Q2":null,"qty_sold_avrage_Q2":1,"days_in_stock_Q2":91,"qty_expected_avrage_Q3":null,"qty_sold_avrage_Q3":0,"days_in_stock_Q3":92,"qty_expected_avrage_Q4":null,"qty_sold_avrage_Q4":4,"days_in_stock_Q4":92,"qty_last_halloween":1,"qty_last_xmas":7,"qtysold_30":3,"qtysold_60":3,"qtysold_90":5,"qtysold_180":8,"qtysold_720":22,"days_in_stock_12":365,"days_in_stock_24":730,"qty":2,"created_at":"2022-01-07","b_fill_up_date":"1970-01-01","qty_ordered":22,"qty_per_month":0.8,"last_time_sold":"2024-02-26","vendor_last_fillup_qty":null,"qtysold_2024":3,"qtysold_2023":8,"qtysold_2022":11,"qtysold_2021":null,"qtysold_2020":null},{"product_id":220858,"sku":346260,"qty_expected_avrage_Q1":null,"qty_sold_avrage_Q1":2,"days_in_stock_Q1":90,"qty_expected_avrage_Q2":null,"qty_sold_avrage_Q2":0,"days_in_stock_Q2":91,"qty_expected_avrage_Q3":null,"qty_sold_avrage_Q3":1,"days_in_stock_Q3":92,"qty_expected_avrage_Q4":null,"qty_sold_avrage_Q4":2,"days_in_stock_Q4":92,"qty_last_halloween":0,"qty_last_xmas":4,"qtysold_30":0,"qtysold_60":2,"qtysold_90":3,"qtysold_180":5,"qtysold_720":9,"days_in_stock_12":365,"days_in_stock_24":533,"qty":1,"created_at":"2022-08-31","b_fill_up_date":"2022-12-16","qty_ordered":9,"qty_per_month":0.5,"last_time_sold":"2024-01-25","vendor_last_fillup_qty":null,"qtysold_2024":2,"qtysold_2023":3,"qtysold_2022":4,"qtysold_2021":null,"qtysold_2020":null},{"product_id":217949,"sku":173746,"qty_expected_avrage_Q1":null,"qty_sold_avrage_Q1":8,"days_in_stock_Q1":76,"qty_expected_avrage_Q2":null,"qty_sold_avrage_Q2":6,"days_in_stock_Q2":91,"qty_expected_avrage_Q3":null,"qty_sold_avrage_Q3":10,"days_in_stock_Q3":92,"qty_expected_avrage_Q4":null,"qty_sold_avrage_Q4":0,"days_in_stock_Q4":92,"qty_last_halloween":2,"qty_last_xmas":1,"qtysold_30":4,"qtysold_60":6,"qtysold_90":6,"qtysold_180":24,"qtysold_720":34,"days_in_stock_12":351,"days_in_stock_24":690,"qty":2,"created_at":"2022-01-20","b_fill_up_date":"2023-07-13","qty_ordered":40,"qty_per_month":1.6,"last_time_sold":"2024-02-21","vendor_last_fillup_qty":null,"qtysold_2024":6,"qtysold_2023":19,"qtysold_2022":15,"qtysold_2021":null,"qtysold_2020":null},{"product_id":218564,"sku":42613,"qty_expected_avrage_Q1":null,"qty_sold_avrage_Q1":4,"days_in_stock_Q1":90,"qty_expected_avrage_Q2":null,"qty_sold_avrage_Q2":3,"days_in_stock_Q2":91,"qty_expected_avrage_Q3":null,"qty_sold_avrage_Q3":2,"days_in_stock_Q3":92,"qty_expected_avrage_Q4":null,"qty_sold_avrage_Q4":1,"days_in_stock_Q4":80,"qty_last_halloween":1,"qty_last_xmas":5,"qtysold_30":3,"qtysold_60":3,"qtysold_90":4,"qtysold_180":10,"qtysold_720":22,"days_in_stock_12":353,"days_in_stock_24":700,"qty":2,"created_at":"2022-02-28","b_fill_up_date":"2023-12-05","qty_ordered":22,"qty_per_month":0.9,"last_time_sold":"2024-02-17","vendor_last_fillup_qty":null,"qtysold_2024":4,"qtysold_2023":6,"qtysold_2022":12,"qtysold_2021":null,"qtysold_2020":null},{"product_id":211858,"sku":131563,"qty_expected_avrage_Q1":null,"qty_sold_avrage_Q1":15,"days_in_stock_Q1":85,"qty_expected_avrage_Q2":null,"qty_sold_avrage_Q2":21,"days_in_stock_Q2":82,"qty_expected_avrage_Q3":null,"qty_sold_avrage_Q3":18,"days_in_stock_Q3":73,"qty_expected_avrage_Q4":null,"qty_sold_avrage_Q4":13,"days_in_stock_Q4":92,"qty_last_halloween":7,"qty_last_xmas":2,"qtysold_30":4,"qtysold_60":12,"qtysold_90":16,"qtysold_180":67,"qtysold_720":106,"days_in_stock_12":332,"days_in_stock_24":659,"qty":4,"created_at":"2019-12-03","b_fill_up_date":"2024-01-25","qty_ordered":150,"qty_per_month":2.9,"last_time_sold":"2024-02-19","vendor_last_fillup_qty":null,"qtysold_2024":12,"qtysold_2023":62,"qtysold_2022":39,"qtysold_2021":23,"qtysold_2020":14},{"product_id":226240,"sku":603428,"qty_expected_avrage_Q1":null,"qty_sold_avrage_Q1":0,"days_in_stock_Q1":49,"qty_expected_avrage_Q2":null,"qty_sold_avrage_Q2":0,"days_in_stock_Q2":0,"qty_expected_avrage_Q3":null,"qty_sold_avrage_Q3":0,"days_in_stock_Q3":0,"qty_expected_avrage_Q4":null,"qty_sold_avrage_Q4":0,"days_in_stock_Q4":0,"qty_last_halloween":null,"qty_last_xmas":null,"qtysold_30":2,"qtysold_60":2,"qtysold_90":2,"qtysold_180":2,"qtysold_720":2,"days_in_stock_12":49,"days_in_stock_24":49,"qty":1,"created_at":"2024-01-12","b_fill_up_date":"1970-01-01","qty_ordered":2,"qty_per_month":1.3,"last_time_sold":"2024-03-01","vendor_last_fillup_qty":null,"qtysold_2024":2,"qtysold_2023":null,"qtysold_2022":null,"qtysold_2021":null,"qtysold_2020":null},{"product_id":217958,"sku":42607,"qty_expected_avrage_Q1":null,"qty_sold_avrage_Q1":18,"days_in_stock_Q1":90,"qty_expected_avrage_Q2":null,"qty_sold_avrage_Q2":19,"days_in_stock_Q2":90,"qty_expected_avrage_Q3":null,"qty_sold_avrage_Q3":24,"days_in_stock_Q3":83,"qty_expected_avrage_Q4":null,"qty_sold_avrage_Q4":32,"days_in_stock_Q4":92,"qty_last_halloween":16,"qty_last_xmas":20,"qtysold_30":7,"qtysold_60":14,"qtysold_90":26,"qtysold_180":89,"qtysold_720":169,"days_in_stock_12":355,"days_in_stock_24":621,"qty":4,"created_at":"2022-01-20","b_fill_up_date":"2023-08-09","qty_ordered":172,"qty_per_month":6.7,"last_time_sold":"2024-02-28","vendor_last_fillup_qty":null,"qtysold_2024":14,"qtysold_2023":90,"qtysold_2022":68,"qtysold_2021":null,"qtysold_2020":null}]'
response = estimate_restock_quantity(data)
def save_to_text_file(text, filename):
with open(filename, "w") as file:
file.write(text)
# save response in file text
save_to_text_file(response, "response.txt")
# print(chat_completion.choices[0].message.content)
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment