-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtestUserDataSaver.py
More file actions
450 lines (322 loc) · 13.2 KB
/
testUserDataSaver.py
File metadata and controls
450 lines (322 loc) · 13.2 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
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
### Use: streamlit run <file.py> ###
import streamlit as st
from Retrieval import Retriver
from llm import llm
from chain import chain
import json
from langchain_core.prompts import ChatPromptTemplate
from langchain_ollama import OllamaLLM
import pdfplumber
from langchain.schema import Document
import chromadb
import os
chromadb.api.client.SharedSystemClient.clear_system_cache()
# https://docs.streamlit.io/
# {
# Using VirtualchatbotTest memory
# (venv) python3 -m venv chatbotTest
# (venv) source chatbotTest/bin/activate
# (chatbotTest) (venv)
# }
modelPirate = llm("ollamapirate")
modelNormal = llm("llama3.1:8b")
modelSmall = llm("llama3.2:3b", 0.8) # temperature = 0.8
modelSmall2 = llm("llama3.2:3b") # temperature = 0
modelNormalRouter = llm("llama3.1:8b", 0, "json")
modelSmallRouter = llm("llama3.2:3b", 0, "json")
###
########## TEMPLATES START ########
###
userDataSaver_instructions = """
You are personal assistant.
You are an expert at saving data about the user from given chat input and data context file.
Use the following retrieved context data.
Overwrite existing information ONLY if new one is more relevant.
You will not save new data as 'null'.
The data will be saved in JSON format with 3 keys.
'Name':
'Age':
'Preferred Language':
If you don't know, or want to save 'null', do not save.
<context>
{context}
</context>
User's input:
{question}
"""
userDataExtract_instructions = """
You are a personal assistant.
You are an expert at extracting data about the user from given context file.
Use user request to extract relevant data.
Use the following piece of retrieved context data.
<context>
{context}
</context>
User request:
{question}
"""
RAG_TEMPLATE_MATH = """
You are an expert at question generation using a given file.
Use the following pieces of retrieved context to generate a new question based on user's request.
The generated question MUST be similar to the original AND on the same level.
If you don't know, just say that you don't know.
You MUST NOT return answer in correct Latex format.
<context>
{context}
</context>
User's request:
{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 math questions:
<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 math questions in calculus 1.
Use the vectorstore for questions on these topics. For all else, and especially for current events, and given information generate independent answer.
Return JSON with single key, datasource, that is 'independent' or 'vectorstore' depending on the question:
{question}"""
###
########## TEMPLATES END ##########
###
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
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.chainRunner.invoke(question)
# print(directionOfQJSON)
directionOfQ = json.loads(directionOfQJSON)["datasource"]
print(f"Choosen way is: {directionOfQ}")
return directionOfQ
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)
path_userData = "/home/tom/תואר/4thYearProject/PythonProjects/userInfo/userData_eng.txt"
path = "/home/tom/תואר/4thYearProject/PythonProjects/calculusJsonDataShort_eng.txt"
if os.path.exists(path_userData):
noData=0
# File exists, open in read and write mode ('r+')
open(path_userData, 'r+') # 'r+' allows reading and writing
else:
noData=1
# File doesn't exist, create and open in write mode ('w')
with open(path_userData, 'w') as f: # 'w' creates and opens for writing
f.write("EMPTY")
print("--Start Data")
data= Retriver(path)
dataUser= Retriver(path_userData)
print("--End Data")
router_chain1 = chain(router_instructions1, modelSmallRouter, data)
router_chain2 =chain(router_instructions2,modelSmallRouter)
def test_router(router_chain):
test_router1 = router_chain.chainRunner.invoke(
"Who is favored to win the NFC Championship game in the 2024 season?"
)
test_router2 = router_chain.chainRunner.invoke(
"Who Is Jona?"
)
test_router3 = router_chain.chainRunner.invoke(
"I need a math question?"
)
print(
json.loads((test_router1)),
json.loads((test_router2)),
json.loads((test_router3)),
)
#test_router(router_chain2)
## TEST RouterDirection ###
# print(RouterDirection(
# "Who is favored to win the NFC Championship game in the 2024 season?",
# router_chain=router_chain2))
#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
userDataSAVER_agent = chain(userDataSaver_instructions, modelSmallRouter, dataUser)
userDataEXTRACTOR_agent = chain(userDataExtract_instructions, modelSmall, dataUser)
chainRAG = chain(RAG_TEMPLATE_MATH, modelSmall, data)
#### TEST RAG CHAIN ####
# question = "generate a question similar to Question number 5"
# ans = (chainRAG.chainRunner.invoke(question)+"\n")
# print(ans+"\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_steamlit():
if noData:
get_user_info()
if "messages" not in st.session_state:
st.session_state.messages = []
for message in st.session_state.messages:
with st.chat_message(message["role"]):
st.markdown(message["content"])
if userInput := st.chat_input("What is up?"):
st.session_state.messages.append({"role": "user", "content": userInput})
with st.chat_message("user"):
st.markdown(userInput)
with st.chat_message("assistant"):
if userInput.lower() == "exit":
st.write("**Exiting the chat. Refresh the page to start again.**")
return
# Determine route and invoke appropriate chain
routerWay = RouterDirection(userInput, router_chain2)
if routerWay == "math":
#Need RAG
#Give a new question from RAG
result = chainRAG.chainRunner.invoke(userInput)
# elif routerWay == "personal":
# result = userDataEXTRACTOR_agent.chainRunner.invoke(userInput)
else:
result = chainChat.invoke({
"history": [
{"role": m["role"], "content": m["content"]}
for m in st.session_state.messages
],
"question": userInput
})
st.markdown(clean_latex(result))
# newData = userDataSAVER_agent.chainRunner.invoke(userInput)
# print(newData+"\n")
# with open(path_userData, 'w') as file:
# file.write(newData)
st.session_state.messages.append({"role": "assistant", "content": result})
DATA_FILE = path_userData
def save_user_data(name, age,degree, taskComplete, topic1):
"""Saves user name and age to a text file."""
with open(DATA_FILE, "w") as file:
file.write(f"{name}\n{age}\n{degree}\n{taskComplete}\n{topic1}")
def load_user_data():
"""Loads user name and age from the text file."""
if os.path.exists(DATA_FILE):
with open(DATA_FILE, "r") as file:
data = file.read().strip().split("\n")
if (data[0] != "EMPTY"):
return data
return None, None, None, None, None
def get_user_info():
"""Collects user's Name and Age before allowing chat interaction."""
st.title("Welcome! Please enter your details to continue.")
# Load user data from file
name, age, degree, taskComplete, topic1 = load_user_data()
if name and age and degree:
st.session_state.name = name
st.session_state.age = age
st.session_state.degree = degree
st.session_state.taskComplete = taskComplete
st.session_state.topic1 = topic1
st.session_state.info_collected = True
st.rerun() # Refresh page to move to chat
with st.form("user_info_form"):
name = st.text_input("Enter your Name:")
age = st.text_input("Enter your Age:")
degree = st.text_input("Enter your Degree:")
submitted = st.form_submit_button("Continue")
if submitted:
if name.strip() and age.strip().isdigit():
taskComplete =0
topic1 =0
st.session_state.name = name
st.session_state.age = age
st.session_state.degree = degree
st.session_state.taskComplete = taskComplete
st.session_state.topic1 = topic1
st.session_state.info_collected = True
save_user_data(name, age, degree, taskComplete, topic1)
st.rerun() # Refresh page to move to chat
else:
st.warning("Please enter a valid Name and Age (numeric).")
# Chat function
def handle_chat():
# st.title(f"Welcome back, {st.session_state.name}! Let's Chat.")
user_data = {"Name": st.session_state.name,
"Age": st.session_state.age,
"Degree": st.session_state.degree,
"Task Complete": st.session_state.taskComplete,
"Topic1 Level": st.session_state.topic1}
st.sidebar.title("User Progress")
st.sidebar.metric("Name", user_data["Name"])
st.sidebar.metric("Age", user_data["Age"])
st.sidebar.metric("Degree", user_data["Degree"])
st.sidebar.metric("Task Complete", user_data["Task Complete"])
st.sidebar.header("Topic 1 Progress")
progress = st.sidebar.progress(int(user_data["Topic1 Level"]))
if "messages" not in st.session_state:
st.session_state.messages = []
for message in st.session_state.messages:
with st.chat_message(message["role"]):
st.markdown(message["content"])
if userInput := st.chat_input("What is up?"):
st.session_state.messages.append({"role": "user", "content": userInput})
with st.chat_message("user"):
st.markdown(userInput)
with st.chat_message("assistant"):
response = f"Hello {st.session_state.name}, you said: {userInput}" # Replace with LLM response
st.markdown(response)
st.session_state.messages.append({"role": "assistant", "content": response})
# Run the app
if "info_collected" not in st.session_state or not st.session_state.info_collected:
get_user_info()
else:
handle_chat()
import time
def basicStreamlit():
prompt = st.text_input("Say something")
if prompt:
st.write(f"User prompt: {prompt}")
user_data = {"Score": 85, "Tasks Completed": 7, "Level": 3}
# Sidebar content
st.sidebar.title("User Progress")
st.sidebar.metric("Score", user_data["Score"])
st.sidebar.metric("Tasks Completed", user_data["Tasks Completed"])
st.sidebar.metric("Level", user_data["Level"])
# Progress bar simulation
progress = st.sidebar.progress(0)
for i in range(101): # Simulating progress
time.sleep(0.05)
progress.progress(i)
# Main content
st.title("Main Application")
st.write("This is your main content area.")
# basicStreamlit()
import re
def clean_latex(input_string):
# Remove [asy] environments and their contents
cleaned_string = re.sub(r'\[asy\](.*?)\[/asy\]', '', input_string, flags=re.DOTALL)
# Remove extra newlines and trim whitespace
#cleaned_string = re.sub(r'\n\s*\n', '\n', cleaned_string).strip()
return cleaned_string
#### For Chat Testing ####
#handle_conv_steamlit()