[docs]classOpenCLIPEmbeddings(BaseModel,Embeddings):"""OpenCLIP Embeddings model."""model:Anypreprocess:Anytokenizer:Any# Select model: https://github.com/mlfoundations/open_clipmodel_name:str="ViT-H-14"checkpoint:str="laion2b_s32b_b79k"model_config=ConfigDict(protected_namespaces=())@model_validator(mode="before")@classmethoddefvalidate_environment(cls,values:Dict)->Any:"""Validate that open_clip and torch libraries are installed."""try:importopen_clip# Fall back to class defaults if not providedmodel_name=values.get("model_name",get_fields(cls)["model_name"].default)checkpoint=values.get("checkpoint",get_fields(cls)["checkpoint"].default)# Load modelmodel,_,preprocess=open_clip.create_model_and_transforms(model_name=model_name,pretrained=checkpoint)tokenizer=open_clip.get_tokenizer(model_name)values["model"]=modelvalues["preprocess"]=preprocessvalues["tokenizer"]=tokenizerexceptImportError:raiseImportError("Please ensure both open_clip and torch libraries are installed. ""pip install open_clip_torch torch")returnvalues
[docs]defembed_documents(self,texts:List[str])->List[List[float]]:text_features=[]fortextintexts:# Tokenize the texttokenized_text=self.tokenizer(text)# Encode the text to get the embeddingsembeddings_tensor=self.model.encode_text(tokenized_text)# Normalize the embeddingsnorm=embeddings_tensor.norm(p=2,dim=1,keepdim=True)normalized_embeddings_tensor=embeddings_tensor.div(norm)# Convert normalized tensor to list and add to the text_features listembeddings_list=normalized_embeddings_tensor.squeeze(0).tolist()text_features.append(embeddings_list)returntext_features
[docs]defembed_image(self,uris:List[str])->List[List[float]]:try:fromPILimportImageas_PILImageexceptImportError:raiseImportError("Please install the PIL library: pip install pillow")# Open images directly as PIL imagespil_images=[_PILImage.open(uri)foruriinuris]image_features=[]forpil_imageinpil_images:# Preprocess the image for the modelpreprocessed_image=self.preprocess(pil_image).unsqueeze(0)# Encode the image to get the embeddingsembeddings_tensor=self.model.encode_image(preprocessed_image)# Normalize the embeddings tensornorm=embeddings_tensor.norm(p=2,dim=1,keepdim=True)normalized_embeddings_tensor=embeddings_tensor.div(norm)# Convert tensor to list and add to the image_features listembeddings_list=normalized_embeddings_tensor.squeeze(0).tolist()image_features.append(embeddings_list)returnimage_features