kolzchut-ragbot 1.7.3__py3-none-any.whl → 1.7.5__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
kolzchut_ragbot/engine.py CHANGED
@@ -10,6 +10,7 @@ from transformers import AutoTokenizer, AutoModelForSequenceClassification
10
10
  from .get_full_documents_utilities import find_page_id_in_all_indices, unite_docs_to_single_instance
11
11
  import torch
12
12
  import os
13
+ import asyncio
13
14
 
14
15
  device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
15
16
  definitions = factory()
@@ -170,7 +171,7 @@ class Engine:
170
171
 
171
172
  return fused_list
172
173
 
173
- def search_documents(self, query: str, top_k: int,retrieval_size: int, max_documents_from_same_page:int):
174
+ def search_documents(self, query: str, top_k: int, retrieval_size: int, max_documents_from_same_page: int):
174
175
  """
175
176
  Searches for documents based on the query and returns the top_k results.
176
177
 
@@ -216,43 +217,129 @@ class Engine:
216
217
 
217
218
  return top_k_documents, all_docs_and_scores
218
219
 
219
- def answer_query(self, query, top_k: int, model, additional_document: dict = None, send_complete_pages_to_llm: bool = False, retrieval_size: int = 50, max_documents_from_same_page:int=3):
220
+ def get_page_content_by_page_id(self, page_id: int) -> tuple:
220
221
  """
221
- Answers a query using the top_k documents and the specified model.
222
+ Fetches the full content of a page and measures how long it takes.
222
223
 
223
224
  Args:
224
- query (str): The query string.
225
- top_k (int): The number of top documents to use for answering the query.
226
- model: The model to use for answering the query.
227
- additional_document (dict, optional): An additional document to include in the search. Default is None.
228
- send_complete_pages_to_llm (bool, optional): Whether to send complete pages to the
229
- retrieval_size(int, optional): The number of documents to fetch from each model. Default is 50.
230
- max_documents_from_same_page(int, optional): The maximum number of documents (paragraphs acutually) to return from the same page. Default is 3.
225
+ page_id (int): The ID of the page to retrieve.
226
+
227
+ Returns:
228
+ tuple: (page_content, elapsed_time) where `page_content` is the retrieved content
229
+ and `elapsed_time` is the time in seconds.
230
+ """
231
+ before_getting_additional_page = time.perf_counter()
232
+ additional_page_content = self.get_full_document_by_page_id(page_id)
233
+ after_getting_additional_page = time.perf_counter()
234
+ elapsed_time = after_getting_additional_page - before_getting_additional_page
235
+ return additional_page_content, elapsed_time
236
+
237
+ def retrieve_documents(self, query: str, top_k: int, retrieval_size: int,
238
+ max_documents_from_same_page: int, send_complete_pages_to_llm: bool) -> tuple:
239
+ """
240
+ Retrieves documents matching a query and optionally converts them to full pages.
241
+
242
+ Args:
243
+ query (str): Search query.
244
+ top_k (int): Number of top documents to return.
245
+ retrieval_size (int): Number of documents to fetch from the source.
246
+ max_documents_from_same_page (int): Max documents from a single page.
247
+ send_complete_pages_to_llm (bool): If True, returns full page content.
231
248
 
232
249
  Returns:
233
- tuple: A tuple containing the top k documents, the answer, and the stats.
250
+ tuple: (top_k_documents, all_docs_and_scores, retrieval_time)
234
251
  """
235
252
  before_retrieval = time.perf_counter()
236
- top_k_documents, all_docs_and_scores = self.search_documents(query=query, top_k=top_k, retrieval_size=retrieval_size,max_documents_from_same_page=max_documents_from_same_page)
253
+ top_k_documents, all_docs_and_scores = self.search_documents(
254
+ query=query,
255
+ top_k=top_k,
256
+ retrieval_size=retrieval_size,
257
+ max_documents_from_same_page=max_documents_from_same_page
258
+ )
237
259
 
238
260
  if send_complete_pages_to_llm:
239
261
  top_k_documents = [self.transform_document_to_full_page(doc) for doc in top_k_documents]
240
262
 
241
- top_k_documents_and_additional_document = top_k_documents.copy()
263
+ retrieval_time = round(time.perf_counter() - before_retrieval, 4)
264
+ print(f"retrieval time: {retrieval_time}")
265
+
266
+ return top_k_documents, all_docs_and_scores, retrieval_time
267
+
268
+ async def answer_query(self, query: str, top_k: int, model, page_id: int | None = None,
269
+ send_complete_pages_to_llm: bool = False, retrieval_size: int = 50,
270
+ max_documents_from_same_page: int = 3) -> tuple:
271
+ """
272
+ Answers a query using top documents and an LLM model, optionally including a full page.
273
+
274
+ Args:
275
+ query (str): Query string.
276
+ top_k (int): Number of top documents to use.
277
+ model: LLM model to generate the answer.
278
+ page_id (int | None): Optional page to include.
279
+ send_complete_pages_to_llm (bool): If True, sends full pages to the LLM.
280
+ retrieval_size (int): Number of documents to fetch (default 50).
281
+ max_documents_from_same_page (int): Max documents from one page (default 3).
242
282
 
283
+ Returns:
284
+ tuple: (top_k_documents, gpt_answer, stats, all_docs_and_scores, request_params)
285
+ """
286
+ before_answer = time.perf_counter()
287
+
288
+ tasks = [
289
+ asyncio.to_thread(
290
+ self.retrieve_documents,
291
+ query, top_k, retrieval_size, max_documents_from_same_page, send_complete_pages_to_llm
292
+ )
293
+ ]
294
+
295
+ if page_id:
296
+ tasks.append(asyncio.to_thread(self.get_page_content_by_page_id, page_id))
297
+
298
+ results = await asyncio.gather(*tasks)
299
+
300
+ # Unpack results
301
+ top_k_documents, all_docs_and_scores, retrieval_time = results[0]
302
+ additional_document = None
303
+ additional_page_time = None
304
+ if page_id:
305
+ additional_document, additional_page_time = results[1]
306
+
307
+ # Combine documents
308
+ top_k_documents_and_additional_document = top_k_documents.copy()
309
+ # Remove documents with the same page_id as the additional_document before appending
243
310
  if additional_document:
311
+ additional_page_id = additional_document.get("page_id")
312
+ top_k_documents_and_additional_document = [
313
+ doc for doc in top_k_documents_and_additional_document
314
+ if doc.get("page_id") != additional_page_id
315
+ ]
244
316
  top_k_documents_and_additional_document.append(additional_document)
245
317
 
246
- retrieval_time = round(time.perf_counter() - before_retrieval, 4)
247
- print(f"retrieval time: {retrieval_time}")
318
+ # Query LLM
319
+ gpt_answer, gpt_elapsed, tokens, request_params = await asyncio.to_thread(
320
+ self.llms_client.answer,
321
+ query, top_k_documents_and_additional_document
322
+ )
323
+ after_answer = time.perf_counter()
324
+ answer_time = after_answer - before_answer
248
325
 
249
- gpt_answer, gpt_elapsed, tokens, request_params = self.llms_client.answer(query, top_k_documents_and_additional_document)
250
326
  stats = {
251
327
  "retrieval_time": retrieval_time,
252
328
  "gpt_model": model,
253
329
  "gpt_time": gpt_elapsed,
254
- "tokens": tokens
330
+ "tokens": tokens,
331
+ "answer_time": answer_time
255
332
  }
333
+
334
+ request_params['timers_ms'] = {
335
+ "answer_time": int(answer_time * 1000),
336
+ "retrieval_time": int(retrieval_time * 1000),
337
+ "llm_time": int(gpt_elapsed * 1000)
338
+ }
339
+
340
+ if additional_page_time:
341
+ request_params['timers_ms']['additional_page_time'] = int(additional_page_time * 1000)
342
+
256
343
  return top_k_documents, gpt_answer, stats, all_docs_and_scores, request_params
257
344
 
258
345
  def transform_document_to_full_page(self, document: dict) -> dict:
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: kolzchut-ragbot
3
- Version: 1.7.3
3
+ Version: 1.7.5
4
4
  Summary: A search engine using machine learning models and Elasticsearch for advanced document retrieval.
5
5
  Home-page: https://github.com/shmuelrob/rag-bot
6
6
  Author: Shmuel Robinov
@@ -2,11 +2,11 @@ kolzchut_ragbot/Document.py,sha256=5OyBBTZyAJFM_1Pjs3SUC-_s5zEJ5U6wjhw12_FFkdE,3
2
2
  kolzchut_ragbot/IntegrateService.py,sha256=rcwUY2RkclCY3l8BGAmNbstdxhxwhLO9oA8BofqLyts,96
3
3
  kolzchut_ragbot/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
4
4
  kolzchut_ragbot/config.py,sha256=uILFvgn9W92-NRaKXYtaoQXpn3KOWKK8SZYRsIAa5Yw,133
5
- kolzchut_ragbot/engine.py,sha256=ttUxJRsX7p-sLuU4sC-W2GIt3pUClPchbckJAMBfT2k,13221
5
+ kolzchut_ragbot/engine.py,sha256=-qNSnyq4A3rQZz_HU_5Djwc35Su0M4tAqNvqLlDQ_UU,16383
6
6
  kolzchut_ragbot/get_full_documents_utilities.py,sha256=YWljmGWM6h1ghLDCAUnDdhmn-0k6R_t7b1g7wSojzvg,1882
7
7
  kolzchut_ragbot/llm_client.py,sha256=JdDeOn2THpkOM2Mwe2DucTaYXul1fL2agIisBuHFtc8,347
8
8
  kolzchut_ragbot/model.py,sha256=HCi3r4YztPknnbgTOA7I-GVaqxn8CzrTeLFkEg-7fg0,6320
9
- kolzchut_ragbot-1.7.3.dist-info/METADATA,sha256=6weg2Y1MWvZ98nuG2laVp_aCVfxdetTrB5psXqCMBNs,1999
10
- kolzchut_ragbot-1.7.3.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
11
- kolzchut_ragbot-1.7.3.dist-info/top_level.txt,sha256=NTZoY4GGw3v_7jm0MgcdHw8simoZ78PsR7Meqmkgd_Q,16
12
- kolzchut_ragbot-1.7.3.dist-info/RECORD,,
9
+ kolzchut_ragbot-1.7.5.dist-info/METADATA,sha256=9jpBjTYYUzmuNuQ_t5L-fPjOJfKoCfBSEree9DnuazE,1999
10
+ kolzchut_ragbot-1.7.5.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
11
+ kolzchut_ragbot-1.7.5.dist-info/top_level.txt,sha256=NTZoY4GGw3v_7jm0MgcdHw8simoZ78PsR7Meqmkgd_Q,16
12
+ kolzchut_ragbot-1.7.5.dist-info/RECORD,,