Update app.py
Browse files
app.py
CHANGED
|
@@ -8,6 +8,8 @@ from sentence_transformers import SentenceTransformer
|
|
| 8 |
from rank_bm25 import BM25Okapi
|
| 9 |
from langchain.llms import OpenAI
|
| 10 |
|
|
|
|
|
|
|
| 11 |
# Global variables for retrieval
|
| 12 |
pdf_texts = []
|
| 13 |
bm25, index, embed_model, document_embeddings = None, None, None, None
|
|
@@ -62,19 +64,20 @@ def hybrid_search(query, top_n=3):
|
|
| 62 |
final_ranked_indices = [top_docs_indices[i] for i in sub_ranked_indices[0]]
|
| 63 |
return [pdf_texts[i] for i in final_ranked_indices]
|
| 64 |
|
| 65 |
-
|
| 66 |
def generate_response(query):
|
| 67 |
"""Generates responses based on retrieved documents and conversation history."""
|
| 68 |
retrieved_docs = hybrid_search(query)
|
| 69 |
memory_context = memory.load_memory_variables({})
|
| 70 |
|
| 71 |
prompt = f"Conversation history: {memory_context}\nRelevant text: {retrieved_docs}\nGenerate a response:"
|
| 72 |
-
llm = OpenAI(model_name="gpt-4")
|
| 73 |
-
response = llm(prompt)
|
| 74 |
|
|
|
|
| 75 |
memory.save_context({"user": query}, {"AI": response})
|
| 76 |
return response
|
| 77 |
|
|
|
|
| 78 |
# Function to process uploaded PDF
|
| 79 |
def process_pdf(uploaded_file):
|
| 80 |
"""Handles PDF upload and initializes retrieval system."""
|
|
|
|
| 8 |
from rank_bm25 import BM25Okapi
|
| 9 |
from langchain.llms import OpenAI
|
| 10 |
|
| 11 |
+
openai_api_key = "YOUR_OPENAI_API_KEY"
|
| 12 |
+
|
| 13 |
# Global variables for retrieval
|
| 14 |
pdf_texts = []
|
| 15 |
bm25, index, embed_model, document_embeddings = None, None, None, None
|
|
|
|
| 64 |
final_ranked_indices = [top_docs_indices[i] for i in sub_ranked_indices[0]]
|
| 65 |
return [pdf_texts[i] for i in final_ranked_indices]
|
| 66 |
|
| 67 |
+
|
| 68 |
def generate_response(query):
|
| 69 |
"""Generates responses based on retrieved documents and conversation history."""
|
| 70 |
retrieved_docs = hybrid_search(query)
|
| 71 |
memory_context = memory.load_memory_variables({})
|
| 72 |
|
| 73 |
prompt = f"Conversation history: {memory_context}\nRelevant text: {retrieved_docs}\nGenerate a response:"
|
| 74 |
+
llm = OpenAI(model_name="gpt-4", openai_api_key=openai_api_key)
|
|
|
|
| 75 |
|
| 76 |
+
response = llm(prompt)
|
| 77 |
memory.save_context({"user": query}, {"AI": response})
|
| 78 |
return response
|
| 79 |
|
| 80 |
+
|
| 81 |
# Function to process uploaded PDF
|
| 82 |
def process_pdf(uploaded_file):
|
| 83 |
"""Handles PDF upload and initializes retrieval system."""
|