Spaces:
Runtime error
Runtime error
Upload utils.py
Browse files123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103import openaiimport reimport jsonimport osimport timeclient = openai.OpenAI(api_key=os.getenv("OPENAI_API_KEY"))def sanitize_linkedin_url(url):if not url:return ""match = re.search(r"/in/([^/?]+)", url)if match:return f"https://www.linkedin.com/in/{match.group(1)}"match = re.search(r"profileUrn=urn:li:fsd_profile:(\w+)", url)if match:return f"https://www.linkedin.com/in/{match.group(1)}"return urldef generate_rtm(company, website, workspace=None):prompt = f"""You are an expert enterprise researcher helping a sales team uncover strategic business insights.Please generate a comprehensive Reason-to-Meet (RTM) report for the following company:Company: {company}Website: {website}Follow this structure:1. Company Overview2. Business Breakdown3. Strategic Initiatives4. Executive Team5. Challenges6. How PagerDuty Can Help7. SourcesUse recent, factual insights from reputable public sources only. Do not speculate. Align your analysis to PagerDuty’s value proposition based on the uploaded resources.Respond in clean markdown format with section headers and readable bullets.Cite sources after each section."""response = client.chat.completions.create(model="gpt-4-turbo",messages=[{"role": "user", "content": prompt}])return response.choices[0].message.content.strip()def generate_prospecting_message(context, persona, linkedin_url, enriched_context, rtm_report, workspace=None, is_followup=False):context_type = "follow-up" if is_followup else "first-touch"base_prompt = f"""You are writing a short, strategic {context_type} email to a prospect.Your goal is to:- Show you understand the company’s strategic initiatives and challenges- Relate to what they’re working on using thoughtful insight (not product pitching)- Connect to their likely role in those efforts- Invite a quick, casual conversationAvoid listing product features. Do not reference PagerDuty directly unless naturally relevant. Use insight and empathy, not a pitch.Tone should be confident, smart, and curious — not salesy or overly formal.Include:- A clear opener that ties to something observed in the Reason to Meet report- A strategic angle or insight that shows you understand their world- A crisp, conversational CTA (e.g., “open to comparing notes?”)---Prospect Info:{enriched_context}LinkedIn: {linkedin_url}---Reason to Meet (RTM) Report:{rtm_report}"""response = client.chat.completions.create(model="gpt-4-turbo",messages=[{"role": "user", "content": base_prompt}])return response.choices[0].message.content.strip()def log_event(workspace, event_type):ts = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())log = {"time": ts, "workspace": workspace, "event": event_type}log_file = "usage_log.json"if os.path.exists(log_file):with open(log_file, "r") as f:data = json.load(f)else:data = []data.append(log)with open(log_file, "w") as f:json.dump(data, f, indent=2)12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364import openaiimport requestsclient = openai.OpenAI()def generate_rtm(company, website):prompt = f"""You are an AI-powered assistant helping an enterprise account executive research and prepare for a strategic outbound opportunity.Provide a detailed Reason to Meet (RTM) report for the following company:Company: {company}Website: {website}The report should include:1. Company Overview2. Business Breakdown3. Strategic Initiatives4. Executive Team (with relevant roles)5. Challenges (e.g., MTTR, monitoring gaps, legacy systems, digital transformation)6. How PagerDuty Can Help (align value to the challenges and initiatives)7. Sources (cite specific URLs or articles found)Make the tone strategic and grounded in facts. Do not speculate."""response = client.chat.completions.create(model="gpt-4",messages=[{"role": "user", "content": prompt}])return response.choices[0].message.content.strip()def generate_prospecting_message(linkedin_url, context, rtm_text, workspace=None, custom_title=None, ghostwriter_url=None, is_followup=False):name = "[Recipient Name]"if "linkedin.com" in linkedin_url:try:resp = requests.get(linkedin_url, timeout=10)name = resp.text.split("firstName")[1].split('"')[1]except Exception:passtitle_part = f"Title: {custom_title}" if custom_title else ""ghostwriter_part = f"Ghostwriter: {ghostwriter_url}" if ghostwriter_url else ""prompt = f"""You are an AI helping to craft a personalized {('follow-up' if is_followup else 'initial')} prospecting message.{name}{title_part}{ghostwriter_part}Context:{context}RTM Report:{rtm_text}Write the message from the point of view of an Enterprise Account Executive at PagerDuty. Do not reference the title of the prospect or use overly generic openings like "I hope this message finds you well." Focus on the challenges or goals inferred from the RTM."""response = client.chat.completions.create(model="gpt-4",messages=[{"role": "user", "content": prompt}])return response.choices[0].message.content.strip()
utils.py+42-81