-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackup1_preClasses_bitMixed.py
More file actions
374 lines (262 loc) · 9.95 KB
/
backup1_preClasses_bitMixed.py
File metadata and controls
374 lines (262 loc) · 9.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
# {
# Using VirtualchatbotTest memory
# (venv) python3 -m venv chatbotTest
# (venv) source chatbotTest/bin/activate
# (chatbotTest) (venv)
# }
from Retrieval import Retriver
from llm import llm
from chain import chain
import json
from langchain_community.document_loaders import WebBaseLoader
from langchain_community.document_loaders import TextLoader
from langchain_community.document_loaders import PyPDFLoader
from langchain_text_splitters import RecursiveCharacterTextSplitter
import pdfplumber
from langchain.schema import Document
from langchain_core.output_parsers import StrOutputParser
from langchain_core.prompts import ChatPromptTemplate
from langchain_core.runnables import RunnablePassthrough
from langchain_chroma import Chroma
from langchain_ollama import OllamaEmbeddings
from langchain_ollama import ChatOllama
from langchain_ollama import OllamaLLM
###
########## DATA LOADING START ########
###
### Web Connection ###
# loaderWeb = WebBaseLoader("https://bedtimestorieskd.com/small-bear//")
# dataWeb = loaderWeb.load()
# Load a text file hebrew
def extract_hebrew_text(pdf_path):
documents = []
with pdfplumber.open(pdf_path) as pdf:
for i, page in enumerate(pdf.pages):
text = page.extract_text()
if text: # Only include pages with text
documents.append(Document(page_content=text, metadata={"page": i + 1}))
return documents
def extract_hebrew_text_toString(pdf_path):
with pdfplumber.open(pdf_path) as pdf:
text = ""
for page in pdf.pages:
text += page.extract_text()
return text
# Load a PDF file hebrew
dataPDF = extract_hebrew_text("/home/tom/ollamaProjects/RAGtextFileHeb.pdf")
##### Choose which data #####
### dataWeb <----> dataTxt EngV,HebV <----> dataPDF EngV,HebX ###
### Story PDF-Eng = 1:48 minutes --> llama3.1:8b
### Story Txt-Eng = 0:54 minutes --> llama3.1:8b
### Story PDF-Eng = 0:24 minutes --> llama3.2:3b
### Story Txt-Eng = 0:24 minutes --> llama3.2:3b
def setDataFiles(FlagPDF,FlagHEB,path):
if(FlagPDF==1): # PDF File
if (FlagHEB==1):
### --NOT WORKING-- #######
loaderPDF = PyPDFLoader(path+"HEB.pdf")
else: # English txt
loaderPDF = PyPDFLoader(path+".pdf")
data = loaderPDF.load()
#print(data)
else: # Txt File
if(FlagHEB==1):
loaderTxt = TextLoader(path+"Heb.txt", encoding="utf-8")
else: #English txt
loaderTxt = TextLoader(path+".txt")
data = loaderTxt.load()
return data
def setVectorData():
text_splitter = RecursiveCharacterTextSplitter(chunk_size=500, chunk_overlap=0)
all_splits = text_splitter.split_documents(setDataFiles(FlagPDF, FlagHEB, path))
local_embeddings = OllamaEmbeddings(model="nomic-embed-text")
vectorstore = Chroma.from_documents(documents=all_splits,
embedding=local_embeddings)
return vectorstore
###
########## DATA LOADING END ########
###
# modelPirate = ChatOllama(
# model="ollamaPirate",
# )
# modelNormal = ChatOllama(
# model="llama3.1:8b",
#
# )
# modelSmall = ChatOllama(
# model="llama3.2:3b",
#
# )
# modelNormalRouter = ChatOllama(
# model="llama3.1:8b",
# temperature=0,
# format="json"
# )
# modelSmallRouter = ChatOllama(
# model="llama3.2:3b",
# temperature=0,
# format="json"
# )
modelPirate = llm("ollamapirate")
modelNormal = llm("llama3.1:8b")
modelSmall = llm("llama3.2:3b")
modelNormalRouter = llm("llama3.1:8b", 0, "json")
modelSmallRouter = llm("llama3.2:3b", 0, "json")
def format_docs(docs):
return "\n\n".join(doc.page_content for doc in docs)
###
########## TEMPLATES START ########
###
RAG_TEMPLATE = """
You are an assistant for question-answering tasks.
Use the following pieces of retrieved context to answer the question.
If you don't know the answer, just say that you don't know.
Use three sentences maximum and keep the answer concise.
<context>
{context}
</context>
Answer the following question:
{question}"""
### Works, slowly, reads all the data each time. ###
router_instructions1 = """
You are an expert at routing a user question to a vectorstore or self answer.
The vectorstore contains documents related to personal data:
<context>
{context}
</context>
Use the vectorstore for questions on these topics. For all else, and especially for current events, generate independent answer.
Return JSON with single key, datasource, that is 'independent' or 'vectorstore' depending on the question:
{question}"""
### Also Works, given what data is stored in vector. ###
router_instructions2 = """
You are an expert at routing a user question to a vectorstore or self answer.
The vectorstore contains documents related to Toms' personal data, parents, hobbies ect.
Use the vectorstore for questions on these topics. For all else, and especially for current events, generate independent answer.
Return JSON with single key, datasource, that is 'independent' or 'vectorstore' depending on the question:
{question}"""
router_prompt1 = ChatPromptTemplate.from_template(router_instructions1)
router_prompt2 = ChatPromptTemplate.from_template(router_instructions2)
rag_prompt = ChatPromptTemplate.from_template(RAG_TEMPLATE)
###
########## TEMPLATES END ##########
###
def setRoutersChain_1_2():
router_chain1 = (
{"context": data.retriver | format_docs, "question": RunnablePassthrough()}
| router_prompt1
| modelSmallRouter
| StrOutputParser()
)
router_chain2 = (
{"question": RunnablePassthrough()}
| router_prompt2
| modelSmallRouter
| StrOutputParser()
)
return router_chain1,router_chain2
#def setRouterChain(promptNum)
def test_router(router_chain):
test_router1 = router_chain.invoke(
"Who is favored to win the NFC Championship game in the 2024 season?"
)
test_router2 = router_chain.invoke(
"Who Is Jona?"
)
test_router3 = router_chain.invoke(
"What Tom studies?"
)
print(
json.loads((test_router1)),
json.loads((test_router2)),
json.loads((test_router3)),
)
### Test of router chain ###
#test_router()
### ###
def RouterDirection(question,router_chain):
##### Use router_chain2 for simple check.
##### User router_chain1 for heavy check. - runs through files.
print("*** Routing QUESTION ****")
directionOfQJSON = router_chain.invoke(question)
directionOfQ = json.loads(directionOfQJSON)["datasource"]
print(f"Choosen way is: {directionOfQ}")
return directionOfQ
## TEST RouterDirection ###
# print(RouterDirection("Who is favored to win the NFC Championship game in the 2024 season?"))
##### RAG Chain Def ######
def setChainRAG(modelType):
chainRAG = (
### Needs $retriver and $modelType and $rag_prompt
{"context": data.retriver | format_docs, "question": RunnablePassthrough()}
| rag_prompt
| modelType.modelRunner
| StrOutputParser()
)
return chainRAG
def reverse_Heb(sentence):
words = sentence.split()
reversed_words = [word[::-1] for word in words] #reverse each word
reversed_words = reversed_words[::-1] #reverse order of words
return ' '.join(reversed_words)
pdf_path = "/home/tom/תואר/4thYearProject/PythonProjects/testMathSyb.pdf"
#
path = "/home/tom/ollamaProjects/RAGtextFile"
# path = "/home/tom/תואר/4thYearProject/PythonProjects/syllabusCalculus1TomTranslated"
# path = "/home/tom/תואר/4thYearProject/PythonProjects/testMathSymb"
FlagPDF=0 # 1=PDF /// 0=txt
FlagHEB=0 # 1=HEB /// 0=ENG
#setVectore need FLAGS and path to load all selected data.
vectorstore = setVectorData()
# retriever = vectorstore.as_retriever()
### TEST retriver class ###
data= Retriver(path,FlagHEB,FlagPDF)
#chains need retriever to set.
# router_chain1, router_chain2 = setRoutersChain_1_2()
router_chain1 = chain(router_prompt1,data,modelSmallRouter)
#chainRAG ---> can select which model to serach with, best is modelSmall
### Story PDF-Eng = 1:48 minutes --> llama3.1:8b
### Story Txt-Eng = 0:54 minutes --> llama3.1:8b
### Story PDF-Eng = 0:24 minutes --> llama3.2:3b
### Story Txt-Eng = 0:24 minutes --> llama3.2:3b
# chainRAG = setChainRAG(modelSmall)
chainRAG = chain(rag_prompt,data, modelSmall)
# question = "What the course about?"
# print(chainRAG.chainRunner.invoke(question)+"\n")
question = "What is the kids name? and who are his parents?"
print(chainRAG.chainRunner.invoke(question)+"\n")
# question = "מה השם של הילד?"
# print(reverse_Heb(chainRAG.invoke(question)))
# # question = "Tell me about Toms favorite place and his parents names"
# # print(chainRAG.invoke(question))
# question = "ספר לי על המקום אהוב על תום ומה שם הוריו."
# print(reverse_Heb(chainRAG.invoke(question)))
# question = "What is special about potatoes?"
# print(chainRAG.invoke(question)+"\n")
templateChat="""
Answer the question below, use less than 200 words.
Here is the conversation history: {history}
Question: {question}
Answer:
"""
modelChatting = OllamaLLM(model="llama3.1:8b")
## Notice - OllamaLLM is !NOT! ChatOllama (used in the start)
promptChat = ChatPromptTemplate.from_template(templateChat)
chainChat = promptChat | modelChatting
#chainRAG
def handle_conv():
history=""
print("Welcome to ChatRAG(Routing), type exit to quit.")
while True:
userInput = input("Input: ")
if userInput.lower() =="exit":
break
if(RouterDirection(userInput,router_chain1)=="vectorstore"):
result = chainRAG.invoke(userInput)
else:
result = chainChat.invoke({
"history": history,
"question": userInput
})
print("AI: ", result)
history = history + f"\nUser: {userInput}\nAI: {result}"
# handle_conv()