greetings / utils.py
vishal1278's picture
moved code out of src/
5b169d1
raw
history blame
3.67 kB
import pandas as pd
from openai import OpenAI
from dotenv import load_dotenv
import os
import sys
sys.path.append(".")
from prompts import SYSTEM_PROMPT, PERSONA_DESCR, CAMPAIGN_DESCR
load_dotenv("../.env")
client = OpenAI(api_key=os.getenv("OPENAI_API_KEY"))
def generate_summary(customer_data):
customer_value = "high" if customer_data["Revenue"] >= 250 else "low"
customer_recency = "recent" if customer_data["Recency"] <= 60 else "not recent"
seg = customer_data["Segment"]
if seg == "Artist":
cust_seg_descr = PERSONA_DESCR["Artist"]
elif seg == "Creator":
cust_seg_descr = PERSONA_DESCR["Creator"]
elif seg == "Curator":
cust_seg_descr = PERSONA_DESCR["Curator"]
elif seg == "Seeker":
cust_seg_descr = PERSONA_DESCR["Seeker"]
else:
print("Invalid Segment")
summary = (
f"This is a {customer_value} value customer who has spent a total of {customer_data['Revenue']} dollars. "
f"Their most recent transaction was {customer_data['Recency']} days ago, which makes him/her a {customer_recency} customer. "
f"The latest product they purchased was {customer_data['LatestProduct']}. "
f"Their favorite department is {customer_data['FavoriteDepartment']}. "
f"{cust_seg_descr} ({seg})"
)
return summary
def generate_welcome_message(summary, campaign_name):
campaign_description = "The personalize message should align with the marketing campaign described below: "
if campaign_name == "Summer Clearance":
campaign_description += CAMPAIGN_DESCR["Summer Clearance"] + "\n"
elif campaign_name == "Makehaul":
campaign_description += CAMPAIGN_DESCR["Makehaul"] + "\n"
elif campaign_name == "Lowest Prices Of the Season":
campaign_description += CAMPAIGN_DESCR["Lowest Prices Of the Season"] + "\n"
else:
print("Invalid Campaign Name")
summary += f"{campaign_description}"
response = client.chat.completions.create(
model="gpt-4o-mini",
messages=[
{"role": "system", "content": SYSTEM_PROMPT},
{"role": "user", "content": summary},
],
)
message = response.choices[0].message.content
return message
def generate_dummy_data():
data = {
"Name": [
"Alice",
"Bob",
"Charlie",
"David",
"Eva",
"Frank",
"Grace",
"Henry",
"Ivy",
"John",
],
"Recency": [20, 50, 100, 10, 120, 70, 30, 150, 60, 40],
"Revenue": [200, 350, 150, 400, 100, 250, 300, 80, 330, 280],
"LatestProduct": [
"All Strung Beads",
"Permanent and Removable Vinyl",
"Creatology Baskets",
"Kids Storage",
"Lap Trays",
"Lemax Spooky Town Collection",
"Frames & Shadow Boxes",
"ALL Fall Stems",
"Folding Step Stool",
"Mini Sticker Printer",
],
"FavoriteDepartment": [
"KIDS ART & EDUCATION",
"HOBBY CRAFTS",
"BAKEWARE",
"FRAMES",
"PAPERCRAFTING",
"STORAGE",
"HOBBY CRAFTS",
"FLORAL ACCESSORIES",
"HOBBY CRAFTS",
"KIDS ART & EDUCATION",
],
"Segment": [
"Artist",
"Creator",
"Curator",
"Artist",
"Seeker",
"Curator",
"Artist",
"Seeker",
"Creator",
"Curator",
],
}
return pd.DataFrame(data)