marketing
Browse files- app.py +196 -35
- entity.png +0 -0
- entity.py +84 -0
- knowledge.py +78 -15
- knowledge_graph1.png +0 -0
- knowledge_graph2.png +0 -0
app.py
CHANGED
@@ -5,6 +5,7 @@ from graphrag import marketingPlan
|
|
5 |
from knowledge import graph
|
6 |
from pii import derisk
|
7 |
from classify import judge
|
|
|
8 |
|
9 |
# Define the Google Analytics script
|
10 |
head = """
|
@@ -42,9 +43,9 @@ Other Links:
|
|
42 |
- https://kevinwkc.github.io/davinci/
|
43 |
""")
|
44 |
|
45 |
-
with gr.Tab("RAG"):
|
46 |
gr.Markdown("""
|
47 |
-
Objective:
|
48 |
================================================
|
49 |
- Retrieval: Public RBC Product Data
|
50 |
- Recommend: RBC Product
|
@@ -67,24 +68,39 @@ Other Links:
|
|
67 |
btn_recommend.click(fn=rbc_product, inputs=in_verbatim, outputs=out_product)
|
68 |
|
69 |
gr.Markdown("""
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
74 |
|
75 |
-
-
|
76 |
-
|
|
|
|
|
77 |
|
78 |
-
|
79 |
-
|
|
|
|
|
80 |
|
81 |
-
|
82 |
-
Promotes underperforming products or clears surplus stock with strategic recommendations.
|
83 |
""")
|
84 |
|
85 |
-
with gr.Tab("Tool Use"):
|
86 |
gr.Markdown("""
|
87 |
-
Objective:
|
88 |
================================================
|
89 |
- Retrieval: Public Product Data using Tavily Search
|
90 |
- Recommend: Competition Product
|
@@ -102,23 +118,38 @@ Promotes underperforming products or clears surplus stock with strategic recomme
|
|
102 |
btn_recommend.click(fn=rival_product, inputs=in_verbatim, outputs=out_product)
|
103 |
|
104 |
gr.Markdown("""
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
109 |
|
110 |
-
|
111 |
-
Captures potential lost sales by guiding customers toward viable substitutes.
|
112 |
|
113 |
-
|
114 |
-
|
|
|
|
|
115 |
|
116 |
-
|
117 |
-
|
|
|
|
|
|
|
|
|
118 |
""")
|
119 |
-
with gr.Tab("
|
120 |
gr.Markdown("""
|
121 |
-
Objective:
|
122 |
=======================
|
123 |
- Reasoning from context, answering the question
|
124 |
""")
|
@@ -180,12 +211,10 @@ Quickly generates tailored campaigns, reducing go-to-market time from weeks to h
|
|
180 |
Uses customer data and behavior to craft messages that resonate with specific segments.
|
181 |
""")
|
182 |
|
183 |
-
with gr.Tab("Knowledge Graph"):
|
184 |
gr.Markdown("""
|
185 |
-
Objective:
|
186 |
=====================================
|
187 |
-
- We create query plan by breaking down into subquery
|
188 |
-
- Using those subquery to create knowledge graph
|
189 |
""")
|
190 |
in_verbatim = gr.Textbox(label="Question")
|
191 |
out_product = gr.JSON(label="Knowledge Graph")
|
@@ -193,15 +222,68 @@ Objective: Explain concept in knowledge graph structured output
|
|
193 |
gr.Examples(
|
194 |
[
|
195 |
[
|
196 |
-
"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
197 |
]
|
198 |
],
|
199 |
[in_verbatim]
|
200 |
)
|
201 |
btn_recommend = gr.Button("Graph It!")
|
|
|
202 |
btn_recommend.click(fn=graph, inputs=in_verbatim, outputs=out_product)
|
203 |
|
|
|
204 |
gr.Markdown("""
|
|
|
|
|
|
|
|
|
|
|
205 |
Benefits of a Knowledge Graph
|
206 |
============
|
207 |
- Smarter Data Relationships
|
@@ -217,9 +299,9 @@ Surfaces hidden patterns and relationships for better analytics and insights.
|
|
217 |
Once created, knowledge graphs can be repurposed across multiple use cases (e.g., search, recommendation, fraud detection).
|
218 |
""")
|
219 |
|
220 |
-
with gr.Tab("
|
221 |
gr.Markdown("""
|
222 |
-
Objective:
|
223 |
================================================
|
224 |
""")
|
225 |
in_verbatim = gr.Textbox(label="Peronal Info")
|
@@ -256,9 +338,9 @@ Allows downstream tasks (like sentiment analysis or topic modeling) to focus on
|
|
256 |
""")
|
257 |
|
258 |
|
259 |
-
with gr.Tab("
|
260 |
gr.Markdown("""
|
261 |
-
Objective: Classify
|
262 |
================================================
|
263 |
- multi class classification, could have multiple label for 1 feedback
|
264 |
- fix classification in this use case: online banking, card, auto finance, mortgage, insurance
|
@@ -298,4 +380,83 @@ Proactively flags 5+ fraud types (identity theft, money laundering) with 40% few
|
|
298 |
- Regulatory Compliance
|
299 |
Auto-classifies documents for FINRA/SEC audits (e.g., Morgan Stanley uses NLP to categorize 3M+ annual communications into 50+ compliance buckets).
|
300 |
""")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
301 |
demo.launch(allowed_paths=["./"])
|
|
|
5 |
from knowledge import graph
|
6 |
from pii import derisk
|
7 |
from classify import judge
|
8 |
+
from entity import resolve
|
9 |
|
10 |
# Define the Google Analytics script
|
11 |
head = """
|
|
|
43 |
- https://kevinwkc.github.io/davinci/
|
44 |
""")
|
45 |
|
46 |
+
with gr.Tab("RAG Recommender"):
|
47 |
gr.Markdown("""
|
48 |
+
Objective: Dynamic RBC Product Recommender: Personalize Offers Using Customer Persona Insights
|
49 |
================================================
|
50 |
- Retrieval: Public RBC Product Data
|
51 |
- Recommend: RBC Product
|
|
|
68 |
btn_recommend.click(fn=rbc_product, inputs=in_verbatim, outputs=out_product)
|
69 |
|
70 |
gr.Markdown("""
|
71 |
+
Companies pour millions into product catalogs, marketing funnels, and user acquisition—yet many still face the same challenge:
|
72 |
+
==================
|
73 |
+
### 📉 Pain points:
|
74 |
+
- High bounce rates and low conversion despite heavy traffic
|
75 |
+
- Customers struggle to find relevant products on their own
|
76 |
+
- One-size-fits-all promotions result in wasted ad spend and poor ROI
|
77 |
+
|
78 |
+
### 🧩 The real question:
|
79 |
+
What if your product catalog could *adapt itself* to each user in real time—just like your best salesperson would?
|
80 |
+
|
81 |
+
### 🎯 The customer need:
|
82 |
+
Businesses need a way to dynamically personalize product discovery, so every customer sees the most relevant items—without manually configuring hundreds of rules.
|
83 |
+
|
84 |
+
## ✅ Enter: Product Recommender Systems
|
85 |
+
|
86 |
+
By analyzing behavioral data, preferences, and historical purchases, a recommender engine surfaces what each user is most likely to want—boosting engagement and revenue.
|
87 |
|
88 |
+
### 📌 Real-world use cases:
|
89 |
+
- **Amazon** attributes up to 35% of its revenue to its recommender system, which tailors the home page, emails, and checkout cross-sells per user.
|
90 |
+
- **Netflix** leverages personalized content recommendations to reduce churn and increase watch time—saving the company over $1B annually in retention value.
|
91 |
+
- **Stitch Fix** uses machine learning-powered recommendations to curate clothing boxes tailored to individual style profiles—scaling personal styling.
|
92 |
|
93 |
+
### 💡 Business benefits:
|
94 |
+
- Higher conversion rates through relevant discovery
|
95 |
+
- Increased average order value (AOV) via cross-sell and upsell
|
96 |
+
- Improved retention and lower customer acquisition cost (CAC)
|
97 |
|
98 |
+
If your product discovery experience isn’t working as hard as your marketing budget, it’s time to make your catalog intelligent—with recommendations that convert.
|
|
|
99 |
""")
|
100 |
|
101 |
+
with gr.Tab("Tool Use Competitive Research"):
|
102 |
gr.Markdown("""
|
103 |
+
Objective: Persona-Driven Financial Product Recommendations: Unlock Competitive Advantage & Feature Innovation
|
104 |
================================================
|
105 |
- Retrieval: Public Product Data using Tavily Search
|
106 |
- Recommend: Competition Product
|
|
|
118 |
btn_recommend.click(fn=rival_product, inputs=in_verbatim, outputs=out_product)
|
119 |
|
120 |
gr.Markdown("""
|
121 |
+
Companies in competitive industries are constantly under pressure to innovate—but often face the same challenge:
|
122 |
+
==================
|
123 |
+
### 📉 Pain points:
|
124 |
+
- Unable to identify gaps or opportunities in competitor products in real-time.
|
125 |
+
- Lack of insight into customer feedback on competitor features.
|
126 |
+
- Difficulty in predicting how new features will be received in the market.
|
127 |
+
|
128 |
+
### 🧩 The real question:
|
129 |
+
How can your product stay ahead of the competition without a clear understanding of what features your competitors are developing, and how they’re performing with customers?
|
130 |
+
|
131 |
+
### 🎯 The customer need:
|
132 |
+
What businesses really need is a data-driven approach to **competitor product research**, one that can identify trends, uncover feature gaps, and provide actionable insights to drive innovation in product development.
|
133 |
+
|
134 |
+
## ✅ Solution: **Competitor Product Research for Feature Development**
|
135 |
|
136 |
+
By leveraging AI, market intelligence, and competitive analysis tools, you can track competitor launches, analyze user sentiment, and evaluate feature performance across the board. This insight helps shape strategic product decisions—ensuring your team isn't building in the dark.
|
|
|
137 |
|
138 |
+
### 📌 Real-world use cases:
|
139 |
+
- **Spotify** tracks competitor music features, leveraging insights from users and music trends to introduce features like playlist sharing and collaborative playlists—leading to increased user engagement.
|
140 |
+
- **Apple** regularly conducts competitor analysis to anticipate and outpace trends, such as implementing health tracking features before they became mainstream in wearables.
|
141 |
+
- **Slack** uses competitor research to build features that cater to the evolving needs of remote teams, like advanced search functionality and integrations with other tools.
|
142 |
|
143 |
+
### 💡 Business benefits:
|
144 |
+
- **Informed product decisions**: Develop features that fill gaps and add value in ways competitors aren’t addressing.
|
145 |
+
- **Faster time-to-market**: Avoid reinventing the wheel by learning from competitors’ successes and mistakes.
|
146 |
+
- **Market positioning**: Stay one step ahead of competitors, ensuring your product remains the best solution for your target audience.
|
147 |
+
|
148 |
+
With the right competitive research, you don’t just react to the market—you anticipate it.
|
149 |
""")
|
150 |
+
with gr.Tab("Graphrag Marketing Plan"):
|
151 |
gr.Markdown("""
|
152 |
+
Objective: Develop a Targeted Marketing Plan Aligned with Customer Personas
|
153 |
=======================
|
154 |
- Reasoning from context, answering the question
|
155 |
""")
|
|
|
211 |
Uses customer data and behavior to craft messages that resonate with specific segments.
|
212 |
""")
|
213 |
|
214 |
+
with gr.Tab("Personalized Knowledge Graph"):
|
215 |
gr.Markdown("""
|
216 |
+
Objective: Transform Personal Pain Points into Actionable Insights with a Dynamic Knowledge Graph Framework
|
217 |
=====================================
|
|
|
|
|
218 |
""")
|
219 |
in_verbatim = gr.Textbox(label="Question")
|
220 |
out_product = gr.JSON(label="Knowledge Graph")
|
|
|
222 |
gr.Examples(
|
223 |
[
|
224 |
[
|
225 |
+
"""
|
226 |
+
Representative: "Thank you for calling Goldman Sachs Credit Card Services. My name is Sarah. May I have your full name and the last 4 digits of your card number for verification?"
|
227 |
+
|
228 |
+
Customer: "This is Michael Chen, card ending 5402."
|
229 |
+
|
230 |
+
Representative: "Thank you, Mr. Chen. I show you have an Apple Card account opened in 2023. How can I assist you today?" (Reference: Search Result Apple Card context)
|
231 |
+
|
232 |
+
Customer: "I'm disputing a $329 charge from TechElectronics from March 15th. I never received the item."
|
233 |
+
|
234 |
+
Representative: "I understand your concern. Let me initiate a dispute investigation. Per our process (Search Result BBB complaint handling):
|
235 |
+
|
236 |
+
We'll apply a temporary credit within 24 hours
|
237 |
+
|
238 |
+
Our team will contact the merchant
|
239 |
+
|
240 |
+
You'll receive email updates at [email protected]
|
241 |
+
|
242 |
+
Final resolution within 60 days
|
243 |
+
|
244 |
+
Would you like me to proceed?"
|
245 |
+
|
246 |
+
Customer: "Yes, but what if they fight it?"
|
247 |
+
|
248 |
+
Representative: "If the merchant disputes your claim, we'll:
|
249 |
+
|
250 |
+
Review all evidence using our 3-phase verification system (Search Result multi-stage investigation)
|
251 |
+
|
252 |
+
Consider your purchase protection benefits
|
253 |
+
|
254 |
+
Escalate to senior specialists if needed
|
255 |
+
|
256 |
+
For security, never share your CVV (339) or full card number with callers. Always call back using the number on your physical card (Search Result scam warning)."
|
257 |
+
|
258 |
+
Customer: "Can I make a partial payment on my balance while this is pending?"
|
259 |
+
|
260 |
+
Representative: "Absolutely. We offer:
|
261 |
+
"minimum_payment": "$35 due April 25",
|
262 |
+
"hardship_program": "0% APR for 6 months",
|
263 |
+
"custom_plan": "Split $600 balance over 3 months"
|
264 |
+
Would you like to enroll in any of these?"
|
265 |
+
|
266 |
+
Customer: "The 3-month plan, please."
|
267 |
+
|
268 |
+
Representative: "Confirmed. Your next payment of $200 will process May 1st. A confirmation email with dispute case #GS-2025-0422-8830 is being sent now. Is there anything else?"
|
269 |
+
|
270 |
+
Customer: "No, thank you."
|
271 |
+
"""
|
272 |
]
|
273 |
],
|
274 |
[in_verbatim]
|
275 |
)
|
276 |
btn_recommend = gr.Button("Graph It!")
|
277 |
+
btn_clear = gr.ClearButton(components=[out_product])
|
278 |
btn_recommend.click(fn=graph, inputs=in_verbatim, outputs=out_product)
|
279 |
|
280 |
+
|
281 |
gr.Markdown("""
|
282 |
+
Example of Customer Profile in Graph
|
283 |
+
=================
|
284 |
+

|
285 |
+

|
286 |
+
|
287 |
Benefits of a Knowledge Graph
|
288 |
============
|
289 |
- Smarter Data Relationships
|
|
|
299 |
Once created, knowledge graphs can be repurposed across multiple use cases (e.g., search, recommendation, fraud detection).
|
300 |
""")
|
301 |
|
302 |
+
with gr.Tab("PII Audit"):
|
303 |
gr.Markdown("""
|
304 |
+
Objective: Automated PII Data Removal: Proactive Compliance & Risk Mitigation
|
305 |
================================================
|
306 |
""")
|
307 |
in_verbatim = gr.Textbox(label="Peronal Info")
|
|
|
338 |
""")
|
339 |
|
340 |
|
341 |
+
with gr.Tab("Classify & Guardrail"):
|
342 |
gr.Markdown("""
|
343 |
+
Objective: Streamline Customer Insights: Auto-Classify Feedback for Product Optimization
|
344 |
================================================
|
345 |
- multi class classification, could have multiple label for 1 feedback
|
346 |
- fix classification in this use case: online banking, card, auto finance, mortgage, insurance
|
|
|
380 |
- Regulatory Compliance
|
381 |
Auto-classifies documents for FINRA/SEC audits (e.g., Morgan Stanley uses NLP to categorize 3M+ annual communications into 50+ compliance buckets).
|
382 |
""")
|
383 |
+
|
384 |
+
with gr.Tab("Resolution for Call Center"):
|
385 |
+
gr.Markdown("""
|
386 |
+
Objective: Proactive Entity Mapping: Clarifying Critical Elements in Complex Call Analysis for Strategic Insight
|
387 |
+
================================================
|
388 |
+
""")
|
389 |
+
in_verbatim = gr.Textbox(label="Content")
|
390 |
+
out_product = gr.Textbox(label="Entity Resolution")
|
391 |
+
|
392 |
+
gr.Examples(
|
393 |
+
[
|
394 |
+
["""
|
395 |
+
Representative: "Thank you for calling Goldman Sachs Credit Card Services. My name is Sarah. May I have your full name and the last 4 digits of your card number for verification?"
|
396 |
+
|
397 |
+
Customer: "This is Michael Chen, card ending 5402."
|
398 |
+
|
399 |
+
Representative: "Thank you, Mr. Chen. I show you have an Apple Card account opened in 2023. How can I assist you today?" (Reference: Search Result Apple Card context)
|
400 |
+
|
401 |
+
Customer: "I'm disputing a $329 charge from TechElectronics from March 15th. I never received the item."
|
402 |
+
|
403 |
+
Representative: "I understand your concern. Let me initiate a dispute investigation. Per our process (Search Result BBB complaint handling):
|
404 |
+
|
405 |
+
We'll apply a temporary credit within 24 hours
|
406 |
+
|
407 |
+
Our team will contact the merchant
|
408 |
+
|
409 |
+
You'll receive email updates at [email protected]
|
410 |
+
|
411 |
+
Final resolution within 60 days
|
412 |
+
|
413 |
+
Would you like me to proceed?"
|
414 |
+
|
415 |
+
Customer: "Yes, but what if they fight it?"
|
416 |
+
|
417 |
+
Representative: "If the merchant disputes your claim, we'll:
|
418 |
+
|
419 |
+
Review all evidence using our 3-phase verification system (Search Result multi-stage investigation)
|
420 |
+
|
421 |
+
Consider your purchase protection benefits
|
422 |
+
|
423 |
+
Escalate to senior specialists if needed
|
424 |
+
|
425 |
+
For security, never share your CVV (339) or full card number with callers. Always call back using the number on your physical card (Search Result scam warning)."
|
426 |
+
|
427 |
+
Customer: "Can I make a partial payment on my balance while this is pending?"
|
428 |
+
|
429 |
+
Representative: "Absolutely. We offer:
|
430 |
+
"minimum_payment": "$35 due April 25",
|
431 |
+
"hardship_program": "0% APR for 6 months",
|
432 |
+
"custom_plan": "Split $600 balance over 3 months"
|
433 |
+
Would you like to enroll in any of these?"
|
434 |
+
|
435 |
+
Customer: "The 3-month plan, please."
|
436 |
+
|
437 |
+
Representative: "Confirmed. Your next payment of $200 will process May 1st. A confirmation email with dispute case #GS-2025-0422-8830 is being sent now. Is there anything else?"
|
438 |
+
|
439 |
+
Customer: "No, thank you."
|
440 |
+
"""]
|
441 |
+
],
|
442 |
+
[in_verbatim]
|
443 |
+
)
|
444 |
+
btn_recommend=gr.Button("Resolve")
|
445 |
+
btn_recommend.click(fn=resolve, inputs=in_verbatim, outputs=out_product)
|
446 |
+
|
447 |
+
gr.Markdown("""
|
448 |
+
Example of Call Resolution
|
449 |
+
===============
|
450 |
+

|
451 |
+
|
452 |
+
Companies like RBC, Comcast, or BMO often face a recurring challenge: long, complex customer service calls filled with vague product references, overlapping account details, and unstructured issue descriptions. This makes it difficult for support teams and analytics engines to extract clear insights or resolve recurring pain points across accounts and products.
|
453 |
+
|
454 |
+
How can teams automatically stitch together fragmented mentions of the same customer, product, or issue—across call transcripts, CRM records, and support tickets—to form a unified view of the actual problem?
|
455 |
+
|
456 |
+
That's where Entity Resolution comes in. By linking related entities hidden across data silos and messy text (like "my internet box" = "ARRIS TG1682G" or "John Smith, J. Smith, and [email protected]"), teams gain a clearer, contextual understanding of customer frustration in real-time.
|
457 |
+
|
458 |
+
For example, Comcast reduced repeat service calls by 17% after deploying entity resolution models on long call transcripts—turning messy feedback into actionable product insights and faster resolutions.
|
459 |
+
|
460 |
+
The result? Less agent time lost, higher customer satisfaction, and data pipelines that actually speak human.
|
461 |
+
""")
|
462 |
demo.launch(allowed_paths=["./"])
|
entity.png
ADDED
![]() |
entity.py
ADDED
@@ -0,0 +1,84 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from pydantic import BaseModel, Field
|
2 |
+
from typing import List
|
3 |
+
|
4 |
+
|
5 |
+
import instructor
|
6 |
+
import os
|
7 |
+
|
8 |
+
from groq import AsyncGroq
|
9 |
+
# Initialize with API key
|
10 |
+
client = AsyncGroq(api_key=os.getenv("GROQ_API_KEY"))
|
11 |
+
|
12 |
+
# Enable instructor patches for Groq client
|
13 |
+
client = instructor.from_groq(client)
|
14 |
+
"""
|
15 |
+
import openai
|
16 |
+
client = instructor.from_openai(
|
17 |
+
openai.OpenAI(
|
18 |
+
base_url="http://localhost:11434/v1",
|
19 |
+
api_key="ollama",
|
20 |
+
),
|
21 |
+
mode=instructor.Mode.JSON,
|
22 |
+
)
|
23 |
+
"""
|
24 |
+
|
25 |
+
llm = 'llama-3.1-8b-instant' if os.getenv("GROQ_API_KEY") else "qwen2.5"
|
26 |
+
|
27 |
+
class Property(BaseModel):
|
28 |
+
key: str
|
29 |
+
value: str
|
30 |
+
resolved_absolute_value: str
|
31 |
+
|
32 |
+
|
33 |
+
class Entity(BaseModel):
|
34 |
+
id: int = Field(
|
35 |
+
...,
|
36 |
+
description="Unique identifier for the entity, used for deduplication, design a scheme allows multiple entities",
|
37 |
+
)
|
38 |
+
subquote_string: List[str] = Field(
|
39 |
+
...,
|
40 |
+
description="Correctly resolved value of the entity, if the entity is a reference to another entity, this should be the id of the referenced entity, include a few more words before and after the value to allow for some context to be used in the resolution",
|
41 |
+
)
|
42 |
+
entity_title: str
|
43 |
+
properties: List[Property] = Field(
|
44 |
+
..., description="List of properties of the entity such as date, amount...etc", examples=[Property(key="Amount", value="+200", resolved_absolute_value="300"),
|
45 |
+
Property(key="Date", value="-5", resolved_absolute_value="2018-09-18")]
|
46 |
+
)
|
47 |
+
dependencies: List[int] = Field(
|
48 |
+
...,
|
49 |
+
description="List of entity ids that this entity depends or relies on to resolve it",
|
50 |
+
)
|
51 |
+
|
52 |
+
|
53 |
+
class DocumentExtraction(BaseModel):
|
54 |
+
entities: List[Entity] = Field(
|
55 |
+
...,
|
56 |
+
description="Body of the answer, each fact should be a separate object with a body and a list of sources such as Organization, Agreement Date, Asset...etc",
|
57 |
+
)
|
58 |
+
|
59 |
+
|
60 |
+
def entity_graph(content) -> DocumentExtraction:
|
61 |
+
return client.chat.completions.create(
|
62 |
+
model=llm, #"deepseek-r1", #"gpt-4","llama3.2", #
|
63 |
+
response_model=DocumentExtraction,
|
64 |
+
temperature=0.1,
|
65 |
+
messages=[
|
66 |
+
{
|
67 |
+
"role": "system",
|
68 |
+
"content": "You're world class entities resolution system. Ensure that each entity and its attributes are correctly resolved, meaning duplicates are merged and dependencies are established. Extract and resolve a list of entities from the following document:",
|
69 |
+
|
70 |
+
},
|
71 |
+
{
|
72 |
+
"role": "user",
|
73 |
+
"content": content,
|
74 |
+
},
|
75 |
+
],
|
76 |
+
)
|
77 |
+
|
78 |
+
def resolve(content):
|
79 |
+
model = entity_graph(content)
|
80 |
+
return model.model_dump_json(indent=2)
|
81 |
+
|
82 |
+
if __name__=='__main__':
|
83 |
+
content=""
|
84 |
+
print(resolve(content))
|
knowledge.py
CHANGED
@@ -4,9 +4,10 @@ import instructor
|
|
4 |
from graphviz import Digraph
|
5 |
from pydantic import BaseModel, Field
|
6 |
|
7 |
-
from groq import Groq
|
8 |
-
import os
|
9 |
|
|
|
|
|
|
|
10 |
# Initialize with API key
|
11 |
client = Groq(api_key=os.getenv("GROQ_API_KEY"))
|
12 |
|
@@ -22,7 +23,7 @@ client = instructor.from_openai(
|
|
22 |
mode=instructor.Mode.JSON,
|
23 |
)
|
24 |
"""
|
25 |
-
llm = 'llama-3.1-8b-instant' if os.getenv("GROQ_API_KEY") else "
|
26 |
|
27 |
class Node(BaseModel, frozen=True):
|
28 |
"""
|
@@ -32,6 +33,8 @@ class Node(BaseModel, frozen=True):
|
|
32 |
description="unique id of the concept in the subject domain, used for deduplication, design a scheme allows multiple concept")
|
33 |
label: str = Field(..., description="description of the concept in the subject domain")
|
34 |
color: str = "orange"
|
|
|
|
|
35 |
|
36 |
|
37 |
class Edge(BaseModel, frozen=True):
|
@@ -78,28 +81,32 @@ from typing import Iterable
|
|
78 |
from textwrap import dedent
|
79 |
|
80 |
|
81 |
-
def generate_graph(q, input) -> KnowledgeGraph:
|
82 |
-
|
83 |
model=llm,
|
84 |
max_retries=5,
|
85 |
messages=[
|
86 |
{
|
87 |
"role": "user",
|
88 |
-
"content": dedent(f"""Help me understand
|
89 |
-
###
|
90 |
-
###
|
|
|
|
|
91 |
### Instruction:
|
92 |
-
Generate at least
|
93 |
-
Generate at least
|
|
|
94 |
### Output Format:
|
95 |
Node with id, label for description of the concept
|
96 |
-
Edge with source's id, target's id, label for description of the relationship between source concept and target concept
|
97 |
"""),
|
98 |
|
99 |
}
|
100 |
],
|
101 |
response_model=KnowledgeGraph)
|
102 |
|
|
|
103 |
|
104 |
class Subissue(BaseModel):
|
105 |
subissue_title: str
|
@@ -146,8 +153,64 @@ def expandIssue(input) -> Iterable[Subissue]:
|
|
146 |
return response
|
147 |
|
148 |
|
149 |
-
def graph(query):
|
150 |
-
queryx = expandIssue(query)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
151 |
|
152 |
-
graph = generate_graph(query, str(queryx))
|
153 |
-
return graph.json()
|
|
|
4 |
from graphviz import Digraph
|
5 |
from pydantic import BaseModel, Field
|
6 |
|
|
|
|
|
7 |
|
8 |
+
import os
|
9 |
+
from datetime import date
|
10 |
+
from groq import Groq
|
11 |
# Initialize with API key
|
12 |
client = Groq(api_key=os.getenv("GROQ_API_KEY"))
|
13 |
|
|
|
23 |
mode=instructor.Mode.JSON,
|
24 |
)
|
25 |
"""
|
26 |
+
llm = 'llama-3.1-8b-instant' if os.getenv("GROQ_API_KEY") else "qwen2.5"
|
27 |
|
28 |
class Node(BaseModel, frozen=True):
|
29 |
"""
|
|
|
33 |
description="unique id of the concept in the subject domain, used for deduplication, design a scheme allows multiple concept")
|
34 |
label: str = Field(..., description="description of the concept in the subject domain")
|
35 |
color: str = "orange"
|
36 |
+
record_date: date = Field(..., description="the date that this Node is recorded")
|
37 |
+
|
38 |
|
39 |
|
40 |
class Edge(BaseModel, frozen=True):
|
|
|
81 |
from textwrap import dedent
|
82 |
|
83 |
|
84 |
+
def generate_graph(q, input=KnowledgeGraph()) -> KnowledgeGraph:
|
85 |
+
state= client.chat.completions.create(
|
86 |
model=llm,
|
87 |
max_retries=5,
|
88 |
messages=[
|
89 |
{
|
90 |
"role": "user",
|
91 |
+
"content": dedent(f"""As a world class iterative knowledge graph builder and a Marketing Data Scientist for delivery personalized solution in Personal and Commercial Banking. Help me understand this person pain points and needs by describing the interaction as a detailed knowledge graph:
|
92 |
+
### Interaction: {q}
|
93 |
+
### Merge from existing KnowledgeGraph, Here is the current state of the graph:
|
94 |
+
{input.model_dump_json()}
|
95 |
+
|
96 |
### Instruction:
|
97 |
+
Generate at least 2 concepts
|
98 |
+
Generate at least 2 relationships
|
99 |
+
Append them into current state without duplication
|
100 |
### Output Format:
|
101 |
Node with id, label for description of the concept
|
102 |
+
Edge with source's id, target's id, label for description of the relationship between source's concept and target's concept
|
103 |
"""),
|
104 |
|
105 |
}
|
106 |
],
|
107 |
response_model=KnowledgeGraph)
|
108 |
|
109 |
+
return input.update(state)
|
110 |
|
111 |
class Subissue(BaseModel):
|
112 |
subissue_title: str
|
|
|
153 |
return response
|
154 |
|
155 |
|
156 |
+
def graph(query, queryx):
|
157 |
+
#queryx = expandIssue(query)
|
158 |
+
if queryx.strip() == "":
|
159 |
+
graph = generate_graph(query)
|
160 |
+
else:
|
161 |
+
graph = generate_graph(query, KnowledgeGraph.model_validate_json(queryx))
|
162 |
+
return graph.model_dump_json(indent=2)
|
163 |
+
|
164 |
+
if __name__=="__main__":
|
165 |
+
query="""
|
166 |
+
Representative: "Thank you for calling Goldman Sachs Credit Card Services. My name is Sarah. May I have your full name and the last 4 digits of your card number for verification?"
|
167 |
+
|
168 |
+
Customer: "This is Michael Chen, card ending 5402."
|
169 |
+
|
170 |
+
Representative: "Thank you, Mr. Chen. I show you have an Apple Card account opened in 2023. How can I assist you today?" (Reference: Search Result Apple Card context)
|
171 |
+
|
172 |
+
Customer: "I'm disputing a $329 charge from TechElectronics from March 15th. I never received the item."
|
173 |
+
|
174 |
+
Representative: "I understand your concern. Let me initiate a dispute investigation. Per our process (Search Result BBB complaint handling):
|
175 |
+
|
176 |
+
We'll apply a temporary credit within 24 hours
|
177 |
+
|
178 |
+
Our team will contact the merchant
|
179 |
+
|
180 |
+
You'll receive email updates at [email protected]
|
181 |
+
|
182 |
+
Final resolution within 60 days
|
183 |
+
|
184 |
+
Would you like me to proceed?"
|
185 |
+
|
186 |
+
Customer: "Yes, but what if they fight it?"
|
187 |
+
|
188 |
+
Representative: "If the merchant disputes your claim, we'll:
|
189 |
+
|
190 |
+
Review all evidence using our 3-phase verification system (Search Result multi-stage investigation)
|
191 |
+
|
192 |
+
Consider your purchase protection benefits
|
193 |
+
|
194 |
+
Escalate to senior specialists if needed
|
195 |
+
|
196 |
+
For security, never share your CVV (339) or full card number with callers. Always call back using the number on your physical card (Search Result scam warning)."
|
197 |
+
|
198 |
+
Customer: "Can I make a partial payment on my balance while this is pending?"
|
199 |
+
|
200 |
+
Representative: "Absolutely. We offer:
|
201 |
+
"minimum_payment": "$35 due April 25",
|
202 |
+
"hardship_program": "0% APR for 6 months",
|
203 |
+
"custom_plan": "Split $600 balance over 3 months"
|
204 |
+
Would you like to enroll in any of these?"
|
205 |
+
|
206 |
+
Customer: "The 3-month plan, please."
|
207 |
+
|
208 |
+
Representative: "Confirmed. Your next payment of $200 will process May 1st. A confirmation email with dispute case #GS-2025-0422-8830 is being sent now. Is there anything else?"
|
209 |
+
|
210 |
+
Customer: "No, thank you."
|
211 |
+
"""
|
212 |
+
graph = generate_graph(query)
|
213 |
+
|
214 |
+
graph2 = generate_graph("My mortgage rate is 9%, I cannot afford it anymore, I need to refinance and I'm unemploy right now.", graph)
|
215 |
+
graph2.draw()
|
216 |
|
|
|
|
knowledge_graph1.png
ADDED
![]() |
knowledge_graph2.png
ADDED
![]() |