import langchain
from langchain.llms import GooglePalm
from langchain.document_loaders import CSVLoader
from langchain.embeddings import HuggingFaceInstructEmbeddings
from langchain.vectorstores import FAISS
from langchain.prompts import PromptTemplate
from langchain.chains import RetrievalQA
import os
from dotenv import load_dotenv
load_dotenv()

import streamlit as st


# vectordb_file_path = "C:/Users/shant_w5mrdz3/OneDrive/Desktop/Langchain_examples/palm/CSV_Palm_Q_A/FAISS_index/index.faiss"
# def create_vector_db():
#   loader = CSVLoader("C:/Users/shant_w5mrdz3/OneDrive/Desktop/Langchain_examples/palm/News_Finance_query_langchain/dataset_sample/codebasics_faqs.csv",source_column="prompt")
#    data = loader.load()
#    vectordb = FAISS.from_documents(documents=data,embedding=embeddings)
#   vectordb.save_local(vectordb_file_path)

@st.cache_resource
def qa_chain():
    llm = GooglePalm(google_api_key=os.environ["GOOGLE_API_KEY"], temperature=0.7)
    embeddings = HuggingFaceInstructEmbeddings()
    loader = CSVLoader("C:/Users/shant_w5mrdz3/OneDrive/Desktop/Langchain_examples/palm/News_Finance_query_langchain/dataset_sample/codebasics_faqs.csv",source_column="prompt")
    data = loader.load()
    vectordb = FAISS.from_documents(documents=data, embedding=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 answer from context only. 
                    In the answer try to provide as much text as possible from "response" from the source document. 
                    If the answer is not found in the context, kindly say "I dont know" . Dont try to make up 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
if __name__ == "__main__":
    chain = qa_chain()
    print(chain("do you have a policy refund?"))






langchain.llms.GooglePalm langchain.chains.RetrievalQA.from_chain_type langchain.vectorstores.FAISS.from_documents langchain.document_loaders.CSVLoader langchain.embeddings.HuggingFaceInstructEmbeddings langchain.prompts.PromptTemplate
[((344, 357), 'dotenv.load_dotenv', 'load_dotenv', ([], {}), '()\n', (355, 357), False, 'from dotenv import load_dotenv\n'), ((917, 989), 'langchain.llms.GooglePalm', 'GooglePalm', ([], {'google_api_key': "os.environ['GOOGLE_API_KEY']", 'temperature': '(0.7)'}), "(google_api_key=os.environ['GOOGLE_API_KEY'], temperature=0.7)\n", (927, 989), False, 'from langchain.llms import GooglePalm\n'), ((1008, 1039), 'langchain.embeddings.HuggingFaceInstructEmbeddings', 'HuggingFaceInstructEmbeddings', ([], {}), '()\n', (1037, 1039), False, 'from langchain.embeddings import HuggingFaceInstructEmbeddings\n'), ((1054, 1228), 'langchain.document_loaders.CSVLoader', 'CSVLoader', (['"""C:/Users/shant_w5mrdz3/OneDrive/Desktop/Langchain_examples/palm/News_Finance_query_langchain/dataset_sample/codebasics_faqs.csv"""'], {'source_column': '"""prompt"""'}), "(\n    'C:/Users/shant_w5mrdz3/OneDrive/Desktop/Langchain_examples/palm/News_Finance_query_langchain/dataset_sample/codebasics_faqs.csv'\n    , source_column='prompt')\n", (1063, 1228), False, 'from langchain.document_loaders import CSVLoader\n'), ((1260, 1318), 'langchain.vectorstores.FAISS.from_documents', 'FAISS.from_documents', ([], {'documents': 'data', 'embedding': 'embeddings'}), '(documents=data, embedding=embeddings)\n', (1280, 1318), False, 'from langchain.vectorstores import FAISS\n'), ((1860, 1945), 'langchain.prompts.PromptTemplate', 'PromptTemplate', ([], {'template': 'prompt_template', 'input_variables': "['context', 'question']"}), "(template=prompt_template, input_variables=['context',\n    'question'])\n", (1874, 1945), False, 'from langchain.prompts import PromptTemplate\n'), ((1955, 2128), 'langchain.chains.RetrievalQA.from_chain_type', 'RetrievalQA.from_chain_type', ([], {'llm': 'llm', 'chain_type': '"""stuff"""', 'retriever': 'retriever', 'input_key': '"""query"""', 'return_source_documents': '(True)', 'chain_type_kwargs': "{'prompt': PROMPT}"}), "(llm=llm, chain_type='stuff', retriever=\n    retriever, input_key='query', return_source_documents=True,\n    chain_type_kwargs={'prompt': PROMPT})\n", (1982, 2128), False, 'from langchain.chains import RetrievalQA\n')]