a
    bgfQ                     @   s  d dl Z d dlZd dlZd dlmZmZ d dlmZmZm	Z	m
Z
mZmZ d dlZd dlmZ d dlmZ d dlmZ d dlmZmZmZmZmZ dgZG d	d
 d
eeZe	eef edddZe	eef edddZe	eef edddZG dd deZ G dd deZ!edddZ"edddZ#edddZ$ee%dddZ&eee% ed d!d"Z'eed#d$d%Z(ed&d'd(d)G d*d deZ)dS )+    N)ABCabstractmethod)AnyCallableDictListMappingOptional)
deprecated)CallbackManagerForLLMRun)LLM)	BaseModel
ConfigDictFieldPrivateAttrmodel_validator
Databricksc                   @   s   e Zd ZU dZeed< eed< eeeedddZeeddd	Zeeed
ddZ	e
deeedef  edddZeedddZdS )_DatabricksClientBasez0A base JSON API client that talks to Databricks.api_url	api_token)methodurlrequestreturnc                 C   sH   dd| j  i}tj||||d}|js@td|j d|j | S )NAuthorizationzBearer )r   r   headersjsonzHTTP z error: )r   requestsr   ok
ValueErrorstatus_codetextr   )selfr   r   r   r   response r$   q/var/www/html/cobodadashboardai.evdpl.com/venv/lib/python3.9/site-packages/langchain_community/llms/databricks.pyr      s    z_DatabricksClientBase.request)r   r   c                 C   s   |  d|d S )NGETr   )r"   r   r$   r$   r%   _get&   s    z_DatabricksClientBase._get)r   r   r   c                 C   s   |  d||S )NPOSTr'   )r"   r   r   r$   r$   r%   _post)   s    z_DatabricksClientBase._postN.r   transform_output_fnr   c                 C   s   d S Nr$   )r"   r   r,   r$   r$   r%   post,   s    z_DatabricksClientBase.postr   c                 C   s   dS )NFr$   r"   r$   r$   r%   llm1   s    z_DatabricksClientBase.llm)N)__name__
__module____qualname____doc__str__annotations__r   r   r(   r*   r   r	   r   r.   propertyboolr1   r$   r$   r$   r%   r      s   

 r   )r#   r   c                 C   s   | d d d S )Nchoicesr   r!   r$   r#   r$   r$   r%   _transform_completions6   s    r<   c                 C   s   | d d d S )N
candidatesr   r!   r$   r;   r$   r$   r%   _transform_llama2_chat:   s    r>   c                 C   s   | d d d d S )Nr:   r   messagecontentr$   r;   r$   r$   r%   _transform_chat>   s    rA   c                       s   e Zd ZU dZeed< eed< eed< dZeed< dZe	ed< dZ
ee ed	< ed
 fddZee	dddZeddeeeef edddZdeeedef  edddZ  ZS ) _DatabricksServingEndpointClientz:An API client that talks to a Databricks serving endpoint.hostendpoint_namedatabricks_uriNclientFexternal_or_foundationtaskdatac              
      s   t  jf i | zddlm} || j| _W n. ty\ } ztd|W Y d }~n
d }~0 0 | j| j}|	dd
 dv | _| jd u r|	d| _d S )Nr   )get_deploy_clientzMFailed to create the client. Please install mlflow with `pip install mlflow`.Zendpoint_type )Zexternal_modelZfoundation_model_apirH   )super__init__Zmlflow.deploymentsrK   rE   rF   ImportErrorZget_endpointrD   getlowerrG   rH   )r"   rJ   rK   eendpoint	__class__r$   r%   rN   L   s    
z)_DatabricksServingEndpointClient.__init__r/   c                 C   s
   | j dv S )N)llm/v1/chatllm/v1/completionsllama2/chat)rH   r0   r$   r$   r%   r1   a   s    z$_DatabricksServingEndpointClient.llmbeforemodevaluesr   c                 C   s6   d|vr2|d }|d }d| d| d}||d< |S )Nr   rC   rD   https://z/serving-endpoints/z/invocationsr$   )clsr]   rC   rD   r   r$   r$   r%   set_api_urle   s    z,_DatabricksServingEndpointClient.set_api_url.r+   c                 C   s   | j rL| jj| j|d}|r$||S | jdkr6t|S | jdkrHt|S |S d|gi}| jj| j|d}|d }t|tr|d n|}| jdkrt	|S |r||S |S d S )N)rS   inputsrV   rW   Zdataframe_recordsZpredictionsr   rX   )
rG   rF   ZpredictrD   rH   rA   r<   
isinstancelistr>   )r"   r   r,   respZwrapped_requestr#   predspredr$   r$   r%   r.   o   s$    



z%_DatabricksServingEndpointClient.post)N)r2   r3   r4   r5   r6   r7   rF   r   rG   r9   rH   r	   rN   r8   r1   r   classmethodr   r`   r   r.   __classcell__r$   r$   rT   r%   rB   B   s$   
	 rB   c                   @   sp   e Zd ZU dZeed< eed< eed< eddeeee	f e	ddd	Z
de	eedef  e	dddZd
S )#_DatabricksClusterDriverProxyClientzBAn API client that talks to a Databricks cluster driver proxy app.rC   
cluster_idcluster_driver_portrY   rZ   r\   c                 C   sB   d|vr>|d }|d }|d }d| d| d| }||d< |S )Nr   rC   rj   rk   r^   z/driver-proxy-api/o/0//r$   )r_   r]   rC   rj   portr   r$   r$   r%   r`      s    z/_DatabricksClusterDriverProxyClient.set_api_urlN.r+   c                 C   s   |  | j|}|r||S |S r-   )r*   r   )r"   r   r,   rd   r$   r$   r%   r.      s    z(_DatabricksClusterDriverProxyClient.post)N)r2   r3   r4   r5   r6   r7   r   rg   r   r   r`   r	   r   r.   r$   r$   r$   r%   ri      s   

 ri   r/   c                  C   s4   zddl m}  |  W S  ty.   tdY n0 dS )zgGet the notebook REPL context if running inside a Databricks notebook.
    Returns None otherwise.
    r   get_contextzBCannot access dbruntime, not running inside a Databricks notebook.N)Z!dbruntime.databricks_repl_contextro   rO   rn   r$   r$   r%   get_repl_context   s    rp   c               
   C   st   t d} | sZzt j} | s$tdW n2 tyX } ztd| W Y d}~n
d}~0 0 | ddd} | S )z{Get the default Databricks workspace hostname.
    Raises an error if the hostname cannot be automatically determined.
    ZDATABRICKS_HOSTz(context doesn't contain browserHostName.zshost was not set and cannot be automatically inferred. Set environment variable 'DATABRICKS_HOST'. Received error: Nr^   zhttp://rl   )osgetenvrp   ZbrowserHostNamer   	Exceptionlstriprstrip)rC   rR   r$   r$   r%   get_default_host   s    
rv   c               
   C   sb   t d } r| S zt j} | s(tdW n2 ty\ } ztd| W Y d}~n
d}~0 0 | S )z{Get the default Databricks personal access token.
    Raises an error if the token cannot be automatically determined.
    ZDATABRICKS_TOKENz!context doesn't contain apiToken.zyapi_token was not set and cannot be automatically inferred. Set environment variable 'DATABRICKS_TOKEN'. Received error: N)rq   rr   rp   ZapiTokenr   rs   )r   rR   r$   r$   r%   get_default_api_token   s    rw   )rJ   r   c                 C   s"   t | tsdS d}tt|| S )zJChecks if a data is a valid hexadecimal string using a regular expression.Fz^[0-9a-fA-F]+$)rb   r6   r9   rematch)rJ   patternr$   r$   r%   _is_hex_string   s    
r{   )rJ   allow_dangerous_deserializationr   c              
   C   s   |st dzddl}W n2 tyJ } zt d| W Y d}~n
d}~0 0 z|t| W S  ty } zt d| W Y d}~n
d}~0 0 dS )z3Loads a pickled function from a hexadecimal string.aW  This code relies on the pickle module. You will need to set allow_dangerous_deserialization=True if you want to opt-in to allow deserialization of data using pickle.Data can be compromised by a malicious actor if not handled properly to include a malicious payload that when deserialized with pickle can execute arbitrary code on your machine.r   N*Please install cloudpickle>=2.0.0. Error: zFFailed to load the pickled function from a hexadecimal string. Error: )r   cloudpicklers   loadsbytesfromhex)rJ   r|   r~   rR   r$   r$   r%    _load_pickled_fn_from_hex_string   s    
$r   )fnr   c              
   C   s   zddl }W n2 ty> } ztd| W Y d}~n
d}~0 0 z||  W S  ty } ztd| W Y d}~n
d}~0 0 dS )z6Pickles a function and returns the hexadecimal string.r   Nr}   zFailed to pickle the function: )r~   rs   r   dumpshex)r   r~   rR   r$   r$   r%   _pickle_fn_to_hex_string   s    $r   z0.3.3z1.0z#databricks_langchain.ChatDatabricks)ZsinceZremovalZalternative_importc                       s  e Zd ZU dZeedZeed< ee	dZ
eed< dZee ed< dZee ed< dZee ed< dZeeeef  ed	< dZee ed
< dZeedef  ed< dZeed< dZeed< dZeed< dZeee  ed< dZee ed< eedZeeef ed< dZee ed< dZ e!ed< e" Z#e$ed< e%ddZ&e'eeef dddZ(e)dd e*eeef ed!d"d#Z+ed$ fd%d&Z,e'eeef dd'd(Z-e'e.eef dd)d*Z/e'edd+d,Z0d0eeee  ee1 eed-d.d/Z2  Z3S )1r   a	  Databricks serving endpoint or a cluster driver proxy app for LLM.

    It supports two endpoint types:

    * **Serving endpoint** (recommended for both production and development).
      We assume that an LLM was deployed to a serving endpoint.
      To wrap it as an LLM you must have "Can Query" permission to the endpoint.
      Set ``endpoint_name`` accordingly and do not set ``cluster_id`` and
      ``cluster_driver_port``.

      If the underlying model is a model registered by MLflow, the expected model
      signature is:

      * inputs::

          [{"name": "prompt", "type": "string"},
           {"name": "stop", "type": "list[string]"}]

      * outputs: ``[{"type": "string"}]``

      If the underlying model is an external or foundation model, the response from the
      endpoint is automatically transformed to the expected format unless
      ``transform_output_fn`` is provided.

    * **Cluster driver proxy app** (recommended for interactive development).
      One can load an LLM on a Databricks interactive cluster and start a local HTTP
      server on the driver node to serve the model at ``/`` using HTTP POST method
      with JSON input/output.
      Please use a port number between ``[3000, 8000]`` and let the server listen to
      the driver IP address or simply ``0.0.0.0`` instead of localhost only.
      To wrap it as an LLM you must have "Can Attach To" permission to the cluster.
      Set ``cluster_id`` and ``cluster_driver_port`` and do not set ``endpoint_name``.
      The expected server schema (using JSON schema) is:

      * inputs::

          {"type": "object",
           "properties": {
              "prompt": {"type": "string"},
              "stop": {"type": "array", "items": {"type": "string"}}},
           "required": ["prompt"]}`

      * outputs: ``{"type": "string"}``

    If the endpoint model signature is different or you want to set extra params,
    you can use `transform_input_fn` and `transform_output_fn` to apply necessary
    transformations before and after the query.
    )default_factoryrC   r   NrD   rj   rk   model_kwargstransform_input_fn.r,   
databricksrE   g        temperature   nstop
max_tokensextra_paramsrH   Fr|   _clientZforbid)extrar/   c                 C   s6   | j | jd}| jr| j|d< | jd ur2| j|d< |S )N)r   r   r   r   )r   r   r   r   )r"   paramsr$   r$   r%   _llm_params  s    


zDatabricks._llm_paramsrY   rZ   r\   c              
   C   s  | d}| d}|r&|r&tdnh|r4d |d< nZ|r:nTz t j }rP||d< tdW n2 ty } ztd| W Y d }~n
d }~0 0 | d}|r|rtdn<|rd |d< n.|d u rtdnt|d	krtd
| n | d }rd|vsJ dd|vsJ d|S )Nrj   rD   z-Cannot set both endpoint_name and cluster_id.z"Context doesn't contain clusterId.zuNeither endpoint_name nor cluster_id was set. And the cluster_id cannot be automatically determined. Received error: rk   z6Cannot set both endpoint_name and cluster_driver_port.z<Must set cluster_driver_port to connect to a cluster driver.r   zInvalid cluster_driver_port: r   promptz*model_kwargs must not contain key 'prompt'r   z(model_kwargs must not contain key 'stop')rP   r   rp   Z	clusterIdrs   int)r_   r]   rj   rD   Zcontext_cluster_idrR   rk   r   r$   r$   r%   set_cluster_id  sN    






zDatabricks.set_cluster_idrI   c                    s   d|v r.t |d r.t|d |dd|d< d|v r\t |d r\t|d |dd|d< t jf i | | jd ur| jd urtdn| jd urt	dt
 | jrt| j| j| j| j| jd| _n0| jr| jrt| j| j| j| jd| _ntd	d S )
Nr   r|   )rJ   r|   r,   z.Cannot set both extra_params and extra_params.z<model_kwargs is deprecated. Please use extra_params instead.)rC   r   rD   rE   rH   )rC   r   rj   rk   zDMust specify either endpoint_name or cluster_id/cluster_driver_port.)r{   r   rP   rM   rN   r   r   r   warningswarnDeprecationWarningrD   rB   rC   r   rE   rH   r   rj   rk   ri   )r"   rJ   rT   r$   r%   rN     sT    






zDatabricks.__init__c                 C   sb   | j | j| j| j| j| j| j| j| j| j	| j
| j| jdu r>dnt| j| jdu rTdnt| jdS )zReturn default params.N)rC   rD   rj   rk   rE   r   r   r   r   r   r   rH   r   r,   )rC   rD   rj   rk   rE   r   r   r   r   r   r   rH   r   r   r,   r0   r$   r$   r%   _default_params  s&    zDatabricks._default_paramsc                 C   s   | j S r-   )r   r0   r$   r$   r%   _identifying_params  s    zDatabricks._identifying_paramsc                 C   s   dS )zReturn type of llm.r   r$   r0   r$   r$   r%   	_llm_type  s    zDatabricks._llm_type)r   r   run_managerkwargsr   c                 K   sl   d|i}| j jr|| j || jp*| j || |rD||d< | jrZ| jf i |}| j j|| jdS )zAQueries the LLM endpoint with the given prompt and stop sequence.r   r   )r,   )	r   r1   updater   r   r   r   r.   r,   )r"   r   r   r   r   r   r$   r$   r%   _call$  s    
zDatabricks._call)NN)4r2   r3   r4   r5   r   rv   rC   r6   r7   rw   r   rD   r	   rj   rk   r   r   r   r   r   r,   rE   r   floatr   r   r   r   r   dictr   rH   r|   r9   r   r   r   r   Zmodel_configr8   r   r   rg   r   rN   r   r   r   r   r   r   rh   r$   r$   rT   r%   r   
  sT   
1

	,.  
)*rq   rx   r   abcr   r   typingr   r   r   r   r   r	   r   Zlangchain_core._apir
   Zlangchain_core.callbacksr   Zlangchain_core.language_modelsr   Zpydanticr   r   r   r   r   __all__r   r6   r<   r>   rA   rB   ri   rp   rv   rw   r9   r{   r   r   r   r$   r$   r$   r%   <module>   s<     I	