[docs]classPaiEasEndpoint(LLM):"""Langchain LLM class to help to access eass llm service. To use this endpoint, must have a deployed eas chat llm service on PAI AliCloud. One can set the environment variable ``eas_service_url`` and ``eas_service_token``. The environment variables can set with your eas service url and service token. Example: .. code-block:: python from langchain_community.llms.pai_eas_endpoint import PaiEasEndpoint eas_chat_endpoint = PaiEasChatEndpoint( eas_service_url="your_service_url", eas_service_token="your_service_token" ) """"""PAI-EAS Service URL"""eas_service_url:str"""PAI-EAS Service TOKEN"""eas_service_token:str"""PAI-EAS Service Infer Params"""max_new_tokens:Optional[int]=512temperature:Optional[float]=0.95top_p:Optional[float]=0.1top_k:Optional[int]=0stop_sequences:Optional[List[str]]=None"""Enable stream chat mode."""streaming:bool=False"""Key/value arguments to pass to the model. Reserved for future use"""model_kwargs:Optional[dict]=Noneversion:Optional[str]="2.0"@pre_initdefvalidate_environment(cls,values:Dict)->Dict:"""Validate that api key and python package exists in environment."""values["eas_service_url"]=get_from_dict_or_env(values,"eas_service_url","EAS_SERVICE_URL")values["eas_service_token"]=get_from_dict_or_env(values,"eas_service_token","EAS_SERVICE_TOKEN")returnvalues@propertydef_llm_type(self)->str:"""Return type of llm."""return"pai_eas_endpoint"@propertydef_default_params(self)->Dict[str,Any]:"""Get the default parameters for calling Cohere API."""return{"max_new_tokens":self.max_new_tokens,"temperature":self.temperature,"top_k":self.top_k,"top_p":self.top_p,"stop_sequences":[],}@propertydef_identifying_params(self)->Mapping[str,Any]:"""Get the identifying parameters."""_model_kwargs=self.model_kwargsor{}return{"eas_service_url":self.eas_service_url,"eas_service_token":self.eas_service_token,**_model_kwargs,}def_invocation_params(self,stop_sequences:Optional[List[str]],**kwargs:Any)->dict:params=self._default_paramsifself.stop_sequencesisnotNoneandstop_sequencesisnotNone:raiseValueError("`stop` found in both the input and default params.")elifself.stop_sequencesisnotNone:params["stop"]=self.stop_sequenceselse:params["stop"]=stop_sequencesifself.model_kwargs:params.update(self.model_kwargs)return{**params,**kwargs}@staticmethoddef_process_response(response:Any,stop:Optional[List[str]],version:Optional[str])->str:ifversion=="1.0":text=responseelse:text=response["response"]ifstop:text=enforce_stop_tokens(text,stop)return"".join(text)def_call(self,prompt:str,stop:Optional[List[str]]=None,run_manager:Optional[CallbackManagerForLLMRun]=None,**kwargs:Any,)->str:params=self._invocation_params(stop,**kwargs)prompt=prompt.strip()response=Nonetry:ifself.streaming:completion=""forchunkinself._stream(prompt,stop,run_manager,**params):completion+=chunk.textreturncompletionelse:response=self._call_eas(prompt,params)_stop=params.get("stop")returnself._process_response(response,_stop,self.version)exceptExceptionaserror:raiseValueError(f"Error raised by the service: {error}")def_call_eas(self,prompt:str="",params:Dict={})->Any:"""Generate text from the eas service."""headers={"Content-Type":"application/json","Authorization":f"{self.eas_service_token}",}ifself.version=="1.0":body={"input_ids":f"{prompt}",}else:body={"prompt":f"{prompt}",}# add params to bodyforkey,valueinparams.items():body[key]=value# make requestresponse=requests.post(self.eas_service_url,headers=headers,json=body)ifresponse.status_code!=200:raiseException(f"Request failed with status code {response.status_code}"f" and message {response.text}")try:returnjson.loads(response.text)exceptExceptionase:ifisinstance(e,json.decoder.JSONDecodeError):returnresponse.textraiseedef_stream(self,prompt:str,stop:Optional[List[str]]=None,run_manager:Optional[CallbackManagerForLLMRun]=None,**kwargs:Any,)->Iterator[GenerationChunk]:invocation_params=self._invocation_params(stop,**kwargs)headers={"User-Agent":"Test Client","Authorization":f"{self.eas_service_token}",}ifself.version=="1.0":pload={"input_ids":prompt,**invocation_params}response=requests.post(self.eas_service_url,headers=headers,json=pload,stream=True)res=GenerationChunk(text=response.text)ifrun_manager:run_manager.on_llm_new_token(res.text)# yield text, if anyyieldreselse:pload={"prompt":prompt,"use_stream_chat":"True",**invocation_params}response=requests.post(self.eas_service_url,headers=headers,json=pload,stream=True)forchunkinresponse.iter_lines(chunk_size=8192,decode_unicode=False,delimiter=b"\0"):ifchunk:data=json.loads(chunk.decode("utf-8"))output=data["response"]# identify stop sequence in generated text, if anystop_seq_found:Optional[str]=Noneforstop_seqininvocation_params["stop"]:ifstop_seqinoutput:stop_seq_found=stop_seq# identify text to yieldtext:Optional[str]=Noneifstop_seq_found:text=output[:output.index(stop_seq_found)]else:text=output# yield text, if anyiftext:res=GenerationChunk(text=text)ifrun_manager:run_manager.on_llm_new_token(res.text)yieldres# break if stop sequence foundifstop_seq_found:break