File size: 26,774 Bytes
5090140
28ed44f
177c5b5
28ed44f
0c730b1
10660a7
 
 
4892e48
435253f
 
b52d39b
bb706d3
10660a7
8ac8380
 
28ed44f
 
0ccfbeb
28ed44f
 
 
8b05473
9e38742
06d258f
3cb16ec
 
 
 
 
c8302a1
28ed44f
c8302a1
7f5b560
63b644a
 
 
096249d
 
 
 
 
 
 
 
 
 
 
 
 
63b644a
3450cd7
041d8cf
 
3450cd7
63b644a
c21734b
3450cd7
75b7282
 
3450cd7
041d8cf
3450cd7
041d8cf
63b644a
 
 
 
 
 
 
041d8cf
c21734b
 
 
 
041d8cf
c21734b
041d8cf
 
63b644a
041d8cf
ccc25c5
041d8cf
 
63b644a
 
041d8cf
75b7282
 
d48360b
 
 
75b7282
 
 
 
 
d48360b
75b7282
d48360b
75b7282
041d8cf
 
 
 
 
 
 
63b644a
 
 
041d8cf
 
63b644a
041d8cf
c21734b
 
 
 
 
 
75b7282
14c16ca
f8cc2f7
 
 
75b7282
 
c21734b
ccc25c5
041d8cf
c21734b
 
75b7282
c21734b
 
 
f8cc2f7
c21734b
 
b52d39b
041d8cf
d48360b
041d8cf
d48360b
ccc25c5
673cc44
ccc25c5
 
2982f30
75b7282
53b9156
041d8cf
d48360b
53b9156
d48360b
b7cb350
c8302a1
 
 
 
 
 
 
 
 
 
8da6a04
c8302a1
 
 
 
412b763
 
 
 
 
 
 
 
c8302a1
 
ddc0536
c8302a1
ddc0536
 
 
 
 
 
 
 
c8302a1
ddc0536
 
 
 
 
 
 
 
 
 
 
c8302a1
ddc0536
28ed44f
8da6a04
 
687c2f0
8da6a04
 
 
 
 
 
687c2f0
8da6a04
 
 
 
 
 
85693d5
8da6a04
 
 
 
85693d5
3450cd7
85693d5
3450cd7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
85693d5
8da6a04
4d152e0
d32ce41
 
 
 
 
 
646f8a3
d32ce41
 
feeb0e7
8da6a04
 
10660a7
 
 
0ccfbeb
10660a7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
40aa611
10660a7
 
 
0ccfbeb
10660a7
1dc5b0f
 
10660a7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1dc5b0f
10660a7
1dc5b0f
10660a7
 
 
 
 
1dc5b0f
10660a7
1dc5b0f
 
10660a7
 
4d152e0
10660a7
1dc5b0f
10660a7
 
 
 
 
 
4d152e0
1dc5b0f
10660a7
1dc5b0f
4d152e0
10660a7
4d152e0
 
10660a7
1dc5b0f
 
0ccfbeb
8b01918
 
4d152e0
8b01918
10660a7
 
c598bfb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3cb16ec
 
 
 
 
 
 
 
 
 
 
 
 
 
a491b68
ebcb412
 
 
3cb16ec
8f325c3
ebcb412
 
f8cc2f7
 
ebcb412
 
 
 
 
 
 
a491b68
3cb16ec
85693d5
ebcb412
4920472
a491b68
673cc44
ebcb412
3cb16ec
ebcb412
 
 
ccc25c5
ebcb412
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3cb16ec
ebcb412
a491b68
ebcb412
 
 
 
 
 
 
 
 
 
 
 
 
3cb16ec
 
 
c598bfb
3cb16ec
c598bfb
 
 
 
 
 
 
 
 
ebcb412
b526692
3cb16ec
 
 
 
 
 
b526692
 
 
 
3cb16ec
b526692
 
ebcb412
 
 
 
 
 
 
 
 
 
 
c21734b
ebcb412
4920472
 
8e9b65b
4920472
 
b6683d4
47402cb
b6683d4
3cb16ec
47402cb
c598bfb
3cb16ec
b6683d4
a491b68
 
 
ef44cd9
 
a491b68
 
 
59368fb
b6683d4
8b05473
3cb16ec
0847b05
c598bfb
 
b6683d4
b526692
3cb16ec
 
 
 
 
 
b526692
 
 
3cb16ec
b526692
 
8da6a04
4920472
 
47402cb
0847b05
d32ce41
26aa94d
34461d3
a491b68
20ff049
ef44cd9
 
 
7a3b01a
ef44cd9
 
 
 
 
feeb0e7
 
ef44cd9
 
 
 
 
 
 
 
 
 
 
 
feeb0e7
ef44cd9
 
 
feeb0e7
 
 
 
ef44cd9
feeb0e7
47402cb
8b01918
28ed44f
47402cb
8da6a04
0f075d7
8b01918
c8302a1
d613eb7
8b01918
 
c8302a1
8da6a04
0f075d7
8b01918
 
041d8cf
a491b68
8b01918
 
 
 
 
 
4b05267
c8302a1
ced5a78
3cb16ec
a491b68
 
c86dfe0
 
 
a491b68
3cb16ec
8b01918
 
 
8da6a04
8b01918
3d30d16
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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
import os
import json
import re
import gradio as gr
import pandas as pd
import requests
import random
import urllib.parse
import spacy
from sklearn.metrics.pairwise import cosine_similarity
import numpy as np
from typing import List, Dict
from tempfile import NamedTemporaryFile
from bs4 import BeautifulSoup
from langchain.prompts import PromptTemplate
from langchain.chains import LLMChain
from langchain_core.prompts import ChatPromptTemplate
from langchain_community.vectorstores import FAISS
from langchain_community.document_loaders import PyPDFLoader
from langchain_core.output_parsers import StrOutputParser
from langchain_community.embeddings import HuggingFaceEmbeddings
from langchain_community.llms import HuggingFaceHub
from langchain_core.documents import Document
from sentence_transformers import SentenceTransformer
from llama_parse import LlamaParse
from llama_cpp import Llama
from llama_cpp_agent.llm_agent import LlamaCppAgent
from llama_cpp_agent.messages_formatter import MessagesFormatterType
from llama_cpp_agent.providers.llama_cpp_endpoint_provider import LlamaCppEndpointSettings


huggingface_token = os.environ.get("HUGGINGFACE_TOKEN")
llama_cloud_api_key = os.environ.get("LLAMA_CLOUD_API_KEY")

# Load SentenceTransformer model
sentence_model = SentenceTransformer('paraphrase-MiniLM-L6-v2')

def load_spacy_model():
    try:
        # Try to load the model
        return spacy.load("en_core_web_sm")
    except OSError:
        # If loading fails, download the model
        os.system("python -m spacy download en_core_web_sm")
        # Try loading again
        return spacy.load("en_core_web_sm")

# Load spaCy model
nlp = load_spacy_model()

class EnhancedContextDrivenChatbot:
    def __init__(self, history_size: int = 10, max_history_chars: int = 5000):
        self.history = []
        self.history_size = history_size
        self.max_history_chars = max_history_chars
        self.entity_tracker = {}
        self.conversation_context = ""
        self.model = None
        self.last_instructions = None
    
    def add_to_history(self, text: str):
        self.history.append(text)
        while len(' '.join(self.history)) > self.max_history_chars or len(self.history) > self.history_size:
            self.history.pop(0)
        
        # Update entity tracker
        doc = nlp(text)
        for ent in doc.ents:
            if ent.label_ not in self.entity_tracker:
                self.entity_tracker[ent.label_] = set()
            self.entity_tracker[ent.label_].add(ent.text)

        # Update conversation context
        self.conversation_context += f" {text}"
        self.conversation_context = ' '.join(self.conversation_context.split()[-100:])  # Keep last 100 words

    def get_context(self):
        return self.conversation_context

    def is_follow_up_question(self, question):
        doc = nlp(question.lower())
        follow_up_indicators = set(['it', 'this', 'that', 'these', 'those', 'he', 'she', 'they', 'them'])
        return any(token.text in follow_up_indicators for token in doc) or question.strip().startswith("What about")

    def extract_topics(self, text):
        doc = nlp(text)
        return [chunk.text for chunk in doc.noun_chunks]

    def extract_instructions(self, text):
        instruction_patterns = [
            r"(.*?),?\s*(?:please\s+)?(provide\s+(?:me\s+)?a\s+.*?|give\s+(?:me\s+)?a\s+.*?|create\s+a\s+.*?)$",
            r"(.*?),?\s*(?:please\s+)?(summarize|analyze|explain|describe|elaborate\s+on).*$",
            r"(.*?),?\s*(?:please\s+)?(in\s+detail|briefly|concisely).*$",
        ]
        
        for pattern in instruction_patterns:
            match = re.match(pattern, text, re.IGNORECASE)
            if match:
                return match.group(1).strip(), match.group(2).strip()

        return text, None

    def get_most_relevant_context(self, question):
        if not self.history:
            return question

        # Create a combined context from history
        combined_context = self.get_context()
        
        # Get embeddings
        context_embedding = sentence_model.encode([combined_context])[0]
        question_embedding = sentence_model.encode([question])[0]
        
        # Calculate similarity
        similarity = cosine_similarity([context_embedding], [question_embedding])[0][0]
        
        # If similarity is high, it's likely a follow-up question
        if similarity > 0.5:  # This threshold can be adjusted
            return f"{combined_context} {question}"
        
        # Otherwise, it might be a new topic
        return question
    
    def rephrase_query(self, question, instructions=None):
        if not self.model:
            return question  # Return original question if no model is available
        
        instruction_prompt = f"Instructions: {instructions}\n" if instructions else ""
        
        prompt = f"""
        Given the conversation context, the current question, and any provided instructions, rephrase the question to include relevant context and rephrase it to more search-engine-friendly query:
        
        Conversation context: {self.get_context()}
        Current question: {question}
        {instruction_prompt}
        Rephrased question:
        """
        
        rephrased_question = generate_chunked_response(self.model, prompt)
        
        return rephrased_question.strip()

    def process_question(self, question):
        core_question, instructions = self.extract_instructions(question)
        
        if self.is_follow_up_question(core_question):
            contextualized_question = self.get_most_relevant_context(core_question)
            contextualized_question = self.rephrase_query(contextualized_question, instructions)
        else:
            contextualized_question = core_question
        
        topics = self.extract_topics(contextualized_question)
        
        self.add_to_history(question)
        self.last_instructions = instructions
        
        return contextualized_question, topics, self.entity_tracker, instructions
        
# Initialize LlamaParse
llama_parser = LlamaParse(
    api_key=llama_cloud_api_key,
    result_type="markdown",
    num_workers=4,
    verbose=True,
    language="en",
)

def load_document(file: NamedTemporaryFile, parser: str = "pypdf") -> List[Document]:
    """Loads and splits the document into pages."""
    if parser == "pypdf":
        loader = PyPDFLoader(file.name)
        return loader.load_and_split()
    elif parser == "llamaparse":
        try:
            documents = llama_parser.load_data(file.name)
            return [Document(page_content=doc.text, metadata={"source": file.name}) for doc in documents]
        except Exception as e:
            print(f"Error using Llama Parse: {str(e)}")
            print("Falling back to PyPDF parser")
            loader = PyPDFLoader(file.name)
            return loader.load_and_split()
    else:
        raise ValueError("Invalid parser specified. Use 'pypdf' or 'llamaparse'.")

def update_vectors(files, parser):
    if not files:
        return "Please upload at least one PDF file."
    
    embed = get_embeddings()
    total_chunks = 0
    
    all_data = []
    for file in files:
        data = load_document(file, parser)
        all_data.extend(data)
        total_chunks += len(data)
    
    if os.path.exists("faiss_database"):
        database = FAISS.load_local("faiss_database", embed, allow_dangerous_deserialization=True)
        database.add_documents(all_data)
    else:
        database = FAISS.from_documents(all_data, embed)
    
    database.save_local("faiss_database")
    
    return f"Vector store updated successfully. Processed {total_chunks} chunks from {len(files)} files using {parser}."

def get_embeddings():
    return HuggingFaceEmbeddings(model_name="sentence-transformers/all-mpnet-base-v2")

def clear_cache():
    if os.path.exists("faiss_database"):
        os.remove("faiss_database")
        return "Cache cleared successfully."
    else:
        return "No cache to clear."

def get_model(temperature, top_p, repetition_penalty):
    return HuggingFaceHub(
        repo_id="mistralai/Mistral-7B-Instruct-v0.3",
        model_kwargs={
            "temperature": temperature,
            "top_p": top_p,
            "repetition_penalty": repetition_penalty,
            "max_length": 800
        },
        huggingfacehub_api_token=huggingface_token
    )

MAX_PROMPT_CHARS = 20000  # Adjust based on your model's limitations

def chunk_text(text: str, max_chunk_size: int = 800) -> List[str]:
    chunks = []
    current_chunk = ""
    for sentence in re.split(r'(?<=[.!?])\s+', text):
        if len(current_chunk) + len(sentence) > max_chunk_size:
            chunks.append(current_chunk.strip())
            current_chunk = sentence
        else:
            current_chunk += " " + sentence
    if current_chunk:
        chunks.append(current_chunk.strip())
    return chunks

def get_most_relevant_chunks(question: str, chunks: List[str], top_k: int = 3) -> List[str]:
    question_embedding = sentence_model.encode([question])[0]
    chunk_embeddings = sentence_model.encode(chunks)
    similarities = cosine_similarity([question_embedding], chunk_embeddings)[0]
    top_indices = np.argsort(similarities)[-top_k:]
    return [chunks[i] for i in top_indices]

def generate_chunked_response(model, prompt, max_tokens=800, max_chunks=5):
    full_response = ""
    for i in range(max_chunks):
        try:
            chunk = model(prompt + full_response, max_new_tokens=max_tokens)
            chunk = chunk.strip()
            if chunk.endswith((".", "!", "?")):
                full_response += chunk
                break
            full_response += chunk
        except Exception as e:
            print(f"Error in generate_chunked_response: {e}")
            break
    return full_response.strip()

def extract_text_from_webpage(html):
    soup = BeautifulSoup(html, 'html.parser')
    for script in soup(["script", "style"]):
        script.extract()
    text = soup.get_text()
    lines = (line.strip() for line in text.splitlines())
    chunks = (phrase.strip() for line in lines for phrase in line.split("  "))
    text = '\n'.join(chunk for chunk in chunks if chunk)
    return text

_useragent_list = [
    "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36",
    "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36",
    "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Edge/91.0.864.59 Safari/537.36",
    "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Edge/91.0.864.59 Safari/537.36",
    "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Safari/537.36",
    "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Safari/537.36",
]

def google_search(term, num_results=5, lang="en", timeout=5, safe="active", ssl_verify=None):
    escaped_term = urllib.parse.quote_plus(term)
    start = 0
    all_results = []
    max_chars_per_page = 8000

    print(f"Starting Google search for term: '{term}'")

    with requests.Session() as session:
        while start < num_results:
            try:
                user_agent = random.choice(_useragent_list)
                headers = {
                    'User-Agent': user_agent
                }
                resp = session.get(
                    url="https://www.google.com/search",
                    headers=headers,
                    params={
                        "q": term,
                        "num": num_results - start,
                        "hl": lang,
                        "start": start,
                        "safe": safe,
                    },
                    timeout=timeout,
                    verify=ssl_verify,
                )
                resp.raise_for_status()
                print(f"Successfully retrieved search results page (start={start})")
            except requests.exceptions.RequestException as e:
                print(f"Error retrieving search results: {e}")
                break

            soup = BeautifulSoup(resp.text, "html.parser")
            result_block = soup.find_all("div", attrs={"class": "g"})
            if not result_block:
                print("No results found on this page")
                break
            
            print(f"Found {len(result_block)} results on this page")
            for result in result_block:
                link = result.find("a", href=True)
                if link:
                    link = link["href"]
                    print(f"Processing link: {link}")
                    try:
                        webpage = session.get(link, headers=headers, timeout=timeout)
                        webpage.raise_for_status()
                        visible_text = extract_text_from_webpage(webpage.text)
                        if len(visible_text) > max_chars_per_page:
                            visible_text = visible_text[:max_chars_per_page] + "..."
                        all_results.append({"link": link, "text": visible_text})
                        print(f"Successfully extracted text from {link}")
                    except requests.exceptions.RequestException as e:
                        print(f"Error retrieving webpage content: {e}")
                        all_results.append({"link": link, "text": None})
                else:
                    print("No link found for this result")
                    all_results.append({"link": None, "text": None})
            start += len(result_block)

    print(f"Search completed. Total results: {len(all_results)}")
    
    if not all_results:
        print("No search results found. Returning a default message.")
        return [{"link": None, "text": "No information found in the web search results."}]

    return all_results

def estimate_tokens(text):
    return len(text.split())

def truncate_text(text, max_tokens):
    words = text.split()
    if len(words) <= max_tokens:
        return text
    return ' '.join(words[:max_tokens])

def rerank_documents(query: str, documents: List[Document], top_k: int = 5) -> List[Document]:
    query_embedding = sentence_model.encode([query])[0]
    doc_embeddings = sentence_model.encode([doc.page_content for doc in documents])
    
    similarities = cosine_similarity([query_embedding], doc_embeddings)[0]
    
    ranked_indices = similarities.argsort()[::-1][:top_k]
    return [documents[i] for i in ranked_indices]

def prepare_context(query: str, documents: List[Document], max_tokens: int) -> str:
    reranked_docs = rerank_documents(query, documents)
    
    context = ""
    for doc in reranked_docs:
        doc_content = f"Source: {doc.metadata.get('source', 'Unknown')}\nContent: {doc.page_content}\n\n"
        if estimate_tokens(context + doc_content) > max_tokens:
            break
        context += doc_content
    
    return truncate_text(context, max_tokens)

# Initialize LlamaCppAgent
def initialize_llama_cpp_agent():
    main_model = LlamaCppEndpointSettings(
        completions_endpoint_url="http://127.0.0.1:8080/completion"
    )
    llama_cpp_agent = LlamaCppAgent(
        main_model,
        debug_output=False,
        system_prompt="You are an AI assistant designed to help with RAG tasks.",
        predefined_messages_formatter_type=MessagesFormatterType.CHATML
    )
    return llama_cpp_agent

# Modify the ask_question function to use LlamaCppAgent
def ask_question(question, temperature, top_p, repetition_penalty, web_search, chatbot, user_instructions):
    if not question:
        return "Please enter a question."

    llama_cpp_agent = initialize_llama_cpp_agent()
    model = get_model(temperature, top_p, repetition_penalty)
    
    # Update the chatbot's model
    chatbot.model = model

    embed = get_embeddings()

    if os.path.exists("faiss_database"):
        database = FAISS.load_local("faiss_database", embed, allow_dangerous_deserialization=True)
    else:
        database = None

    max_attempts = 3
    max_input_tokens = 20000
    max_output_tokens = 800

    if web_search:
        contextualized_question, topics, entity_tracker, _ = chatbot.process_question(question)
        
        try:
            search_results = google_search(contextualized_question, num_results=5)
        except Exception as e:
            print(f"Error in web search: {e}")
            return f"I apologize, but I encountered an error while searching for information: {str(e)}"

        all_answers = []

        for attempt in range(max_attempts):
            try:
                web_docs = [Document(page_content=result["text"], metadata={"source": result["link"]}) for result in search_results if result["text"]]

                if not web_docs:
                    return "I'm sorry, but I couldn't find any relevant information from the web search."

                if database is None:
                    database = FAISS.from_documents(web_docs, embed)
                else:
                    database.add_documents(web_docs)

                database.save_local("faiss_database")

                context_str = prepare_context(contextualized_question, web_docs, max_input_tokens // 2)

                instruction_prompt = f"User Instructions: {user_instructions}\n" if user_instructions else ""

                prompt_template = f"""
                Answer the question based on the following web search results, conversation context, entity information, and user instructions:
                Web Search Results:
                {{context}}
                Conversation Context: {{conv_context}}
                Current Question: {{question}}
                Topics: {{topics}}
                Entity Information: {{entities}}
                {instruction_prompt}
                Provide a concise and relevant answer to the question.
                """

                current_conv_context = truncate_text(chatbot.get_context(), max_input_tokens // 4)
                current_topics = topics[:5]
                current_entities = {k: list(v)[:3] for k, v in entity_tracker.items()}

                formatted_prompt = prompt_template.format(
                    context=context_str,
                    conv_context=current_conv_context,
                    question=question,
                    topics=", ".join(current_topics),
                    entities=json.dumps(current_entities)
                )

                if estimate_tokens(formatted_prompt) > max_input_tokens:
                    formatted_prompt = truncate_text(formatted_prompt, max_input_tokens)

                try:
                    # Use LlamaCppAgent for initial response generation
                    initial_response = llama_cpp_agent.get_chat_response(formatted_prompt, temperature=temperature)
                    
                    # Use generate_chunked_response for further refinement if needed
                    full_response = generate_chunked_response(model, initial_response, max_tokens=max_output_tokens)
                    
                    answer = extract_answer(full_response, user_instructions)
                    all_answers.append(answer)
                    break
                except Exception as e:
                    print(f"Error in response generation: {e}")
                    if attempt == max_attempts - 1:
                        all_answers.append(f"I apologize, but I encountered an error while generating the response. Please try again with a simpler question.")

            except Exception as e:
                print(f"Error in ask_question (attempt {attempt + 1}): {e}")
                if attempt == max_attempts - 1:
                    all_answers.append(f"I apologize, but an unexpected error occurred. Please try again with a different question or check your internet connection.")

        answer = "\n\n".join(all_answers)
        sources = set(doc.metadata['source'] for doc in web_docs)
        sources_section = "\n\nSources:\n" + "\n".join(f"- {source}" for source in sources)
        answer += sources_section

        chatbot.add_to_history(answer)

        return answer

    else:  # PDF document chat
        for attempt in range(max_attempts):
            try:
                if database is None:
                    return "No documents available. Please upload PDF documents to answer questions."

                retriever = database.as_retriever(search_kwargs={"k": 5})
                relevant_docs = retriever.get_relevant_documents(question)
                
                context_str = prepare_context(question, relevant_docs, max_input_tokens // 2)

                instruction_prompt = f"User Instructions: {user_instructions}\n" if user_instructions else ""

                prompt_template = f"""
                Answer the question based on the following context from the PDF document:
                Context:
                {{context}}
                Question: {{question}}
                {instruction_prompt}
                Provide a summarized and direct answer to the question.
                """

                formatted_prompt = prompt_template.format(context=context_str, question=question)

                if estimate_tokens(formatted_prompt) > max_input_tokens:
                    formatted_prompt = truncate_text(formatted_prompt, max_input_tokens)

                try:
                    # Use LlamaCppAgent for initial response generation
                    initial_response = llama_cpp_agent.get_chat_response(formatted_prompt, temperature=temperature)
                    
                    # Use generate_chunked_response for further refinement if needed
                    full_response = generate_chunked_response(model, initial_response, max_tokens=max_output_tokens)
                    
                    answer = extract_answer(full_response, user_instructions)
                    return answer
                except Exception as e:
                    print(f"Error in response generation: {e}")
                    if attempt == max_attempts - 1:
                        return f"I apologize, but I encountered an error while generating the response. Please try again with a simpler question."

            except Exception as e:
                print(f"Error in ask_question (attempt {attempt + 1}): {e}")
                if attempt == max_attempts - 1:
                    return f"I apologize, but an unexpected error occurred. Please try again with a different question."

    return "An unexpected error occurred. Please try again later."


def extract_answer(full_response, instructions=None):
    answer_patterns = [
        r"Provide a concise and direct answer to the question without mentioning the web search or these instructions:",
        r"Provide a concise and direct answer to the question:",
        r"Provide a concise and relevant answer to the question.",
        r"Answer:",
        r"Provide a summarized and direct answer to the question.",
        r"If the context doesn't contain relevant information, state that the information is not available in the document.",
        r"Provide a summarized and direct answer to the original question without mentioning the web search or these instructions:",
        r"Do not include any source information in your answer."     
    ]

    for pattern in answer_patterns:
        match = re.split(pattern, full_response, flags=re.IGNORECASE)
        if len(match) > 1:
            full_response = match[-1].strip()
            break
    
    # Remove any remaining instruction-like phrases
    cleanup_patterns = [
        r"without mentioning the web search or these instructions\.",
        r"Do not include any source information in your answer\.",
        r"If the context doesn't contain relevant information, state that the information is not available in the document\."
    ]

    for pattern in cleanup_patterns:
        full_response = re.sub(pattern, "", full_response, flags=re.IGNORECASE).strip()
    
    # Remove the user instructions if present
    if instructions:
        instruction_pattern = rf"User Instructions:\s*{re.escape(instructions)}.*?\n"
        full_response = re.sub(instruction_pattern, "", full_response, flags=re.IGNORECASE | re.DOTALL)
    
    return full_response.strip()

# Gradio interface
with gr.Blocks() as demo:
    gr.Markdown("# Enhanced PDF Document Chat and Web Search")
    
    with gr.Row():
        file_input = gr.Files(label="Upload your PDF documents", file_types=[".pdf"])
        parser_dropdown = gr.Dropdown(choices=["pypdf", "llamaparse"], label="Select PDF Parser", value="pypdf")
        update_button = gr.Button("Upload PDF")
    
    update_output = gr.Textbox(label="Update Status")
    update_button.click(update_vectors, inputs=[file_input, parser_dropdown], outputs=update_output)
    
    with gr.Row():
        with gr.Column(scale=2):
            chatbot = gr.Chatbot(label="Conversation")
            question_input = gr.Textbox(label="Ask a question")
            instructions_input = gr.Textbox(label="Instructions for response (optional)", placeholder="Enter any specific instructions for the response here")
            submit_button = gr.Button("Submit")
        with gr.Column(scale=1):
            temperature_slider = gr.Slider(label="Temperature", minimum=0.0, maximum=1.0, value=0.5, step=0.1)
            top_p_slider = gr.Slider(label="Top P", minimum=0.0, maximum=1.0, value=0.9, step=0.1)
            repetition_penalty_slider = gr.Slider(label="Repetition Penalty", minimum=1.0, maximum=2.0, value=1.0, step=0.1)
            web_search_checkbox = gr.Checkbox(label="Enable Web Search", value=False)

    enhanced_context_driven_chatbot = EnhancedContextDrivenChatbot()

    # Update the chat function to use the modified ask_question function
    def chat(question, history, temperature, top_p, repetition_penalty, web_search, user_instructions):
        answer = ask_question(question, temperature, top_p, repetition_penalty, web_search, enhanced_context_driven_chatbot, user_instructions)
        history.append((question, answer))
        return "", history
    
    submit_button.click(chat, inputs=[question_input, chatbot, temperature_slider, top_p_slider, repetition_penalty_slider, web_search_checkbox, instructions_input], outputs=[question_input, chatbot])

    clear_button = gr.Button("Clear Cache")
    clear_output = gr.Textbox(label="Cache Status")
    clear_button.click(clear_cache, inputs=[], outputs=clear_output)

if __name__ == "__main__":
    demo.launch()