Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions rre-tools/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ uv sync
uv sync --group dev
```

# remove all cached packages
uv cache clean
```

## Running Dataset Generator

Before running the command below, you need to have running search engine instance
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ search_engine_url: "http://localhost:8983/solr/"
# - "book"

# Maximum number of documents to retrieve from the search engine to generate queries
doc_number: 2
number_of_docs: 2

# List of fields from documents used to generate queries and relevance scoring
# These should match fields available in the search engine schema
Expand Down
2 changes: 1 addition & 1 deletion rre-tools/docs/dataset_generator/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ one fields, the documents are filtered for both fields (AND-like)
> - "fantasy"
> - type:
> - "book"
> - **doc_number**: Maximum number of documents to retrieve from the search engine to generate queries (e.g. 100)
> - **number_of_docs**: Maximum number of documents to retrieve from the search engine to generate queries (e.g. 100)
> - **doc_fields**: List of fields from documents used to generate queries and relevance scoring. These should match
> fields available in the search engine schema and must be at least one field
> - an example could be:
Expand Down
2 changes: 1 addition & 1 deletion rre-tools/src/rre_tools/dataset_generator/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class Config(BaseModel):
None,
description="Optional list of filter conditions for documents"
)
doc_number: int = Field(..., gt=0, description="Number of documents to retrieve from the search engine.")
number_of_docs: int = Field(..., gt=0, description="Number of documents to retrieve from the search engine.")
doc_fields: List[str] = Field(..., min_length=1, description="Fields used for context and scoring.")
queries: Optional[FilePath] = Field(None, description="Optional file containing predefined queries.")
generate_queries_from_documents: Optional[bool] = True
Expand Down
4 changes: 2 additions & 2 deletions rre-tools/src/rre_tools/dataset_generator/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ def generate_and_add_queries(config: Config, data_store: DataStore, llm_service:
"""Retrieve docs and generate queries with LLM Service. Adds docs, queries and ratings to the datastore."""
docs_to_generate_queries: List[Document] = search_engine.fetch_for_query_generation(
documents_filter=config.documents_filter,
doc_number=config.doc_number,
number_of_docs=config.number_of_docs,
doc_fields=config.doc_fields
)

Expand All @@ -68,7 +68,7 @@ def generate_and_add_queries(config: Config, data_store: DataStore, llm_service:
if remaining == 0:
return

num_queries_per_doc: int = int( (remaining // max(1, config.doc_number)) + 1) # always greater or equal to 1
num_queries_per_doc: int = int((remaining // max(1, config.number_of_docs)) + 1) # always greater or equal to 1
log.debug(f"Number of documents retrieved for generation: {len(docs_to_generate_queries)}")
log.debug(f"Pending queries to generate: {remaining}")
log.debug(f"Number of queries per document: {num_queries_per_doc}")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def _fetch_all_payload(self) -> Dict[str, Any]:

def fetch_for_query_generation(self,
documents_filter: Union[None, List[Dict[str, List[str]]]],
doc_number: int,
number_of_docs: int,
doc_fields: List[str],
start: int = 0) -> List[Document]:
"""
Expand All @@ -55,7 +55,7 @@ def fetch_for_query_generation(self,
Args:
documents_filter (Union[None, List[Dict[str, List[str]]]]): Optional list of field filters to apply.
Each filter is a dictionary mapping field names to allowed values.
doc_number (int): Number of documents to retrieve.
number_of_docs (int): Number of documents to retrieve.
doc_fields (List[str]): List of field names to include in the output.
start (int, optional): Starting index. Defaults to 0.

Expand Down Expand Up @@ -85,7 +85,7 @@ def fetch_for_query_generation(self,

# Construct the payload (Elasticsearch query body)
payload = {
"size": doc_number,
"size": number_of_docs,
"query": query,
"from": start,
"_source": doc_fields
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def _fetch_all_payload(self) -> Dict[str, Any]:

def fetch_for_query_generation(self,
documents_filter: Union[None, List[Dict[str, List[str]]]],
doc_number: int,
number_of_docs: int,
doc_fields: List[str],
start: int = 0) -> List[Document]:
"""Fetches a list of documents for query generation based on optional filters."""
Expand Down Expand Up @@ -75,7 +75,7 @@ def fetch_for_query_generation(self,
"query": query,
"_source": fields,
"from": start,
"size": doc_number
"size": number_of_docs
}

return self._search(payload)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from pydantic import HttpUrl
from rre_tools.shared.models.document import Document

DOC_NUMBER_EACH_FETCH = 100
NUMBER_OF_DOCS_EACH_FETCH = 100

class BaseSearchEngine(ABC):
def __init__(self, endpoint: HttpUrl):
Expand All @@ -31,18 +31,18 @@ def fetch_all(self, doc_fields: List[str]) -> Iterator[Document]:
while start < total_hits:
batch = self.fetch_for_query_generation(
documents_filter=None,
doc_number=DOC_NUMBER_EACH_FETCH,
number_of_docs=NUMBER_OF_DOCS_EACH_FETCH,
doc_fields=doc_fields,
start=start
)
if not batch:
break
for doc in batch:
yield doc
# if we didn't reach the end of the docs, then len(batch) == DOC_NUMBER_EACH_FETCH
# if we reached the end of the docs. then len(batch) <= DOC_NUMBER_EACH_FETCH -> next iteration we exit the
# loop since we are adding DOC_NUMBER_EACH_FETCH (not len(batch)) and start becomes greater than total_hits
start += DOC_NUMBER_EACH_FETCH
# if we didn't reach the end of the docs, then len(batch) == NUMBER_OF_DOCS_EACH_FETCH if we reached the
# end of the docs. then len(batch) <= NUMBER_OF_DOCS_EACH_FETCH -> next iteration we exit the loop since
# we are adding NUMBER_OF_DOCS_EACH_FETCH (not len(batch)) and start becomes greater than total_hits
start += NUMBER_OF_DOCS_EACH_FETCH


def _parse_query_template(self, path: Path | str) -> Dict[str, Any]:
Expand Down Expand Up @@ -72,7 +72,7 @@ def _replace_placeholder(self, obj: Any, placeholder: str, keyword: str | None)
@abstractmethod
def fetch_for_query_generation(self,
documents_filter: Union[None, List[Dict[str, List[str]]]],
doc_number: int,
number_of_docs: int,
doc_fields: List[str],
start: int = 0) \
-> List[Document]:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,22 +57,22 @@ def _get_total_hits(self, payload: Dict[str, Any]) -> int:

def fetch_for_query_generation(self,
documents_filter: Union[None, List[Dict[str, List[str]]]],
doc_number: int, doc_fields: List[str], start: int = 0) \
number_of_docs: int, doc_fields: List[str], start: int = 0) \
-> List[Document]:
"""
Fetches a set of documents from Solr for the purpose of query generation.

Args:
documents_filter (Union[None, List[Dict[str, List[str]]]]): Optional filter constraints for fields and their allowed values.
doc_number (int): Number of documents to retrieve.
number_of_docs (int): Number of documents to retrieve.
doc_fields (List[str]): List of field names to include in the output.
start (int, optional): Starting index of the query. Defaults to 0.

Returns:
List[Document]: A list of retrieved documents as `Document` objects.
"""
payload: Dict[str, Any] = self._fetch_all_payload
payload['rows'] = doc_number
payload['rows'] = number_of_docs
payload['start'] = start
payload['fl'] = self._unify_fields(doc_fields)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ def _normalize_field_value(v: Any) -> List[str]:
def fetch_for_query_generation(
self,
documents_filter: Union[None, List[Dict[str, List[str]]]],
doc_number: int,
number_of_docs: int,
doc_fields: Optional[List[str]],
start: int = 0,
) -> List[Document]:
Expand All @@ -140,7 +140,7 @@ def fetch_for_query_generation(

Args:
documents_filter: Optional list of filter dictionaries for query restriction.
doc_number: Number of documents to retrieve.
number_of_docs: Number of documents to retrieve.
doc_fields: Optional list of fields to include in the response.
start: Optional start index to retrieve documents from.

Expand All @@ -155,7 +155,7 @@ def fetch_for_query_generation(
yql = self._build_yql(doc_fields or [], where)

payload["yql"] = yql
payload["hits"] = int(doc_number)
payload["hits"] = int(number_of_docs)
payload["presentation.format"] = "json"
payload['offset'] = start

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def test_good_config__expects__all_parameters_read(config):
{"genre": ["horror", "fantasy"]},
{"type": ["book"]}
]
assert config.doc_number == 100
assert config.number_of_docs == 100
assert config.doc_fields == ["title", "description"]
assert config.queries == FilePath("tests/resources/queries.txt")
assert config.generate_queries_from_documents is True
Expand Down Expand Up @@ -55,7 +55,7 @@ def test_missing_required_field__expects__raises_validation_error(resource_folde
_ = Config.load(resource_folder / file_name)


def test_invalid_doc_number_type__expects__raises_validation_error(resource_folder):
def test_invalid_number_of_docs_type__expects__raises_validation_error(resource_folder):
file_name = "invalid_type.yaml"
with pytest.raises(ValidationError):
_ = Config.load(resource_folder / file_name)
Expand Down Expand Up @@ -85,7 +85,7 @@ def test_autosave_valid_positive_int__expects__parsed(tmp_path):
"search_engine_type: \"solr\"\n"
"collection_name: \"testcore\"\n"
"search_engine_url: \"http://localhost:8983/solr/\"\n"
"doc_number: 2\n"
"number_of_docs: 2\n"
"doc_fields: [\"title\"]\n"
"num_queries_needed: 2\n"
"relevance_scale: \"binary\"\n"
Expand All @@ -107,7 +107,7 @@ def test_autosave_invalid_non_positive__expects__raises_validation_error(tmp_pat
"search_engine_type: \"solr\"\n"
"collection_name: \"testcore\"\n"
"search_engine_url: \"http://localhost:8983/solr/\"\n"
"doc_number: 2\n"
"number_of_docs: 2\n"
"doc_fields: [\"title\"]\n"
"num_queries_needed: 2\n"
"relevance_scale: \"binary\"\n"
Expand Down
2 changes: 1 addition & 1 deletion rre-tools/tests/dataset_generator/test_main_autosave.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def test_main_passes_autosave_option_to_datastore(monkeypatch, tmp_path: Path):
collection_name="testcore",
search_engine_url="http://localhost:8983/solr/",
documents_filter=None,
doc_number=1,
number_of_docs=1,
doc_fields=["title"],
queries=None,
generate_queries_from_documents=False,
Expand Down
2 changes: 1 addition & 1 deletion rre-tools/tests/resources/good_config_elasticsearch.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ query_template: "tests/resources/template_elasticsearch.json"
search_engine_type: "elasticsearch"
collection_name: "testcore"
search_engine_url: 'http://localhost:9200/'
doc_number: 100
number_of_docs: 100
doc_fields:
- "title"
- "description"
Expand Down
2 changes: 1 addition & 1 deletion rre-tools/tests/resources/good_config_opensearch.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ query_template: "tests/resources/template_opensearch.json"
search_engine_type: "opensearch"
collection_name: "testcore"
search_engine_url: "http://localhost:9200/"
doc_number: 20
number_of_docs: 20
doc_fields:
- "title"
- "description"
Expand Down
2 changes: 1 addition & 1 deletion rre-tools/tests/resources/good_config_solr.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ documents_filter:
- "fantasy"
- type:
- "book"
doc_number: 100
number_of_docs: 100
doc_fields:
- "title"
- "description"
Expand Down
2 changes: 1 addition & 1 deletion rre-tools/tests/resources/good_config_vespa.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ documents_filter:
- "fantasy"
- type:
- "news"
doc_number: 100
number_of_docs: 100
doc_fields:
- "title"
- "description"
Expand Down
2 changes: 1 addition & 1 deletion rre-tools/tests/resources/invalid_type.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
search_engine_type: "solr"
search_engine_url: "http://localhost"
doc_number: "one hundred" # Invalid type
number_of_docs: "one hundred" # Invalid type
doc_fields: ["title"]
num_queries_needed: 5
relevance_scale: "binary"
Expand Down
2 changes: 1 addition & 1 deletion rre-tools/tests/resources/missing_both_templates.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
search_engine_type: "solr"
collection_name: "testcore"
search_engine_url: "http://localhost"
doc_number: 10
number_of_docs: 10
doc_fields: ["title"]
num_queries_needed: 5
relevance_scale: "binary"
Expand Down
2 changes: 1 addition & 1 deletion rre-tools/tests/resources/missing_optional.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
search_engine_type: "solr"
collection_name: "testcore"
search_engine_url: "http://localhost"
doc_number: 10
number_of_docs: 10
doc_fields: ["title"]
num_queries_needed: 5
relevance_scale: "binary"
Expand Down
2 changes: 1 addition & 1 deletion rre-tools/tests/resources/missing_required.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
search_engine_type: "solr"
collection_name: "testcore"
# Missing: search_engine_url
doc_number: 100
number_of_docs: 100
doc_fields: ["title"]
num_queries_needed: 5
relevance_scale: "binary"
Expand Down
2 changes: 1 addition & 1 deletion rre-tools/tests/resources/mteb_config.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
search_engine_type: "solr"
collection_name: "testcore"
search_engine_url: "http://localhost:8983/solr/"
doc_number: 100
number_of_docs: 100
doc_fields:
- "title"
- "description"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

from rre_tools.shared.logger import configure_logging
from rre_tools.dataset_generator.config import Config
from rre_tools.shared.search_engines.search_engine_base import DOC_NUMBER_EACH_FETCH
from rre_tools.shared.search_engines.search_engine_base import NUMBER_OF_DOCS_EACH_FETCH
from mocks.elasticsearch import MockResponseElasticsearchEngine

from rre_tools.shared.search_engines import ElasticsearchSearchEngine
Expand Down Expand Up @@ -47,7 +47,7 @@ def test_elasticsearch_search_engine_fetch_for_query_generation__expects__result
)
# search_engine.extract_documents_to_generate_queries, which contains requests.post, uses the monkeypatch
result = search_engine.fetch_for_query_generation(documents_filter=elasticsearch_config.documents_filter,
doc_number=elasticsearch_config.doc_number,
number_of_docs=elasticsearch_config.number_of_docs,
doc_fields=elasticsearch_config.doc_fields)
assert result[0] == Document(**mock_dict)

Expand All @@ -73,9 +73,9 @@ def test_elasticsearch_engine_fetch_all__expects__results_returned(monkeypatch,
def mock_post(*args, **kwargs):
call_counter["count"] += 1
if call_counter["count"] == 1:
return MockResponseElasticsearchEngine(json_data=[], total_hits=2* DOC_NUMBER_EACH_FETCH, status_code=200)
return MockResponseElasticsearchEngine(json_data=[], total_hits=2 * NUMBER_OF_DOCS_EACH_FETCH, status_code=200)
elif call_counter["count"] == 2 or call_counter["count"] == 3:
return MockResponseElasticsearchEngine(json_data=[mock_doc] * DOC_NUMBER_EACH_FETCH, status_code=200)
return MockResponseElasticsearchEngine(json_data=[mock_doc] * NUMBER_OF_DOCS_EACH_FETCH, status_code=200)
else:
return MockResponseElasticsearchEngine(json_data=[], status_code=200)

Expand All @@ -89,7 +89,7 @@ def mock_post(*args, **kwargs):
doc_list = [first]
for doc in result:
doc_list.append(doc)
assert len(doc_list) == 2 * DOC_NUMBER_EACH_FETCH
assert len(doc_list) == 2 * NUMBER_OF_DOCS_EACH_FETCH


def test_elasticsearch_search_engine_negative_post_fetch_for_query_generation__expects__raises_http_error(monkeypatch, elasticsearch_config):
Expand All @@ -102,7 +102,7 @@ def test_elasticsearch_search_engine_negative_post_fetch_for_query_generation__e
with pytest.raises(HTTPError):
search_engine.fetch_for_query_generation(
documents_filter=elasticsearch_config.documents_filter,
doc_number=elasticsearch_config.doc_number,
number_of_docs=elasticsearch_config.number_of_docs,
doc_fields=elasticsearch_config.doc_fields
)

Expand Down
Loading