File size: 1,959 Bytes
ee74471 3525314 ee74471 c863d53 c613c64 c863d53 c613c64 0f2b5ae ee74471 |
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 |
import dspy
import pandas as pd
#lm = dspy.LM('ollama_chat/deepseek-r1', api_base='http://localhost:11434', api_key='')
#lm = dspy.LM('huggingface/Qwen/Qwen2.5-Coder-32B-Instruct')
lm = dspy.LM('huggingface/meta-llama/Llama-3.2-1B')
dspy.configure(lm=lm)
df = pd.read_csv("product2.csv")
df['content']=df['product']+"; "+df['purpose']+"; "+df['benefit']+"; "+df['fee']
corpus = [row['content'] for i,row in df.iterrows()]
'huggingface/BAAI/bge-small-en-v1.5'
"""
from sentence_transformers import SentenceTransformer
# Load an extremely efficient local model for retrieval
model = SentenceTransformer("sentence-transformers/all-MiniLM-L6-v2", device="cpu")
embedder = dspy.Embedder(model.encode)
"""
embedder = dspy.Embedder('huggingface/BAAI/bge-small-en-v1.5')
class RecommendProduct(dspy.Signature):
"""
Recommend RBC financial product based on verbatim
"""
context = dspy.InputField(desc="may contain relevant product information")
verbatim = dspy.InputField()
product = dspy.OutputField(desc="product name with benefit")
class RAG(dspy.Module):
def __init__(self, num_passages=3):
super().__init__()
self.retrieve = dspy.retrievers.Embeddings(embedder=embedder, corpus=corpus, k=num_passages)
self.recommender = dspy.ChainOfThought(RecommendProduct)
def forward(self, verbatim):
context = self.retrieve(verbatim).passages
prediction = self.recommender(context=context, verbatim=verbatim)
return dspy.Prediction(context=context, product=prediction.product)
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."
qa = RAG(num_passages=2)
def rbc_product(customer:str):
response = qa(verbatim=f"Which RBC personal banking product best serve the follow customer needs, pain points: {customer}")
return response.product |