Skip to content

Commit

Permalink
refactor: support jina embeddings v3 (#39)
Browse files Browse the repository at this point in the history
This PR should allow pymilvus to support `jina-embeddings-v3:

with 2 additional fields:

task: to properly load adapters
dimensions: allow the user to truncate dimension with MRL.

This PR is the continuation of
#38 and we can close that.

Signed-off-by: Mohammad Kalim Akram <[email protected]>
  • Loading branch information
makram93 authored Sep 16, 2024
1 parent 1a4a232 commit c19565b
Showing 1 changed file with 18 additions and 8 deletions.
26 changes: 18 additions & 8 deletions milvus_model/dense/jinaai.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@
class JinaEmbeddingFunction(BaseEmbeddingFunction):
def __init__(
self,
model_name: str = "jina-embeddings-v2-base-en",
model_name: str = "jina-embeddings-v3",
api_key: Optional[str] = None,
task: str = 'retrieval.passage',
dimensions: Optional[int] = None,
**kwargs,
):
if api_key is None:
Expand All @@ -33,8 +35,8 @@ def __init__(
self._session.headers.update(
{"Authorization": f"Bearer {self.api_key}", "Accept-Encoding": "identity"}
)
self.model_name = model_name
self._dim = None
self.task = task
self._dim = dimensions

@property
def dim(self):
Expand All @@ -43,17 +45,25 @@ def dim(self):
return self._dim

def encode_queries(self, queries: List[str]) -> List[np.array]:
return self._call_jina_api(queries)
return self._call_jina_api(queries, task='retrieval.query')

def encode_documents(self, documents: List[str]) -> List[np.array]:
return self._call_jina_api(documents)
return self._call_jina_api(documents, task='retrieval.passage')

def __call__(self, texts: List[str]) -> List[np.array]:
return self._call_jina_api(texts)
return self._call_jina_api(texts, task=self.task)

def _call_jina_api(self, texts: List[str]):
def _call_jina_api(self, texts: List[str], task: Optional[str] = None):
data = {
"input": texts,
"model": self.model_name,
"task": task,
}
if self._dim is not None:
data["dimensions"] = self._dim
resp = self._session.post( # type: ignore[assignment]
API_URL, json={"input": texts, "model": self.model_name},
API_URL,
json=data,
).json()
if "data" not in resp:
raise RuntimeError(resp["detail"])
Expand Down

0 comments on commit c19565b

Please sign in to comment.