init
Browse files- app.py +60 -0
- product2.csv +11 -0
- rag.py +42 -0
- requirements.txt +12 -0
- tool.py +25 -0
app.py
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from rag import rbc_product
|
| 3 |
+
from tool import rival_product
|
| 4 |
+
|
| 5 |
+
with gr.Blocks() as demo:
|
| 6 |
+
with gr.Tab("RAG"):
|
| 7 |
+
gr.Markdown("""
|
| 8 |
+
Marketing
|
| 9 |
+
------------
|
| 10 |
+
GraphRAG: Models customer-product relationship networks for next-best-action predictions
|
| 11 |
+
|
| 12 |
+
DSPy: Optimizes cross-sell/upsell prompt variations through A/B testing
|
| 13 |
+
|
| 14 |
+
Risk & Audit
|
| 15 |
+
------------
|
| 16 |
+
GraphRAG: Maps transactional relationships into dynamic knowledge graphs to detect multi-layered fraud patterns
|
| 17 |
+
|
| 18 |
+
Tool Use: Integrates fraud detection APIs, anomaly scoring models, and regulatory compliance checkers
|
| 19 |
+
|
| 20 |
+
DSPy: Optimizes fraud explanation prompts for regulatory reporting
|
| 21 |
+
""")
|
| 22 |
+
gr.Markdown("""
|
| 23 |
+
Retrieval: Public RBC Product Data
|
| 24 |
+
|
| 25 |
+
Recommend: RBC Product
|
| 26 |
+
""")
|
| 27 |
+
in_verbatim = gr.Textbox(label="Verbatim")
|
| 28 |
+
out_product = gr.Textbox(label="Product")
|
| 29 |
+
|
| 30 |
+
|
| 31 |
+
gr.Examples(
|
| 32 |
+
[
|
| 33 |
+
["Low APR and great customer service. I would highly recommend if you’re looking for a great credit card company and looking to rebuild your credit. I have had my credit limit increased annually and the annual fee is very low."]
|
| 34 |
+
],
|
| 35 |
+
[in_verbatim]
|
| 36 |
+
)
|
| 37 |
+
btn_recommend=gr.Button("Recommend")
|
| 38 |
+
btn_recommend.click(fn=rbc_product, inputs=in_verbatim, outputs=out_product)
|
| 39 |
+
|
| 40 |
+
with gr.Tab("Tool Use"):
|
| 41 |
+
gr.Markdown("""
|
| 42 |
+
Retrieval: Public Product Data using Tavily Search
|
| 43 |
+
|
| 44 |
+
Recommend: Competition Product
|
| 45 |
+
""")
|
| 46 |
+
in_verbatim = gr.Textbox(label="Verbatim")
|
| 47 |
+
out_product = gr.Textbox(label="Product")
|
| 48 |
+
|
| 49 |
+
gr.Examples(
|
| 50 |
+
[
|
| 51 |
+
["Low APR and great customer service. I would highly recommend if you’re looking for a great credit card company and looking to rebuild your credit. I have had my credit limit increased annually and the annual fee is very low."]
|
| 52 |
+
],
|
| 53 |
+
[in_verbatim]
|
| 54 |
+
)
|
| 55 |
+
btn_recommend=gr.Button("Recommend")
|
| 56 |
+
btn_recommend.click(fn=rival_product, inputs=in_verbatim, outputs=out_product)
|
| 57 |
+
|
| 58 |
+
|
| 59 |
+
|
| 60 |
+
demo.launch(allowed_paths=["./xgb","./ts"])
|
product2.csv
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
product,purpose,benefit,fee
|
| 2 |
+
RBC Newcomer Advantage,Support newcomers settling in Canada,No credit history required for car/home loans,Varies by loan type
|
| 3 |
+
RBC Advantage Banking for Students,Banking for full-time students,"Unlimited debits, free Interac e-Transfers",No monthly fee
|
| 4 |
+
RBC Day to Day Banking,Basic banking needs,"12 free debits/month, unlimited transit transactions",$4/month
|
| 5 |
+
RBC Digital Choice Business Account,Digital banking for small businesses,Unlimited electronic transactions,$6/month
|
| 6 |
+
RBC Professionals' Package,Financial solutions for accredited professionals,Tailored advice and solutions,Varies by service
|
| 7 |
+
RBC Newcomer Mortgage,Home financing for newcomers,No Canadian credit history required,Market rates
|
| 8 |
+
RBC Student Credit Card,Credit building for students,Fee waiver for first year,$0 first year
|
| 9 |
+
RBC Retirement Savings Account,Retirement planning,Tax-advantaged savings options,$25/year
|
| 10 |
+
RBC Small Business Loan,Business financing,Flexible repayment terms,Prime+2%
|
| 11 |
+
RBC Avion Credit Card,Travel rewards,Earn Avion points on purchases,$120 annual fee
|
rag.py
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import dspy
|
| 2 |
+
|
| 3 |
+
import pandas as pd
|
| 4 |
+
|
| 5 |
+
#lm = dspy.LM('ollama_chat/deepseek-r1', api_base='http://localhost:11434', api_key='')
|
| 6 |
+
lm = dspy.LM('huggingface/Qwen/Qwen2.5-Coder-32B-Instruct')
|
| 7 |
+
dspy.configure(lm=lm)
|
| 8 |
+
|
| 9 |
+
df = pd.read_csv("product2.csv")
|
| 10 |
+
df['content']=df['product']+"; "+df['purpose']+"; "+df['benefit']+"; "+df['fee']
|
| 11 |
+
|
| 12 |
+
corpus = [row['content'] for i,row in df.iterrows()]
|
| 13 |
+
|
| 14 |
+
embedder = dspy.Embedder('openai/text-embedding-3-small', dimensions=512)
|
| 15 |
+
class RecommendProduct(dspy.Signature):
|
| 16 |
+
"""
|
| 17 |
+
Recommend RBC financial product based on verbatim
|
| 18 |
+
"""
|
| 19 |
+
context = dspy.InputField(desc="may contain relevant product information")
|
| 20 |
+
verbatim = dspy.InputField()
|
| 21 |
+
product = dspy.OutputField(desc="product name with benefit")
|
| 22 |
+
|
| 23 |
+
class RAG(dspy.Module):
|
| 24 |
+
def __init__(self, num_passages=3):
|
| 25 |
+
super().__init__()
|
| 26 |
+
self.retrieve = dspy.retrievers.Embeddings(embedder=embedder, corpus=corpus, k=num_passages)
|
| 27 |
+
self.recommender = dspy.ChainOfThought(RecommendProduct)
|
| 28 |
+
|
| 29 |
+
def forward(self, verbatim):
|
| 30 |
+
context = self.retrieve(verbatim).passages
|
| 31 |
+
prediction = self.recommender(context=context, verbatim=verbatim)
|
| 32 |
+
return dspy.Prediction(context=context, product=prediction.product)
|
| 33 |
+
|
| 34 |
+
|
| 35 |
+
customer="Low APR and great customer service. I would highly recommend if you’re looking for a great credit card company and looking to rebuild your credit. I have had my credit limit increased annually and the annual fee is very low."
|
| 36 |
+
|
| 37 |
+
|
| 38 |
+
qa = RAG(num_passages=2)
|
| 39 |
+
|
| 40 |
+
def rbc_product(customer:str):
|
| 41 |
+
response = qa(verbatim=f"Which RBC personal banking product best serve the follow customer needs, pain points: {customer}")
|
| 42 |
+
return response.product
|
requirements.txt
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
markdownify
|
| 2 |
+
requests
|
| 3 |
+
duckduckgo_search
|
| 4 |
+
pandas
|
| 5 |
+
langchain
|
| 6 |
+
langgraph
|
| 7 |
+
litellm==1.63
|
| 8 |
+
openai==1.61.0
|
| 9 |
+
dspy
|
| 10 |
+
llama-index
|
| 11 |
+
faiss-cpu
|
| 12 |
+
tavily-python
|
tool.py
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import dspy
|
| 3 |
+
|
| 4 |
+
from dspy.predict.react import Tool
|
| 5 |
+
from tavily import TavilyClient
|
| 6 |
+
|
| 7 |
+
#lm = dspy.LM('ollama_chat/deepseek-r1', api_base='http://localhost:11434', api_key='')
|
| 8 |
+
lm = dspy.LM('huggingface/Qwen/Qwen2.5-Coder-32B-Instruct')
|
| 9 |
+
dspy.configure(lm=lm)
|
| 10 |
+
|
| 11 |
+
search_client = TavilyClient(api_key=os.environ["T_TOKEN"])
|
| 12 |
+
|
| 13 |
+
INST="""Recommend banking financial product based on verbatim"""
|
| 14 |
+
def web_search(query: str) -> list[str]:
|
| 15 |
+
"""Run a web search and return the personal banking product from the top 5 search results"""
|
| 16 |
+
response = search_client.search(query)
|
| 17 |
+
return [r["content"] for r in response["results"]]
|
| 18 |
+
|
| 19 |
+
agent = dspy.ReAct("verbatim -> product", tools=[Tool(web_search)])
|
| 20 |
+
|
| 21 |
+
customer="Low APR and great customer service. I would highly recommend if you’re looking for a great credit card company and looking to rebuild your credit. I have had my credit limit increased annually and the annual fee is very low."
|
| 22 |
+
|
| 23 |
+
def rival_product(customer:str):
|
| 24 |
+
prediction = agent(verbatim=f"Which banking product best serve this customer needs, pain points: {customer}")
|
| 25 |
+
return prediction.product
|