2022-06-29 21:09:48 +02:00
|
|
|
# System Packages
|
2023-05-12 11:37:34 +02:00
|
|
|
import logging
|
2022-06-29 21:09:48 +02:00
|
|
|
from pathlib import Path
|
2023-06-14 01:55:58 +02:00
|
|
|
import os
|
2022-06-29 21:09:48 +02:00
|
|
|
|
2022-09-10 12:05:21 +02:00
|
|
|
# External Packages
|
|
|
|
import pytest
|
2023-07-15 23:33:15 +02:00
|
|
|
import torch
|
2023-07-14 10:19:38 +02:00
|
|
|
from khoj.utils.config import SearchModels
|
2022-09-10 12:05:21 +02:00
|
|
|
|
2021-10-03 05:28:33 +02:00
|
|
|
# Internal Packages
|
2023-07-14 10:19:38 +02:00
|
|
|
from khoj.utils.state import content_index, search_models
|
2023-02-14 21:50:51 +01:00
|
|
|
from khoj.search_type import text_search
|
|
|
|
from khoj.utils.rawconfig import ContentConfig, SearchConfig, TextContentConfig
|
|
|
|
from khoj.processor.org_mode.org_to_jsonl import OrgToJsonl
|
2023-06-14 01:55:58 +02:00
|
|
|
from khoj.processor.github.github_to_jsonl import GithubToJsonl
|
2021-10-03 05:28:33 +02:00
|
|
|
|
|
|
|
|
|
|
|
# Test
|
2022-09-11 00:09:24 +02:00
|
|
|
# ----------------------------------------------------------------------------------------------------
|
2023-02-17 17:04:26 +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-02-17 17:04:26 +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
|
2023-02-17 17:04:26 +01:00
|
|
|
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
|
|
|
# ----------------------------------------------------------------------------------------------------
|
2023-07-14 10:19:38 +02:00
|
|
|
def test_asymmetric_setup(content_config: ContentConfig, search_models: SearchModels):
|
2021-10-03 05:28:33 +02:00
|
|
|
# Act
|
|
|
|
# Regenerate notes embeddings during asymmetric setup
|
2023-07-14 10:19:38 +02:00
|
|
|
notes_model = text_search.setup(
|
|
|
|
OrgToJsonl, content_config.org, search_models.text_search.bi_encoder, regenerate=True
|
|
|
|
)
|
2021-10-03 05:28:33 +02:00
|
|
|
|
|
|
|
# Assert
|
|
|
|
assert len(notes_model.entries) == 10
|
|
|
|
assert len(notes_model.corpus_embeddings) == 10
|
|
|
|
|
|
|
|
|
2023-05-12 11:37:34 +02:00
|
|
|
# ----------------------------------------------------------------------------------------------------
|
2023-07-14 10:19:38 +02:00
|
|
|
def test_text_content_index_only_updates_on_changes(content_config: ContentConfig, search_models: SearchModels, caplog):
|
2023-05-12 11:37:34 +02:00
|
|
|
# Arrange
|
|
|
|
caplog.set_level(logging.INFO, logger="khoj")
|
|
|
|
|
|
|
|
# Act
|
|
|
|
# Generate initial notes embeddings during asymmetric setup
|
2023-07-14 10:19:38 +02:00
|
|
|
text_search.setup(OrgToJsonl, content_config.org, search_models.text_search.bi_encoder, regenerate=True)
|
2023-05-12 11:37:34 +02:00
|
|
|
initial_logs = caplog.text
|
|
|
|
caplog.clear() # Clear logs
|
|
|
|
|
|
|
|
# Run asymmetric setup again with no changes to data source. Ensure index is not updated
|
2023-07-14 10:19:38 +02:00
|
|
|
text_search.setup(OrgToJsonl, content_config.org, search_models.text_search.bi_encoder, regenerate=False)
|
2023-05-12 11:37:34 +02:00
|
|
|
final_logs = caplog.text
|
|
|
|
|
|
|
|
# Assert
|
|
|
|
assert "📩 Saved computed text embeddings to" in initial_logs
|
|
|
|
assert "📩 Saved computed text embeddings to" not in final_logs
|
|
|
|
|
|
|
|
|
2021-10-03 05:28:33 +02:00
|
|
|
# ----------------------------------------------------------------------------------------------------
|
2023-06-29 05:11:26 +02:00
|
|
|
@pytest.mark.anyio
|
|
|
|
async def test_asymmetric_search(content_config: ContentConfig, search_config: SearchConfig):
|
2021-10-03 05:28:33 +02:00
|
|
|
# Arrange
|
2023-07-14 10:19:38 +02:00
|
|
|
search_models.text_search = text_search.initialize_model(search_config.asymmetric)
|
|
|
|
content_index.org = text_search.setup(
|
|
|
|
OrgToJsonl, content_config.org, search_models.text_search.bi_encoder, regenerate=True
|
|
|
|
)
|
2021-10-03 05:28:33 +02:00
|
|
|
query = "How to git install application?"
|
|
|
|
|
|
|
|
# Act
|
2023-07-14 10:19:38 +02:00
|
|
|
hits, entries = await text_search.query(
|
|
|
|
query, search_model=search_models.text_search, content=content_index.org, rank_results=True
|
|
|
|
)
|
2021-10-03 05:28:33 +02:00
|
|
|
|
2023-02-17 17:04:26 +01:00
|
|
|
results = text_search.collate_results(hits, entries, count=1)
|
2021-10-03 05:28:33 +02:00
|
|
|
|
|
|
|
# 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-07-14 10:19:38 +02:00
|
|
|
def test_entry_chunking_by_max_tokens(org_config_with_only_new_file: TextContentConfig, search_models: SearchModels):
|
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")
|
2023-02-17 17:04:26 +01:00
|
|
|
for index in range(max_tokens + 1):
|
2022-12-23 19:52:02 +01:00
|
|
|
f.write(f"{index} ")
|
|
|
|
|
|
|
|
# Act
|
|
|
|
# reload embeddings, entries, notes model after adding new org-mode file
|
2023-02-17 17:04:26 +01:00
|
|
|
initial_notes_model = text_search.setup(
|
2023-07-14 10:19:38 +02:00
|
|
|
OrgToJsonl, org_config_with_only_new_file, search_models.text_search.bi_encoder, regenerate=False
|
2023-02-17 17:04:26 +01:00
|
|
|
)
|
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-07-14 10:19:38 +02:00
|
|
|
def test_asymmetric_reload(content_config: ContentConfig, search_models: SearchModels, new_org_file: Path):
|
2022-06-29 21:09:48 +02:00
|
|
|
# Arrange
|
2023-07-14 10:19:38 +02:00
|
|
|
initial_notes_model = text_search.setup(
|
|
|
|
OrgToJsonl, content_config.org, search_models.text_search.bi_encoder, 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
|
2023-02-17 17:04:26 +01:00
|
|
|
content_config.org.input_files = [f"{new_org_file}"]
|
2023-01-09 20:17:36 +01:00
|
|
|
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
|
2023-02-17 17:04:26 +01:00
|
|
|
regenerated_notes_model = text_search.setup(
|
2023-07-14 10:19:38 +02:00
|
|
|
OrgToJsonl, content_config.org, search_models.text_search.bi_encoder, regenerate=True
|
2023-02-17 17:04:26 +01:00
|
|
|
)
|
2022-06-29 21:09:48 +02:00
|
|
|
|
|
|
|
# Act
|
|
|
|
# reload embeddings, entries, notes model from previously generated notes jsonl and model embeddings files
|
2023-07-14 10:19:38 +02:00
|
|
|
initial_notes_model = text_search.setup(
|
|
|
|
OrgToJsonl, content_config.org, search_models.text_search.bi_encoder, 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-07-14 10:19:38 +02:00
|
|
|
def test_incremental_update(content_config: ContentConfig, search_models: SearchModels, new_org_file: Path):
|
2022-09-07 02:06:29 +02:00
|
|
|
# Arrange
|
2023-07-14 10:19:38 +02:00
|
|
|
initial_notes_model = text_search.setup(
|
|
|
|
OrgToJsonl, content_config.org, search_models.text_search.bi_encoder, 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-02-17 17:04:26 +01:00
|
|
|
content_config.org.input_files = [f"{new_org_file}"]
|
2023-07-14 10:19:38 +02:00
|
|
|
initial_notes_model = text_search.setup(
|
|
|
|
OrgToJsonl, content_config.org, search_models.text_search.bi_encoder, 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 = []
|
2023-06-14 01:55:58 +02:00
|
|
|
|
|
|
|
|
|
|
|
# ----------------------------------------------------------------------------------------------------
|
|
|
|
@pytest.mark.skipif(os.getenv("GITHUB_PAT_TOKEN") is None, reason="GITHUB_PAT_TOKEN not set")
|
2023-07-14 10:19:38 +02:00
|
|
|
def test_asymmetric_setup_github(content_config: ContentConfig, search_models: SearchModels):
|
2023-06-14 01:55:58 +02:00
|
|
|
# Act
|
2023-06-18 11:24:25 +02:00
|
|
|
# Regenerate github embeddings to test asymmetric setup without caching
|
2023-07-14 10:19:38 +02:00
|
|
|
github_model = text_search.setup(
|
|
|
|
GithubToJsonl, content_config.github, search_models.text_search.bi_encoder, regenerate=True
|
|
|
|
)
|
2023-06-14 01:55:58 +02:00
|
|
|
|
|
|
|
# Assert
|
|
|
|
assert len(github_model.entries) > 1
|
2023-07-15 23:33:15 +02:00
|
|
|
|
|
|
|
|
|
|
|
def compare_index(initial_notes_model, final_notes_model):
|
|
|
|
mismatched_entries, mismatched_embeddings = [], []
|
|
|
|
for index in range(len(initial_notes_model.entries)):
|
|
|
|
if initial_notes_model.entries[index].to_json() != final_notes_model.entries[index].to_json():
|
|
|
|
mismatched_entries.append(index)
|
|
|
|
|
|
|
|
# verify new entry embedding appended to embeddings tensor, without disrupting order or content of existing embeddings
|
|
|
|
for index in range(len(initial_notes_model.corpus_embeddings)):
|
|
|
|
if not torch.equal(final_notes_model.corpus_embeddings[index], initial_notes_model.corpus_embeddings[index]):
|
|
|
|
mismatched_embeddings.append(index)
|
|
|
|
|
|
|
|
error_details = ""
|
|
|
|
if mismatched_entries:
|
|
|
|
mismatched_entries_str = ",".join(map(str, mismatched_entries))
|
|
|
|
error_details += f"Entries at {mismatched_entries_str} not equal\n"
|
|
|
|
if mismatched_embeddings:
|
|
|
|
mismatched_embeddings_str = ", ".join(map(str, mismatched_embeddings))
|
|
|
|
error_details += f"Embeddings at {mismatched_embeddings_str} not equal\n"
|
|
|
|
|
|
|
|
return error_details
|