dtyago commited on
Commit
44296f1
1 Parent(s): 1a8333a

Fixed vectodb collection issue

Browse files
app/api/userchat.py CHANGED
@@ -1,6 +1,7 @@
1
  from fastapi import APIRouter, Depends, HTTPException, Body
2
  from ..dependencies import get_current_user
3
  from ..utils.chat_rag import llm_infer
 
4
  from typing import Any
5
 
6
  router = APIRouter()
@@ -10,7 +11,7 @@ async def chat_with_llama(user_input: str = Body(..., embed=True), current_user:
10
  # Example logic for model inference (pseudo-code, adjust as necessary)
11
  try:
12
  user_id = current_user["user_id"]
13
- model_response = llm_infer(user_id=user_id, prompt=user_input)
14
  # Optionally, store chat history
15
  # chromadb_face_helper.store_chat_history(user_id=current_user["user_id"], user_input=user_input, model_response=model_response)
16
  except Exception as e:
 
1
  from fastapi import APIRouter, Depends, HTTPException, Body
2
  from ..dependencies import get_current_user
3
  from ..utils.chat_rag import llm_infer
4
+ from ..utils.chat_rag import sanitize_collection_name
5
  from typing import Any
6
 
7
  router = APIRouter()
 
11
  # Example logic for model inference (pseudo-code, adjust as necessary)
12
  try:
13
  user_id = current_user["user_id"]
14
+ model_response = llm_infer(user_collection_name=sanitize_collection_name(user_id), prompt=user_input)
15
  # Optionally, store chat history
16
  # chromadb_face_helper.store_chat_history(user_id=current_user["user_id"], user_input=user_input, model_response=model_response)
17
  except Exception as e:
app/api/userupload.py CHANGED
@@ -4,6 +4,7 @@ import os
4
  from app.dependencies import get_current_user
5
  # Assuming a utility for processing PDFs and generating embeddings
6
  from ..utils.doc_ingest import ingest_document
 
7
 
8
  router = APIRouter()
9
 
@@ -22,7 +23,7 @@ async def upload_file(file: UploadFile = File(...), current_user: Any = Depends(
22
 
23
  try:
24
  # Process PDF and store embeddings
25
- ingest_document(file_location, current_user["user_id"])
26
  except Exception as e:
27
  # If processing fails, attempt to clean up the file before re-raising the error
28
  os.remove(file_location)
 
4
  from app.dependencies import get_current_user
5
  # Assuming a utility for processing PDFs and generating embeddings
6
  from ..utils.doc_ingest import ingest_document
7
+ from ..utils.chat_rag import sanitize_collection_name
8
 
9
  router = APIRouter()
10
 
 
23
 
24
  try:
25
  # Process PDF and store embeddings
26
+ ingest_document(file_location, sanitize_collection_name(current_user["user_id"]))
27
  except Exception as e:
28
  # If processing fails, attempt to clean up the file before re-raising the error
29
  os.remove(file_location)
app/utils/chat_rag.py CHANGED
@@ -1,5 +1,7 @@
1
  #list of librarys for requirement.txt
2
  import os
 
 
3
  from langchain.document_loaders import PyPDFLoader
4
 
5
  # Import embeddings module from langchain for vector representations of text
@@ -29,18 +31,32 @@ from langchain.callbacks.streaming_stdout import StreamingStdOutCallbackHandler
29
 
30
  CHROMADB_LOC = "/home/user/data/chromadb"
31
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
32
  # Modify vectordb initialization to be dynamic based on user_id
33
- def get_vectordb_for_user(user_id):
34
- collection_name = f"user_{user_id}_collection"
35
  vectordb = Chroma(
36
- collection_name=collection_name,
37
  embedding_function=HuggingFaceEmbeddings(model_name='sentence-transformers/all-MiniLM-L6-v2'),
38
- persist_directory=f"{CHROMADB_LOC}/{collection_name}", # Optional: Separate directory for each user's data
39
  )
40
  return vectordb
41
 
42
 
43
- def pdf_to_vec(filename, collection_name):
44
  document = []
45
  loader = PyPDFLoader(filename)
46
  document.extend(loader.load()) #which library is this from?
@@ -55,7 +71,7 @@ def pdf_to_vec(filename, collection_name):
55
  document_chunks = document_splitter.split_documents(document) #which library is this from?
56
 
57
  # Create a Chroma vector database from the document chunks with the specified embeddings, and set a directory for persistence
58
- vectordb = Chroma.from_documents(document_chunks, embedding=embeddings, collection_name=collection_name, persist_directory=CHROMADB_LOC) ## change to GUI path
59
 
60
  # Persist the created vector database to disk in the specified directory
61
  vectordb.persist() #this is mandatory?
@@ -94,8 +110,8 @@ def load_llm():
94
 
95
 
96
  #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
97
- def default_chain(llm, user_id):
98
- vectordb = get_vectordb_for_user(user_id) # Use the dynamic vectordb based on user_id
99
  sum_template = """
100
  As a machine learning education specialist, our expertise is pivotal in deepening the comprehension of complex machine learning concepts for both educators and students.
101
 
@@ -169,13 +185,13 @@ def default_chain(llm, user_id):
169
  return default_chain,router_chain,destination_chains
170
 
171
  # Adjust llm_infer to accept user_id and use it for user-specific processing
172
- def llm_infer(user_id, prompt):
173
 
174
  llm = load_llm() # load_llm is singleton for entire system
175
 
176
- vectordb = get_vectordb_for_user(user_id) # Vector collection for each us.
177
 
178
- default_chain, router_chain, destination_chains = get_or_create_chain(user_id, llm) # Now user-specific
179
 
180
  chain = MultiPromptChain(
181
  router_chain=router_chain,
@@ -191,13 +207,13 @@ def llm_infer(user_id, prompt):
191
  # Assuming a simplified caching mechanism for demonstration
192
  chain_cache = {}
193
 
194
- def get_or_create_chain(user_id, llm):
195
  if 'default_chain' in chain_cache and 'router_chain' in chain_cache:
196
  default_chain = chain_cache['default_chain']
197
  router_chain = chain_cache['router_chain']
198
  destination_chains = chain_cache['destination_chains']
199
  else:
200
- vectordb = get_vectordb_for_user(user_id) # User-specific vector database
201
  sum_template = """
202
  As a machine learning education specialist, our expertise is pivotal in deepening the comprehension of complex machine learning concepts for both educators and students.
203
 
 
1
  #list of librarys for requirement.txt
2
  import os
3
+ import re
4
+ import hashlib
5
  from langchain.document_loaders import PyPDFLoader
6
 
7
  # Import embeddings module from langchain for vector representations of text
 
31
 
32
  CHROMADB_LOC = "/home/user/data/chromadb"
33
 
34
+ def sanitize_collection_name(email):
35
+ # Replace invalid characters with an underscore
36
+ sanitized = re.sub(r'[^a-zA-Z0-9_-]', '_', email)
37
+ # Ensure the name is within the length limits
38
+ if len(sanitized) > 63:
39
+ # Hashing the name to ensure uniqueness and length constraint
40
+ hash_suffix = hashlib.sha256(email.encode()).hexdigest()[:8]
41
+ sanitized = sanitized[:55] + "_" + hash_suffix
42
+ # Ensure it starts and ends with an alphanumeric character
43
+ if not re.match(r'^[a-zA-Z0-9].*[a-zA-Z0-9]$', sanitized):
44
+ sanitized = "a" + sanitized + "1"
45
+ return sanitized
46
+
47
+
48
  # Modify vectordb initialization to be dynamic based on user_id
49
+ def get_vectordb_for_user(user_collection_name):
50
+
51
  vectordb = Chroma(
52
+ collection_name=user_collection_name,
53
  embedding_function=HuggingFaceEmbeddings(model_name='sentence-transformers/all-MiniLM-L6-v2'),
54
+ persist_directory=f"{CHROMADB_LOC}/{user_collection_name}", # Optional: Separate directory for each user's data
55
  )
56
  return vectordb
57
 
58
 
59
+ def pdf_to_vec(filename, user_collection_name):
60
  document = []
61
  loader = PyPDFLoader(filename)
62
  document.extend(loader.load()) #which library is this from?
 
71
  document_chunks = document_splitter.split_documents(document) #which library is this from?
72
 
73
  # Create a Chroma vector database from the document chunks with the specified embeddings, and set a directory for persistence
74
+ vectordb = Chroma.from_documents(document_chunks, embedding=embeddings, collection_name=user_collection_name, persist_directory=CHROMADB_LOC) ## change to GUI path
75
 
76
  # Persist the created vector database to disk in the specified directory
77
  vectordb.persist() #this is mandatory?
 
110
 
111
 
112
  #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
113
+ def default_chain(llm, user_collection_name):
114
+ vectordb = get_vectordb_for_user(user_collection_name) # Use the dynamic vectordb based on user_id
115
  sum_template = """
116
  As a machine learning education specialist, our expertise is pivotal in deepening the comprehension of complex machine learning concepts for both educators and students.
117
 
 
185
  return default_chain,router_chain,destination_chains
186
 
187
  # Adjust llm_infer to accept user_id and use it for user-specific processing
188
+ def llm_infer(user_collection_name, prompt):
189
 
190
  llm = load_llm() # load_llm is singleton for entire system
191
 
192
+ vectordb = get_vectordb_for_user(user_collection_name) # Vector collection for each us.
193
 
194
+ default_chain, router_chain, destination_chains = get_or_create_chain(user_collection_name, llm) # Now user-specific
195
 
196
  chain = MultiPromptChain(
197
  router_chain=router_chain,
 
207
  # Assuming a simplified caching mechanism for demonstration
208
  chain_cache = {}
209
 
210
+ def get_or_create_chain(user_collection_name, llm):
211
  if 'default_chain' in chain_cache and 'router_chain' in chain_cache:
212
  default_chain = chain_cache['default_chain']
213
  router_chain = chain_cache['router_chain']
214
  destination_chains = chain_cache['destination_chains']
215
  else:
216
+ vectordb = get_vectordb_for_user(user_collection_name) # User-specific vector database
217
  sum_template = """
218
  As a machine learning education specialist, our expertise is pivotal in deepening the comprehension of complex machine learning concepts for both educators and students.
219
 
requirements.txt CHANGED
@@ -1,7 +1,7 @@
1
  fastapi==0.95.2 # Core framework for building APIs.
2
  uvicorn[standard]==0.18.3 # ASGI server for FastAPI, supports live reloading.
3
  requests==2.28.* # For making HTTP requests, if needed by our app.
4
- torch==1.11.* # PyTorch, for handling deep learning models.
5
  sentencepiece==0.1.* # For chat text processing
6
  mtcnn==0.1.1 # For face detection in images.
7
  python-jose[cryptography]==3.3.* # For creating, parsing, and verifying JWT tokens.
 
1
  fastapi==0.95.2 # Core framework for building APIs.
2
  uvicorn[standard]==0.18.3 # ASGI server for FastAPI, supports live reloading.
3
  requests==2.28.* # For making HTTP requests, if needed by our app.
4
+ torch # PyTorch, for handling deep learning models.
5
  sentencepiece==0.1.* # For chat text processing
6
  mtcnn==0.1.1 # For face detection in images.
7
  python-jose[cryptography]==3.3.* # For creating, parsing, and verifying JWT tokens.