-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: introduce
SparseEmbedding
(#7382)
* introduce SparseEmbedding * reno * add to pydoc config
- Loading branch information
Showing
8 changed files
with
94 additions
and
7 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
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,26 @@ | ||
from typing import List | ||
|
||
|
||
class SparseEmbedding: | ||
""" | ||
Class representing a sparse embedding. | ||
""" | ||
|
||
def __init__(self, indices: List[int], values: List[float]): | ||
""" | ||
:param indices: List of indices of non-zero elements in the embedding. | ||
:param values: List of values of non-zero elements in the embedding. | ||
:raises ValueError: If the indices and values lists are not of the same length. | ||
""" | ||
if len(indices) != len(values): | ||
raise ValueError("Length of indices and values must be the same.") | ||
self.indices = indices | ||
self.values = values | ||
|
||
def to_dict(self): | ||
return {"indices": self.indices, "values": self.values} | ||
|
||
@classmethod | ||
def from_dict(cls, sparse_embedding_dict): | ||
return cls(indices=sparse_embedding_dict["indices"], values=sparse_embedding_dict["values"]) |
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,7 @@ | ||
--- | ||
features: | ||
- | | ||
Introduce a new `SparseEmbedding` class which can be used to store a sparse | ||
vector representation of a Document. | ||
It will be instrumental to support Sparse Embedding Retrieval with | ||
the subsequent introduction of Sparse Embedders and Sparse Embedding Retrievers. |
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,23 @@ | ||
import pytest | ||
|
||
from haystack.dataclasses.sparse_embedding import SparseEmbedding | ||
|
||
|
||
class TestSparseEmbedding: | ||
def test_init(self): | ||
se = SparseEmbedding(indices=[0, 2, 4], values=[0.1, 0.2, 0.3]) | ||
assert se.indices == [0, 2, 4] | ||
assert se.values == [0.1, 0.2, 0.3] | ||
|
||
def test_init_with_wrong_parameters(self): | ||
with pytest.raises(ValueError): | ||
SparseEmbedding(indices=[0, 2], values=[0.1, 0.2, 0.3, 0.4]) | ||
|
||
def test_to_dict(self): | ||
se = SparseEmbedding(indices=[0, 2, 4], values=[0.1, 0.2, 0.3]) | ||
assert se.to_dict() == {"indices": [0, 2, 4], "values": [0.1, 0.2, 0.3]} | ||
|
||
def test_from_dict(self): | ||
se = SparseEmbedding.from_dict({"indices": [0, 2, 4], "values": [0.1, 0.2, 0.3]}) | ||
assert se.indices == [0, 2, 4] | ||
assert se.values == [0.1, 0.2, 0.3] |
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