-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQA.py
More file actions
46 lines (35 loc) · 1.13 KB
/
QA.py
File metadata and controls
46 lines (35 loc) · 1.13 KB
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
# QA.py
import os
import google.generativeai as palm
from Vector import load_vector_storage, create_vector_storage
# Load environment variables from .env file
from dotenv import load_dotenv
load_dotenv()
def answer_question(pdf_path, question):
# Load or create vector storage
model_name = "all-MiniLM-L6-v2"
if not os.path.exists("faiss_index"):
create_vector_storage(pdf_path, model_name)
db = load_vector_storage(model_name)
# Configure Google API
google_api_key = os.getenv('GOOGLE_API_KEY')
palm.configure(api_key=google_api_key)
# Define the query
query = question
# Integrate retriever
retriever = db.as_retriever()
try:
docs = retriever.invoke(query)
if docs:
print("Documents retrieved successfully")
else:
print("No documents retrieved for the query.")
except Exception as e:
print("Error occurred while retrieving documents:", e)
# Generate the answer
completion = palm.generate_text(
model='models/text-bison-001',
prompt=query,
temperature=0.1
)
return completion.result