|
import os |
|
from dotenv import load_dotenv |
|
from langchain.llms import GooglePalm |
|
from langchain.chains import RetrievalQA |
|
from langchain.vectorstores import FAISS |
|
from langchain.prompts import PromptTemplate |
|
|
|
load_dotenv() |
|
|
|
|
|
llm = GooglePalm(google_api_key=os.environ["GOOGLE_PALM_API"], temperature=0.1) |
|
vectordb_file_path = "faiss_index_V2" |
|
|
|
def get_qa_chain(embeddings): |
|
|
|
vectordb = FAISS.load_local(vectordb_file_path, embeddings) |
|
|
|
|
|
retriever = vectordb.as_retriever(score_threshold=0.7) |
|
|
|
prompt_template = """Given the following context and a question, generate an answer based on this context only. |
|
In the answer try to provide as much text as possible from the source document context without making much changes. |
|
If the answer is not found in the context, kindly state "I don't know." Don't try to make up an answer. |
|
|
|
CONTEXT: {context} |
|
|
|
QUESTION: {question}""" |
|
|
|
PROMPT = PromptTemplate( |
|
template=prompt_template, input_variables=["context", "question"] |
|
) |
|
|
|
chain = RetrievalQA.from_chain_type(llm=llm, |
|
chain_type="stuff", |
|
retriever=retriever, |
|
input_key="query", |
|
return_source_documents=True, |
|
chain_type_kwargs={"prompt": PROMPT}) |
|
|
|
return chain |