"""Wrapper around Konko AI's Completion API."""importloggingimportwarningsfromtypingimportAny,Dict,List,Optionalfromlangchain_core.callbacksimport(AsyncCallbackManagerForLLMRun,CallbackManagerForLLMRun,)fromlangchain_core.language_models.llmsimportLLMfromlangchain_core.pydantic_v1importSecretStr,root_validatorfromlangchain_community.utils.openaiimportis_openai_v1logger=logging.getLogger(__name__)
[docs]classKonko(LLM):"""Konko AI models. To use, you'll need an API key. This can be passed in as init param ``konko_api_key`` or set as environment variable ``KONKO_API_KEY``. Konko AI API reference: https://docs.konko.ai/reference/ """base_url:str="https://api.konko.ai/v1/completions""""Base inference API URL."""konko_api_key:SecretStr"""Konko AI API key."""model:str"""Model name. Available models listed here: https://docs.konko.ai/reference/get_models """temperature:Optional[float]=None"""Model temperature."""top_p:Optional[float]=None"""Used to dynamically adjust the number of choices for each predicted token based on the cumulative probabilities. A value of 1 will always yield the same output. A temperature less than 1 favors more correctness and is appropriate for question answering or summarization. A value greater than 1 introduces more randomness in the output. """top_k:Optional[int]=None"""Used to limit the number of choices for the next predicted word or token. It specifies the maximum number of tokens to consider at each step, based on their probability of occurrence. This technique helps to speed up the generation process and can improve the quality of the generated text by focusing on the most likely options. """max_tokens:Optional[int]=None"""The maximum number of tokens to generate."""repetition_penalty:Optional[float]=None"""A number that controls the diversity of generated text by reducing the likelihood of repeated sequences. Higher values decrease repetition. """logprobs:Optional[int]=None"""An integer that specifies how many top token log probabilities are included in the response for each token generation step. """classConfig:extra="forbid"@root_validator(pre=True)defvalidate_environment(cls,values:Dict[str,Any])->Dict[str,Any]:"""Validate that python package exists in environment."""try:importkonkoexceptImportError:raiseImportError("Could not import konko python package. ""Please install it with `pip install konko`.")ifnothasattr(konko,"_is_legacy_openai"):warnings.warn("You are using an older version of the 'konko' package. ""Please consider upgrading to access new features""including the completion endpoint.")returnvalues
@propertydefdefault_params(self)->Dict[str,Any]:return{"model":self.model,"temperature":self.temperature,"top_p":self.top_p,"top_k":self.top_k,"max_tokens":self.max_tokens,"repetition_penalty":self.repetition_penalty,}def_call(self,prompt:str,stop:Optional[List[str]]=None,run_manager:Optional[CallbackManagerForLLMRun]=None,**kwargs:Any,)->str:"""Call out to Konko's text generation endpoint. Args: prompt: The prompt to pass into the model. Returns: The string generated by the model.. """importkonkopayload=self.construct_payload(prompt,stop,**kwargs)try:ifis_openai_v1():response=konko.completions.create(**payload)else:response=konko.Completion.create(**payload)exceptAttributeError:raiseValueError("`konko` has no `Completion` attribute, this is likely ""due to an old version of the konko package. Try upgrading it ""with `pip install --upgrade konko`.")ifis_openai_v1():output=response.choices[0].textelse:output=response["choices"][0]["text"]returnoutputasyncdef_acall(self,prompt:str,stop:Optional[List[str]]=None,run_manager:Optional[AsyncCallbackManagerForLLMRun]=None,**kwargs:Any,)->str:"""Asynchronously call out to Konko's text generation endpoint. Args: prompt: The prompt to pass into the model. Returns: The string generated by the model. """importkonkopayload=self.construct_payload(prompt,stop,**kwargs)try:ifis_openai_v1():client=konko.AsyncKonko()response=awaitclient.completions.create(**payload)else:response=awaitkonko.Completion.acreate(**payload)exceptAttributeError:raiseValueError("`konko` has no `Completion` attribute, this is likely ""due to an old version of the konko package. Try upgrading it ""with `pip install --upgrade konko`.")ifis_openai_v1():output=response.choices[0].textelse:output=response["choices"][0]["text"]returnoutput