[docs]classWatsonxEmbeddings(BaseModel,LangChainEmbeddings):"""IBM watsonx.ai embedding models."""model_id:str"""Type of model to use."""project_id:Optional[str]=None"""ID of the Watson Studio project."""space_id:Optional[str]=None"""ID of the Watson Studio space."""url:SecretStr=Field(alias="url",default_factory=secret_from_env("WATSONX_URL",default=None),# type: ignore[assignment])"""URL to the Watson Machine Learning or CPD instance."""apikey:Optional[SecretStr]=Field(alias="apikey",default_factory=secret_from_env("WATSONX_APIKEY",default=None))"""API key to the Watson Machine Learning or CPD instance."""token:Optional[SecretStr]=Field(alias="token",default_factory=secret_from_env("WATSONX_TOKEN",default=None))"""Token to the CPD instance."""password:Optional[SecretStr]=Field(alias="password",default_factory=secret_from_env("WATSONX_PASSWORD",default=None),)"""Password to the CPD instance."""username:Optional[SecretStr]=Field(alias="username",default_factory=secret_from_env("WATSONX_USERNAME",default=None),)"""Username to the CPD instance."""instance_id:Optional[SecretStr]=Field(alias="instance_id",default_factory=secret_from_env("WATSONX_INSTANCE_ID",default=None),)"""Instance_id of the CPD instance."""version:Optional[SecretStr]=None"""Version of the CPD instance."""params:Optional[Dict]=None"""Model parameters to use during request generation."""verify:Union[str,bool,None]=None"""You can pass one of following as verify: * the path to a CA_BUNDLE file * the path of directory with certificates of trusted CAs * True - default path to truststore will be taken * False - no verification will be made"""watsonx_embed:Embeddings=Field(default=None)#: :meta private:watsonx_client:Optional[APIClient]=Field(default=None)#: :meta private:model_config=ConfigDict(extra="forbid",arbitrary_types_allowed=True,protected_namespaces=(),)@model_validator(mode="after")defvalidate_environment(self)->Self:"""Validate that credentials and python package exists in environment."""ifisinstance(self.watsonx_client,APIClient):watsonx_embed=Embeddings(model_id=self.model_id,params=self.params,api_client=self.watsonx_client,project_id=self.project_id,space_id=self.space_id,verify=self.verify,)self.watsonx_embed=watsonx_embedelse:check_for_attribute(self.url,"url","WATSONX_URL")if"cloud.ibm.com"inself.url.get_secret_value():ifnotself.tokenandnotself.apikey:raiseValueError("Did not find 'apikey' or 'token',"" please add an environment variable"" `WATSONX_APIKEY` or 'WATSONX_TOKEN' ""which contains it,"" or pass 'apikey' or 'token'"" as a named parameter.")else:ifnotself.tokenandnotself.passwordandnotself.apikey:raiseValueError("Did not find 'token', 'password' or 'apikey',"" please add an environment variable"" `WATSONX_TOKEN`, 'WATSONX_PASSWORD' or 'WATSONX_APIKEY' ""which contains it,"" or pass 'token', 'password' or 'apikey'"" as a named parameter.")elifself.token:check_for_attribute(self.token,"token","WATSONX_TOKEN")elifself.password:check_for_attribute(self.password,"password","WATSONX_PASSWORD")check_for_attribute(self.username,"username","WATSONX_USERNAME")elifself.apikey:check_for_attribute(self.apikey,"apikey","WATSONX_APIKEY")check_for_attribute(self.username,"username","WATSONX_USERNAME")ifnotself.instance_id:check_for_attribute(self.instance_id,"instance_id","WATSONX_INSTANCE_ID")credentials=Credentials(url=self.url.get_secret_value()ifself.urlelseNone,api_key=self.apikey.get_secret_value()ifself.apikeyelseNone,token=self.token.get_secret_value()ifself.tokenelseNone,password=self.password.get_secret_value()ifself.passwordelseNone,username=self.username.get_secret_value()ifself.usernameelseNone,instance_id=self.instance_id.get_secret_value()ifself.instance_idelseNone,version=self.version.get_secret_value()ifself.versionelseNone,verify=self.verify,)watsonx_embed=Embeddings(model_id=self.model_id,params=self.params,credentials=credentials,project_id=self.project_id,space_id=self.space_id,)self.watsonx_embed=watsonx_embedreturnself