dtyago commited on
Commit
5cd4845
1 Parent(s): 447bc57

Vectordb ref caching

Browse files
Files changed (1) hide show
  1. app/utils/chat_rag.py +10 -3
app/utils/chat_rag.py CHANGED
@@ -58,6 +58,13 @@ def get_vectordb_for_user(user_collection_name):
58
  )
59
  return vectordb
60
 
 
 
 
 
 
 
 
61
 
62
  def pdf_to_vec(filename, user_collection_name):
63
 
@@ -121,7 +128,7 @@ def default_chain(llm, user_collection_name):
121
  # Get Chromadb location
122
  CHROMADB_LOC = os.getenv('CHROMADB_LOC')
123
 
124
- vectordb = get_vectordb_for_user(user_collection_name) # Use the dynamic vectordb based on user_id
125
  sum_template = """
126
  As a machine learning education specialist, our expertise is pivotal in deepening the comprehension of complex machine learning concepts for both educators and students.
127
 
@@ -206,7 +213,7 @@ def llm_infer(user_collection_name, prompt):
206
 
207
  llm = load_llm() # load_llm is singleton for entire system
208
 
209
- vectordb = get_vectordb_for_user(user_collection_name) # Vector collection for each us.
210
 
211
  default_chain, router_chain, destination_chains = get_or_create_chain(user_collection_name, llm) # Now user-specific
212
 
@@ -230,7 +237,7 @@ def get_or_create_chain(user_collection_name, llm):
230
  router_chain = chain_cache['router_chain']
231
  destination_chains = chain_cache['destination_chains']
232
  else:
233
- vectordb = get_vectordb_for_user(user_collection_name) # User-specific vector database
234
  sum_template = """
235
  As a machine learning education specialist, our expertise is pivotal in deepening the comprehension of complex machine learning concepts for both educators and students.
236
 
 
58
  )
59
  return vectordb
60
 
61
+ vectordb_cache = {}
62
+
63
+ def get_vectordb_for_user_cached(user_collection_name):
64
+ if user_collection_name not in vectordb_cache:
65
+ vectordb_cache[user_collection_name] = get_vectordb_for_user(user_collection_name)
66
+ return vectordb_cache[user_collection_name]
67
+
68
 
69
  def pdf_to_vec(filename, user_collection_name):
70
 
 
128
  # Get Chromadb location
129
  CHROMADB_LOC = os.getenv('CHROMADB_LOC')
130
 
131
+ vectordb = get_vectordb_for_user_cached(user_collection_name) # Use the dynamic vectordb based on user_id
132
  sum_template = """
133
  As a machine learning education specialist, our expertise is pivotal in deepening the comprehension of complex machine learning concepts for both educators and students.
134
 
 
213
 
214
  llm = load_llm() # load_llm is singleton for entire system
215
 
216
+ vectordb = get_vectordb_for_user_cached(user_collection_name) # Vector collection for each us.
217
 
218
  default_chain, router_chain, destination_chains = get_or_create_chain(user_collection_name, llm) # Now user-specific
219
 
 
237
  router_chain = chain_cache['router_chain']
238
  destination_chains = chain_cache['destination_chains']
239
  else:
240
+ vectordb = get_vectordb_for_user_cached(user_collection_name) # User-specific vector database
241
  sum_template = """
242
  As a machine learning education specialist, our expertise is pivotal in deepening the comprehension of complex machine learning concepts for both educators and students.
243