# Import Langchain dependencies
from langchain.document_loaders import PyPDFLoader
from langchain.indexes import VectorstoreIndexCreator
from langchain.chains import RetrievalQA
from langchain.embeddings import HuggingFaceEmbeddings
from langchain.text_splitter import RecursiveCharacterTextSplitter
# Bring in streamlit for UI dev
import streamlit as st
# Bring in watsonx interface
from watsonxlangchain import LangChainInterface

# Setup credentials dictionary
creds = {
	"apiKey": "",
	"url": "",
}

# Create LLM using langchain
llm = LangChainInterface(
	credentials = creds,
	model = "meta-llama/llama-2-70b-chat",
	params = {
		"decoding_method": "sample",
		"max_new_tokens": 200,
		"temperature": 0.5
	},
	project_id=""
)

# This function loads a PDF of your choosing
@st.cache_resource
def load_pdf():
	# Update PDF name here to whatever you like
	pdf_name= "what is generative ai.pdf"
	loaders = [PyPDFLoader(pdf_name)]
	# Create index - aka vector database - aka chromadb
	index = VectorstoreIndexCreator(
		embedding = HuggingFaceEmbeddings(model_name="all-MiniLM-L12-v2"),
		text_splitter=RecursiveCharacterTextSplitter(chunk_size=100, chunk_overlap=0)
	).from_loaders(loaders)
	# Return the vector database
	return index	

# Load er on up
index = load_pdf()

# Create a Q&A chain
chain = RetrievalQA.from_chain_type(
	llm=llm,
	chain_type="stuff",
	retriever=index.vectorstore.as_retriever()
)

# Setup the app title
st.title("Ask watsonx")

# Setup a session state message variable to hild all the old messages
if "messages" not in st.session_state:
	st.session_state.messages = []

# Display all the historical messages
for message in st.session_state.messages:
	st.chat_message(message["role"])

# Build a prompt input template to display the prompts
prompt = st.chat_input("Pass Your Prompt Here")

# If the user hits enter then
if prompt:
	# Display the prompt
	st.chat_message("user").markdown(prompt)
	# Store the user prompt in state
	st.session_state.messages.append({"role":"user", "content":prompt})
	# Send the prompt to the llm
	st.chat_message("assistant").markdown(response)
	# Store the LLM response in state
	st.session_state.messages.append(
		{"role": "assistant", "content": response}
	)

langchain.document_loaders.PyPDFLoader langchain.embeddings.HuggingFaceEmbeddings langchain.text_splitter.RecursiveCharacterTextSplitter
[((539, 717), 'watsonxlangchain.LangChainInterface', 'LangChainInterface', ([], {'credentials': 'creds', 'model': '"""meta-llama/llama-2-70b-chat"""', 'params': "{'decoding_method': 'sample', 'max_new_tokens': 200, 'temperature': 0.5}", 'project_id': '""""""'}), "(credentials=creds, model='meta-llama/llama-2-70b-chat',\n    params={'decoding_method': 'sample', 'max_new_tokens': 200,\n    'temperature': 0.5}, project_id='')\n", (557, 717), False, 'from watsonxlangchain import LangChainInterface\n'), ((1432, 1455), 'streamlit.title', 'st.title', (['"""Ask watsonx"""'], {}), "('Ask watsonx')\n", (1440, 1455), True, 'import streamlit as st\n'), ((1778, 1816), 'streamlit.chat_input', 'st.chat_input', (['"""Pass Your Prompt Here"""'], {}), "('Pass Your Prompt Here')\n", (1791, 1816), True, 'import streamlit as st\n'), ((1680, 1712), 'streamlit.chat_message', 'st.chat_message', (["message['role']"], {}), "(message['role'])\n", (1695, 1712), True, 'import streamlit as st\n'), ((1958, 2027), 'streamlit.session_state.messages.append', 'st.session_state.messages.append', (["{'role': 'user', 'content': prompt}"], {}), "({'role': 'user', 'content': prompt})\n", (1990, 2027), True, 'import streamlit as st\n'), ((2141, 2217), 'streamlit.session_state.messages.append', 'st.session_state.messages.append', (["{'role': 'assistant', 'content': response}"], {}), "({'role': 'assistant', 'content': response})\n", (2173, 2217), True, 'import streamlit as st\n'), ((908, 929), 'langchain.document_loaders.PyPDFLoader', 'PyPDFLoader', (['pdf_name'], {}), '(pdf_name)\n', (919, 929), False, 'from langchain.document_loaders import PyPDFLoader\n'), ((1882, 1905), 'streamlit.chat_message', 'st.chat_message', (['"""user"""'], {}), "('user')\n", (1897, 1905), True, 'import streamlit as st\n'), ((2057, 2085), 'streamlit.chat_message', 'st.chat_message', (['"""assistant"""'], {}), "('assistant')\n", (2072, 2085), True, 'import streamlit as st\n'), ((1032, 1085), 'langchain.embeddings.HuggingFaceEmbeddings', 'HuggingFaceEmbeddings', ([], {'model_name': '"""all-MiniLM-L12-v2"""'}), "(model_name='all-MiniLM-L12-v2')\n", (1053, 1085), False, 'from langchain.embeddings import HuggingFaceEmbeddings\n'), ((1103, 1166), 'langchain.text_splitter.RecursiveCharacterTextSplitter', 'RecursiveCharacterTextSplitter', ([], {'chunk_size': '(100)', 'chunk_overlap': '(0)'}), '(chunk_size=100, chunk_overlap=0)\n', (1133, 1166), False, 'from langchain.text_splitter import RecursiveCharacterTextSplitter\n')]