2022-06-29 21:09:48 +02:00
|
|
|
# System Packages
|
2022-09-10 12:05:21 +02:00
|
|
|
from copy import deepcopy
|
2022-06-29 21:09:48 +02:00
|
|
|
from pathlib import Path
|
|
|
|
|
2022-09-10 12:05:21 +02:00
|
|
|
# External Packages
|
|
|
|
import pytest
|
|
|
|
|
2021-10-03 05:28:33 +02:00
|
|
|
# Internal Packages
|
2022-08-06 02:05:35 +02:00
|
|
|
from src.utils.state import model
|
2022-07-21 16:05:43 +02:00
|
|
|
from src.search_type import text_search
|
2023-01-09 20:17:36 +01:00
|
|
|
from src.utils.rawconfig import ContentConfig, SearchConfig, TextContentConfig
|
2022-09-14 09:53:43 +02:00
|
|
|
from src.processor.org_mode.org_to_jsonl import OrgToJsonl
|
2021-10-03 05:28:33 +02:00
|
|
|
|
|
|
|
|
|
|
|
# Test
|
2022-09-11 00:09:24 +02:00
|
|
|
# ----------------------------------------------------------------------------------------------------
|
2023-01-09 20:17:36 +01:00
|
|
|
def test_asymmetric_setup_with_missing_file_raises_error(org_config_with_only_new_file: TextContentConfig, search_config: SearchConfig):
|
2022-09-11 00:09:24 +02:00
|
|
|
# Arrange
|
2023-01-09 20:17:36 +01:00
|
|
|
# Ensure file mentioned in org.input-files is missing
|
|
|
|
single_new_file = Path(org_config_with_only_new_file.input_files[0])
|
|
|
|
single_new_file.unlink()
|
2022-09-11 00:09:24 +02:00
|
|
|
|
|
|
|
# Act
|
|
|
|
# Generate notes embeddings during asymmetric setup
|
|
|
|
with pytest.raises(FileNotFoundError):
|
2023-01-09 20:17:36 +01:00
|
|
|
text_search.setup(OrgToJsonl, org_config_with_only_new_file, search_config.asymmetric, regenerate=True)
|
2022-09-11 00:09:24 +02:00
|
|
|
|
|
|
|
|
2022-09-10 12:05:21 +02:00
|
|
|
# ----------------------------------------------------------------------------------------------------
|
2023-01-09 20:17:36 +01:00
|
|
|
def test_asymmetric_setup_with_empty_file_raises_error(org_config_with_only_new_file: TextContentConfig, search_config: SearchConfig):
|
2022-09-10 12:05:21 +02:00
|
|
|
# Act
|
|
|
|
# Generate notes embeddings during asymmetric setup
|
|
|
|
with pytest.raises(ValueError, match=r'^No valid entries found*'):
|
2023-01-09 20:17:36 +01:00
|
|
|
text_search.setup(OrgToJsonl, org_config_with_only_new_file, search_config.asymmetric, regenerate=True)
|
2022-09-10 12:05:21 +02:00
|
|
|
|
|
|
|
|
2021-10-03 05:28:33 +02:00
|
|
|
# ----------------------------------------------------------------------------------------------------
|
2022-01-15 02:54:38 +01:00
|
|
|
def test_asymmetric_setup(content_config: ContentConfig, search_config: SearchConfig):
|
2021-10-03 05:28:33 +02:00
|
|
|
# Act
|
|
|
|
# Regenerate notes embeddings during asymmetric setup
|
2022-09-14 09:53:43 +02:00
|
|
|
notes_model = text_search.setup(OrgToJsonl, content_config.org, search_config.asymmetric, regenerate=True)
|
2021-10-03 05:28:33 +02:00
|
|
|
|
|
|
|
# Assert
|
|
|
|
assert len(notes_model.entries) == 10
|
|
|
|
assert len(notes_model.corpus_embeddings) == 10
|
|
|
|
|
|
|
|
|
|
|
|
# ----------------------------------------------------------------------------------------------------
|
2022-01-15 02:54:38 +01:00
|
|
|
def test_asymmetric_search(content_config: ContentConfig, search_config: SearchConfig):
|
2021-10-03 05:28:33 +02:00
|
|
|
# Arrange
|
2022-09-14 09:53:43 +02:00
|
|
|
model.notes_search = text_search.setup(OrgToJsonl, content_config.org, search_config.asymmetric, regenerate=True)
|
2021-10-03 05:28:33 +02:00
|
|
|
query = "How to git install application?"
|
|
|
|
|
|
|
|
# Act
|
2022-07-21 16:05:43 +02:00
|
|
|
hits, entries = text_search.query(
|
2021-10-03 05:28:33 +02:00
|
|
|
query,
|
2022-07-26 20:56:36 +02:00
|
|
|
model = model.notes_search,
|
|
|
|
rank_results=True)
|
2021-10-03 05:28:33 +02:00
|
|
|
|
2022-07-21 16:05:43 +02:00
|
|
|
results = text_search.collate_results(
|
2021-10-03 05:28:33 +02:00
|
|
|
hits,
|
2022-07-12 16:48:49 +02:00
|
|
|
entries,
|
2021-10-03 05:28:33 +02:00
|
|
|
count=1)
|
|
|
|
|
|
|
|
# Assert
|
2022-07-19 16:26:16 +02:00
|
|
|
# Actual_data should contain "Khoj via Emacs" entry
|
2022-09-15 22:34:43 +02:00
|
|
|
search_result = results[0].entry
|
2021-10-03 05:28:33 +02:00
|
|
|
assert "git clone" in search_result
|
2022-06-29 21:09:48 +02:00
|
|
|
|
|
|
|
|
2022-12-23 19:52:02 +01:00
|
|
|
# ----------------------------------------------------------------------------------------------------
|
2023-01-09 20:17:36 +01:00
|
|
|
def test_entry_chunking_by_max_tokens(org_config_with_only_new_file: TextContentConfig, search_config: SearchConfig):
|
2022-12-23 19:52:02 +01:00
|
|
|
# Arrange
|
2022-12-26 01:45:40 +01:00
|
|
|
# Insert org-mode entry with size exceeding max token limit to new org file
|
2022-12-23 19:52:02 +01:00
|
|
|
max_tokens = 256
|
2023-01-09 20:17:36 +01:00
|
|
|
new_file_to_index = Path(org_config_with_only_new_file.input_files[0])
|
|
|
|
with open(new_file_to_index, "w") as f:
|
2022-12-23 19:52:02 +01:00
|
|
|
f.write(f"* Entry more than {max_tokens} words\n")
|
|
|
|
for index in range(max_tokens+1):
|
|
|
|
f.write(f"{index} ")
|
|
|
|
|
|
|
|
# Act
|
|
|
|
# reload embeddings, entries, notes model after adding new org-mode file
|
2023-01-09 20:17:36 +01:00
|
|
|
initial_notes_model = text_search.setup(OrgToJsonl, org_config_with_only_new_file, search_config.asymmetric, regenerate=False)
|
2022-12-23 19:52:02 +01:00
|
|
|
|
|
|
|
# Assert
|
|
|
|
# verify newly added org-mode entry is split by max tokens
|
2023-01-09 20:17:36 +01:00
|
|
|
assert len(initial_notes_model.entries) == 2
|
|
|
|
assert len(initial_notes_model.corpus_embeddings) == 2
|
2022-12-23 19:52:02 +01:00
|
|
|
|
|
|
|
|
2022-06-29 21:09:48 +02:00
|
|
|
# ----------------------------------------------------------------------------------------------------
|
2023-01-09 20:17:36 +01:00
|
|
|
def test_asymmetric_reload(content_config: ContentConfig, search_config: SearchConfig, new_org_file: Path):
|
2022-06-29 21:09:48 +02:00
|
|
|
# Arrange
|
2022-12-23 20:18:22 +01:00
|
|
|
initial_notes_model= text_search.setup(OrgToJsonl, content_config.org, search_config.asymmetric, regenerate=True)
|
2022-06-29 21:09:48 +02:00
|
|
|
|
|
|
|
assert len(initial_notes_model.entries) == 10
|
|
|
|
assert len(initial_notes_model.corpus_embeddings) == 10
|
|
|
|
|
2023-01-09 20:17:36 +01:00
|
|
|
# append org-mode entry to first org input file in config
|
|
|
|
content_config.org.input_files = [f'{new_org_file}']
|
|
|
|
with open(new_org_file, "w") as f:
|
2022-06-29 21:09:48 +02:00
|
|
|
f.write("\n* A Chihuahua doing Tango\n- Saw a super cute video of a chihuahua doing the Tango on Youtube\n")
|
|
|
|
|
|
|
|
# regenerate notes jsonl, model embeddings and model to include entry from new file
|
2022-09-14 09:53:43 +02:00
|
|
|
regenerated_notes_model = text_search.setup(OrgToJsonl, content_config.org, search_config.asymmetric, regenerate=True)
|
2022-06-29 21:09:48 +02:00
|
|
|
|
|
|
|
# Act
|
|
|
|
# reload embeddings, entries, notes model from previously generated notes jsonl and model embeddings files
|
2022-09-14 09:53:43 +02:00
|
|
|
initial_notes_model = text_search.setup(OrgToJsonl, content_config.org, search_config.asymmetric, regenerate=False)
|
2022-06-29 21:09:48 +02:00
|
|
|
|
|
|
|
# Assert
|
|
|
|
assert len(regenerated_notes_model.entries) == 11
|
|
|
|
assert len(regenerated_notes_model.corpus_embeddings) == 11
|
|
|
|
|
2023-01-09 20:17:36 +01:00
|
|
|
# Assert
|
2022-06-29 21:09:48 +02:00
|
|
|
# verify new entry loaded from updated embeddings, entries
|
|
|
|
assert len(initial_notes_model.entries) == 11
|
|
|
|
assert len(initial_notes_model.corpus_embeddings) == 11
|
|
|
|
|
|
|
|
# Cleanup
|
2023-01-09 20:17:36 +01:00
|
|
|
# reset input_files in config to empty list
|
2022-07-06 23:25:31 +02:00
|
|
|
content_config.org.input_files = []
|
2022-09-07 02:06:29 +02:00
|
|
|
|
|
|
|
|
|
|
|
# ----------------------------------------------------------------------------------------------------
|
2023-01-09 20:17:36 +01:00
|
|
|
def test_incremental_update(content_config: ContentConfig, search_config: SearchConfig, new_org_file: Path):
|
2022-09-07 02:06:29 +02:00
|
|
|
# Arrange
|
2022-09-14 09:53:43 +02:00
|
|
|
initial_notes_model = text_search.setup(OrgToJsonl, content_config.org, search_config.asymmetric, regenerate=True)
|
2022-09-07 02:06:29 +02:00
|
|
|
|
|
|
|
assert len(initial_notes_model.entries) == 10
|
|
|
|
assert len(initial_notes_model.corpus_embeddings) == 10
|
|
|
|
|
2023-01-09 20:17:36 +01:00
|
|
|
# append org-mode entry to first org input file in config
|
|
|
|
with open(new_org_file, "w") as f:
|
2022-09-07 02:06:29 +02:00
|
|
|
f.write("\n* A Chihuahua doing Tango\n- Saw a super cute video of a chihuahua doing the Tango on Youtube\n")
|
|
|
|
|
|
|
|
# Act
|
|
|
|
# update embeddings, entries with the newly added note
|
2023-01-09 20:17:36 +01:00
|
|
|
content_config.org.input_files = [f'{new_org_file}']
|
2022-09-14 09:53:43 +02:00
|
|
|
initial_notes_model = text_search.setup(OrgToJsonl, content_config.org, search_config.asymmetric, regenerate=False)
|
2022-09-07 02:06:29 +02:00
|
|
|
|
2023-01-09 20:17:36 +01:00
|
|
|
# Assert
|
2022-09-07 02:06:29 +02:00
|
|
|
# verify new entry added in updated embeddings, entries
|
|
|
|
assert len(initial_notes_model.entries) == 11
|
|
|
|
assert len(initial_notes_model.corpus_embeddings) == 11
|
|
|
|
|
|
|
|
# Cleanup
|
2023-01-09 20:17:36 +01:00
|
|
|
# reset input_files in config to empty list
|
2022-09-07 02:06:29 +02:00
|
|
|
content_config.org.input_files = []
|