-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
OpenAI embeddings for vector search (#1869)
* pinning onnx runtime dep and adding openai api key setting * adding util method to generate points * adding docstring * adding initial fastembed and openai encoder classes * adding working encoder for search and similarity * adding dummy encoder and fixing tests * fixing model name generation in qdrant collection * switch to using dummy encoder * adding daily task to embed new learning resources * adding test for new celery task * adjusting test * fixing openai embedding method * fixing embed method * fixing test flakiness * adding litellm encoder * adding docstrings and moving dummy encoder into conftest * adding litellm dependency * added ability to filter out existing embedded resources and bumped up frequency of new resource embedding task * fix tests * adding tests for creating qdrant collections * fixing tests and removing period from daily embedding task * remove period from celery task def * fix encoe * fix encode method * fix encode method * fixing scroll * fixing scroll in test * fixing scroll in test
- Loading branch information
Showing
17 changed files
with
1,405 additions
and
201 deletions.
There are no files selected for viewing
This file contains 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 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 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
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains 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 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,39 @@ | ||
import numpy as np | ||
import pytest | ||
|
||
from vector_search.encoders.base import BaseEncoder | ||
|
||
|
||
class DummyEmbedEncoder(BaseEncoder): | ||
""" | ||
A dummy encoder that returns random vectors | ||
""" | ||
|
||
def __init__(self, model_name="dummy-embedding"): | ||
self.model_name = model_name | ||
|
||
def encode(self, text: str) -> list: # noqa: ARG002 | ||
return np.random.random((10, 1)) | ||
|
||
def encode_batch(self, texts: list[str]) -> list[list[float]]: | ||
return np.random.random((10, len(texts))) | ||
|
||
|
||
@pytest.fixture(autouse=True) | ||
def _use_dummy_encoder(settings): | ||
settings.QDRANT_ENCODER = "vector_search.conftest.DummyEmbedEncoder" | ||
|
||
|
||
@pytest.fixture(autouse=True) | ||
def _use_test_qdrant_settings(settings, mocker): | ||
settings.QDRANT_HOST = "https://test" | ||
settings.QDRANT_BASE_COLLECTION_NAME = "test" | ||
mock_qdrant = mocker.patch("qdrant_client.QdrantClient") | ||
mock_qdrant.scroll.return_value = [ | ||
[], | ||
None, | ||
] | ||
mocker.patch( | ||
"vector_search.utils.qdrant_client", | ||
return_value=mock_qdrant, | ||
) |
Empty file.
This file contains 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,37 @@ | ||
from abc import ABC, abstractmethod | ||
|
||
|
||
class BaseEncoder(ABC): | ||
""" | ||
Base encoder class | ||
""" | ||
|
||
def model_short_name(self): | ||
""" | ||
Return the short name of the model | ||
used as the vector name in qdrant | ||
""" | ||
split_model_name = self.model_name.split("/") | ||
model_name = self.model_name | ||
if len(split_model_name) > 1: | ||
model_name = split_model_name[1] | ||
return model_name | ||
|
||
def encode(self, text): | ||
""" | ||
Embed a single text | ||
""" | ||
return next(iter(self.encode_batch([text]))) | ||
|
||
@abstractmethod | ||
def encode_batch(self, texts: list[str]) -> list[list[float]]: | ||
""" | ||
Embed multiple texts | ||
""" | ||
return [self.encode(text) for text in texts] | ||
|
||
def dim(self): | ||
""" | ||
Return the dimension of the embeddings | ||
""" | ||
return len(self.encode("test")) |
This file contains 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,27 @@ | ||
from fastembed import TextEmbedding | ||
|
||
from vector_search.encoders.base import BaseEncoder | ||
|
||
|
||
class FastEmbedEncoder(BaseEncoder): | ||
""" | ||
FastEmbed encoder | ||
""" | ||
|
||
def __init__(self, model_name="BAAI/bge-small-en-v1.5"): | ||
self.model_name = model_name | ||
self.model = TextEmbedding(model_name=model_name, lazy_load=True) | ||
|
||
def encode_batch(self, texts: list[str]) -> list[list[float]]: | ||
return self.model.embed(texts) | ||
|
||
def dim(self): | ||
""" | ||
Return the dimension of the embeddings | ||
""" | ||
supported_models = [ | ||
model_config | ||
for model_config in self.model.list_supported_models() | ||
if model_config["model"] == self.model.model_name | ||
] | ||
return supported_models[0]["dim"] |
This file contains 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,20 @@ | ||
from litellm import embedding | ||
|
||
from vector_search.encoders.base import BaseEncoder | ||
|
||
|
||
class LiteLLMEncoder(BaseEncoder): | ||
""" | ||
LiteLLM encoder | ||
""" | ||
|
||
def __init__(self, model_name="text-embedding-3-small"): | ||
self.model_name = model_name | ||
|
||
def encode_batch(self, texts: list[str]) -> list[list[float]]: | ||
return [ | ||
result["embedding"] | ||
for result in embedding(model=self.model_name, input=texts).to_dict()[ | ||
"data" | ||
] | ||
] |
This file contains 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,12 @@ | ||
from django.conf import settings | ||
from django.utils.module_loading import import_string | ||
|
||
|
||
def dense_encoder(): | ||
""" | ||
Return the dense encoder based on settings | ||
""" | ||
Encoder = import_string(settings.QDRANT_ENCODER) | ||
if settings.QDRANT_DENSE_MODEL: | ||
return Encoder(model_name=settings.QDRANT_DENSE_MODEL) | ||
return Encoder() |
This file contains 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 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 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.