-
Notifications
You must be signed in to change notification settings - Fork 38
DAGE-47: Add MtebWriter #200
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
nseidan
wants to merge
5
commits into
dataset-generator
Choose a base branch
from
DAGE-47_mteb_writer
base: dataset-generator
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,32 @@ | ||
import argparse | ||
import re | ||
import html | ||
from pathlib import Path | ||
|
||
import re | ||
from typing import Any | ||
|
||
_TAG_REGEX = re.compile('<.*?>') | ||
|
||
|
||
def parse_args(): | ||
parser = argparse.ArgumentParser(description='Parse arguments for CLI.') | ||
|
||
parser.add_argument('-c', '--config_file', type=str, | ||
help='Config file path to use for the application [default: \"config.yaml\"]', | ||
required=False, default="config.yaml") | ||
|
||
parser.add_argument('-v', '--verbose',action='store_true', | ||
parser.add_argument('-v', '--verbose', action='store_true', | ||
help='Activate debug mode for logging [default: False]') | ||
|
||
return parser.parse_args() | ||
|
||
|
||
def clean_text(text: str) -> str: | ||
text_without_html = re.sub(_TAG_REGEX, '', text).strip() | ||
return html.unescape(re.sub(r"\s{2,}", " ", text_without_html)) | ||
|
||
|
||
def _to_string(value: Any) -> str: | ||
if value is None: | ||
return "" | ||
if isinstance(value, (list, tuple)): | ||
return " ".join(str(val) for val in value if val is not None) | ||
return str(value) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
import json | ||
import logging | ||
import os | ||
from pathlib import Path | ||
|
||
from src.config import Config | ||
from src.search_engine.data_store import DataStore | ||
from src.utils import _to_string | ||
from src.writers.abstract_writer import AbstractWriter | ||
|
||
log = logging.getLogger(__name__) | ||
|
||
|
||
class MtebWriter(AbstractWriter): | ||
""" | ||
MtebWriter: Write data namely corpus, queries, and candidates to JSONL file for MTEB | ||
https://github.com/embeddings-benchmark/mteb | ||
|
||
Corpus format: id,title,text | ||
Queries format: id,text | ||
Candidates format: query_id,doc_id,rating | ||
""" | ||
|
||
@classmethod | ||
def build(cls, config: Config, data_store: DataStore): | ||
return cls(datastore=data_store) | ||
|
||
def _write_corpus(self, corpus_path: Path) -> None: | ||
""" | ||
Writes corpus records extracted from search engine to JSONL file: | ||
{"id": <doc_id>, "title": <title>, "text": <description>} | ||
""" | ||
with corpus_path.open("w", encoding="utf-8") as file: | ||
for doc in self.datastore.get_documents(): | ||
doc_id = str(doc.id) | ||
fields = doc.fields | ||
title = _to_string(fields.get("title")) | ||
text = _to_string(fields.get("description")) | ||
|
||
row = {"id": doc_id, "title": title, "text": text} | ||
file.write(json.dumps(row, ensure_ascii=False) + "\n") | ||
|
||
def _write_queries(self, queries_path: Path) -> None: | ||
""" | ||
Writes queries LLM-generated and/or user-defined records to JSONL file: | ||
{"id": <query_id>, "text": <query_text>} | ||
""" | ||
with queries_path.open("w", encoding="utf-8") as file: | ||
for query_context in self.datastore.get_queries(): | ||
query_id = query_context.get_query_id() | ||
query_text = query_context.get_query_text() | ||
|
||
row = {"id": query_id, "text": query_text} | ||
file.write(json.dumps(row, ensure_ascii=False) + "\n") | ||
|
||
def _write_candidates(self, candidates_path: Path) -> None: | ||
""" | ||
Writes candidates to JSONL file: | ||
{"query_id": <query_id>, "doc_id": <doc_id>, "rating": <rating_score>} | ||
""" | ||
with candidates_path.open("w", encoding="utf-8") as file: | ||
for query_context in self.datastore.get_queries(): | ||
query_id = query_context.get_query_id() | ||
for doc_id in query_context.get_doc_ids(): | ||
if query_context.has_rating_score(doc_id): | ||
rating_score = query_context.get_rating_score(doc_id) | ||
|
||
row = {"query_id": query_id, "doc_id": doc_id, "rating": rating_score} | ||
file.write(json.dumps(row, ensure_ascii=False) + "\n") | ||
|
||
def write(self, output_path: str | Path) -> None: | ||
""" | ||
Write corpus, queries, and candidates JSONL files for MTEB. | ||
""" | ||
path = Path(output_path) | ||
os.makedirs(path, exist_ok=True) | ||
try: | ||
self._write_corpus(path / "corpus.jsonl") | ||
log.info("Corpus written successfully") | ||
|
||
self._write_queries(path / "queries.jsonl") | ||
log.info("Queries written successfully") | ||
|
||
self._write_candidates(path / "candidates.jsonl") | ||
log.info("Candidates written successfully") | ||
|
||
except Exception as e: | ||
log.exception("Failed to write MTEB files: %s", e) | ||
raise |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
rre-dataset-generator/tests/unit/resources/mteb_config.yaml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
query_template: "q=#$query##&fq=genre:horror&wt=json" | ||
search_engine_type: "solr" | ||
index_name: "testcore" | ||
search_engine_collection_endpoint: "http://localhost:8983/solr/testcore/" | ||
doc_number: 100 | ||
doc_fields: | ||
- "title" | ||
- "description" | ||
queries: "tests/unit/resources/queries.txt" | ||
generate_queries_from_documents: true | ||
num_queries_needed: 10 | ||
relevance_scale: "graded" | ||
llm_configuration_file: "tests/unit/resources/llm_config.yaml" | ||
output_format: "mteb" | ||
output_destination: "output" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.