[docs]classPipelineAI(LLM,BaseModel):"""PipelineAI large language models. To use, you should have the ``pipeline-ai`` python package installed, and the environment variable ``PIPELINE_API_KEY`` set with your API key. Any parameters that are valid to be passed to the call can be passed in, even if not explicitly saved on this class. Example: .. code-block:: python from langchain_community.llms import PipelineAI pipeline = PipelineAI(pipeline_key="") """pipeline_key:str="""""The id or tag of the target pipeline"""pipeline_kwargs:Dict[str,Any]=Field(default_factory=dict)"""Holds any pipeline parameters valid for `create` call not explicitly specified."""pipeline_api_key:Optional[SecretStr]=NoneclassConfig:extra="forbid"@root_validator(pre=True)defbuild_extra(cls,values:Dict[str,Any])->Dict[str,Any]:"""Build extra kwargs from additional params that were passed in."""all_required_field_names={field.aliasforfieldinget_fields(cls).values()}extra=values.get("pipeline_kwargs",{})forfield_nameinlist(values):iffield_namenotinall_required_field_names:iffield_nameinextra:raiseValueError(f"Found {field_name} supplied twice.")logger.warning(f"""{field_name} was transferred to pipeline_kwargs. Please confirm that {field_name} is what you intended.""")extra[field_name]=values.pop(field_name)values["pipeline_kwargs"]=extrareturnvalues@pre_initdefvalidate_environment(cls,values:Dict)->Dict:"""Validate that api key and python package exists in environment."""pipeline_api_key=convert_to_secret_str(get_from_dict_or_env(values,"pipeline_api_key","PIPELINE_API_KEY"))values["pipeline_api_key"]=pipeline_api_keyreturnvalues@propertydef_identifying_params(self)->Mapping[str,Any]:"""Get the identifying parameters."""return{**{"pipeline_key":self.pipeline_key},**{"pipeline_kwargs":self.pipeline_kwargs},}@propertydef_llm_type(self)->str:"""Return type of llm."""return"pipeline_ai"def_call(self,prompt:str,stop:Optional[List[str]]=None,run_manager:Optional[CallbackManagerForLLMRun]=None,**kwargs:Any,)->str:"""Call to Pipeline Cloud endpoint."""try:frompipelineimportPipelineCloudexceptImportError:raiseImportError("Could not import pipeline-ai python package. ""Please install it with `pip install pipeline-ai`.")client=PipelineCloud(token=self.pipeline_api_key.get_secret_value())# type: ignore[union-attr]params=self.pipeline_kwargsor{}params={**params,**kwargs}run=client.run_pipeline(self.pipeline_key,[prompt,params])try:text=run.result_preview[0][0]exceptAttributeError:raiseAttributeError(f"A pipeline run should have a `result_preview` attribute."f"Run was: {run}")ifstopisnotNone:# I believe this is required since the stop tokens# are not enforced by the pipeline parameterstext=enforce_stop_tokens(text,stop)returntext