Source code for langchain_community.embeddings.text2vec
"""Wrapper around text2vec embedding models."""fromtypingimportAny,List,Optionalfromlangchain_core.embeddingsimportEmbeddingsfrompydanticimportBaseModel,ConfigDict
[docs]classText2vecEmbeddings(Embeddings,BaseModel):"""text2vec embedding models. Install text2vec first, run 'pip install -U text2vec'. The github repository for text2vec is : https://github.com/shibing624/text2vec Example: .. code-block:: python from langchain_community.embeddings.text2vec import Text2vecEmbeddings embedding = Text2vecEmbeddings() embedding.embed_documents([ "This is a CoSENT(Cosine Sentence) model.", "It maps sentences to a 768 dimensional dense vector space.", ]) embedding.embed_query( "It can be used for text matching or semantic search." ) """model_name_or_path:Optional[str]=Noneencoder_type:Any="MEAN"max_seq_length:int=256device:Optional[str]=Nonemodel:Any=Nonemodel_config=ConfigDict(protected_namespaces=())def__init__(self,*,model:Any=None,model_name_or_path:Optional[str]=None,**kwargs:Any,):try:fromtext2vecimportSentenceModelexceptImportErrorase:raiseImportError("Unable to import text2vec, please install with ""`pip install -U text2vec`.")fromemodel_kwargs={}ifmodel_name_or_pathisnotNone:model_kwargs["model_name_or_path"]=model_name_or_pathmodel=modelorSentenceModel(**model_kwargs,**kwargs)super().__init__(model=model,model_name_or_path=model_name_or_path,**kwargs)
[docs]defembed_documents(self,texts:List[str])->List[List[float]]:"""Embed documents using the text2vec embeddings model. Args: texts: The list of texts to embed. Returns: List of embeddings, one for each text. """returnself.model.encode(texts)
[docs]defembed_query(self,text:str)->List[float]:"""Embed a query using the text2vec embeddings model. Args: text: The text to embed. Returns: Embeddings for the text. """returnself.model.encode(text)