SSSSSSSiao commited on
Commit
3cb3961
·
verified ·
1 Parent(s): b1425af

Improve extraction prompt and model

Browse files
Files changed (1) hide show
  1. app.py +48 -9
app.py CHANGED
@@ -4,7 +4,8 @@ import gradio as gr
4
  import torch
5
  from transformers import AutoModelForCausalLM, AutoTokenizer
6
 
7
- MODEL_ID = "Qwen/Qwen2.5-0.5B-Instruct"
 
8
 
9
  ORDER_COLUMNS = [
10
  "customer",
@@ -22,9 +23,41 @@ tokenizer = AutoTokenizer.from_pretrained(MODEL_ID)
22
  model = AutoModelForCausalLM.from_pretrained(MODEL_ID, torch_dtype=torch.float32)
23
  model.eval()
24
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
25
  SYSTEM_PROMPT = """
26
- You extract customer orders from messy DMs for tiny sellers.
27
- Return only valid JSON with this exact shape:
 
28
  {
29
  "orders": [
30
  {
@@ -44,13 +77,19 @@ Return only valid JSON with this exact shape:
44
  {"customer": "", "reply": ""}
45
  ]
46
  }
47
- Use empty strings for unknown values. Put missing details in missing_fields.
48
- """
49
 
50
- EXAMPLE_INPUT = """Maya: Hi! Can I get 2 dozen cupcakes for Saturday morning? Half vanilla, half chocolate.
51
- Sam: Need 1 birthday cake, chocolate, for pickup Friday 5pm. I can pay Venmo.
52
- Lena: Do you still have lemon bars? I need some for tomorrow but not sure how many yet.
53
- Chris: 12 cookies please, pickup at the farmers market. Paid already.
 
 
 
 
 
 
 
 
54
  """
55
 
56
  def extract_json(text):
 
4
  import torch
5
  from transformers import AutoModelForCausalLM, AutoTokenizer
6
 
7
+ # MODEL_ID = "Qwen/Qwen2.5-0.5B-Instruct"
8
+ MODEL_ID = "Qwen/Qwen2.5-1.5B-Instruct"
9
 
10
  ORDER_COLUMNS = [
11
  "customer",
 
23
  model = AutoModelForCausalLM.from_pretrained(MODEL_ID, torch_dtype=torch.float32)
24
  model.eval()
25
 
26
+ # SYSTEM_PROMPT = """
27
+ # You extract customer orders from messy DMs for tiny sellers.
28
+ # Return only valid JSON with this exact shape:
29
+ # {
30
+ # "orders": [
31
+ # {
32
+ # "customer": "",
33
+ # "item": "",
34
+ # "quantity": "",
35
+ # "flavor": "",
36
+ # "pickup_time": "",
37
+ # "delivery_address": "",
38
+ # "payment_status": "",
39
+ # "notes": "",
40
+ # "missing_fields": []
41
+ # }
42
+ # ],
43
+ # "prep_list": [],
44
+ # "reply_drafts": [
45
+ # {"customer": "", "reply": ""}
46
+ # ]
47
+ # }
48
+ # Use empty strings for unknown values. Put missing details in missing_fields.
49
+ # """
50
+
51
+ # EXAMPLE_INPUT = """Maya: Hi! Can I get 2 dozen cupcakes for Saturday morning? Half vanilla, half chocolate.
52
+ # Sam: Need 1 birthday cake, chocolate, for pickup Friday 5pm. I can pay Venmo.
53
+ # Lena: Do you still have lemon bars? I need some for tomorrow but not sure how many yet.
54
+ # Chris: 12 cookies please, pickup at the farmers market. Paid already.
55
+ # """
56
+
57
  SYSTEM_PROMPT = """
58
+ You are a careful order extraction engine for tiny sellers.
59
+
60
+ Extract customer orders from messy DMs. Return only valid JSON with this exact shape:
61
  {
62
  "orders": [
63
  {
 
77
  {"customer": "", "reply": ""}
78
  ]
79
  }
 
 
80
 
81
+ Critical rules:
82
+ - Do not invent customer names.
83
+ - Do not change customer names.
84
+ - Do not merge different customers.
85
+ - Include every customer message that looks like an order or possible order.
86
+ - Use only facts explicitly present in the messages.
87
+ - If a value is unknown, use an empty string.
88
+ - Do not add order_id or total_cost unless the message mentions them.
89
+ - For pickup orders, put the pickup time in pickup_time and leave delivery_address empty unless a real address is given.
90
+ - If the customer is unsure, still include the order and put uncertainty in notes.
91
+ - missing_fields should only include practical fields the seller needs to ask for, such as quantity, flavor, pickup_time, delivery_address, or payment_status.
92
+ - Reply drafts should be short, friendly, and ask only for missing information.
93
  """
94
 
95
  def extract_json(text):