[docs]classYiLLM(LLM):"""Yi large language models."""model:str="yi-large"temperature:float=0.3top_p:float=0.95timeout:int=60model_kwargs:Dict[str,Any]=Field(default_factory=dict)yi_api_key:Optional[SecretStr]=Noneregion:Literal["auto","domestic","international"]="auto"yi_api_url_domestic:str="https://api.lingyiwanwu.com/v1/chat/completions"yi_api_url_international:str="https://api.01.ai/v1/chat/completions"def__init__(self,**kwargs:Any):kwargs["yi_api_key"]=convert_to_secret_str(get_from_dict_or_env(kwargs,"yi_api_key","YI_API_KEY"))super().__init__(**kwargs)@propertydef_default_params(self)->Dict[str,Any]:return{"model":self.model,"temperature":self.temperature,"top_p":self.top_p,**self.model_kwargs,}def_post(self,request:Any)->Any:headers={"Content-Type":"application/json","Authorization":f"Bearer {self.yi_api_key.get_secret_value()}",# type: ignore}urls=[]ifself.region=="domestic":urls=[self.yi_api_url_domestic]elifself.region=="international":urls=[self.yi_api_url_international]else:# autourls=[self.yi_api_url_domestic,self.yi_api_url_international]forurlinurls:try:response=requests.post(url,headers=headers,json=request,timeout=self.timeout,)ifresponse.status_code==200:parsed_json=json.loads(response.text)returnparsed_json["choices"][0]["message"]["content"]elif(response.status_code!=403):# If not a permission error, raise immediatelyresponse.raise_for_status()exceptrequests.RequestExceptionase:ifurl==urls[-1]:# If this is the last URL to tryraiseValueError(f"An error has occurred: {e}")else:logger.warning(f"Failed to connect to {url}, trying next URL")continueraiseValueError("Failed to connect to all available URLs")def_call(self,prompt:str,stop:Optional[List[str]]=None,run_manager:Optional[CallbackManagerForLLMRun]=None,**kwargs:Any,)->str:request=self._default_paramsrequest["messages"]=[{"role":"user","content":prompt}]request.update(kwargs)text=self._post(request)ifstopisnotNone:text=enforce_stop_tokens(text,stop)returntext@propertydef_llm_type(self)->str:"""Return type of chat_model."""return"yi-llm"