Source code for langchain_community.embeddings.minimax
from__future__importannotationsimportloggingfromtypingimportAny,Callable,Dict,List,Optionalimportrequestsfromlangchain_core.embeddingsimportEmbeddingsfromlangchain_core.utilsimportconvert_to_secret_str,get_from_dict_or_env,pre_initfrompydanticimportBaseModel,ConfigDict,Field,SecretStrfromtenacityimport(before_sleep_log,retry,stop_after_attempt,wait_exponential,)logger=logging.getLogger(__name__)def_create_retry_decorator()->Callable[[Any],Any]:"""Returns a tenacity retry decorator."""multiplier=1min_seconds=1max_seconds=4max_retries=6returnretry(reraise=True,stop=stop_after_attempt(max_retries),wait=wait_exponential(multiplier=multiplier,min=min_seconds,max=max_seconds),before_sleep=before_sleep_log(logger,logging.WARNING),)
[docs]defembed_with_retry(embeddings:MiniMaxEmbeddings,*args:Any,**kwargs:Any)->Any:"""Use tenacity to retry the completion call."""retry_decorator=_create_retry_decorator()@retry_decoratordef_embed_with_retry(*args:Any,**kwargs:Any)->Any:returnembeddings.embed(*args,**kwargs)return_embed_with_retry(*args,**kwargs)
[docs]classMiniMaxEmbeddings(BaseModel,Embeddings):"""MiniMax embedding model integration. Setup: To use, you should have the environment variable ``MINIMAX_GROUP_ID`` and ``MINIMAX_API_KEY`` set with your API token. .. code-block:: bash export MINIMAX_API_KEY="your-api-key" export MINIMAX_GROUP_ID="your-group-id" Key init args — completion params: model: Optional[str] Name of ZhipuAI model to use. api_key: Optional[str] Automatically inferred from env var `MINIMAX_GROUP_ID` if not provided. group_id: Optional[str] Automatically inferred from env var `MINIMAX_GROUP_ID` if not provided. See full list of supported init args and their descriptions in the params section. Instantiate: .. code-block:: python from langchain_community.embeddings import MiniMaxEmbeddings embed = MiniMaxEmbeddings( model="embo-01", # api_key="...", # group_id="...", # other ) Embed single text: .. code-block:: python input_text = "The meaning of life is 42" embed.embed_query(input_text) .. code-block:: python [0.03016241, 0.03617699, 0.0017198119, -0.002061239, -0.00029994643, -0.0061320597, -0.0043635326, ...] Embed multiple text: .. code-block:: python input_texts = ["This is a test query1.", "This is a test query2."] embed.embed_documents(input_texts) .. code-block:: python [ [-0.0021588828, -0.007608119, 0.029349545, -0.0038194496, 0.008031177, -0.004529633, -0.020150753, ...], [ -0.00023150232, -0.011122423, 0.016930554, 0.0083089275, 0.012633711, 0.019683322, -0.005971041, ...] ] """# noqa: E501endpoint_url:str="https://api.minimax.chat/v1/embeddings""""Endpoint URL to use."""model:str="embo-01""""Embeddings model name to use."""embed_type_db:str="db""""For embed_documents"""embed_type_query:str="query""""For embed_query"""minimax_group_id:Optional[str]=Field(default=None,alias="group_id")"""Group ID for MiniMax API."""minimax_api_key:Optional[SecretStr]=Field(default=None,alias="api_key")"""API Key for MiniMax API."""model_config=ConfigDict(populate_by_name=True,extra="forbid",)
[docs]@pre_initdefvalidate_environment(cls,values:Dict)->Dict:"""Validate that group id and api key exists in environment."""minimax_group_id=get_from_dict_or_env(values,["minimax_group_id","group_id"],"MINIMAX_GROUP_ID")minimax_api_key=convert_to_secret_str(get_from_dict_or_env(values,["minimax_api_key","api_key"],"MINIMAX_API_KEY"))values["minimax_group_id"]=minimax_group_idvalues["minimax_api_key"]=minimax_api_keyreturnvalues
[docs]defembed(self,texts:List[str],embed_type:str,)->List[List[float]]:payload={"model":self.model,"type":embed_type,"texts":texts,}# HTTP headers for authorizationheaders={"Authorization":f"Bearer {self.minimax_api_key.get_secret_value()}",# type: ignore[union-attr]"Content-Type":"application/json",}params={"GroupId":self.minimax_group_id,}# send requestresponse=requests.post(self.endpoint_url,params=params,headers=headers,json=payload)parsed_response=response.json()# check for errorsifparsed_response["base_resp"]["status_code"]!=0:raiseValueError(f"MiniMax API returned an error: {parsed_response['base_resp']}")embeddings=parsed_response["vectors"]returnembeddings
[docs]defembed_documents(self,texts:List[str])->List[List[float]]:"""Embed documents using a MiniMax embedding endpoint. Args: texts: The list of texts to embed. Returns: List of embeddings, one for each text. """embeddings=embed_with_retry(self,texts=texts,embed_type=self.embed_type_db)returnembeddings
[docs]defembed_query(self,text:str)->List[float]:"""Embed a query using a MiniMax embedding endpoint. Args: text: The text to embed. Returns: Embeddings for the text. """embeddings=embed_with_retry(self,texts=[text],embed_type=self.embed_type_query)returnembeddings[0]