kolzchut-ragbot 1.7.3__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.
- {kolzchut_ragbot-1.7.3 → kolzchut_ragbot-1.7.4}/PKG-INFO +1 -1
- {kolzchut_ragbot-1.7.3 → kolzchut_ragbot-1.7.4}/kolzchut_ragbot/engine.py +99 -17
- {kolzchut_ragbot-1.7.3 → kolzchut_ragbot-1.7.4}/kolzchut_ragbot.egg-info/PKG-INFO +1 -1
- {kolzchut_ragbot-1.7.3 → kolzchut_ragbot-1.7.4}/pyproject.toml +1 -1
- {kolzchut_ragbot-1.7.3 → kolzchut_ragbot-1.7.4}/setup.py +1 -1
- {kolzchut_ragbot-1.7.3 → kolzchut_ragbot-1.7.4}/test/test_engine.py +7 -5
- {kolzchut_ragbot-1.7.3 → kolzchut_ragbot-1.7.4}/README.md +0 -0
- {kolzchut_ragbot-1.7.3 → kolzchut_ragbot-1.7.4}/kolzchut_ragbot/Document.py +0 -0
- {kolzchut_ragbot-1.7.3 → kolzchut_ragbot-1.7.4}/kolzchut_ragbot/IntegrateService.py +0 -0
- {kolzchut_ragbot-1.7.3 → kolzchut_ragbot-1.7.4}/kolzchut_ragbot/__init__.py +0 -0
- {kolzchut_ragbot-1.7.3 → kolzchut_ragbot-1.7.4}/kolzchut_ragbot/config.py +0 -0
- {kolzchut_ragbot-1.7.3 → kolzchut_ragbot-1.7.4}/kolzchut_ragbot/get_full_documents_utilities.py +0 -0
- {kolzchut_ragbot-1.7.3 → kolzchut_ragbot-1.7.4}/kolzchut_ragbot/llm_client.py +0 -0
- {kolzchut_ragbot-1.7.3 → kolzchut_ragbot-1.7.4}/kolzchut_ragbot/model.py +0 -0
- {kolzchut_ragbot-1.7.3 → kolzchut_ragbot-1.7.4}/kolzchut_ragbot.egg-info/SOURCES.txt +0 -0
- {kolzchut_ragbot-1.7.3 → kolzchut_ragbot-1.7.4}/kolzchut_ragbot.egg-info/dependency_links.txt +0 -0
- {kolzchut_ragbot-1.7.3 → kolzchut_ragbot-1.7.4}/kolzchut_ragbot.egg-info/requires.txt +0 -0
- {kolzchut_ragbot-1.7.3 → kolzchut_ragbot-1.7.4}/kolzchut_ragbot.egg-info/top_level.txt +0 -0
- {kolzchut_ragbot-1.7.3 → kolzchut_ragbot-1.7.4}/setup.cfg +0 -0
- {kolzchut_ragbot-1.7.3 → kolzchut_ragbot-1.7.4}/test/test_configs.py +0 -0
- {kolzchut_ragbot-1.7.3 → kolzchut_ragbot-1.7.4}/test/test_docs.py +0 -0
- {kolzchut_ragbot-1.7.3 → kolzchut_ragbot-1.7.4}/test/test_document.py +0 -0
- {kolzchut_ragbot-1.7.3 → kolzchut_ragbot-1.7.4}/test/test_model.py +0 -0
@@ -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
|
220
|
+
def get_page_content_by_page_id(self, page_id: int) -> tuple:
|
220
221
|
"""
|
221
|
-
|
222
|
+
Fetches the full content of a page and measures how long it takes.
|
222
223
|
|
223
224
|
Args:
|
224
|
-
|
225
|
-
|
226
|
-
|
227
|
-
|
228
|
-
|
229
|
-
|
230
|
-
|
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:
|
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(
|
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
|
-
|
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
|
-
|
247
|
-
|
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:
|
@@ -7,7 +7,7 @@ from setuptools import setup, find_packages
|
|
7
7
|
|
8
8
|
setup(
|
9
9
|
name='kolzchut-ragbot',
|
10
|
-
version='1.7.
|
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
|
-
|
140
|
-
|
141
|
-
actual_top_k_documents, actual_gpt_answer, actual_stats, all_docs_and_score, request_params =
|
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
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
{kolzchut_ragbot-1.7.3 → kolzchut_ragbot-1.7.4}/kolzchut_ragbot/get_full_documents_utilities.py
RENAMED
File without changes
|
File without changes
|
File without changes
|
File without changes
|
{kolzchut_ragbot-1.7.3 → kolzchut_ragbot-1.7.4}/kolzchut_ragbot.egg-info/dependency_links.txt
RENAMED
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|