Commit
•
0d2c2fd
1
Parent(s):
f15d324
Updated Model Class Initialisation (#2)
Browse files- Updated Model Class Initialisation (12af78aec696bbe5358416cda729e054ada9ff3c)
Co-authored-by: Meggison <Oritsemisan@users.noreply.huggingface.co>
- model_pipelineV2.py +41 -11
model_pipelineV2.py
CHANGED
@@ -13,23 +13,53 @@ from langchain_core.runnables import RunnableLambda, RunnablePassthrough
|
|
13 |
from operator import itemgetter
|
14 |
from langchain_text_splitters import RecursiveCharacterTextSplitter
|
15 |
|
16 |
-
class
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
17 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
18 |
DEFAULT_DOCUMENT_PROMPT = PromptTemplate.from_template(template="{page_content}")
|
19 |
def __init__(self):
|
20 |
self.curr_dir = os.path.dirname(__file__)
|
21 |
-
self.knowledge_dir =
|
22 |
-
|
23 |
-
self.prompt_dir = 'prompts'
|
24 |
self.child_splitter = RecursiveCharacterTextSplitter(chunk_size=200)
|
25 |
self.parent_splitter = RecursiveCharacterTextSplitter(chunk_size=500)
|
26 |
-
self.
|
27 |
-
self.vectorstore, self.store =
|
28 |
-
self.
|
29 |
-
self.llm =
|
30 |
-
self.memory = ConversationBufferMemory(return_messages=True,
|
31 |
-
|
32 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
33 |
|
34 |
def get_prompts(self, system_file_path='system_prompt_template.txt',
|
35 |
condense_file_path='condense_question_prompt_template.txt'):
|
|
|
13 |
from operator import itemgetter
|
14 |
from langchain_text_splitters import RecursiveCharacterTextSplitter
|
15 |
|
16 |
+
class VectorStoreSingleton:
|
17 |
+
_instance = None
|
18 |
+
|
19 |
+
@classmethod
|
20 |
+
def get_instance(cls):
|
21 |
+
if cls._instance is None:
|
22 |
+
cls._instance = create_vectorstore() # Your existing function to create the vectorstore
|
23 |
+
return cls._instance
|
24 |
+
|
25 |
+
class LanguageModelSingleton:
|
26 |
+
_instance = None
|
27 |
|
28 |
+
@classmethod
|
29 |
+
def get_instance(cls):
|
30 |
+
if cls._instance is None:
|
31 |
+
cls._instance = load_llm() # Your existing function to load the LLM
|
32 |
+
return cls._instance
|
33 |
+
|
34 |
+
|
35 |
+
class ModelPipeLine:
|
36 |
DEFAULT_DOCUMENT_PROMPT = PromptTemplate.from_template(template="{page_content}")
|
37 |
def __init__(self):
|
38 |
self.curr_dir = os.path.dirname(__file__)
|
39 |
+
self.knowledge_dir = os.path.join(os.path.dirname(os.path.dirname(self.curr_dir)), 'data', 'Knowledge_base')
|
40 |
+
self.prompt_dir = os.path.join(os.path.dirname(os.path.dirname(self.curr_dir)), 'src', 'prompts')
|
|
|
41 |
self.child_splitter = RecursiveCharacterTextSplitter(chunk_size=200)
|
42 |
self.parent_splitter = RecursiveCharacterTextSplitter(chunk_size=500)
|
43 |
+
self._documents = None # Initialize as None for lazy loading
|
44 |
+
self.vectorstore, self.store = VectorStoreSingleton.get_instance()
|
45 |
+
self._retriever = None # Corrected: Initialize _retriever as None for lazy loading
|
46 |
+
self.llm = LanguageModelSingleton.get_instance()
|
47 |
+
self.memory = ConversationBufferMemory(return_messages=True, output_key="answer", input_key="question")
|
48 |
+
|
49 |
+
@property
|
50 |
+
def documents(self):
|
51 |
+
if self._documents is None:
|
52 |
+
self._documents = process_pdf_document([
|
53 |
+
os.path.join(self.knowledge_dir, 'depression_1.pdf'),
|
54 |
+
os.path.join(self.knowledge_dir, 'depression_2.pdf')
|
55 |
+
])
|
56 |
+
return self._documents
|
57 |
+
|
58 |
+
@property
|
59 |
+
def retriever(self):
|
60 |
+
if self._retriever is None:
|
61 |
+
self._retriever = rag_retriever(self.vectorstore, self.store, self.documents, self.parent_splitter, self.child_splitter)
|
62 |
+
return self._retriever
|
63 |
|
64 |
def get_prompts(self, system_file_path='system_prompt_template.txt',
|
65 |
condense_file_path='condense_question_prompt_template.txt'):
|