-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclassifier.py
More file actions
346 lines (292 loc) · 11.6 KB
/
classifier.py
File metadata and controls
346 lines (292 loc) · 11.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
"""
Classifier module - uses LLM to extract structured loyalty program data.
Supports OpenAI API (GPT-4/GPT-3.5) and can be extended for other providers.
"""
import os
import json
from typing import Optional
from datetime import datetime
from schemas import ParsedContent, LoyaltyProgram
# System prompt for loyalty program extraction - FastBite Rewards structure
# STRICT: No hallucination - only extract what is explicitly stated
SYSTEM_PROMPT = """You are an expert at extracting structured loyalty program information from web page content.
## CRITICAL ANTI-HALLUCINATION RULES:
1. ONLY extract information that is EXPLICITLY stated in the source text
2. DO NOT invent or infer ANY values not directly stated
3. If something is not mentioned, use null or empty arrays - NEVER guess
4. Quote exact text from source when possible
5. When in doubt, leave it empty
## EXTRACTION GUIDE:
### programName
- The official name of the loyalty program (e.g., "Starbucks Rewards", "MyMcDonald's Rewards")
- Must be explicitly stated on the page
### description
- The official program description or tagline
- Use exact text from the source
### strategy.industry
- The business category (e.g., "Quick Service Restaurant", "Coffee", "Fast Food")
- Infer from brand context if obvious (Starbucks = Coffee)
### strategy.programType
- Usually "B2C Customer Loyalty" for consumer programs
- Could be "Membership", "Subscription" if stated
### strategy.goals
- Business objectives like "Increase purchase frequency", "Improve retention"
- ONLY if explicitly stated or clearly implied
### strategy.behaviors
- Actions customers can take: "Purchases", "App usage", "Referrals", "Social sharing", "Reviews"
- ONLY include if the program explicitly rewards these behaviors
### strategy.audience
- Target customer segments mentioned: "Frequent visitors", "Mobile app users", "New customers"
- ONLY if explicitly described
### strategy.channels
- How customers interact: "Mobile app", "Website", "In-store POS", "Email"
- Include channels explicitly mentioned
### design.segments
- Named customer segments with criteria (e.g., "Gold Members: 5000+ points annually")
- Include approx_users ONLY if stated
- MOST programs don't explicitly define segments - leave empty if not stated
### design.tiers
- ONLY membership tier levels explicitly named (e.g., "Bronze", "Silver", "Gold", "Platinum")
- Each tier needs: name, range (point threshold), multiplierText (earning multiplier), benefits
- DO NOT confuse redemption levels (25 Stars, 100 Stars) with membership tiers
- If no tiers are explicitly named, leave empty
### design.incentives
- Promotional campaigns and special offers
- Examples: "Birthday Reward", "Referral Bonus", "Double Points Days", "Welcome Offer", "Free Refills"
- Include name and description
- IMPORTANT: Extract ALL benefits and special offers mentioned
### design.rewards.loyalty_points
- The earning rate: "1 point per $1", "2 Stars per $1", "10 points per $1"
- Extract EXACTLY as stated
### design.rewards.achievement_badges
- Gamification badges/achievements with Name, Criteria, and Reward
- Examples: "First Purchase Badge - Complete first order - 50 bonus points"
- ONLY if explicitly mentioned
### design.rewards.gift_cards
- Gift card redemption options with Name (value), redemption_points, validity_days
- ONLY if explicitly listed
### design.rewards.catalog_products
- Items available for point redemption
- Name: exact item name or category
- point_cost: exact point cost as stated
- IMPORTANT: Extract ALL redemption tiers mentioned (e.g., 25 Stars, 100 Stars, 200 Stars, 300 Stars, 400 Stars)
- Include ALL items at each tier level
- This is where redemption items go (NOT membership tiers)
## OUTPUT FORMAT:
Return valid JSON matching this exact structure:
{
"programName": "string or null",
"description": "string or null",
"strategy": {
"industry": "string or null",
"programType": "string or null",
"goals": ["string array - only if stated"],
"behaviors": ["string array - only if rewarded"],
"audience": ["string array - only if stated"],
"channels": ["string array - only if mentioned"]
},
"design": {
"segments": [
{"name": "string", "criteria": "string", "approx_users": number_or_null}
],
"tiers": [
{"name": "string", "range": "string", "multiplierText": "string", "benefits": ["string"]}
],
"incentives": [
{"name": "string", "description": "string or null"}
],
"rewards": {
"loyalty_points": {"points_per_dollar": "string or null"},
"achievement_badges": [
{"Name": "string", "Criteria": "string", "Reward": "string"}
],
"gift_cards": [
{"Name": "string", "redemption_points": "string", "validity_days": "string"}
],
"catalog_products": [
{"Name": "string", "point_cost": "string"}
]
}
}
}
REMEMBER: Extract only what IS there. Leave empty what ISN'T. Never invent."""
def classify_with_openai(parsed: ParsedContent, model: str = "gpt-4o-mini") -> Optional[dict]:
"""
Send parsed content to OpenAI for classification.
Requires OPENAI_API_KEY environment variable.
"""
api_key = os.environ.get("OPENAI_API_KEY")
if not api_key:
print("Warning: OPENAI_API_KEY not set. Skipping LLM classification.")
return None
try:
import openai
client = openai.OpenAI(api_key=api_key)
# Truncate full_text if too long (keep under ~20k tokens, ~25k chars)
text = parsed.full_text[:25000]
user_prompt = f"""Extract loyalty program information from this {parsed.brand} webpage:
URL: {parsed.url}
--- PAGE CONTENT ---
{text}
--- END CONTENT ---
Return the structured JSON extraction."""
response = client.chat.completions.create(
model=model,
messages=[
{"role": "system", "content": SYSTEM_PROMPT},
{"role": "user", "content": user_prompt}
],
temperature=0.1,
response_format={"type": "json_object"}
)
result_text = response.choices[0].message.content
return json.loads(result_text)
except ImportError:
print("Error: openai package not installed. Run: pip install openai")
return None
except json.JSONDecodeError as e:
print(f"Error parsing LLM response as JSON: {e}")
return None
except Exception as e:
print(f"Error calling OpenAI API: {e}")
return None
def build_loyalty_program(parsed: ParsedContent, llm_result: Optional[dict]) -> LoyaltyProgram:
"""
Build a LoyaltyProgram object from parsed content and LLM classification.
Always returns full structure with null values for missing fields.
"""
from schemas import (
Strategy, Design, Segment, Tier, Incentive,
Rewards, LoyaltyPoints, AchievementBadge, GiftCard, CatalogProduct
)
# Default empty structure
default_strategy = Strategy(
industry=None,
programType=None,
goals=[],
behaviors=[],
audience=[],
channels=[]
)
default_rewards = Rewards(
loyalty_points=LoyaltyPoints(points_per_dollar=None),
achievement_badges=[],
gift_cards=[],
catalog_products=[]
)
default_design = Design(
segments=[],
tiers=[],
incentives=[],
rewards=default_rewards
)
if llm_result:
# Build Strategy from LLM result
strategy_data = llm_result.get("strategy", {}) or {}
strategy = Strategy(
industry=strategy_data.get("industry"),
programType=strategy_data.get("programType"),
goals=strategy_data.get("goals", []) or [],
behaviors=strategy_data.get("behaviors", []) or [],
audience=strategy_data.get("audience", []) or [],
channels=strategy_data.get("channels", []) or []
)
# Build Design from LLM result
design_data = llm_result.get("design", {}) or {}
segments = [
Segment(
name=seg.get("name"),
criteria=seg.get("criteria"),
approx_users=seg.get("approx_users")
) for seg in (design_data.get("segments", []) or [])
]
tiers = [
Tier(
name=tier.get("name"),
range=tier.get("range"),
multiplierText=tier.get("multiplierText"),
benefits=tier.get("benefits", []) or []
) for tier in (design_data.get("tiers", []) or [])
]
incentives = [
Incentive(
name=inc.get("name"),
description=inc.get("description")
) for inc in (design_data.get("incentives", []) or [])
]
# Build Rewards
rewards_data = design_data.get("rewards", {}) or {}
loyalty_points_data = rewards_data.get("loyalty_points", {}) or {}
loyalty_points = LoyaltyPoints(
points_per_dollar=loyalty_points_data.get("points_per_dollar")
)
achievement_badges = [
AchievementBadge(
Name=badge.get("Name"),
Criteria=badge.get("Criteria"),
Reward=badge.get("Reward")
) for badge in (rewards_data.get("achievement_badges", []) or [])
]
gift_cards = [
GiftCard(
Name=gc.get("Name"),
redemption_points=gc.get("redemption_points"),
validity_days=gc.get("validity_days")
) for gc in (rewards_data.get("gift_cards", []) or [])
]
catalog_products = [
CatalogProduct(
Name=prod.get("Name"),
point_cost=prod.get("point_cost")
) for prod in (rewards_data.get("catalog_products", []) or [])
]
rewards = Rewards(
loyalty_points=loyalty_points,
achievement_badges=achievement_badges,
gift_cards=gift_cards,
catalog_products=catalog_products
)
design = Design(
segments=segments,
tiers=tiers,
incentives=incentives,
rewards=rewards
)
return LoyaltyProgram(
programName=llm_result.get("programName"),
description=llm_result.get("description"),
strategy=strategy,
design=design,
brand=parsed.brand,
url=parsed.url,
scraped_at=datetime.utcnow(),
raw_text_length=len(parsed.full_text)
)
else:
# Fallback: return full structure with nulls
return LoyaltyProgram(
programName=parsed.title,
description=None,
strategy=default_strategy,
design=default_design,
brand=parsed.brand,
url=parsed.url,
scraped_at=datetime.utcnow(),
raw_text_length=len(parsed.full_text)
)
def classify(parsed: ParsedContent, skip_llm: bool = False, model: str = "gpt-4o-mini") -> LoyaltyProgram:
"""
Main classification function.
Takes ParsedContent and returns structured LoyaltyProgram.
"""
llm_result = None
if not skip_llm:
print(f" Classifying with LLM ({model})...")
llm_result = classify_with_openai(parsed, model=model)
if llm_result:
print(f" ✓ LLM extraction complete")
else:
print(f" ✗ LLM extraction failed, using fallback")
else:
print(f" Skipping LLM classification (--skip-llm)")
return build_loyalty_program(parsed, llm_result)