ray-embedding 0.9.9__tar.gz → 0.10.0__tar.gz

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.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: ray-embedding
3
- Version: 0.9.9
3
+ Version: 0.10.0
4
4
  Summary: Deploy SentenceTransformers embedding models to a ray cluster
5
5
  Author: Crispin Almodovar
6
6
  Author-email:
@@ -69,25 +69,51 @@ class EmbeddingModel:
69
69
  assert request.model == self.served_model_name, (
70
70
  f"Model '{request.model}' is not supported. Use '{self.served_model_name}' instead."
71
71
  )
72
- return await self.__create_embeddings_batch(request)
72
+
73
+ if isinstance(request.input, str):
74
+ request.input = [request.input]
75
+
76
+ truncate_dim = request.dimensions or self.matryoshka_dim
77
+
78
+ # Compute embeddings and convert to a PyTorch tensor on the GPU
79
+ embeddings = self.embedding_model.encode(
80
+ request.input, convert_to_tensor=True, normalize_embeddings=True
81
+ ).to(self.torch_device)
82
+
83
+ if truncate_dim is not None:
84
+ # Truncate and re-normalize the embeddings
85
+ embeddings = embeddings[:, :truncate_dim]
86
+ embeddings = embeddings / torch.norm(embeddings, dim=1, keepdim=True)
87
+
88
+ # Move all embeddings to CPU at once before conversion
89
+ embeddings = embeddings.cpu().tolist()
90
+
91
+ # Convert embeddings to list format for response
92
+ response_data = [
93
+ {"index": idx, "embedding": emb}
94
+ for idx, emb in enumerate(embeddings)
95
+ ]
96
+ return EmbeddingResponse(object="list", data=response_data, model=request.model)
97
+
73
98
  except Exception as e:
74
99
  self.logger.error(e)
75
100
  raise HTTPException(status_code=500, detail=str(e))
76
101
 
77
102
  @serve.batch(max_batch_size=8, batch_wait_timeout_s=0.25)
78
103
  async def __create_embeddings_batch(self, requests_batch: List[EmbeddingRequest]) -> List[EmbeddingResponse]:
79
- self_0 = self[0] if isinstance(self, list) else self
104
+ self_0 = self[0] if isinstance(self, list) else self # Ray also passes an array of self refs; just take the first one
105
+ embedding_model, matryoshka_dim, torch_device = self_0.embedding_model, self_0.matryoshka_dim, self_0.torch_device
80
106
 
81
107
  inputs, truncate_dims, num_inputs_list = [], [], []
82
108
  for request in requests_batch:
83
109
  input_text = request.input if isinstance(request.input, list) else [request.input] # Can be a list of texts
84
110
  inputs.extend(input_text)
85
111
  num_inputs_list.append(len(input_text))
86
- truncate_dims.append(request.dimensions or self_0.matryoshka_dim)
112
+ truncate_dims.append(request.dimensions or matryoshka_dim)
87
113
 
88
- embeddings = self_0.embedding_model.encode(
114
+ embeddings = embedding_model.encode(
89
115
  inputs, convert_to_tensor=True, normalize_embeddings=True, show_progress_bar=False,
90
- ).to(self_0.torch_device)
116
+ ).to(torch_device)
91
117
 
92
118
  model_name = requests_batch[0].model
93
119
  truncate_needed = any(dim is not None for dim in truncate_dims)
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: ray-embedding
3
- Version: 0.9.9
3
+ Version: 0.10.0
4
4
  Summary: Deploy SentenceTransformers embedding models to a ray cluster
5
5
  Author: Crispin Almodovar
6
6
  Author-email:
@@ -1,6 +1,6 @@
1
1
  [metadata]
2
2
  name = ray-embedding
3
- version = 0.9.9
3
+ version = 0.10.0
4
4
  author = Crispin Almodovar
5
5
  author_email =
6
6
  description = Deploy SentenceTransformers embedding models to a ray cluster
File without changes