File size: 8,960 Bytes
1312d73
1275035
 
 
1312d73
 
 
 
 
 
 
 
 
 
 
1275035
 
 
 
 
 
 
 
 
 
 
 
 
 
 
de3899a
 
 
 
 
 
1275035
68324d4
1275035
68324d4
 
 
 
 
 
 
 
 
 
1275035
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1312d73
1275035
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1312d73
1275035
 
 
 
1312d73
1275035
 
 
 
1312d73
1275035
 
 
 
 
 
 
 
 
 
 
 
1312d73
1275035
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1312d73
1275035
 
 
 
 
1312d73
1275035
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
import os
import sqlite3
import streamlit as st
from werkzeug.security import generate_password_hash, check_password_hash
from langchain_groq import ChatGroq
from langchain_huggingface import HuggingFaceEmbeddings
from langchain_community.document_loaders.csv_loader import CSVLoader
from langchain_text_splitters import RecursiveCharacterTextSplitter
from langchain_core.vectorstores import InMemoryVectorStore
from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder
from langchain.tools import Tool
from langchain.agents import AgentExecutor, create_tool_calling_agent
from langchain_core.messages import HumanMessage, AIMessage


# --- Database Setup ---

conn = sqlite3.connect("user.db")
c = conn.cursor()
c.execute('''CREATE TABLE IF NOT EXISTS users
             (id INTEGER PRIMARY KEY AUTOINCREMENT,
              username TEXT UNIQUE NOT NULL,
              password TEXT NOT NULL,
              previous_chat_history TEXT,
              previous_products_bought TEXT)''')

conn.commit()
conn.close()

class User:
    def __init__(self, id, username, password, chat_history = None, products_bought = None):
        self.id = id
        self.username = username
        self.password = paswword
        self.chat_history = chat_history or []
        self.products_bought = products_bought or []


    @classmethod
    def create(cls, username, password):
        hashed_pw = generate_password_hash(password)
        conn = sqlite3.connect('users.db')
        c = conn.cursor()
        c.execute('INSERT INTO users (username, password) VALUES (?, ?)',(username, hashed_pw))
        user_id = c.lastrowid
        conn.commit()
        conn.close()
        return cls(user_id, username, hashed_pw)    


    @classmethod
    def get_by_username(cls, username):
        conn = sqlite3.connect('users.db')
        c = conn.cursor()
        c.execute('SELECT * FROM users WHERE username = ?', (username,))
        user = c.fetchone()
        conn.close()
        if user:
            return cls(user[0], user[1], user[2], 
                      eval(user[3]) if user[3] else [],
                      eval(user[4]) if user[4] else [])
        return None

    def update_chat_history(self, new_messages):
        conn = sqlite3.connect('users.db')
        c = conn.cursor()
        updated_history = self.chat_history + new_messages
        c.execute('UPDATE users SET previous_chat_history = ? WHERE id = ?',
                 (str(updated_history), self.id))
        conn.commit()
        conn.close()    

    def update_products_bought(self, new_products):
        conn = sqlite3.connect('users.db')
        c = conn.cursor()
        updated_products = self.products_bought + new_products
        c.execute('UPDATE users SET previous_products_bought = ? WHERE id = ?',
                 (str(updated_products), self.id))
        conn.commit()
        conn.close()    
        

# --- AI Agent Setup ---
# Load the LLM model from Groq
os.environ["GROQ_API_KEY"] = st.secrets["GROQ_API_KEY"]
llm = ChatGroq(
    temperature=0.1,
    model_name="llama3-8b-8192",
    api_key=st.secrets["GROQ_API_KEY"],
)

# Load the HuggingFace Embeddings
embeddings = HuggingFaceEmbeddings(model_name="sentence-transformers/all-MiniLM-L6-v2")

# Load and process CSV data
@st.cache_resource
def load_data():
    loader = CSVLoader(file_path="electronics_products.csv")
    docs = loader.load()
    text_splitter = RecursiveCharacterTextSplitter(chunk_size=100, chunk_overlap=20)
    splits = text_splitter.split_documents(docs)
    vectorstore = InMemoryVectorStore.from_documents(documents=splits, embedding=embeddings)
    return vectorstore.as_retriever()

retriever = load_data()

def retrieve_query(query: str):
    """Retrieves documents related to the query."""
    return retriever.get_relevant_documents(query)

tool = Tool(
    name="retriever",
    func=retrieve_query,
    description="Useful for retrieving product information"
)

# Agent setup
# System prompt template
system_prompt = """
      You are {agent_name}, the AI Sales Assistant for {company_name} ({company_business}).

      Company Profile:
      - Company Name: {company_name}
      - Business: {company_business}
      - Key Features: {key_features}

      Conversation Flow:
      1. Introduction
      2. Qualification
      3. Understanding Needs
      4. Needs Analysis
      5. Solution Presentation
      6. Confirmation
      7. If the prospect agrees to purchase, thank them and provide the payment link: https://www.example.com/payment

      Guidelines:
      - Maintain natural, professional conversations
      - Follow company policies
      - Be helpful and polite
      """
# Define the company and agent details
company_name = "TechElectronics"
company_business = "Consumer Electronics Retailer"
agent_name = "Alex"
key_features = "Cutting-edge technology, Competitive pricing, Excellent customer service"

# Format the system prompt with the company and agent details
formatted_system_prompt = system_prompt.format(
    agent_name=agent_name,
    company_name=company_name,
    company_business=company_business,
    key_features=key_features
)

prompt = ChatPromptTemplate.from_messages([
    ("system", formatted_system_prompt),
    MessagesPlaceholder(variable_name="chat_history"),
    ("human", "{input}"),
    MessagesPlaceholder(variable_name="agent_scratchpad")
])

tools = [tool]
agent = create_tool_calling_agent(llm, tools, prompt)
agent_executor = AgentExecutor(agent=agent, tools=tools, verbose=True)


# --- Streamlit UI ---
def main():
    st.title(f"{company_name} AI Sales Assistant")
    
    # Initialize session state
    if 'user' not in st.session_state:
        st.session_state.user = None
        st.session_state.chat_history = []
    
    # Authentication
    if not st.session_state.user:
        st.header("Login/Register")
        tab1, tab2 = st.tabs(["Login", "Register"])
        
        with tab1:
            with st.form("Login"):
                username = st.text_input("Username")
                password = st.text_input("Password", type="password")
                if st.form_submit_button("Login"):
                    user = User.get_by_username(username)
                    if user and check_password_hash(user.password, password):
                        st.session_state.user = user
                        st.session_state.chat_history = user.chat_history
                        st.rerun()
                    else:
                        st.error("Invalid credentials")
        
        with tab2:
            with st.form("Register"):
                new_user = st.text_input("New Username")
                new_pass = st.text_input("New Password", type="password")
                if st.form_submit_button("Register"):
                    if User.get_by_username(new_user):
                        st.error("Username already exists")
                    else:
                        user = User.create(new_user, new_pass)
                        st.session_state.user = user
                        st.session_state.chat_history = []
                        st.rerun()

    else:
        # Chat Interface
        st.header(f"Welcome to {company_name}, {st.session_state.user.username}!")
        st.subheader("Chat with our AI Sales Assistant")
        
        # Display chat history
        for msg in st.session_state.chat_history:
            if msg["type"] == "human":
                with st.chat_message("user"):
                    st.write(msg["content"])
            else:
                with st.chat_message("assistant"):
                    st.write(msg["content"])

        if prompt := st.chat_input("Type you Message here..."):
            #Add user message to chat

            with st.chat_message("user"):
                st.write(prompt)

            # Get AI response
            with st.chat_message("assistant"):
                response = agent_executor.invoke({
                    "input": prompt,
                    "chat_history": [HumanMessage(content=msg["content"]) if msg["type"] == "human" else AIMessage(content=msg["content"]) 
                                   for msg in st.session_state.chat_history]
                })["output"]
                st.write(response)
                
                # Check if payment link is provided
                if "https://www.example.com/payment" in response:
                    st.session_state.user.update_products_bought(["Latest Product"])
                    st.success("Product added to your purchases!")    
                    
            # Update chat history in database
            new_messages = [
                {"type": "human", "content": prompt},
                {"type": "ai", "content": response}
            ]
            st.session_state.user.update_chat_history(new_messages)
            st.session_state.chat_history += new_messages

if __name__ == "__main__":
    main()