mirror of
https://github.com/khoj-ai/khoj.git
synced 2024-11-23 23:48:56 +01:00
eda4b65ddb
- Move embeddings to CUDA GPU for compute, when available - Normalize embeddings and Use Dot Product instead of Cosine
78 lines
No EOL
2.6 KiB
Python
78 lines
No EOL
2.6 KiB
Python
# Standard Packages
|
|
import pytest
|
|
import torch
|
|
|
|
# Internal Packages
|
|
from src.search_type import asymmetric, image_search
|
|
from src.utils.rawconfig import ContentConfig, TextContentConfig, ImageContentConfig, SearchConfig, SymmetricSearchConfig, AsymmetricSearchConfig, ImageSearchConfig
|
|
|
|
|
|
@pytest.fixture(scope='session')
|
|
def search_config(tmp_path_factory):
|
|
model_dir = tmp_path_factory.mktemp('data')
|
|
|
|
search_config = SearchConfig()
|
|
|
|
search_config.asymmetric = SymmetricSearchConfig(
|
|
encoder = "sentence-transformers/paraphrase-MiniLM-L6-v2",
|
|
cross_encoder = "cross-encoder/ms-marco-MiniLM-L-6-v2",
|
|
model_directory = model_dir
|
|
)
|
|
|
|
search_config.asymmetric = AsymmetricSearchConfig(
|
|
encoder = "sentence-transformers/msmarco-MiniLM-L-6-v3",
|
|
cross_encoder = "cross-encoder/ms-marco-MiniLM-L-6-v2",
|
|
model_directory = model_dir
|
|
)
|
|
|
|
search_config.image = ImageSearchConfig(
|
|
encoder = "clip-ViT-B-32",
|
|
model_directory = model_dir
|
|
)
|
|
|
|
return search_config
|
|
|
|
|
|
@pytest.fixture(scope='session')
|
|
def model_dir(search_config):
|
|
model_dir = search_config.asymmetric.model_directory
|
|
device = torch.device("cuda:0") if torch.cuda.is_available() else torch.device("cpu")
|
|
|
|
# Generate Image Embeddings from Test Images
|
|
content_config = ContentConfig()
|
|
content_config.image = ImageContentConfig(
|
|
input_directory = 'tests/data/images',
|
|
embeddings_file = model_dir.joinpath('image_embeddings.pt'),
|
|
batch_size = 10,
|
|
use_xmp_metadata = False)
|
|
|
|
image_search.setup(content_config.image, search_config.image, regenerate=False, verbose=True)
|
|
|
|
# Generate Notes Embeddings from Test Notes
|
|
content_config.org = TextContentConfig(
|
|
input_files = None,
|
|
input_filter = 'tests/data/notes/*.org',
|
|
compressed_jsonl = model_dir.joinpath('notes.jsonl.gz'),
|
|
embeddings_file = model_dir.joinpath('note_embeddings.pt'))
|
|
|
|
asymmetric.setup(content_config.org, search_config.asymmetric, regenerate=False, device=device, verbose=True)
|
|
|
|
return model_dir
|
|
|
|
|
|
@pytest.fixture(scope='session')
|
|
def content_config(model_dir):
|
|
content_config = ContentConfig()
|
|
content_config.org = TextContentConfig(
|
|
input_files = None,
|
|
input_filter = 'tests/data/notes/*.org',
|
|
compressed_jsonl = model_dir.joinpath('notes.jsonl.gz'),
|
|
embeddings_file = model_dir.joinpath('note_embeddings.pt'))
|
|
|
|
content_config.image = ImageContentConfig(
|
|
input_directory = 'tests/data/images',
|
|
embeddings_file = model_dir.joinpath('image_embeddings.pt'),
|
|
batch_size = 10,
|
|
use_xmp_metadata = False)
|
|
|
|
return content_config |