pymilvus.model 0.3.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- pymilvus/model/__init__.py +5 -0
- pymilvus/model/base.py +34 -0
- pymilvus/model/dense/__init__.py +21 -0
- pymilvus/model/dense/cohere.py +79 -0
- pymilvus/model/dense/instructor.py +76 -0
- pymilvus/model/dense/instructor_embedding/instructor_impl.py +713 -0
- pymilvus/model/dense/jinaai.py +78 -0
- pymilvus/model/dense/mistralai.py +67 -0
- pymilvus/model/dense/nomic.py +65 -0
- pymilvus/model/dense/onnx.py +67 -0
- pymilvus/model/dense/openai.py +61 -0
- pymilvus/model/dense/sentence_transformer.py +76 -0
- pymilvus/model/dense/voyageai.py +100 -0
- pymilvus/model/hybrid/__init__.py +4 -0
- pymilvus/model/hybrid/bge_m3.py +113 -0
- pymilvus/model/hybrid/mgte.py +107 -0
- pymilvus/model/hybrid/mgte_embedding/gte_impl.py +108 -0
- pymilvus/model/reranker/__init__.py +13 -0
- pymilvus/model/reranker/bgereranker.py +69 -0
- pymilvus/model/reranker/cohere.py +32 -0
- pymilvus/model/reranker/cross_encoder.py +41 -0
- pymilvus/model/reranker/jinaai.py +52 -0
- pymilvus/model/reranker/voyageai.py +24 -0
- pymilvus/model/sparse/__init__.py +4 -0
- pymilvus/model/sparse/bm25/__init__.py +9 -0
- pymilvus/model/sparse/bm25/bm25.py +222 -0
- pymilvus/model/sparse/bm25/lang.yaml +137 -0
- pymilvus/model/sparse/bm25/tokenizers.py +226 -0
- pymilvus/model/sparse/splade.py +97 -0
- pymilvus/model/sparse/splade_embedding/splade_impl.py +143 -0
- pymilvus/model/sparse/utils.py +5 -0
- pymilvus/model/utils/__init__.py +92 -0
- pymilvus/model/utils/dependency_control.py +12 -0
- pymilvus.model-0.3.0.dist-info/LICENSE +201 -0
- pymilvus.model-0.3.0.dist-info/METADATA +55 -0
- pymilvus.model-0.3.0.dist-info/RECORD +38 -0
- pymilvus.model-0.3.0.dist-info/WHEEL +5 -0
- pymilvus.model-0.3.0.dist-info/top_level.txt +1 -0
pymilvus/model/base.py
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
from abc import abstractmethod
|
|
2
|
+
from typing import List
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
class BaseEmbeddingFunction:
|
|
6
|
+
@abstractmethod
|
|
7
|
+
def __call__(self, texts: List[str]):
|
|
8
|
+
""" """
|
|
9
|
+
|
|
10
|
+
@abstractmethod
|
|
11
|
+
def encode_queries(self, queries: List[str]):
|
|
12
|
+
""" """
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
class BaseRerankFunction:
|
|
16
|
+
@abstractmethod
|
|
17
|
+
def __call__(self, query: str, documents: List[str], top_k: int):
|
|
18
|
+
""" """
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
class RerankResult:
|
|
22
|
+
def __init__(self, text: str, score: float, index: int):
|
|
23
|
+
self.text = text
|
|
24
|
+
self.score = score
|
|
25
|
+
self.index = index
|
|
26
|
+
|
|
27
|
+
def to_dict(self):
|
|
28
|
+
return {"text": self.text, "score": self.score, "index": self.index}
|
|
29
|
+
|
|
30
|
+
def __str__(self):
|
|
31
|
+
return f"RerankResult(text={self.text!r}, score={self.score}, index={self.index})"
|
|
32
|
+
|
|
33
|
+
def __repr__(self):
|
|
34
|
+
return f"RerankResult(text={self.text!r}, score={self.score}, index={self.index})"
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
from pymilvus.model.dense.openai import OpenAIEmbeddingFunction
|
|
2
|
+
from pymilvus.model.dense.sentence_transformer import SentenceTransformerEmbeddingFunction
|
|
3
|
+
from pymilvus.model.dense.voyageai import VoyageEmbeddingFunction
|
|
4
|
+
from pymilvus.model.dense.jinaai import JinaEmbeddingFunction
|
|
5
|
+
from pymilvus.model.dense.onnx import OnnxEmbeddingFunction
|
|
6
|
+
from pymilvus.model.dense.cohere import CohereEmbeddingFunction
|
|
7
|
+
from pymilvus.model.dense.mistralai import MistralAIEmbeddingFunction
|
|
8
|
+
from pymilvus.model.dense.nomic import NomicEmbeddingFunction
|
|
9
|
+
from pymilvus.model.dense.instructor import InstructorEmbeddingFunction
|
|
10
|
+
|
|
11
|
+
__all__ = [
|
|
12
|
+
"OpenAIEmbeddingFunction",
|
|
13
|
+
"SentenceTransformerEmbeddingFunction",
|
|
14
|
+
"VoyageEmbeddingFunction",
|
|
15
|
+
"JinaEmbeddingFunction",
|
|
16
|
+
"OnnxEmbeddingFunction",
|
|
17
|
+
"CohereEmbeddingFunction",
|
|
18
|
+
"MistralAIEmbeddingFunction",
|
|
19
|
+
"NomicEmbeddingFunction",
|
|
20
|
+
"InstructorEmbeddingFunction"
|
|
21
|
+
]
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
from typing import List, Optional
|
|
2
|
+
import struct
|
|
3
|
+
from collections import defaultdict
|
|
4
|
+
import numpy as np
|
|
5
|
+
|
|
6
|
+
from pymilvus.model.base import BaseEmbeddingFunction
|
|
7
|
+
from pymilvus.model.utils import import_cohere
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
class CohereEmbeddingFunction(BaseEmbeddingFunction):
|
|
12
|
+
def __init__(self,
|
|
13
|
+
model_name: str = "embed-english-light-v3.0",
|
|
14
|
+
api_key: Optional[str] = None,
|
|
15
|
+
input_type: str = "search_document",
|
|
16
|
+
embedding_types: Optional[List[str]] = None,
|
|
17
|
+
truncate: Optional[str] = None,
|
|
18
|
+
**kwargs):
|
|
19
|
+
self.model_name = model_name
|
|
20
|
+
self.input_type = input_type
|
|
21
|
+
self.embedding_types = embedding_types
|
|
22
|
+
self.truncate = truncate
|
|
23
|
+
|
|
24
|
+
import_cohere()
|
|
25
|
+
import cohere
|
|
26
|
+
|
|
27
|
+
if isinstance(embedding_types, list):
|
|
28
|
+
if len(embedding_types) > 1:
|
|
29
|
+
raise ValueError("Only one embedding type can be specified using current PyMilvus model library.")
|
|
30
|
+
elif embedding_types[0] == "int8" or embedding_types[0] == "uint8":
|
|
31
|
+
raise ValueError("Currently int8 or uint8 is not supported with PyMilvus model library.")
|
|
32
|
+
else:
|
|
33
|
+
pass
|
|
34
|
+
|
|
35
|
+
self.client = cohere.Client(api_key, **kwargs)
|
|
36
|
+
self._cohereai_model_meta_info = defaultdict(dict)
|
|
37
|
+
self._cohereai_model_meta_info["embed-english-v3.0"]["dim"] = 1024
|
|
38
|
+
self._cohereai_model_meta_info["embed-english-light-v3.0"]["dim"] = 384
|
|
39
|
+
self._cohereai_model_meta_info["embed-english-v2.0"]["dim"] = 4096
|
|
40
|
+
self._cohereai_model_meta_info["embed-english-light-v2.0"]["dim"] = 1024
|
|
41
|
+
self._cohereai_model_meta_info["embed-multilingual-v3.0"]["dim"] = 1024
|
|
42
|
+
self._cohereai_model_meta_info["embed-multilingual-light-v3.0"]["dim"] = 384
|
|
43
|
+
self._cohereai_model_meta_info["embed-multilingual-v2.0"]["dim"] = 768
|
|
44
|
+
|
|
45
|
+
def _call_cohere_api(self, texts: List[str], input_type: str) -> List[np.array]:
|
|
46
|
+
embeddings = self.client.embed(
|
|
47
|
+
texts=texts,
|
|
48
|
+
model=self.model_name,
|
|
49
|
+
input_type=input_type,
|
|
50
|
+
embedding_types=self.embedding_types,
|
|
51
|
+
truncate=self.truncate
|
|
52
|
+
).embeddings
|
|
53
|
+
if self.embedding_types is None:
|
|
54
|
+
results = [np.array(data, dtype=np.float32) for data in embeddings]
|
|
55
|
+
else:
|
|
56
|
+
results = getattr(embeddings, self.embedding_types[0], None)
|
|
57
|
+
if self.embedding_types[0] == "binary":
|
|
58
|
+
results = [struct.pack('b' * len(int8_vector), *int8_vector) for int8_vector in results]
|
|
59
|
+
elif self.embedding_types[0] == "ubinary":
|
|
60
|
+
results = [struct.pack('B' * len(uint8_vector), *uint8_vector) for uint8_vector in results]
|
|
61
|
+
elif self.embedding_types[0] == "float":
|
|
62
|
+
results = [np.array(result, dtype=np.float32) for result in results]
|
|
63
|
+
else:
|
|
64
|
+
pass
|
|
65
|
+
return results
|
|
66
|
+
|
|
67
|
+
def encode_documents(self, documents: List[str]) -> List[np.array]:
|
|
68
|
+
return self._call_cohere_api(documents, input_type="search_document")
|
|
69
|
+
|
|
70
|
+
def encode_queries(self, queries: List[str]) -> List[np.array]:
|
|
71
|
+
return self._call_cohere_api(queries, input_type="search_query")
|
|
72
|
+
|
|
73
|
+
def __call__(self, texts: List[str]) -> List[np.array]:
|
|
74
|
+
return self._call_cohere_api(texts, self.input_type)
|
|
75
|
+
|
|
76
|
+
@property
|
|
77
|
+
def dim(self):
|
|
78
|
+
return self._cohereai_model_meta_info[self.model_name]["dim"]
|
|
79
|
+
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
from typing import List, Optional
|
|
2
|
+
import numpy as np
|
|
3
|
+
|
|
4
|
+
from pymilvus.model.base import BaseEmbeddingFunction
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
class InstructorEmbeddingFunction(BaseEmbeddingFunction):
|
|
8
|
+
def __init__(
|
|
9
|
+
self,
|
|
10
|
+
model_name: str = "hkunlp/instructor-xl",
|
|
11
|
+
batch_size: int = 32,
|
|
12
|
+
query_instruction: str = "Represent the question for retrieval:",
|
|
13
|
+
doc_instruction: str = "Represent the document for retrieval:",
|
|
14
|
+
device: str = "cpu",
|
|
15
|
+
normalize_embeddings: bool = True,
|
|
16
|
+
**kwargs,
|
|
17
|
+
):
|
|
18
|
+
from .instructor_embedding.instructor_impl import Instructor
|
|
19
|
+
|
|
20
|
+
self.model_name = model_name
|
|
21
|
+
self.query_instruction = query_instruction
|
|
22
|
+
self.doc_instruction = doc_instruction
|
|
23
|
+
self.batch_size = batch_size
|
|
24
|
+
self.normalize_embeddings = normalize_embeddings
|
|
25
|
+
|
|
26
|
+
_model_config = dict({"model_name_or_path": model_name, "device": device}, **kwargs)
|
|
27
|
+
self.model = Instructor(**_model_config)
|
|
28
|
+
|
|
29
|
+
def __call__(self, texts: List[str]) -> List[np.array]:
|
|
30
|
+
return self._encode([[self.doc_instruction, text] for text in texts])
|
|
31
|
+
|
|
32
|
+
def _encode(self, texts: List[str]) -> List[np.array]:
|
|
33
|
+
embs = self.model.encode(
|
|
34
|
+
texts,
|
|
35
|
+
batch_size=self.batch_size,
|
|
36
|
+
show_progress_bar=False,
|
|
37
|
+
convert_to_numpy=True,
|
|
38
|
+
normalize_embeddings=self.normalize_embeddings
|
|
39
|
+
)
|
|
40
|
+
return list(embs)
|
|
41
|
+
|
|
42
|
+
@property
|
|
43
|
+
def dim(self):
|
|
44
|
+
return self.model.get_sentence_embedding_dimension()
|
|
45
|
+
|
|
46
|
+
def encode_queries(self, queries: List[str]) -> List[np.array]:
|
|
47
|
+
instructed_queries = [[self.query_instruction, query] for query in queries]
|
|
48
|
+
return self._encode(instructed_queries)
|
|
49
|
+
|
|
50
|
+
def encode_documents(self, documents: List[str]) -> List[np.array]:
|
|
51
|
+
instructed_documents = [[self.doc_instruction, document] for document in documents]
|
|
52
|
+
return self._encode(instructed_documents)
|
|
53
|
+
|
|
54
|
+
def _encode_query(self, query: str) -> np.array:
|
|
55
|
+
instructed_query = self.query_instruction + query
|
|
56
|
+
embs = self.model.encode(
|
|
57
|
+
sentences=[instructed_query],
|
|
58
|
+
batch_size=1,
|
|
59
|
+
show_progress_bar=False,
|
|
60
|
+
convert_to_numpy=True,
|
|
61
|
+
normalize_embeddings=self.normalize_embeddings,
|
|
62
|
+
)
|
|
63
|
+
return embs[0]
|
|
64
|
+
|
|
65
|
+
def _encode_document(self, document: str) -> np.array:
|
|
66
|
+
instructed_document = self.doc_instruction + document
|
|
67
|
+
embs = self.model.encode(
|
|
68
|
+
sentences=[instructed_document],
|
|
69
|
+
batch_size=1,
|
|
70
|
+
show_progress_bar=False,
|
|
71
|
+
convert_to_numpy=True,
|
|
72
|
+
normalize_embeddings=self.normalize_embeddings,
|
|
73
|
+
)
|
|
74
|
+
return embs[0]
|
|
75
|
+
|
|
76
|
+
|