[docs]@deprecated(since="0.0.12",removal="1.0",alternative_import="langchain_google_vertexai.VertexAIEmbeddings",)classVertexAIEmbeddings(_VertexAICommon,Embeddings):# type: ignore[override]"""Google Cloud VertexAI embedding models."""# Instance contextinstance:Dict[str,Any]={}#: :meta private:show_progress_bar:bool=False"""Whether to show a tqdm progress bar. Must have `tqdm` installed."""
[docs]@pre_initdefvalidate_environment(cls,values:Dict)->Dict:"""Validates that the python package exists in environment."""cls._try_init_vertexai(values)ifvalues["model_name"]=="textembedding-gecko-default":logger.warning("Model_name will become a required arg for VertexAIEmbeddings ""starting from Feb-01-2024. Currently the default is set to ""textembedding-gecko@001")values["model_name"]="textembedding-gecko@001"try:fromvertexai.language_modelsimportTextEmbeddingModelexceptImportError:raise_vertex_import_error()values["client"]=TextEmbeddingModel.from_pretrained(values["model_name"])returnvalues
def__init__(self,# the default value would be removed after Feb-01-2024model_name:str="textembedding-gecko-default",project:Optional[str]=None,location:str="us-central1",request_parallelism:int=5,max_retries:int=6,credentials:Optional[Any]=None,**kwargs:Any,):"""Initialize the sentence_transformer."""super().__init__(project=project,location=location,credentials=credentials,request_parallelism=request_parallelism,max_retries=max_retries,model_name=model_name,**kwargs,)self.instance["max_batch_size"]=kwargs.get("max_batch_size",_MAX_BATCH_SIZE)self.instance["batch_size"]=self.instance["max_batch_size"]self.instance["min_batch_size"]=kwargs.get("min_batch_size",_MIN_BATCH_SIZE)self.instance["min_good_batch_size"]=self.instance["min_batch_size"]self.instance["lock"]=threading.Lock()self.instance["batch_size_validated"]=Falseself.instance["task_executor"]=ThreadPoolExecutor(max_workers=request_parallelism)self.instance["embeddings_task_type_supported"]=notself.client._endpoint_name.endswith("/textembedding-gecko@001")@staticmethoddef_split_by_punctuation(text:str)->List[str]:"""Splits a string by punctuation and whitespace characters."""split_by=string.punctuation+"\t\n "pattern=f"([{split_by}])"# Using re.split to split the text based on the patternreturn[segmentforsegmentinre.split(pattern,text)ifsegment]@staticmethoddef_prepare_batches(texts:List[str],batch_size:int)->List[List[str]]:"""Splits texts in batches based on current maximum batch size and maximum tokens per request. """text_index=0texts_len=len(texts)batch_token_len=0batches:List[List[str]]=[]current_batch:List[str]=[]iftexts_len==0:return[]whiletext_index<texts_len:current_text=texts[text_index]# Number of tokens per a text is conservatively estimated# as 2 times number of words, punctuation and whitespace characters.# Using `count_tokens` API will make batching too expensive.# Utilizing a tokenizer, would add a dependency that would not# necessarily be reused by the application using this class.current_text_token_cnt=(len(VertexAIEmbeddings._split_by_punctuation(current_text))*2)end_of_batch=Falseifcurrent_text_token_cnt>_MAX_TOKENS_PER_BATCH:# Current text is too big even for a single batch.# Such request will fail, but we still make a batch# so that the app can get the error from the API.iflen(current_batch)>0:# Adding current batch if not empty.batches.append(current_batch)current_batch=[current_text]text_index+=1end_of_batch=Trueelif(batch_token_len+current_text_token_cnt>_MAX_TOKENS_PER_BATCHorlen(current_batch)==batch_size):end_of_batch=Trueelse:iftext_index==texts_len-1:# Last element - even though the batch may be not big,# we still need to make it.end_of_batch=Truebatch_token_len+=current_text_token_cntcurrent_batch.append(current_text)text_index+=1ifend_of_batch:batches.append(current_batch)current_batch=[]batch_token_len=0returnbatchesdef_get_embeddings_with_retry(self,texts:List[str],embeddings_type:Optional[str]=None)->List[List[float]]:"""Makes a Vertex AI model request with retry logic."""fromgoogle.api_core.exceptionsimport(Aborted,DeadlineExceeded,ResourceExhausted,ServiceUnavailable,)errors=[ResourceExhausted,ServiceUnavailable,Aborted,DeadlineExceeded,]retry_decorator=create_base_retry_decorator(error_types=errors,# type: ignore[arg-type]max_retries=self.max_retries,# type: ignore[arg-type])@retry_decoratordef_completion_with_retry(texts_to_process:List[str])->Any:ifembeddings_typeandself.instance["embeddings_task_type_supported"]:fromvertexai.language_modelsimportTextEmbeddingInputrequests=[TextEmbeddingInput(text=t,task_type=embeddings_type)fortintexts_to_process]else:requests=texts_to_processembeddings=self.client.get_embeddings(requests)return[embs.valuesforembsinembeddings]return_completion_with_retry(texts)def_prepare_and_validate_batches(self,texts:List[str],embeddings_type:Optional[str]=None)->Tuple[List[List[float]],List[List[str]]]:"""Prepares text batches with one-time validation of batch size. Batch size varies between GCP regions and individual project quotas. # Returns embeddings of the first text batch that went through, # and text batches for the rest of the texts. """fromgoogle.api_core.exceptionsimportInvalidArgumentbatches=VertexAIEmbeddings._prepare_batches(texts,self.instance["batch_size"])# If batch size if less or equal to one that went through before,# then keep batches as they are.iflen(batches[0])<=self.instance["min_good_batch_size"]:return[],batcheswithself.instance["lock"]:# If largest possible batch size was validated# while waiting for the lock, then check for rebuilding# our batches, and return.ifself.instance["batch_size_validated"]:iflen(batches[0])<=self.instance["batch_size"]:return[],batcheselse:return[],VertexAIEmbeddings._prepare_batches(texts,self.instance["batch_size"])# Figure out largest possible batch size by trying to push# batches and lowering their size in half after every failure.first_batch=batches[0]first_result=[]had_failure=FalsewhileTrue:try:first_result=self._get_embeddings_with_retry(first_batch,embeddings_type)breakexceptInvalidArgument:had_failure=Truefirst_batch_len=len(first_batch)iffirst_batch_len==self.instance["min_batch_size"]:raisefirst_batch_len=max(self.instance["min_batch_size"],int(first_batch_len/2))first_batch=first_batch[:first_batch_len]first_batch_len=len(first_batch)self.instance["min_good_batch_size"]=max(self.instance["min_good_batch_size"],first_batch_len)# If had a failure and recovered# or went through with the max size, then it's a legit batch size.ifhad_failureorfirst_batch_len==self.instance["max_batch_size"]:self.instance["batch_size"]=first_batch_lenself.instance["batch_size_validated"]=True# If batch size was updated,# rebuild batches with the new batch size# (texts that went through are excluded here).iffirst_batch_len!=self.instance["max_batch_size"]:batches=VertexAIEmbeddings._prepare_batches(texts[first_batch_len:],self.instance["batch_size"])else:# Still figuring out max batch size.batches=batches[1:]# Returning embeddings of the first text batch that went through,# and text batches for the rest of texts.returnfirst_result,batches
[docs]defembed(self,texts:List[str],batch_size:int=0,embeddings_task_type:Optional[Literal["RETRIEVAL_QUERY","RETRIEVAL_DOCUMENT","SEMANTIC_SIMILARITY","CLASSIFICATION","CLUSTERING",]]=None,)->List[List[float]]:"""Embed a list of strings. Args: texts: List[str] The list of strings to embed. batch_size: [int] The batch size of embeddings to send to the model. If zero, then the largest batch size will be detected dynamically at the first request, starting from 250, down to 5. embeddings_task_type: [str] optional embeddings task type, one of the following RETRIEVAL_QUERY - Text is a query in a search/retrieval setting. RETRIEVAL_DOCUMENT - Text is a document in a search/retrieval setting. SEMANTIC_SIMILARITY - Embeddings will be used for Semantic Textual Similarity (STS). CLASSIFICATION - Embeddings will be used for classification. CLUSTERING - Embeddings will be used for clustering. Returns: List of embeddings, one for each text. """iflen(texts)==0:return[]embeddings:List[List[float]]=[]first_batch_result:List[List[float]]=[]ifbatch_size>0:# Fixed batch size.batches=VertexAIEmbeddings._prepare_batches(texts,batch_size)else:# Dynamic batch size, starting from 250 at the first call.first_batch_result,batches=self._prepare_and_validate_batches(texts,embeddings_task_type)# First batch result may have some embeddings already.# In such case, batches have texts that were not processed yet.embeddings.extend(first_batch_result)tasks=[]ifself.show_progress_bar:try:fromtqdmimporttqdmiter_=tqdm(batches,desc="VertexAIEmbeddings")exceptImportError:logger.warning("Unable to show progress bar because tqdm could not be imported. ""Please install with `pip install tqdm`.")iter_=batcheselse:iter_=batchesforbatchiniter_:tasks.append(self.instance["task_executor"].submit(self._get_embeddings_with_retry,texts=batch,embeddings_type=embeddings_task_type,))iflen(tasks)>0:wait(tasks)fortintasks:embeddings.extend(t.result())returnembeddings
[docs]defembed_documents(self,texts:List[str],batch_size:int=0)->List[List[float]]:"""Embed a list of documents. Args: texts: List[str] The list of texts to embed. batch_size: [int] The batch size of embeddings to send to the model. If zero, then the largest batch size will be detected dynamically at the first request, starting from 250, down to 5. Returns: List of embeddings, one for each text. """returnself.embed(texts,batch_size,"RETRIEVAL_DOCUMENT")
[docs]defembed_query(self,text:str)->List[float]:"""Embed a text. Args: text: The text to embed. Returns: Embedding for the text. """embeddings=self.embed([text],1,"RETRIEVAL_QUERY")returnembeddings[0]