a
    bg_z                     @   s   d dl Z d dlmZ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 d dlmZmZ d dl	mZ G d	d
 d
eZG dd deZdS )    N)AnyDictIteratorListOptionalTupleUnion)CallbackManagerForLLMRun)LLM)GenerationChunk)convert_to_secret_strget_from_dict_or_env)Field	SecretStr)Responsec                       s  e Zd ZU dZeddZeed< eeddZ	eed< edddZ
eed< edddZeed	< ed
dZeed< dZeeeef  ed< G dd dZeed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Zedd fddZeeeef dddZd/eee  eeef dd d!Zd0eee ef eee  ee ed"d#d$Z eed%d&d'Z!ee"e# d%d(d)Z$d1eee ef eee  ee% ee"e# d*d+d,Z&d2eee ef eee  ee% eed*d-d.Z'  Z(S )3SambaStudiou  
    SambaStudio large language models.

    Setup:
        To use, you should have the environment variables
        ``SAMBASTUDIO_URL`` set with your SambaStudio environment URL.
        ``SAMBASTUDIO_API_KEY``  set with your SambaStudio endpoint API key.
        https://sambanova.ai/products/enterprise-ai-platform-sambanova-suite
        read extra documentation in https://docs.sambanova.ai/sambastudio/latest/index.html
        Example:
        .. code-block:: python
            from langchain_community.llms.sambanova  import SambaStudio
            SambaStudio(
                sambastudio_url="your-SambaStudio-environment-URL",
                sambastudio_api_key="your-SambaStudio-API-key,
                model_kwargs={
                    "model" : model or expert name (set for Bundle endpoints),
                    "max_tokens" : max number of tokens to generate,
                    "temperature" : model temperature,
                    "top_p" : model top p,
                    "top_k" : model top k,
                    "do_sample" : wether to do sample
                    "process_prompt": wether to process prompt
                        (set for Bundle generic v1 and v2 endpoints)
                },
            )
    Key init args — completion params:
        model: str
            The name of the model to use, e.g., Meta-Llama-3-70B-Instruct-4096
            (set for Bundle endpoints).
        streaming: bool
            Whether to use streaming handler when using non streaming methods
        model_kwargs: dict
            Extra Key word arguments to pass to the model:
                max_tokens: int
                    max tokens to generate
                temperature: float
                    model temperature
                top_p: float
                    model top p
                top_k: int
                    model top k
                do_sample: bool
                    wether to do sample
                process_prompt:
                    wether to process prompt
                    (set for Bundle generic v1 and v2 endpoints)
    Key init args — client params:
        sambastudio_url: str
            SambaStudio endpoint Url
        sambastudio_api_key: str
            SambaStudio endpoint api key

    Instantiate:
        .. code-block:: python

            from langchain_community.llms import SambaStudio

            llm = SambaStudio=(
                sambastudio_url = set with your SambaStudio deployed endpoint URL,
                sambastudio_api_key = set with your SambaStudio deployed endpoint Key,
                model_kwargs = {
                    "model" : model or expert name (set for Bundle endpoints),
                    "max_tokens" : max number of tokens to generate,
                    "temperature" : model temperature,
                    "top_p" : model top p,
                    "top_k" : model top k,
                    "do_sample" : wether to do sample
                    "process_prompt" : wether to process prompt
                        (set for Bundle generic v1 and v2 endpoints)
                }
            )

    Invoke:
        .. code-block:: python
            prompt = "tell me a joke"
            response = llm.invoke(prompt)

    Stream:
        .. code-block:: python

        for chunk in llm.stream(prompt):
            print(chunk, end="", flush=True)

    Async:
        .. code-block:: python

        response = llm.ainvoke(prompt)
        await response

     defaultsambastudio_urlsambastudio_api_keyT)r   excludebase_urlstreaming_urlF	streamingNmodel_kwargsc                   @   s   e Zd ZdZdS )zSambaStudio.ConfigTN__name__
__module____qualname__Zpopulate_by_name r    r    p/var/www/html/cobodadashboardai.evdpl.com/venv/lib/python3.9/site-packages/langchain_community/llms/sambanova.pyConfig|   s   r"   returnc                 C   s   dS )9Return whether this model can be serialized by Langchain.Tr    clsr    r    r!   is_lc_serializable   s    zSambaStudio.is_lc_serializablec                 C   s
   dddS )Nr   r   )r   r   r    selfr    r    r!   
lc_secrets   s    zSambaStudio.lc_secretsc                 C   s   d| j id| jiS )Return a dictionary of identifying parameters.

        This information is used by the LangChain callback system, which
        is used for tracing purposes make it possible to monitor LLMs.
        r   r   )r   r   r)   r    r    r!   _identifying_params   s    zSambaStudio._identifying_paramsc                 C   s   dS )zReturn type of llm.zsambastudio-llmr    r)   r    r    r!   	_llm_type   s    zSambaStudio._llm_typekwargsr$   c                    sT   t |dd|d< tt |dd|d< | |d \|d< |d< t jf i | dS )'init and validate environment variablesr   ZSAMBASTUDIO_URLr   ZSAMBASTUDIO_API_KEYr   r   N)r   r   _get_sambastudio_urlssuper__init__r*   r0   	__class__r    r!   r4      s    
zSambaStudio.__init__)urlr$   c                 C   sZ   d|v r|}|}n@d|v r,| dd}|}n&|}d|v rJd|d}ntd||fS )a3  
        Get streaming and non streaming URLs from the given URL

        Args:
            url: string with sambastudio base or streaming endpoint url

        Returns:
            base_url: string with url to do non streaming calls
            streaming_url: string with url to do streaming calls
        chat/completionsstreamzstream/r   Zgenericzgeneric/streamUnsupported URL)replacejoinsplit
ValueError)r*   r8   r   Z
stream_urlr    r    r!   r2      s    z!SambaStudio._get_sambastudio_urls)stopr$   c                 C   sD  |du rg }| j pi }|dg | }t|dkr:||d< d| jv rd| v r^|d|d< d| v rx|d|d< d	| v r|d	 |}nd
| jv rd| v r|d|d< d| v r|d|d< |}nhd| jv r.d| v r|d|d< d| v r|d|d< dd | D }ntd| j d|S )aD  
        Get the tuning parameters to use when calling the LLM.

        Args:
            stop: Stop words to use when generating. Model output is cut off at the
                first occurrence of any of the stop substrings.

        Returns:
            The tuning parameters in the format required by api to use
        NZstop_sequencesr   r9   Zselect_expertmodelZmax_tokens_to_generate
max_tokensprocess_promptapi/v2/predict/genericapi/predict/genericc                 S   s&   i | ]\}}|t |jt|d qS ))typevalue)rF   r   str).0kvr    r    r!   
<dictcomp>   s   z2SambaStudio._get_tuning_params.<locals>.<dictcomp>r;   9only openai, generic v1 and generic v2 APIs are supported)r   getlenr   keyspopitemsr?   )r*   r@   Z_model_kwargsZ_stop_sequencesZtuning_paramsr    r    r!   _get_tuning_params   sL    



zSambaStudio._get_tuning_paramspromptr@   r   r$   c                 C   s  t |tr|g}| |}d| jv rnd|d dg}||d|}dd | D }d| j  d	d
}nd| jv r|ddrt	ddd|d dgd}n|d }d|dg}dd | D }||d}d| j i}nd| jv rr|ddr<|d ddkr2t	ddd|d dgd}n|d }n|d }|rV||d}n|g|d}d| j i}nt
d| j dt }	|r|	j| j||dd}
n|	j| j||dd}
|
jd krtd!|
j d"|
j d"|
S )#a  
        Performs a post request to the LLM API.

        Args:
        prompt: The prompt to pass into the model
        stop: list of stop tokens
        streaming: wether to do a streaming call

        Returns:
            A request Response object
        r9   userr   rolecontent)messagesr:   c                 S   s   i | ]\}}|d ur||qS Nr    rI   keyrG   r    r    r!   rL         z/SambaStudio._handle_request.<locals>.<dictcomp>Bearer application/jsonAuthorizationzContent-TyperD   rC   Fzsambaverse-conversation-idN)Z
message_idrX   rY   )Zconversation_idrZ   Zitem0)idrG   c                 S   s   i | ]\}}|d ur||qS r[   r    r\   r    r    r!   rL   2  r^   )rR   paramsr]   rE   rG   True)instancerd   )Z	instancesrd   r;   rM   Theadersjsonr:      2Sambanova / complete call failed with status code .)
isinstancerH   rS   r   rR   r   get_secret_valuerN   ri   dumpsr?   requestsSessionpostr   r   status_codeRuntimeErrortext)r*   rU   r@   r   rd   messages_dictdatarh   rR   http_sessionresponser    r    r!   _handle_request  s~    




	



zSambaStudio._handle_requestry   r$   c              
   C   s   z|  }W n: tyF } z"td| d|j W Y d}~n
d}~0 0 d| jv rh|d d d d }nNd	| jv r|d
 d d d }n.d| jv r|d d d }ntd| j d|S )
        Process a non streaming response from the api

        Args:
            response: A request Response object

        Returns
            completion: a string with model generation
        ;Sambanova /complete call failed couldn't get JSON response 
response: Nr9   choicesr   messagerY   rD   rR   rG   
completionrE   Zpredictionsr;   rM   )ri   	Exceptionrt   ru   r   r?   r*   ry   Zresponse_dicter   r    r    r!   _process_responseh  s$    


zSambaStudio._process_responsec           
      c   s  zddl }W n ty&   tdY n0 d| jv rV||}| D ]}|jdkrptd|j d|j dz|jdkrt	|jt
rt|j}ntd|j d|j d|d	rtd|j d|j dt|d
 dkr|d
 d d d }nd}t|d}|V  W qF tyN } z"td| d|j W Y d}~qFd}~0 0 qFn.d| jv r| D ]x}	z6t|	}|d d d d d }t|d}|V  W n: ty } z td| d|	 W Y d}~n
d}~0 0 qjnd| jv rr| D ]t}	z2t|	}|d d d d }t|d}|V  W n: tyj } z td| d|	 W Y d}~n
d}~0 0 qntd| j ddS )
        Process a streaming response from the api

        Args:
            response: An iterable request Response object

        Yields:
            GenerationChunk: a GenerationChunk with model partial generation
        r   NTcould not import sseclient libraryPlease install it with `pip install sseclient-py`.r9   error_event1Sambanova /complete call failed with status code rl   [DONE]errorr   deltarY   r   ru   3Error getting content chunk raw streamed response: data: rD   resultrR   rG   Zstream_tokenzline: rE   	responsesr;   rM   )	sseclientImportErrorr   	SSEClienteventseventrt   rs   rw   rm   rH   ri   loadsrN   rO   r   r   
iter_linesr?   )
r*   ry   r   clientr   rw   rY   generated_chunkr   liner    r    r!   _process_stream_response  s    





 


 


 z$SambaStudio._process_stream_responserU   r@   run_managerr0   r$   c                 k   s:   | j ||dd}| |D ]}|r.||j |V  qdS )a\  Call out to Sambanova's complete endpoint.

        Args:
            prompt: The prompt to pass into the model.
            stop: a list of strings on which the model should stop generating.
            run_manager: A run manager with callbacks for the LLM.
        Yields:
            chunk: GenerationChunk with model partial generation
        Tr   Nrz   r   Zon_llm_new_tokenru   r*   rU   r@   r   r0   ry   chunkr    r    r!   _stream  s
    zSambaStudio._streamc                 K   sT   | j r6d}| jf |||d|D ]}||j7 }q"|S | j||dd}| |}|S )a  Call out to Sambanova's complete endpoint.

        Args:
            prompt: The prompt to pass into the model.
            stop: a list of strings on which the model should stop generating.

        Returns:
            result: string with model generation
        r   rU   r@   r   Fr   r   r   ru   rz   r   r*   rU   r@   r   r0   r   r   ry   r    r    r!   _call  s    

zSambaStudio._call)N)NF)NN)NN))r   r   r   __doc__r   r   rH   __annotations__r   r   r   r   r   boolr   r   r   r   r"   classmethodr(   propertyr+   r-   r.   r4   r   r2   r   rS   r   r   rz   r   r   r   r   r	   r   r   __classcell__r    r    r6   r!   r      s^   
\"D  
f$b  
  
r   c                       s  e Zd ZU dZeddZeed< eeddZ	eed< eddZ
eed< eddZeed	< ed
dZeed< eddZeed< eddZee ed< eddZee ed< eddidZeed< G dd dZeed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Zedd fd d!Zd/e e!e ef ee!e  ee e"d"d#d$Z#e"ed%d&d'Z$e"e%e& d%d(d)Z'd0e e!e ef ee!e  ee( eed*d+d,Z)d1e e!e ef ee!e  ee( ee%e& d*d-d.Z*  Z+S )2SambaNovaCloudu  
    SambaNova Cloud large language models.

    Setup:
        To use, you should have the environment variables:
        ``SAMBANOVA_URL`` set with SambaNova Cloud URL.
        defaults to http://cloud.sambanova.ai/
        ``SAMBANOVA_API_KEY`` set with your SambaNova Cloud API Key.
        Example:
        .. code-block:: python
            from langchain_community.llms.sambanova  import SambaNovaCloud
            SambaNovaCloud(
                sambanova_api_key="your-SambaNovaCloud-API-key,
                model = model name,
                max_tokens = max number of tokens to generate,
                temperature = model temperature,
                top_p = model top p,
                top_k = model top k
            )
    Key init args — completion params:
        model: str
            The name of the model to use, e.g., Meta-Llama-3-70B-Instruct-4096
            (set for CoE endpoints).
        streaming: bool
            Whether to use streaming handler when using non streaming methods
        max_tokens: int
            max tokens to generate
        temperature: float
            model temperature
        top_p: float
            model top p
        top_k: int
            model top k

    Key init args — client params:
        sambanova_url: str
            SambaNovaCloud Url defaults to http://cloud.sambanova.ai/
        sambanova_api_key: str
            SambaNovaCloud api key
    Instantiate:
        .. code-block:: python
            from langchain_community.llms.sambanova  import SambaNovaCloud
            SambaNovaCloud(
                sambanova_api_key="your-SambaNovaCloud-API-key,
                model = model name,
                max_tokens = max number of tokens to generate,
                temperature = model temperature,
                top_p = model top p,
                top_k = model top k
            )
    Invoke:
        .. code-block:: python
            prompt = "tell me a joke"
            response = llm.invoke(prompt)
    Stream:
        .. code-block:: python
        for chunk in llm.stream(prompt):
            print(chunk, end="", flush=True)
    Async:
        .. code-block:: python
        response = llm.ainvoke(prompt)
        await response
    r   r   sambanova_urlsambanova_api_keyzMeta-Llama-3.1-8B-InstructrA   Fr   i   rB   gffffff?temperatureNtop_ptop_kZinclude_usageTstream_optionsc                   @   s   e Zd ZdZdS )zSambaNovaCloud.ConfigTNr   r    r    r    r!   r"   {  s   r"   r#   c                 C   s   dS )r%   Fr    r&   r    r    r!   r(   ~  s    z!SambaNovaCloud.is_lc_serializablec                 C   s   ddiS )Nr   r    r)   r    r    r!   r+     s    zSambaNovaCloud.lc_secretsc                 C   s"   | j | j| j| j| j| j| jdS )r,   rA   r   rB   r   r   r   r   r   r)   r    r    r!   r-     s    z"SambaNovaCloud._identifying_paramsc                 C   s   dS )z7Get the type of language model used by this chat model.zsambanovacloud-llmr    r)   r    r    r!   r.     s    zSambaNovaCloud._llm_typer/   c                    s>   t |dddd|d< tt |dd|d< t jf i | dS )r1   r   ZSAMBANOVA_URLz,https://api.sambanova.ai/v1/chat/completionsr   r   ZSAMBANOVA_API_KEYN)r   r   r3   r4   r5   r6   r    r!   r4     s    

zSambaNovaCloud.__init__rT   c           	   	   C   s   t |tr|g}d|d dg}||| j|| j| j| j| jd}dd | D }d| j	  dd	}t
 }|r|j| j||d
d}n|j| j||dd}|jdkrtd|j d|j d|S )z
        Performs a post request to the LLM API.

        Args:
            prompt: The prompt to pass into the model.
            stop: list of stop tokens

        Returns:
            A request Response object
        rV   r   rW   )rZ   r:   rB   r@   rA   r   r   r   c                 S   s   i | ]\}}|d ur||qS r[   r    r\   r    r    r!   rL     r^   z2SambaNovaCloud._handle_request.<locals>.<dictcomp>r_   r`   ra   Trg   Frj   rk   rl   )rm   rH   rB   rA   r   r   r   rR   r   rn   rp   rq   rr   r   rs   rt   ru   )	r*   rU   r@   r   rv   rw   rh   rx   ry   r    r    r!   rz     sB    




zSambaNovaCloud._handle_requestr{   c              
   C   s`   z|  }W n: tyF } z"td| d|j W Y d}~n
d}~0 0 |d d d d }|S )r|   r}   r~   Nr   r   r   rY   )ri   r   rt   ru   r   r    r    r!   r     s    z SambaNovaCloud._process_responsec           	      c   sJ  zddl }W n ty&   tdY n0 ||}| D ]}|jdkrdtd|j d|j dz|jdkrt|jt	rt
|j}ntd|j d|j d|drtd|j d|j dt|d	 dkr|d	 d d
 d }nd}t|d}|V  W q: tyB } z"td| d|j W Y d}~q:d}~0 0 q:dS )r   r   Nr   r   r   rl   r   r   r   r   rY   r   r   r   r   )r   r   r   r   r   rt   rs   rw   rm   rH   ri   r   rN   rO   r   r   )	r*   ry   r   r   r   rw   rY   r   r   r    r    r!   r     s\    





z'SambaNovaCloud._process_stream_responser   c                 K   sT   | j r6d}| jf |||d|D ]}||j7 }q"|S | j||dd}| |}|S )  Call out to SambaNovaCloud complete endpoint.

        Args:
            prompt: The prompt to pass into the model.
            stop: Optional list of stop words to use when generating.

        Returns:
            The string generated by the model.
        r   r   Fr   r   r   r    r    r!   r   1  s    

zSambaNovaCloud._callc                 k   s:   | j ||dd}| |D ]}|r.||j |V  qdS )r   Tr   Nr   r   r    r    r!   r   N  s
    zSambaNovaCloud._stream)NF)NN)NN),r   r   r   r   r   r   rH   r   r   r   rA   r   r   rB   intr   floatr   r   r   r   dictr"   r   r(   r   r   r+   r   r-   r.   r4   r   r   r   rz   r   r   r   r   r	   r   r   r   r    r    r6   r!   r     s`   
@  
6<  
   
r   )ri   typingr   r   r   r   r   r   r   rp   Z langchain_core.callbacks.managerr	   Z#langchain_core.language_models.llmsr
   Zlangchain_core.outputsr   Zlangchain_core.utilsr   r   Zpydanticr   r   r   r   r   r    r    r    r!   <module>   s   $    