!pip install -qU langchain_experimental langchain_community langchain fastembed pypdf langchain-groq chromadb %pip install --upgrade --quiet langchain langchain-community %pip install langchain_openai from langchain.document_loaders import PyPDFLoader from langchain.text_splitter import RecursiveCharacterTextSplitter loader = PyPDFLoader("hesc103.pdf") documents = loader.load() from langchain.text_splitter import RecursiveCharacterTextSplitter text_splitter = RecursiveCharacterTextSplitter( chunk_size=1000, chunk_overlap=100, length_function=len, is_separator_regex=False ) naive_chunks = text_splitter.split_documents(documents) from langchain_community.embeddings.fastembed import FastEmbedEmbeddings embed_model = FastEmbedEmbeddings(model_name="BAAI/bge-base-en-v1.5") from google.colab import userdata from langchain_groq import ChatGroq groq_api_key = "key" from langchain_community.vectorstores import Chroma naive_chunk_vectorstore = Chroma.from_documents(naive_chunks, embedding=embed_model) from langchain_core.prompts import ChatPromptTemplate rag_template = """\ Answer the question based only on the following Context: {context} Question: {question} """ rag_prompt = ChatPromptTemplate.from_template(rag_template) from langchain.chains import ConversationChain # from langchain.llms import OpenAI from langchain.memory import ConversationBufferMemory chat_model = ChatGroq(temperature=0, model_name="mixtral-8x7b-32768", api_key="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",) from langchain_core.runnables import RunnablePassthrough from langchain_core.output_parsers import StrOutputParser naive_rag_chain = ( {"context" : naive_chunk_retriever, "question" : RunnablePassthrough()} | rag_prompt | chat_model | StrOutputParser() ) memory = ConversationBufferMemory() conversation = ConversationChain(llm=naive_rag_chain, memory=memory, verbose=True) while True: user_input = input("You: ") if user_input.lower() == "exit": break response = conversation.predict(input=user_input) print("AI: ", response)