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
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#list of librarys for requirement.txt
|
| 2 |
+
from langchain.document_loaders import PyPDFLoader
|
| 3 |
+
|
| 4 |
+
# Import embeddings module from langchain for vector representations of text
|
| 5 |
+
from langchain.embeddings import HuggingFaceEmbeddings
|
| 6 |
+
|
| 7 |
+
# Import text splitter for handling large texts
|
| 8 |
+
from langchain.text_splitter import CharacterTextSplitter
|
| 9 |
+
|
| 10 |
+
# Import vector store for database operations
|
| 11 |
+
from langchain.vectorstores import Chroma
|
| 12 |
+
|
| 13 |
+
# for loading of llama gguf model
|
| 14 |
+
from langchain.llms import LlamaCpp
|
| 15 |
+
|
| 16 |
+
from langchain.chains.router.llm_router import LLMRouterChain, RouterOutputParser
|
| 17 |
+
from langchain.chains.router.multi_prompt_prompt import MULTI_PROMPT_ROUTER_TEMPLATE
|
| 18 |
+
|
| 19 |
+
from langchain.chains.router import MultiPromptChain
|
| 20 |
+
from langchain.chains import ConversationChain
|
| 21 |
+
from langchain.chains.llm import LLMChain
|
| 22 |
+
from langchain.prompts import PromptTemplate
|
| 23 |
+
from langchain.memory import ConversationBufferMemory
|
| 24 |
+
from langchain.chains import ConversationalRetrievalChain
|
| 25 |
+
|
| 26 |
+
from langchain.callbacks.manager import CallbackManager
|
| 27 |
+
from langchain.callbacks.streaming_stdout import StreamingStdOutCallbackHandler
|
| 28 |
+
|
| 29 |
+
def pdf_to_vec(filename):
|
| 30 |
+
document = []
|
| 31 |
+
loader = PyPDFLoader(filename)
|
| 32 |
+
document.extend(loader.load()) #which library is this from?
|
| 33 |
+
|
| 34 |
+
# Initialize HuggingFaceEmbeddings with the 'sentence-transformers/all-MiniLM-L6-v2' model for generating text embeddings
|
| 35 |
+
embeddings = HuggingFaceEmbeddings(model_name='sentence-transformers/all-MiniLM-L6-v2')
|
| 36 |
+
|
| 37 |
+
# Initialize a CharacterTextSplitter to split the loaded documents into smaller chunks
|
| 38 |
+
document_splitter = CharacterTextSplitter(separator='\n', chunk_size=500, chunk_overlap=100)
|
| 39 |
+
|
| 40 |
+
# Use the splitter to divide the 'document' content into manageable chunks
|
| 41 |
+
document_chunks = document_splitter.split_documents(document) #which library is this from?
|
| 42 |
+
|
| 43 |
+
# Create a Chroma vector database from the document chunks with the specified embeddings, and set a directory for persistence
|
| 44 |
+
vectordb = Chroma.from_documents(document_chunks, embedding=embeddings, persist_directory='./data') ## change to GUI path
|
| 45 |
+
|
| 46 |
+
# Persist the created vector database to disk in the specified directory
|
| 47 |
+
vectordb.persist() #this is mandatory?
|
| 48 |
+
|
| 49 |
+
return(vectordb)
|
| 50 |
+
#return collection # Return the collection as the asset
|
| 51 |
+
|
| 52 |
+
def load_llm():
|
| 53 |
+
#callback_manager = CallbackManager([StreamingStdOutCallbackHandler()])
|
| 54 |
+
llm = LlamaCpp(
|
| 55 |
+
#streaming = True,
|
| 56 |
+
model_path="/content/llama-2-7b-mini-ibased.Q5_K_M.gguf", #/content/data/llama-2-7b-mcq_2-gguf.gguf. # change to GUI path. llama-2-7b-mini-ibased.Q5_K_M.gguf llama-2-7b-mcq_2.Q5_K_M.gguf
|
| 57 |
+
#n_gpu_layers=-1,
|
| 58 |
+
n_batch=512,
|
| 59 |
+
temperature=0.1,
|
| 60 |
+
top_p=1,
|
| 61 |
+
#verbose=False,
|
| 62 |
+
#callback_manager=callback_manager,
|
| 63 |
+
max_tokens=2000,
|
| 64 |
+
)
|
| 65 |
+
return llm
|
| 66 |
+
|
| 67 |
+
|
| 68 |
+
#step 5, to instantiate once to create default_chain,router_chain,destination_chains into chain and set vectordb. so will not re-create per prompt
|
| 69 |
+
def default_chain():
|
| 70 |
+
|
| 71 |
+
sum_template = """
|
| 72 |
+
As a machine learning education specialist, your expertise is pivotal in deepening the comprehension of complex machine learning concepts for both educators and students.
|
| 73 |
+
|
| 74 |
+
Your role entails:
|
| 75 |
+
|
| 76 |
+
Providing Detailed Explanations: Deliver comprehensive answers to these questions, elucidating the underlying technical principles.
|
| 77 |
+
Assisting in Exam Preparation: Support educators in formulating sophisticated exam and quiz questions, including MCQs, accompanied by thorough explanations.
|
| 78 |
+
Summarizing Course Material: Distill key information from course materials, articulating complex ideas within the context of advanced machine learning practices.
|
| 79 |
+
|
| 80 |
+
Objective: to summarize and explain the key points.
|
| 81 |
+
Here the question:
|
| 82 |
+
{input}"""
|
| 83 |
+
|
| 84 |
+
mcq_template = """
|
| 85 |
+
As a machine learning education specialist, your expertise is pivotal in deepening the comprehension of complex machine learning concepts for both educators and students.
|
| 86 |
+
|
| 87 |
+
Your role entails:
|
| 88 |
+
Crafting Insightful Questions: Develop thought-provoking questions that explore the intricacies of machine learning topics.
|
| 89 |
+
Generating MCQs: Create MCQs for each machine learning topic, comprising a question, four choices (A-D), and the correct answer, along with a rationale explaining the answer.
|
| 90 |
+
|
| 91 |
+
Objective: to create multiple choice question in this format
|
| 92 |
+
[question:
|
| 93 |
+
options A:
|
| 94 |
+
options B:
|
| 95 |
+
options C:
|
| 96 |
+
options D:
|
| 97 |
+
correct_answer:
|
| 98 |
+
explanation:]
|
| 99 |
+
|
| 100 |
+
Here the question:
|
| 101 |
+
{input}"""
|
| 102 |
+
|
| 103 |
+
prompt_infos = [
|
| 104 |
+
{
|
| 105 |
+
"name": "SUMMARIZE",
|
| 106 |
+
"description": "Good for summarizing and explaination ",
|
| 107 |
+
"prompt_template": sum_template,
|
| 108 |
+
},
|
| 109 |
+
{
|
| 110 |
+
"name": "MCQ",
|
| 111 |
+
"description": "Good for creating multiple choices questions",
|
| 112 |
+
"prompt_template": mcq_template,
|
| 113 |
+
},
|
| 114 |
+
]
|
| 115 |
+
|
| 116 |
+
destination_chains = {}
|
| 117 |
+
|
| 118 |
+
for p_info in prompt_infos:
|
| 119 |
+
name = p_info["name"]
|
| 120 |
+
prompt_template = p_info["prompt_template"]
|
| 121 |
+
prompt = PromptTemplate(template=prompt_template, input_variables=["input"])
|
| 122 |
+
chain = LLMChain(llm=llm, prompt=prompt)
|
| 123 |
+
destination_chains[name] = chain
|
| 124 |
+
#default_chain = ConversationChain(llm=llm, output_key="text")
|
| 125 |
+
#memory = ConversationBufferMemory(memory_key='chat_history', return_messages=True)
|
| 126 |
+
|
| 127 |
+
default_chain = ConversationalRetrievalChain.from_llm(llm=llm,
|
| 128 |
+
retriever=vectordb.as_retriever(search_kwargs={'k': 3}),
|
| 129 |
+
verbose=True, output_key="text" )
|
| 130 |
+
|
| 131 |
+
destinations = [f"{p['name']}: {p['description']}" for p in prompt_infos]
|
| 132 |
+
destinations_str = "\n".join(destinations)
|
| 133 |
+
router_template = MULTI_PROMPT_ROUTER_TEMPLATE.format(destinations=destinations_str)
|
| 134 |
+
router_prompt = PromptTemplate(
|
| 135 |
+
template=router_template,
|
| 136 |
+
input_variables=["input"],
|
| 137 |
+
output_parser=RouterOutputParser(),
|
| 138 |
+
)
|
| 139 |
+
router_chain = LLMRouterChain.from_llm(llm, router_prompt)
|
| 140 |
+
|
| 141 |
+
return default_chain,router_chain,destination_chains
|
| 142 |
+
|
| 143 |
+
def llm_infer(default_chain,router_chain,destination_chains,prompt):
|
| 144 |
+
|
| 145 |
+
chain = MultiPromptChain(
|
| 146 |
+
router_chain=router_chain,
|
| 147 |
+
destination_chains=destination_chains,
|
| 148 |
+
default_chain=default_chain,
|
| 149 |
+
#memory=ConversationBufferMemory(k=2), # memory_key='chat_history', return_messages=True
|
| 150 |
+
verbose=True,
|
| 151 |
+
)
|
| 152 |
+
response = chain.run(prompt)
|
| 153 |
+
|
| 154 |
+
return response
|
requirements.txt
CHANGED
|
@@ -14,4 +14,8 @@ bcrypt==4.1.* # For hashing secrets
|
|
| 14 |
opencv-python-headless==4.5.5.* # For image handling images
|
| 15 |
tensorflow # Tensorflow is needed by MTCNN for facial recognition
|
| 16 |
scipy # The scipy is required for keras-facenet
|
| 17 |
-
tinydb # The in memory database for storing JWT tokens
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
opencv-python-headless==4.5.5.* # For image handling images
|
| 15 |
tensorflow # Tensorflow is needed by MTCNN for facial recognition
|
| 16 |
scipy # The scipy is required for keras-facenet
|
| 17 |
+
tinydb # The in memory database for storing JWT tokens
|
| 18 |
+
langchain # Langgchain for RAG
|
| 19 |
+
llama-cpp-python # To load the model
|
| 20 |
+
sentence-transformers # For text embeddings
|
| 21 |
+
pypdf # Handling PDF files
|
static/.DS_Store
CHANGED
|
Binary files a/static/.DS_Store and b/static/.DS_Store differ
|
|
|