import os import sys sys.path.append('../..') #langchain !pip install langchain from langchain.text_splitter import RecursiveCharacterTextSplitter, CharacterTextSplitter from langchain.embeddings import HuggingFaceEmbeddings from langchain.prompts import PromptTemplate from langchain.chains import RetrievalQA from langchain.prompts import ChatPromptTemplate from langchain.schema import StrOutputParser from langchain.schema.runnable import Runnable from langchain.schema.runnable.config import RunnableConfig from langchain.chains import ( LLMChain, ConversationalRetrievalChain) from langchain.vectorstores import Chroma from langchain.memory import ConversationBufferMemory from langchain.chains import LLMChain from langchain.prompts.prompt import PromptTemplate from langchain.prompts.chat import ChatPromptTemplate, SystemMessagePromptTemplate from langchain.prompts import SystemMessagePromptTemplate, HumanMessagePromptTemplate, ChatPromptTemplate, MessagesPlaceholder from langchain.document_loaders import PyPDFDirectoryLoader from langchain_community.llms import HuggingFaceHub from pydantic import BaseModel import shutil loader = PyPDFDirectoryLoader('pdfs') data=loader.load() # split documents text_splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=150) docs = text_splitter.split_documents(data) # define embedding embeddings = HuggingFaceEmbeddings(model_name='sentence-transformers/all-MiniLM-l6-v2') # create vector database from data persist_directory = 'docs/chroma/' # Remove old database files if any shutil.rmtree(persist_directory, ignore_errors=True) vectordb = Chroma.from_documents( documents=docs, embedding=embeddings, persist_directory=persist_directory ) # define retriever retriever = vectordb.as_retriever(search_type="mmr") template = """Your name is AngryGreta and you are a recycling chatbot created to help people. Use the following pieces of context to answer the question at the end. Answer in the same language of the question. Keep the answer as concise as possible. Always say "thanks for asking!" at the end of the answer. CONTEXT: {context} CHAT HISTORY: {chat_history} Question: {question} Helpful Answer:""" # Create the chat prompt templates system_prompt = SystemMessagePromptTemplate.from_template(template) qa_prompt = ChatPromptTemplate( messages=[ system_prompt, MessagesPlaceholder(variable_name="chat_history"), HumanMessagePromptTemplate.from_template("{question}") ] ) llm = HuggingFaceHub( repo_id="mistralai/Mixtral-8x7B-Instruct-v0.1", task="text-generation", model_kwargs={ "max_new_tokens": 512, "top_k": 30, "temperature": 0.1, "repetition_penalty": 1.03, }, ) llm_chain = LLMChain(llm=llm, prompt=qa_prompt) memory = ConversationBufferMemory(llm=llm, memory_key="chat_history", output_key='answer', return_messages=True) qa_chain = ConversationalRetrievalChain.from_llm( llm = llm, memory = memory, retriever = retriever, verbose = True, combine_docs_chain_kwargs={'prompt': qa_prompt}, get_chat_history = lambda h : h )