File size: 12,025 Bytes
dc4de02
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2393995
dc4de02
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
import os
from typing import List, Dict
from chainlit.types import AskFileResponse
from qdrant_client import QdrantClient
from qdrant_client.http.models import Distance, VectorParams
from langchain_qdrant import QdrantVectorStore
from langchain_text_splitters import RecursiveCharacterTextSplitter
from langchain_community.document_loaders import PyMuPDFLoader
from langchain_openai.embeddings import OpenAIEmbeddings
from langchain.storage import LocalFileStore
from langchain.embeddings import CacheBackedEmbeddings
from langchain_core.prompts import ChatPromptTemplate
from langchain_core.globals import set_llm_cache
from langchain_openai import ChatOpenAI
from langchain_core.caches import InMemoryCache
from operator import itemgetter
from langchain_core.runnables.passthrough import RunnablePassthrough
from langchain.schema.runnable.config import RunnableConfig
from langsmith.evaluation import LangChainStringEvaluator, evaluate
from datetime import datetime
from utils_evaluate_objections import generate_response_to_objection
import pandas as pd 
import uuid
import chainlit as cl
import dotenv
import tempfile

## Aaron to improve
## add customer-based value prop to load: 
## compare and contract sales rep value prop & business domain
## use that list to generate objections that is close to sales opportunity 

# Load environment variables from .env file
dotenv.load_dotenv()

# Access the OpenAI API key
OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")

set_llm_cache(InMemoryCache())

def process_value_prop_pdf(file: AskFileResponse) -> str:
    """
    Process the value proposition PDF file and return its content as a string.

    Args:
        file (AskFileResponse): The uploaded PDF file.

    Returns:
        str: The extracted content from the PDF.
    """
    # Create a temporary file to store the uploaded content
    with tempfile.NamedTemporaryFile(mode="wb", delete=False) as temp_file:
        temp_file.write(file.content)
        temp_file_path = temp_file.name

    # Load the PDF using PyMuPDFLoader
    loader = PyMuPDFLoader(temp_file_path)
    documents = loader.load()

    # Combine the content of all pages into a single string
    value_prop_text = "\n".join(doc.page_content for doc in documents)

    # Return the text extracted from the PDF
    return value_prop_text

text_splitter = RecursiveCharacterTextSplitter(separators=["\n\n","\n"],chunk_size=200, chunk_overlap=20)

# QDrant Client Setup
collection_name = f"pdf_to_parse_{uuid.uuid4()}"
client = QdrantClient(":memory:")
client.create_collection(
    collection_name=collection_name,
    vectors_config=VectorParams(size=1536, distance=Distance.COSINE),
)

# Embedding Model
core_embeddings = OpenAIEmbeddings(model="text-embedding-3-small")

# Internal object to store objections
objections = []

def process_text_file(file: AskFileResponse):
    import tempfile
    with tempfile.NamedTemporaryFile(mode="w", delete=False) as temp_file:
        with open(temp_file.name, "wb") as f:
            f.write(file.content)

    Loader = PyMuPDFLoader
    loader = Loader(temp_file.name)
    documents = loader.load()
    docs = text_splitter.split_documents(documents)
    for i, doc in enumerate(docs):
        doc.metadata["source"] = f"source_{i}"
    return docs

# Function to generate a response to the user's objection
def generate_response_to_objection(user_response, num):
    from langchain_openai import ChatOpenAI
    from ragas.llms.base import LangchainLLMWrapper
    openai_model = LangchainLLMWrapper(ChatOpenAI(model_name="gpt-4o"))
    scorer = SatisfyRate(llm=openai_model)
    
    satify_0_1 = scorer.single_turn_ascore(user_response['objection'][num], user_response['response'][num])
    # Implement your logic to generate a response based on the user's input
    return f"Response to your objection: {user_response['objection'][num],user_response['response'][num],  satify_0_1}" # Placeholder response


@cl.on_chat_start
async def on_chat_start():
    # Ask for the first PDF file (Potential Customer Business Domain)
    files = None
    while files is None:
        files = await cl.AskFileMessage(
            content="Please upload a PDF for Potential Customer Business Domain to begin!",
            accept=["application/pdf"],
            max_size_mb=2,
            timeout=180,
        ).send()

    first_file = files[0]

    # Notify the user that the file is being processed
    msg = cl.Message(
        content=f"Processing `{first_file.name}`...", disable_human_feedback=True
    )
    await msg.send()

    # Process the first file
    texts = process_text_file(first_file)
    print(f"Processing {len(texts)} text chunks from the first file")

    # Ask for the second PDF file (Value Proposition)
    files = None
    while files is None:
        files = await cl.AskFileMessage(
            content="Please upload our customer-specific value proposition PDF.",
            accept=["application/pdf"],
            max_size_mb=2,
            timeout=180,
        ).send()

    second_file = files[0]

    # Notify the user that the second file is being processed
    msg = cl.Message(
        content=f"Processing `{second_file.name}`...", disable_human_feedback=True
    )
    await msg.send()

    # Process the second file
    value_prop_content = process_value_prop_pdf(second_file)
    print(f"Processing {len(value_prop_content)} text chunks from the second file")
    #print(value_prop_content)
    
    
    
    # Create a Local File Store for caching
    store = LocalFileStore("./cache/")
    cached_embedder = CacheBackedEmbeddings.from_bytes_store(
        core_embeddings, store, namespace=core_embeddings.model
    )

    # QDrant Vector Store Setup
    vectorstore = QdrantVectorStore(
        client=client,
        collection_name=collection_name,
        embedding=cached_embedder
    )
    vectorstore.add_documents(texts)
    retriever = vectorstore.as_retriever(search_type="mmr", search_kwargs={"k": 5})

    chat_openai = ChatOpenAI() #model='gpt-4o')

    # RAG Chain for generating objections
    objection_prompt_template = """\
    Internally, review the value proposition information of sales rep's company then review your Context. 
    Internally, find areas where the sales' product/service could help add value and where it fails to fit.
    Internally, review this final list and think step-by-step on what likely objections to buying product/service.
    Using these thoughts, generate 5 Context-based sales objections.
    The output is numbered objections only.
    For example:
    '1. Our current pricing structure is already optimized and we do not see the immediate need for AI assistance in pricing complex structural options in Foreign Exchange.'
    '2. We have a dedicated team handling customer experience and efficiency, and we do not see how integrating AI for pricing options would significantly improve these aspects.',
    '3. While we acknowledge the importance of technology and innovation in banking, we are currently focusing on other areas for digital transformation and do not prioritize the use of AI in pricing at this time.'
    '4. Our customer base might not be ready for a shift towards AI-driven pricing models, and introducing such a change could potentially create confusion and resistance among our clients.',
    '5. We are cautious about the potential risks and uncertainties associated with relying heavily on AI for pricing, especially in the volatile Foreign Exchange market where human expertise and judgment are highly valued.'
    The output is NOT intro phrases or ** text **:
    Context: {context}
    Value Proposition: {{value_prop_content}}
    Sales Opportunity: {{sales_opportunity}}
    """

    # Create a chain for generating objections with the retrieved context
    objection_chain = (
        {"context": itemgetter("question") | retriever} 
        | RunnablePassthrough.assign(context=itemgetter("context")) 
        | ChatPromptTemplate.from_messages([
            ("system", "You a potential customer interested in the offering from this sales rep. Please use context business name and your name found in sales_opportunity."),
            ("human", objection_prompt_template)
        ])
        | chat_openai
    )

    # Ask the user for the sales opportunity
    sales_opportunity = await cl.AskUserMessage(
        content="Please describe the sales opportunity you want to discuss.", timeout=300
    ).send()

    #print(sales_opportunity['content'])
    # Retrieve the documents based on the query (here we're simulating with the sales opportunity)
    retrieved_docs = retriever.get_relevant_documents(value_prop_content)

    # Extract the content of the retrieved documents (chunks)
    context_chunks = [doc.page_content for doc in retrieved_docs]

    # Combine the retrieved context chunks into a single string
    context = "\n\n".join(context_chunks)

    # Log and display the retrieved chunks to Chainlit
    await cl.Message(content=f"Retrieved context chunks:\n{context}", author="retriever").send()
    
    #print (sales_opportunity["content"])
    # Generate objections using the chain, with the context included
    #print ({"question": "Generate sales objections from {{value_prop_content}}", "sales_opportunity": sales_opportunity["content"], "context": context})
    objection_response = objection_chain.invoke({"question": "Generate 3 sales objections", "sales_opportunity": sales_opportunity["content"], "context": context})

    objections.extend(objection_response.content.split('\n'))  # Assuming each objection is on a new line
    # Remove empty strings or strings with only spaces
    cleaned_objections = [objection for objection in objections if objection.strip()]

    # Output the cleaned list
    print(cleaned_objections)

    # Store the objection chain in user session
    cl.user_session.set("objection_chain", objection_chain)
    cl.user_session.set("objections", objections)

    await cl.Message(content="We are ready to enter Sales 'Sparring'. Ok? ").send()

@cl.on_message
async def main(message):
    """
    This function will be called every time a message is received from a session.

    We will use the LCEL RAG chain to generate a response to the user query.

    The LCEL RAG chain is stored in the user session, and is unique to each user session - this is why we can access it here.
    """
    await cl.AskUserMessage(
        content="Are you ready?", timeout=300
    ).send()

    objection_chain = cl.user_session.get("objection_chain")

    #msg = cl.Message(content="")

    # Retrieve the list of objections
    objections = cl.user_session.get("objections")
    #print (objections[0])
    objection_responses = {}

    # Iterate through each objection
    for i, objection in enumerate(objections):
        # Return the objection in the form of a question
        await cl.Message(content=f"Objection: {objection}").send()

        # Capture user input
        user_response = await cl.AskUserMessage(
            content="How would you respond to this objection?", timeout=600
        ).send()

        objection_responses[objection] = user_response['content']

        # Process the user's response (you can implement your logic here)
        #new_objection_response = generate_response_to_objection(user_response.content)

        # Send the response back to the user
        #await cl.Message(content=f"Response to objection {i + 1}: {new_objection_response}").send()
    print (objection_responses)
    data = []
    for objection, response in objection_responses.items():
        data.append({
            "timestamp": datetime.now(),  # Capture the current timestamp
            "objection": objection,
            "response": response
        })

    # Create a DataFrame
    user_response = pd.DataFrame(data)
    timestamp = datetime.now().strftime('%Y%m%d_%H%M%S')

    #response = await generate_response_to_objection(user_response, 0)
    #user_response.to_csv(f'data/user_response_{timestamp}.csv', index=False)