import os
import openai
import pinecone
from langchain.document_loaders import DirectoryLoader
from langchain.text_splitter import RecursiveCharacterTextSplitter
from langchain.embeddings.openai import OpenAIEmbeddings
from langchain.vectorstores import Pinecone
from langchain.llms import OpenAI
from langchain.chat_models import ChatOpenAI
from langchain.chains.question_answering import load_qa_chain
from dotenv import load_dotenv
from langchain.chains import ConversationalRetrievalChain
from langchain.memory import ConversationBufferMemory

#loading environment variables
load_dotenv()
OPENAI_API_KEY= os.getenv('OPENAI_API_KEY')
PINECONE_API_KEY = os.getenv('PINECONE_API_KEY')
PINECONE_ENV = os.getenv('PINECONE_ENV')

#loading data
directory = 'Data'
def load_docs(directory):
  loader = DirectoryLoader(directory)
  documents = loader.load()
  return documents
documents = load_docs(directory)
#print(len(documents))


def split_docs(documents, chunk_size=1500, chunk_overlap=75):
  text_splitter = RecursiveCharacterTextSplitter(chunk_size=chunk_size, chunk_overlap=chunk_overlap)
  docs = text_splitter.split_documents(documents)
  return docs
docs = split_docs(documents)
# print(len(docs))


embeddings = OpenAIEmbeddings(model ="text-embedding-ada-002")
# text-embedding-ada-002 is getting better values than ada

# creating pinecone index 
pinecone.init(
    api_key= PINECONE_API_KEY,
    environment=PINECONE_ENV
)
index_name = "llmchatbot"
index = Pinecone.from_documents(docs, embeddings, index_name=index_name)

#gives out 4 similar documents by doing semantic search of vector database 
def get_similiar_docs(query, k=4, score=False):
  if score:
    similar_docs = index.similarity_search_with_score(query, k=k)
  else:
    similar_docs = index.similarity_search(query, k=k)
  return similar_docs

memory = ConversationBufferMemory(memory_key="chat_history", return_messages=True)
qa = ConversationalRetrievalChain.from_llm(OpenAI(model ="text-davinci-003", temperature = 0), index.as_retriever(), memory = memory)


#chainlit 
import chainlit as cl
from chainlit import langchain_factory
from chainlit import AskUserMessage, Message, on_chat_start
from chainlit import on_message
from chainlit import user_session

@langchain_factory(use_async=True)
def model():
   qa = ConversationalRetrievalChain.from_llm(OpenAI(model ="text-davinci-003", temperature = 0), index.as_retriever(), memory = memory)
   return qa

@on_chat_start
async def main():
    await Message( content= 'Hello! How can I help you?').send()

langchain.document_loaders.DirectoryLoader langchain.text_splitter.RecursiveCharacterTextSplitter langchain.memory.ConversationBufferMemory langchain.vectorstores.Pinecone.from_documents langchain.llms.OpenAI langchain.embeddings.openai.OpenAIEmbeddings
[((579, 592), 'dotenv.load_dotenv', 'load_dotenv', ([], {}), '()\n', (590, 592), False, 'from dotenv import load_dotenv\n'), ((609, 636), 'os.getenv', 'os.getenv', (['"""OPENAI_API_KEY"""'], {}), "('OPENAI_API_KEY')\n", (618, 636), False, 'import os\n'), ((656, 685), 'os.getenv', 'os.getenv', (['"""PINECONE_API_KEY"""'], {}), "('PINECONE_API_KEY')\n", (665, 685), False, 'import os\n'), ((701, 726), 'os.getenv', 'os.getenv', (['"""PINECONE_ENV"""'], {}), "('PINECONE_ENV')\n", (710, 726), False, 'import os\n'), ((1220, 1268), 'langchain.embeddings.openai.OpenAIEmbeddings', 'OpenAIEmbeddings', ([], {'model': '"""text-embedding-ada-002"""'}), "(model='text-embedding-ada-002')\n", (1236, 1268), False, 'from langchain.embeddings.openai import OpenAIEmbeddings\n'), ((1357, 1422), 'pinecone.init', 'pinecone.init', ([], {'api_key': 'PINECONE_API_KEY', 'environment': 'PINECONE_ENV'}), '(api_key=PINECONE_API_KEY, environment=PINECONE_ENV)\n', (1370, 1422), False, 'import pinecone\n'), ((1468, 1532), 'langchain.vectorstores.Pinecone.from_documents', 'Pinecone.from_documents', (['docs', 'embeddings'], {'index_name': 'index_name'}), '(docs, embeddings, index_name=index_name)\n', (1491, 1532), False, 'from langchain.vectorstores import Pinecone\n'), ((1831, 1904), 'langchain.memory.ConversationBufferMemory', 'ConversationBufferMemory', ([], {'memory_key': '"""chat_history"""', 'return_messages': '(True)'}), "(memory_key='chat_history', return_messages=True)\n", (1855, 1904), False, 'from langchain.memory import ConversationBufferMemory\n'), ((2241, 2274), 'chainlit.langchain_factory', 'langchain_factory', ([], {'use_async': '(True)'}), '(use_async=True)\n', (2258, 2274), False, 'from chainlit import langchain_factory\n'), ((798, 824), 'langchain.document_loaders.DirectoryLoader', 'DirectoryLoader', (['directory'], {}), '(directory)\n', (813, 824), False, 'from langchain.document_loaders import DirectoryLoader\n'), ((1010, 1097), 'langchain.text_splitter.RecursiveCharacterTextSplitter', 'RecursiveCharacterTextSplitter', ([], {'chunk_size': 'chunk_size', 'chunk_overlap': 'chunk_overlap'}), '(chunk_size=chunk_size, chunk_overlap=\n    chunk_overlap)\n', (1040, 1097), False, 'from langchain.text_splitter import RecursiveCharacterTextSplitter\n'), ((1948, 1995), 'langchain.llms.OpenAI', 'OpenAI', ([], {'model': '"""text-davinci-003"""', 'temperature': '(0)'}), "(model='text-davinci-003', temperature=0)\n", (1954, 1995), False, 'from langchain.llms import OpenAI\n'), ((2334, 2381), 'langchain.llms.OpenAI', 'OpenAI', ([], {'model': '"""text-davinci-003"""', 'temperature': '(0)'}), "(model='text-davinci-003', temperature=0)\n", (2340, 2381), False, 'from langchain.llms import OpenAI\n'), ((2482, 2527), 'chainlit.Message', 'Message', ([], {'content': '"""Hello! How can I help you?"""'}), "(content='Hello! How can I help you?')\n", (2489, 2527), False, 'from chainlit import AskUserMessage, Message, on_chat_start\n')]