[docs]classOCIModelDeploymentLLM(LLM):"""Base class for LLM deployed on OCI Data Science Model Deployment."""auth:dict=Field(default_factory=dict,exclude=True)"""ADS auth dictionary for OCI authentication: https://accelerated-data-science.readthedocs.io/en/latest/user_guide/cli/authentication.html. This can be generated by calling `ads.common.auth.api_keys()` or `ads.common.auth.resource_principal()`. If this is not provided then the `ads.common.default_signer()` will be used."""max_tokens:int=256"""Denotes the number of tokens to predict per generation."""temperature:float=0.2"""A non-negative float that tunes the degree of randomness in generation."""k:int=0"""Number of most likely tokens to consider at each step."""p:float=0.75"""Total probability mass of tokens to consider at each step."""endpoint:str="""""The uri of the endpoint from the deployed Model Deployment model."""best_of:int=1"""Generates best_of completions server-side and returns the "best" (the one with the highest log probability per token). """stop:Optional[List[str]]=None"""Stop words to use when generating. Model output is cut off at the first occurrence of any of these substrings."""@pre_initdefvalidate_environment(# pylint: disable=no-self-argumentcls,values:Dict)->Dict:"""Validate that python package exists in environment."""try:importadsexceptImportErrorasex:raiseImportError("Could not import ads python package. ""Please install it with `pip install oracle_ads`.")fromexifnotvalues.get("auth",None):values["auth"]=ads.common.auth.default_signer()values["endpoint"]=get_from_dict_or_env(values,"endpoint","OCI_LLM_ENDPOINT",)returnvalues@propertydef_default_params(self)->Dict[str,Any]:"""Default parameters for the model."""raiseNotImplementedError@propertydef_identifying_params(self)->Dict[str,Any]:"""Get the identifying parameters."""return{**{"endpoint":self.endpoint},**self._default_params,}def_construct_json_body(self,prompt:str,params:dict)->dict:"""Constructs the request body as a dictionary (JSON)."""raiseNotImplementedErrordef_invocation_params(self,stop:Optional[List[str]],**kwargs:Any)->dict:"""Combines the invocation parameters with default parameters."""params=self._default_paramsifself.stopisnotNoneandstopisnotNone:raiseValueError("`stop` found in both the input and default params.")elifself.stopisnotNone:params["stop"]=self.stopelifstopisnotNone:params["stop"]=stopelse:# Don't set "stop" in param as None. It should be a list.params["stop"]=[]return{**params,**kwargs}def_process_response(self,response_json:dict)->str:raiseNotImplementedErrordef_call(self,prompt:str,stop:Optional[List[str]]=None,run_manager:Optional[CallbackManagerForLLMRun]=None,**kwargs:Any,)->str:"""Call out to OCI Data Science Model Deployment endpoint. Args: prompt (str): The prompt to pass into the model. stop (List[str], Optional): List of stop words to use when generating. kwargs: requests_kwargs: Additional ``**kwargs`` to pass to requests.post Returns: The string generated by the model. Example: .. code-block:: python response = oci_md("Tell me a joke.") """requests_kwargs=kwargs.pop("requests_kwargs",{})params=self._invocation_params(stop,**kwargs)body=self._construct_json_body(prompt,params)logger.info(f"LLM API Request:\n{prompt}")response=self._send_request(data=body,endpoint=self.endpoint,**requests_kwargs)completion=self._process_response(response)logger.info(f"LLM API Completion:\n{completion}")returncompletiondef_send_request(self,data:Any,endpoint:str,header:Optional[dict]={},**kwargs:Any,)->Dict:"""Sends request to the oci data science model deployment endpoint. Args: data (Json serializable): data need to be sent to the endpoint. endpoint (str): The model HTTP endpoint. header (dict, optional): A dictionary of HTTP headers to send to the specified url. Defaults to {}. kwargs: Additional ``**kwargs`` to pass to requests.post. Raises: Exception: Raise when invoking fails. Returns: A JSON representation of a requests.Response object. """ifnotheader:header={}header["Content-Type"]=(header.pop("content_type",DEFAULT_CONTENT_TYPE_JSON)orDEFAULT_CONTENT_TYPE_JSON)request_kwargs={"json":data}request_kwargs["headers"]=headertimeout=kwargs.pop("timeout",DEFAULT_TIME_OUT)attempts=0whileattempts<2:request_kwargs["auth"]=self.auth.get("signer")response=requests.post(endpoint,timeout=timeout,**request_kwargs,**kwargs)ifresponse.status_code==401:self._refresh_signer()attempts+=1continuebreaktry:response.raise_for_status()response_json=response.json()exceptException:logger.error("DEBUG INFO: request_kwargs=%s, status_code=%s, content=%s",request_kwargs,response.status_code,response.content,)raisereturnresponse_jsondef_refresh_signer(self)->None:ifself.auth.get("signer",None)andhasattr(self.auth["signer"],"refresh_security_token"):self.auth["signer"].refresh_security_token()
[docs]classOCIModelDeploymentTGI(OCIModelDeploymentLLM):"""OCI Data Science Model Deployment TGI Endpoint. To use, you must provide the model HTTP endpoint from your deployed model, e.g. https://<MD_OCID>/predict. To authenticate, `oracle-ads` has been used to automatically load credentials: https://accelerated-data-science.readthedocs.io/en/latest/user_guide/cli/authentication.html Make sure to have the required policies to access the OCI Data Science Model Deployment endpoint. See: https://docs.oracle.com/en-us/iaas/data-science/using/model-dep-policies-auth.htm#model_dep_policies_auth__predict-endpoint Example: .. code-block:: python from langchain_community.llms import ModelDeploymentTGI oci_md = ModelDeploymentTGI(endpoint="https://<MD_OCID>/predict") """do_sample:bool=True"""If set to True, this parameter enables decoding strategies such as multi-nominal sampling, beam-search multi-nominal sampling, Top-K sampling and Top-p sampling. """watermark:bool=True"""Watermarking with `A Watermark for Large Language Models <https://arxiv.org/abs/2301.10226>`_. Defaults to True."""return_full_text:bool=False"""Whether to prepend the prompt to the generated text. Defaults to False."""@propertydef_llm_type(self)->str:"""Return type of llm."""return"oci_model_deployment_tgi_endpoint"@propertydef_default_params(self)->Dict[str,Any]:"""Get the default parameters for invoking OCI model deployment TGI endpoint."""return{"best_of":self.best_of,"max_new_tokens":self.max_tokens,"temperature":self.temperature,"top_k":self.kifself.k>0elseNone,# `top_k` must be strictly positive'"top_p":self.p,"do_sample":self.do_sample,"return_full_text":self.return_full_text,"watermark":self.watermark,}def_construct_json_body(self,prompt:str,params:dict)->dict:return{"inputs":prompt,"parameters":params,}def_process_response(self,response_json:dict)->str:returnstr(response_json.get("generated_text",response_json))+"\n"
[docs]classOCIModelDeploymentVLLM(OCIModelDeploymentLLM):"""VLLM deployed on OCI Data Science Model Deployment To use, you must provide the model HTTP endpoint from your deployed model, e.g. https://<MD_OCID>/predict. To authenticate, `oracle-ads` has been used to automatically load credentials: https://accelerated-data-science.readthedocs.io/en/latest/user_guide/cli/authentication.html Make sure to have the required policies to access the OCI Data Science Model Deployment endpoint. See: https://docs.oracle.com/en-us/iaas/data-science/using/model-dep-policies-auth.htm#model_dep_policies_auth__predict-endpoint Example: .. code-block:: python from langchain_community.llms import OCIModelDeploymentVLLM oci_md = OCIModelDeploymentVLLM( endpoint="https://<MD_OCID>/predict", model="mymodel" ) """model:str"""The name of the model."""n:int=1"""Number of output sequences to return for the given prompt."""k:int=-1"""Number of most likely tokens to consider at each step."""frequency_penalty:float=0.0"""Penalizes repeated tokens according to frequency. Between 0 and 1."""presence_penalty:float=0.0"""Penalizes repeated tokens. Between 0 and 1."""use_beam_search:bool=False"""Whether to use beam search instead of sampling."""ignore_eos:bool=False"""Whether to ignore the EOS token and continue generating tokens after the EOS token is generated."""logprobs:Optional[int]=None"""Number of log probabilities to return per output token."""@propertydef_llm_type(self)->str:"""Return type of llm."""return"oci_model_deployment_vllm_endpoint"@propertydef_default_params(self)->Dict[str,Any]:"""Get the default parameters for calling vllm."""return{"best_of":self.best_of,"frequency_penalty":self.frequency_penalty,"ignore_eos":self.ignore_eos,"logprobs":self.logprobs,"max_tokens":self.max_tokens,"model":self.model,"n":self.n,"presence_penalty":self.presence_penalty,"stop":self.stop,"temperature":self.temperature,"top_k":self.k,"top_p":self.p,"use_beam_search":self.use_beam_search,}def_construct_json_body(self,prompt:str,params:dict)->dict:return{"prompt":prompt,**params,}def_process_response(self,response_json:dict)->str:returnresponse_json["choices"][0]["text"]