in experiments/legacy/backend/embeddings.py [0:0]
def get_embedding(self, text: Optional[str] = None,
image: Optional[str] = None, base64: bool = False):
"""Invoke Vertex multimodal embedding API.
You can pass text and/or image. If neither is passed will raise exception
Args:
text: text to embed
image: can be local file path, GCS URI or base64 encoded image
base64: True indicates image is base64. False (default) will be
interpreted as image path (either local or GCS)
Returns:
named tuple with the following attributes:
text_embedding: 1408 dimension vector of type Sequence[float]
image_embedding: 1408 dimension vector of type Sequence[float] OR None if
no image provide
"""
if not text and not image:
raise ValueError('At least one of text or image_bytes must be specified.')
instance = struct_pb2.Struct()
if text:
if len(text) >= 1024:
logging.warning('Text must be less than 1024 characters. Truncating text.')
text = text[:1023]
instance.fields['text'].string_value = text
if image:
image_struct = instance.fields['image'].struct_value
if base64:
image_struct.fields['bytesBase64Encoded'].string_value = image
elif image.lower().startswith('gs://'):
image_struct.fields['gcsUri'].string_value = image
else:
with open(image, "rb") as f:
image_bytes = f.read()
encoded_content = base64.b64encode(image_bytes).decode("utf-8")
image_struct.fields['bytesBase64Encoded'].string_value = encoded_content
instances = [instance]
endpoint = (f"projects/{self.project}/locations/{self.location}"
"/publishers/google/models/multimodalembedding@001")
response = self.client.predict(endpoint=endpoint, instances=instances)
text_embedding = None
if text:
text_emb_value = response.predictions[0]['textEmbedding']
text_embedding = [v for v in text_emb_value]
image_embedding = None
if image:
image_emb_value = response.predictions[0]['imageEmbedding']
image_embedding = [v for v in image_emb_value]
return EmbeddingResponse(
text_embedding=text_embedding,
image_embedding=image_embedding)