from langchain.document_loaders import DirectoryLoader
from langchain.text_splitter import CharacterTextSplitter
import os
import pinecone 
from langchain.vectorstores import Pinecone
from langchain.embeddings.openai import OpenAIEmbeddings
from langchain.chains import RetrievalQA
from langchain.chat_models import ChatOpenAI
import streamlit as st
from dotenv import load_dotenv

load_dotenv()


PINECONE_API_KEY = os.getenv('PINECONE_API_KEY')
PINECONE_ENV = os.getenv('PINECONE_ENV')
OPENAI_API_KEY = os.getenv('OPENAI_API_KEY')

os.environ['OPENAI_API_KEY'] = OPENAI_API_KEY


def doc_preprocessing():
    loader = DirectoryLoader(
        'data/',
        glob='**/*.pdf',     # only the PDFs
        show_progress=True
    )
    docs = loader.load()
    text_splitter = CharacterTextSplitter(
        chunk_size=1000, 
        chunk_overlap=0
    )
    docs_split = text_splitter.split_documents(docs)
    return docs_split

@st.cache_resource
def embedding_db():
    # we use the openAI embedding model
    embeddings = OpenAIEmbeddings()
    pinecone.init(
        api_key=PINECONE_API_KEY,
        environment=PINECONE_ENV
    )
    docs_split = doc_preprocessing()
    doc_db = Pinecone.from_documents(
        docs_split, 
        embeddings, 
        index_name='langchain-demo-indexes'
    )
    return doc_db

llm = ChatOpenAI()
doc_db = embedding_db()

def retrieval_answer(query):
    qa = RetrievalQA.from_chain_type(
    llm=llm, 
    chain_type='stuff',
    retriever=doc_db.as_retriever(),
    )
    query = query
    result = qa.run(query)
    return result

def main():
    st.title("Question and Answering App powered by LLM and Pinecone")

    text_input = st.text_input("Ask your query...") 
    if st.button("Ask Query"):
        if len(text_input)>0:
            st.info("Your Query: " + text_input)
            answer = retrieval_answer(text_input)
            st.success(answer)

if __name__ == "__main__":
    main()

    








langchain.text_splitter.CharacterTextSplitter langchain.document_loaders.DirectoryLoader langchain.vectorstores.Pinecone.from_documents langchain.chat_models.ChatOpenAI langchain.embeddings.openai.OpenAIEmbeddings
[((393, 406), 'dotenv.load_dotenv', 'load_dotenv', ([], {}), '()\n', (404, 406), False, 'from dotenv import load_dotenv\n'), ((431, 460), 'os.getenv', 'os.getenv', (['"""PINECONE_API_KEY"""'], {}), "('PINECONE_API_KEY')\n", (440, 460), False, 'import os\n'), ((477, 502), 'os.getenv', 'os.getenv', (['"""PINECONE_ENV"""'], {}), "('PINECONE_ENV')\n", (486, 502), False, 'import os\n'), ((521, 548), 'os.getenv', 'os.getenv', (['"""OPENAI_API_KEY"""'], {}), "('OPENAI_API_KEY')\n", (530, 548), False, 'import os\n'), ((1382, 1394), 'langchain.chat_models.ChatOpenAI', 'ChatOpenAI', ([], {}), '()\n', (1392, 1394), False, 'from langchain.chat_models import ChatOpenAI\n'), ((642, 703), 'langchain.document_loaders.DirectoryLoader', 'DirectoryLoader', (['"""data/"""'], {'glob': '"""**/*.pdf"""', 'show_progress': '(True)'}), "('data/', glob='**/*.pdf', show_progress=True)\n", (657, 703), False, 'from langchain.document_loaders import DirectoryLoader\n'), ((805, 860), 'langchain.text_splitter.CharacterTextSplitter', 'CharacterTextSplitter', ([], {'chunk_size': '(1000)', 'chunk_overlap': '(0)'}), '(chunk_size=1000, chunk_overlap=0)\n', (826, 860), False, 'from langchain.text_splitter import CharacterTextSplitter\n'), ((1066, 1084), 'langchain.embeddings.openai.OpenAIEmbeddings', 'OpenAIEmbeddings', ([], {}), '()\n', (1082, 1084), False, 'from langchain.embeddings.openai import OpenAIEmbeddings\n'), ((1090, 1155), 'pinecone.init', 'pinecone.init', ([], {'api_key': 'PINECONE_API_KEY', 'environment': 'PINECONE_ENV'}), '(api_key=PINECONE_API_KEY, environment=PINECONE_ENV)\n', (1103, 1155), False, 'import pinecone\n'), ((1233, 1322), 'langchain.vectorstores.Pinecone.from_documents', 'Pinecone.from_documents', (['docs_split', 'embeddings'], {'index_name': '"""langchain-demo-indexes"""'}), "(docs_split, embeddings, index_name=\n    'langchain-demo-indexes')\n", (1256, 1322), False, 'from langchain.vectorstores import Pinecone\n'), ((1662, 1728), 'streamlit.title', 'st.title', (['"""Question and Answering App powered by LLM and Pinecone"""'], {}), "('Question and Answering App powered by LLM and Pinecone')\n", (1670, 1728), True, 'import streamlit as st\n'), ((1749, 1783), 'streamlit.text_input', 'st.text_input', (['"""Ask your query..."""'], {}), "('Ask your query...')\n", (1762, 1783), True, 'import streamlit as st\n'), ((1793, 1815), 'streamlit.button', 'st.button', (['"""Ask Query"""'], {}), "('Ask Query')\n", (1802, 1815), True, 'import streamlit as st\n'), ((1861, 1897), 'streamlit.info', 'st.info', (["('Your Query: ' + text_input)"], {}), "('Your Query: ' + text_input)\n", (1868, 1897), True, 'import streamlit as st\n'), ((1962, 1980), 'streamlit.success', 'st.success', (['answer'], {}), '(answer)\n', (1972, 1980), True, 'import streamlit as st\n')]