kolzchut-ragbot 1.7.2__tar.gz → 1.7.4__tar.gz

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.
Files changed (23) hide show
  1. {kolzchut_ragbot-1.7.2 → kolzchut_ragbot-1.7.4}/PKG-INFO +1 -1
  2. {kolzchut_ragbot-1.7.2 → kolzchut_ragbot-1.7.4}/kolzchut_ragbot/engine.py +99 -17
  3. {kolzchut_ragbot-1.7.2 → kolzchut_ragbot-1.7.4}/kolzchut_ragbot.egg-info/PKG-INFO +1 -1
  4. {kolzchut_ragbot-1.7.2 → kolzchut_ragbot-1.7.4}/pyproject.toml +1 -1
  5. {kolzchut_ragbot-1.7.2 → kolzchut_ragbot-1.7.4}/setup.py +1 -1
  6. {kolzchut_ragbot-1.7.2 → kolzchut_ragbot-1.7.4}/test/test_engine.py +7 -5
  7. {kolzchut_ragbot-1.7.2 → kolzchut_ragbot-1.7.4}/README.md +0 -0
  8. {kolzchut_ragbot-1.7.2 → kolzchut_ragbot-1.7.4}/kolzchut_ragbot/Document.py +0 -0
  9. {kolzchut_ragbot-1.7.2 → kolzchut_ragbot-1.7.4}/kolzchut_ragbot/IntegrateService.py +0 -0
  10. {kolzchut_ragbot-1.7.2 → kolzchut_ragbot-1.7.4}/kolzchut_ragbot/__init__.py +0 -0
  11. {kolzchut_ragbot-1.7.2 → kolzchut_ragbot-1.7.4}/kolzchut_ragbot/config.py +0 -0
  12. {kolzchut_ragbot-1.7.2 → kolzchut_ragbot-1.7.4}/kolzchut_ragbot/get_full_documents_utilities.py +0 -0
  13. {kolzchut_ragbot-1.7.2 → kolzchut_ragbot-1.7.4}/kolzchut_ragbot/llm_client.py +0 -0
  14. {kolzchut_ragbot-1.7.2 → kolzchut_ragbot-1.7.4}/kolzchut_ragbot/model.py +0 -0
  15. {kolzchut_ragbot-1.7.2 → kolzchut_ragbot-1.7.4}/kolzchut_ragbot.egg-info/SOURCES.txt +0 -0
  16. {kolzchut_ragbot-1.7.2 → kolzchut_ragbot-1.7.4}/kolzchut_ragbot.egg-info/dependency_links.txt +0 -0
  17. {kolzchut_ragbot-1.7.2 → kolzchut_ragbot-1.7.4}/kolzchut_ragbot.egg-info/requires.txt +0 -0
  18. {kolzchut_ragbot-1.7.2 → kolzchut_ragbot-1.7.4}/kolzchut_ragbot.egg-info/top_level.txt +0 -0
  19. {kolzchut_ragbot-1.7.2 → kolzchut_ragbot-1.7.4}/setup.cfg +0 -0
  20. {kolzchut_ragbot-1.7.2 → kolzchut_ragbot-1.7.4}/test/test_configs.py +0 -0
  21. {kolzchut_ragbot-1.7.2 → kolzchut_ragbot-1.7.4}/test/test_docs.py +0 -0
  22. {kolzchut_ragbot-1.7.2 → kolzchut_ragbot-1.7.4}/test/test_document.py +0 -0
  23. {kolzchut_ragbot-1.7.2 → kolzchut_ragbot-1.7.4}/test/test_model.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: kolzchut-ragbot
3
- Version: 1.7.2
3
+ Version: 1.7.4
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
@@ -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,124 @@ 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()
243
309
  if additional_document:
244
310
  top_k_documents_and_additional_document.append(additional_document)
245
311
 
246
- retrieval_time = round(time.perf_counter() - before_retrieval, 4)
247
- print(f"retrieval time: {retrieval_time}")
312
+ # Query LLM
313
+ gpt_answer, gpt_elapsed, tokens, request_params = await asyncio.to_thread(
314
+ self.llms_client.answer,
315
+ query, top_k_documents_and_additional_document
316
+ )
317
+ after_answer = time.perf_counter()
318
+ answer_time = after_answer - before_answer
248
319
 
249
- gpt_answer, gpt_elapsed, tokens, request_params = self.llms_client.answer(query, top_k_documents_and_additional_document)
250
320
  stats = {
251
321
  "retrieval_time": retrieval_time,
252
322
  "gpt_model": model,
253
323
  "gpt_time": gpt_elapsed,
254
- "tokens": tokens
324
+ "tokens": tokens,
325
+ "answer_time": answer_time
326
+ }
327
+
328
+ request_params['timers_ms'] = {
329
+ "answer_time": int(answer_time*1000),
330
+ "retrieval_time": int(retrieval_time*1000),
331
+ "llm_time": int(gpt_elapsed*1000)
255
332
  }
333
+
334
+ if additional_page_time:
335
+ request_params['timers_ms']['additional_page_time'] = int(additional_page_time*1000)
336
+
337
+
256
338
  return top_k_documents, gpt_answer, stats, all_docs_and_scores, request_params
257
339
 
258
340
  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.2
3
+ Version: 1.7.4
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
@@ -14,7 +14,7 @@ push = false
14
14
 
15
15
  [tool.poetry]
16
16
  name = "ragbot"
17
- version = "1.7.2"
17
+ version = "1.7.4"
18
18
  description = ""
19
19
  authors = ["Your Name <your.email@example.com>"]
20
20
 
@@ -7,7 +7,7 @@ from setuptools import setup, find_packages
7
7
 
8
8
  setup(
9
9
  name='kolzchut-ragbot',
10
- version='1.7.2',
10
+ version='1.7.4',
11
11
  author='Shmuel Robinov',
12
12
  author_email='shmuel_robinov@webiks.com',
13
13
  description='A search engine using machine learning models and Elasticsearch for advanced document retrieval.',
@@ -3,6 +3,7 @@ import importlib
3
3
  from unittest.mock import patch, MagicMock, ANY
4
4
  import sys
5
5
  import os
6
+ import asyncio
6
7
 
7
8
  sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))
8
9
  import kolzchut_ragbot.engine
@@ -136,9 +137,9 @@ class TestEngine:
136
137
  ], {})
137
138
 
138
139
  llm_client.answer.return_value = ('answer', 0.5, 100, {})
139
- # Make retrieval_time deterministic (0)
140
- with patch('time.perf_counter', side_effect=[100.0, 100.0]):
141
- actual_top_k_documents, actual_gpt_answer, actual_stats, all_docs_and_score, request_params = engine.answer_query("test query", 5, 'gpt-4o')
140
+ with patch('time.perf_counter', side_effect=[100.0, 100.0, 100.0, 100.0]):
141
+ result = asyncio.run(engine.answer_query("test query", 5, 'gpt-4o'))
142
+ actual_top_k_documents, actual_gpt_answer, actual_stats, all_docs_and_score, request_params = result
142
143
 
143
144
  expected_top_k_documents = [
144
145
  {'page_id': 3, 'title': 'title3'},
@@ -149,10 +150,11 @@ class TestEngine:
149
150
  ]
150
151
  expected_gpt_answer = llm_client.answer.return_value[0]
151
152
  expected_stats = {
152
- "retrieval_time": 0,
153
+ "retrieval_time": 0.0,
153
154
  "gpt_model": 'gpt-4o',
154
155
  "gpt_time": llm_client.answer.return_value[1],
155
- "tokens": llm_client.answer.return_value[2]
156
+ "tokens": llm_client.answer.return_value[2],
157
+ "answer_time": 0.0
156
158
  }
157
159
 
158
160
  assert expected_top_k_documents == actual_top_k_documents