Update app.py
Browse files
app.py
CHANGED
@@ -19,11 +19,25 @@ logging.basicConfig(
|
|
19 |
|
20 |
GROQ_API_KEY = "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"
|
21 |
client = Groq(api_key=GROQ_API_KEY)
|
22 |
-
|
23 |
PDF_PATH = 'Robert Ciesla - The Book of Chatbots_ From ELIZA to ChatGPT-Springer (2024).pdf'
|
24 |
sentence_transformer_model = SentenceTransformer('all-MiniLM-L6-v2')
|
25 |
cache = {}
|
26 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
27 |
# --------------------- PDF Processing ---------------------
|
28 |
|
29 |
def read_pdf(file_path):
|
@@ -42,21 +56,10 @@ def read_pdf(file_path):
|
|
42 |
sentences_with_pages.append({'sentence': sentence, 'page_number': page_num + 1})
|
43 |
return sentences_with_pages
|
44 |
|
|
|
45 |
sentences_with_pages = read_pdf(PDF_PATH)
|
46 |
vector_index, sentences_with_pages = vectorize_text(sentences_with_pages)
|
47 |
|
48 |
-
def vectorize_text(sentences_with_pages):
|
49 |
-
try:
|
50 |
-
sentences = [item['sentence'] for item in sentences_with_pages]
|
51 |
-
embeddings = sentence_transformer_model.encode(sentences, show_progress_bar=True)
|
52 |
-
index = faiss.IndexFlatL2(embeddings.shape[1])
|
53 |
-
index.add(np.array(embeddings))
|
54 |
-
logging.info(f"Added {len(sentences)} sentences to the vector store.")
|
55 |
-
return index, sentences_with_pages
|
56 |
-
except Exception as e:
|
57 |
-
logging.error(f"Error during vectorization: {str(e)}")
|
58 |
-
return None, None
|
59 |
-
|
60 |
# --------------------- Query Handling ---------------------
|
61 |
|
62 |
def generate_query_embedding(query):
|
@@ -111,14 +114,11 @@ def generate_answer(query):
|
|
111 |
|
112 |
# Construct primary prompt
|
113 |
prompt = f"""
|
114 |
-
Use the following context from "The Book of Chatbots" to answer the question. If additional
|
115 |
-
|
116 |
**Context (Pages {page_numbers_str}):**
|
117 |
{combined_text}
|
118 |
-
|
119 |
**User's question:**
|
120 |
{query}
|
121 |
-
|
122 |
**Remember to indicate the specific page numbers.**
|
123 |
"""
|
124 |
primary_responses = generate_diverse_responses(prompt)
|
@@ -126,11 +126,9 @@ Use the following context from "The Book of Chatbots" to answer the question. If
|
|
126 |
|
127 |
# Construct additional prompt for explanations
|
128 |
explanation_prompt = f"""
|
129 |
-
The user has a question about a complex topic. Could you provide an
|
130 |
-
|
131 |
**User's question:**
|
132 |
{query}
|
133 |
-
|
134 |
**Primary answer:**
|
135 |
{primary_answer}
|
136 |
"""
|
@@ -147,7 +145,6 @@ The user has a question about a complex topic. Could you provide an explaination
|
|
147 |
# General knowledge fallback
|
148 |
prompt = f"""
|
149 |
The user asked a question that is not covered in "The Book of Chatbots." Please provide a helpful answer using general knowledge.
|
150 |
-
|
151 |
**User's question:**
|
152 |
{query}
|
153 |
"""
|
|
|
19 |
|
20 |
GROQ_API_KEY = "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"
|
21 |
client = Groq(api_key=GROQ_API_KEY)
|
|
|
22 |
PDF_PATH = 'Robert Ciesla - The Book of Chatbots_ From ELIZA to ChatGPT-Springer (2024).pdf'
|
23 |
sentence_transformer_model = SentenceTransformer('all-MiniLM-L6-v2')
|
24 |
cache = {}
|
25 |
|
26 |
+
# --------------------- Vectorization Function ---------------------
|
27 |
+
|
28 |
+
def vectorize_text(sentences_with_pages):
|
29 |
+
"""Vectorize sentences using SentenceTransformer and create a FAISS index."""
|
30 |
+
try:
|
31 |
+
sentences = [item['sentence'] for item in sentences_with_pages]
|
32 |
+
embeddings = sentence_transformer_model.encode(sentences, show_progress_bar=True)
|
33 |
+
index = faiss.IndexFlatL2(embeddings.shape[1])
|
34 |
+
index.add(np.array(embeddings))
|
35 |
+
logging.info(f"Added {len(sentences)} sentences to the vector store.")
|
36 |
+
return index, sentences_with_pages
|
37 |
+
except Exception as e:
|
38 |
+
logging.error(f"Error during vectorization: {str(e)}")
|
39 |
+
return None, None
|
40 |
+
|
41 |
# --------------------- PDF Processing ---------------------
|
42 |
|
43 |
def read_pdf(file_path):
|
|
|
56 |
sentences_with_pages.append({'sentence': sentence, 'page_number': page_num + 1})
|
57 |
return sentences_with_pages
|
58 |
|
59 |
+
# Read and Vectorize PDF Content
|
60 |
sentences_with_pages = read_pdf(PDF_PATH)
|
61 |
vector_index, sentences_with_pages = vectorize_text(sentences_with_pages)
|
62 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
63 |
# --------------------- Query Handling ---------------------
|
64 |
|
65 |
def generate_query_embedding(query):
|
|
|
114 |
|
115 |
# Construct primary prompt
|
116 |
prompt = f"""
|
117 |
+
Use the following context from "The Book of Chatbots" to answer the question. If additional explanation is needed, provide an example.
|
|
|
118 |
**Context (Pages {page_numbers_str}):**
|
119 |
{combined_text}
|
|
|
120 |
**User's question:**
|
121 |
{query}
|
|
|
122 |
**Remember to indicate the specific page numbers.**
|
123 |
"""
|
124 |
primary_responses = generate_diverse_responses(prompt)
|
|
|
126 |
|
127 |
# Construct additional prompt for explanations
|
128 |
explanation_prompt = f"""
|
129 |
+
The user has a question about a complex topic. Could you provide an explanation or example and real-life example for better understanding?
|
|
|
130 |
**User's question:**
|
131 |
{query}
|
|
|
132 |
**Primary answer:**
|
133 |
{primary_answer}
|
134 |
"""
|
|
|
145 |
# General knowledge fallback
|
146 |
prompt = f"""
|
147 |
The user asked a question that is not covered in "The Book of Chatbots." Please provide a helpful answer using general knowledge.
|
|
|
148 |
**User's question:**
|
149 |
{query}
|
150 |
"""
|