[docs]classAI21LLM(BaseLLM,AI21Base):"""AI21 large language models. Different model types support different parameters and different parameter values. Please read the [AI21 reference documentation] (https://docs.ai21.com/reference) for your model to understand which parameters are available. AI21LLM supports only the older Jurassic models. We recommend using ChatAI21 with the newest models, for better results and more features. Example: .. code-block:: python from langchain_ai21 import AI21LLM model = AI21LLM( # defaults to os.environ.get("AI21_API_KEY") api_key="my_api_key" ) """model:str"""Model type you wish to interact with. You can view the options at https://github.com/AI21Labs/ai21-python?tab=readme-ov-file#model-types"""num_results:int=1"""The number of responses to generate for a given prompt."""max_tokens:int=16"""The maximum number of tokens to generate for each response."""min_tokens:int=0"""The minimum number of tokens to generate for each response. _Not supported for all models._"""temperature:float=0.7"""A value controlling the "creativity" of the model's responses."""top_p:float=1"""A value controlling the diversity of the model's responses."""top_k_return:int=0"""The number of top-scoring tokens to consider for each generation step. _Not supported for all models._"""frequency_penalty:Optional[Any]=None"""A penalty applied to tokens that are frequently generated. _Not supported for all models._"""presence_penalty:Optional[Any]=None""" A penalty applied to tokens that are already present in the prompt. _Not supported for all models._"""count_penalty:Optional[Any]=None"""A penalty applied to tokens based on their frequency in the generated responses. _Not supported for all models._"""custom_model:Optional[str]=Noneepoch:Optional[int]=NoneclassConfig:"""Configuration for this pydantic object."""allow_population_by_field_name=True@propertydef_llm_type(self)->str:"""Return type of LLM."""return"ai21-llm"@propertydef_default_params(self)->Mapping[str,Any]:base_params={"model":self.model,"num_results":self.num_results,"max_tokens":self.max_tokens,"min_tokens":self.min_tokens,"temperature":self.temperature,"top_p":self.top_p,"top_k_return":self.top_k_return,}ifself.count_penaltyisnotNone:base_params["count_penalty"]=self.count_penalty.to_dict()ifself.custom_modelisnotNone:base_params["custom_model"]=self.custom_modelifself.epochisnotNone:base_params["epoch"]=self.epochifself.frequency_penaltyisnotNone:base_params["frequency_penalty"]=self.frequency_penalty.to_dict()ifself.presence_penaltyisnotNone:base_params["presence_penalty"]=self.presence_penalty.to_dict()returnbase_paramsdef_build_params_for_request(self,stop:Optional[List[str]]=None,**kwargs:Any)->Mapping[str,Any]:params={}ifstopisnotNone:if"stop"inkwargs:raiseValueError("stop is defined in both stop and kwargs")params["stop_sequences"]=stopreturn{**self._default_params,**params,**kwargs,}def_generate(self,prompts:List[str],stop:Optional[List[str]]=None,run_manager:Optional[CallbackManagerForLLMRun]=None,**kwargs:Any,)->LLMResult:generations:List[List[Generation]]=[]token_count=0params=self._build_params_for_request(stop=stop,**kwargs)forpromptinprompts:response=self._invoke_completion(prompt=prompt,**params)generation=self._response_to_generation(response)generations.append(generation)token_count+=self.client.count_tokens(prompt)llm_output={"token_count":token_count,"model_name":self.model}returnLLMResult(generations=generations,llm_output=llm_output)asyncdef_agenerate(self,prompts:List[str],stop:Optional[List[str]]=None,run_manager:Optional[AsyncCallbackManagerForLLMRun]=None,**kwargs:Any,)->LLMResult:# Change implementation if integration natively supports async generation.returnawaitasyncio.get_running_loop().run_in_executor(None,partial(self._generate,**kwargs),prompts,stop,run_manager)def_invoke_completion(self,prompt:str,**kwargs:Any,)->CompletionsResponse:returnself.client.completion.create(prompt=prompt,**kwargs,)def_response_to_generation(self,response:CompletionsResponse)->List[Generation]:return[Generation(text=completion.data.text,# type: ignore[arg-type]generation_info=completion.to_dict(),)forcompletioninresponse.completions]