-
Notifications
You must be signed in to change notification settings - Fork 4
/
recommendation.py
149 lines (118 loc) · 5.13 KB
/
recommendation.py
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
import os
from dotenv import load_dotenv
import streamlit as st
from langchain.chains import RetrievalQA, LLMChain
from langchain.chat_models import ChatOpenAI
from langchain.document_loaders import DataFrameLoader
from langchain.embeddings.openai import OpenAIEmbeddings
from langchain.memory import ConversationBufferMemory
from langchain.prompts import PromptTemplate
from langchain.text_splitter import CharacterTextSplitter
from langchain.vectorstores import FAISS
# Load environment variables from .env file
load_dotenv()
def process_data(refined_df):
"""
Process the refined dataset and create the vector store.
Args:
refined_df (pd.DataFrame): Preprocessed dataset DataFrame.
Returns:
vectorstore (FAISS): Vector store containing the processed data.
"""
refined_df['combined_info'] = refined_df.apply(lambda row: f"Product ID: {row['pid']}. Product URL: {row['product_url']}. Product Name: {row['product_name']}. Primary Category: {row['primary_category']}. Retail Price: ${row['retail_price']}. Discounted Price: ${row['discounted_price']}. Primary Image Link: {row['primary_image_link']}. Description: {row['description']}. Brand: {row['brand']}. Gender: {row['gender']}", axis=1)
loader = DataFrameLoader(refined_df, page_content_column="combined_info")
docs = loader.load()
text_splitter = CharacterTextSplitter(chunk_size=1000, chunk_overlap=200)
texts = text_splitter.split_documents(docs)
embeddings = OpenAIEmbeddings(openai_api_key=os.getenv("OPENAI_API_KEY"))
vectorstore = FAISS.from_documents(texts, embeddings)
return vectorstore
def save_vectorstore(vectorstore, directory):
"""
Save the vector store to a directory.
Args:
vectorstore (FAISS): Vector store to be saved.
directory (str): Directory to save the vector store.
"""
vectorstore.save_local(directory)
def load_vectorstore(directory, embeddings):
"""
Load the vector store from a directory.
Args:
directory (str): Directory containing the saved vector store.
embeddings (OpenAIEmbeddings): Embeddings object.
Returns:
vectorstore (FAISS): Loaded vector store.
"""
vectorstore = FAISS.load_local(directory, embeddings, allow_dangerous_deserialization = True )
return vectorstore
def display_product_recommendation(refined_df):
"""
Display product recommendation section.
Args:
refined_df (pd.DataFrame): Preprocessed dataset DataFrame.
"""
st.header("Product Recommendation")
vectorstore_dir = 'vectorstore'
embeddings = OpenAIEmbeddings(openai_api_key=os.getenv("OPENAI_API_KEY"))
if os.path.exists(vectorstore_dir):
vectorstore = load_vectorstore(vectorstore_dir, embeddings)
else:
vectorstore = process_data(refined_df)
save_vectorstore(vectorstore, vectorstore_dir)
manual_template = """
Kindly suggest three similar products based on the description I have provided below:
Product Department: {department},
Product Category: {category},
Product Brand: {brand},
Maximum Price range: {price}.
Please provide complete answers including product department name, product category, product name, price, and stock quantity.
"""
prompt_manual = PromptTemplate(
input_variables=["department", "category", "brand", "price"],
template=manual_template,
)
llm = ChatOpenAI(openai_api_key=os.getenv("OPENAI_API_KEY"),
model_name='gpt-3.5-turbo', temperature=0)
chain = LLMChain(
llm=llm,
prompt=prompt_manual,
verbose=True)
chatbot_template = """
You are a friendly, conversational retail shopping assistant that helps customers find products that match their preferences.
From the following context and chat history, assist customers in finding what they are looking for based on their input.
For each question, suggest three products, including their category, price, and current stock quantity.
Sort the answer by the cheapest product.
If you don't know the answer, just say that you don't know, don't try to make up an answer.
{context}
Chat history: {history}
Input: {question}
Your Response:
"""
chatbot_prompt = PromptTemplate(
input_variables=["context", "history", "question"],
template=chatbot_template,
)
memory = ConversationBufferMemory(memory_key="history", input_key="question", return_messages=True)
qa = RetrievalQA.from_chain_type(
llm=llm,
chain_type='stuff',
retriever=vectorstore.as_retriever(),
verbose=True,
chain_type_kwargs={
"verbose": True,
"prompt": chatbot_prompt,
"memory": memory}
)
department = st.text_input("Product Department")
category = st.text_input("Product Category")
brand = st.text_input("Product Brand")
price = st.text_input("Maximum Price Range")
if st.button("Get Recommendations"):
response = chain.run(
department=department,
category=category,
brand=brand,
price=price
)
st.write(response)