U
    h                     @   s   d Z ddlZddlmZmZmZmZ ddlmZ ddl	m
Z
 ddlmZ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 eeZe
ddddG dd deZdS )z,Wrapper around Together AI's Completion API.    N)AnyDictListOptional)ClientSession)
deprecated)AsyncCallbackManagerForLLMRunCallbackManagerForLLMRun)LLM)	SecretStrroot_validatorconvert_to_secret_strget_from_dict_or_env)Requestsz0.0.12z1.0zlangchain_together.Together)ZsinceZremovalZalternative_importc                   @   sL  e Zd ZU dZdZeed< eed< 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d
< dZe	e
 ed< dZe	e ed< G dd dZeddeedddZeedddZeedddZeedddZeeeef dddZd#ee	ee  e	e eeddd Zd$ee	ee  e	e  eedd!d"Z!dS )%TogetheraL  LLM models from `Together`.

    To use, you'll need an API key which you can find here:
    https://api.together.xyz/settings/api-keys. This can be passed in as init param
    ``together_api_key`` or set as environment variable ``TOGETHER_API_KEY``.

    Together AI API reference: https://docs.together.ai/reference/inference
    z"https://api.together.xyz/inferencebase_urltogether_api_keymodelNtemperaturetop_ptop_k
max_tokensrepetition_penaltylogprobsc                   @   s   e Zd ZdZdS )zTogether.ConfigZforbidN)__name__
__module____qualname__extra r   r   E/tmp/pip-unpacked-wheel-9gdii04g/langchain_community/llms/together.pyConfigE   s   r!   T)pre)valuesreturnc                 C   s   t t|dd|d< |S )z,Validate that api key exists in environment.r   ZTOGETHER_API_KEYr   )clsr#   r   r   r    validate_environmentH   s    
zTogether.validate_environment)r$   c                 C   s   dS )zReturn type of model.Ztogetherr   selfr   r   r    	_llm_typeP   s    zTogether._llm_type)outputr$   c                 C   s   |d d d d S )Nr*   choicesr   textr   )r(   r*   r   r   r    _format_outputU   s    zTogether._format_outputc                  C   s   ddl m}  d|  S )Nr   __version__z
langchain/)Zlangchain_communityr/   r.   r   r   r    get_user_agentX   s    zTogether.get_user_agentc                 C   s   | j | j| j| j| j| jdS )Nr   r   r   r   r   r   r1   r'   r   r   r    default_params^   s    zTogether.default_params)promptstoprun_managerkwargsr$   c                 K   s  d| j   dd}|r.t|dkr.|d n|}| j||d|}dd | D }t|d	}|j| j|d
}	|	jdkrt	d|	j n>|	jdkrt
d|	j n"|	jdkrt	d|	j d|	j |	 }
|
ddkr|
dd}t	|| |
}|S )zCall out to Together's text generation endpoint.

        Args:
            prompt: The prompt to pass into the model.

        Returns:
            The string generated by the model..
        Bearer application/jsonAuthorizationzContent-Type   r   r3   r4   c                 S   s   i | ]\}}|d k	r||qS Nr   .0kvr   r   r    
<dictcomp>   s       z"Together._call.<locals>.<dictcomp>)headers)urldata  Together Server: Error   &Together received an invalid payload:    5Together returned an unexpected response with status : statusfinishederrorUndefined Error)r   get_secret_valuelenr2   itemsr   postr   status_code	Exception
ValueErrorr,   jsongetr-   )r(   r3   r4   r5   r6   rC   stop_to_usepayloadrequestresponserE   err_msgr*   r   r   r    _calli   s6    




zTogether._callc                    s`  d| j   dd}|r.t|dkr.|d n|}| j||d|}dd | D }t 4 I d	H }|j| j||d
4 I d	H }	|	jdkrt	d|	j n>|	jdkrt
d|	j n"|	jdkrt	d|	j d|	j |	 I d	H }
|
ddkr|
dd}t	|| |
}|W  5 Q I d	H R  W  5 Q I d	H R  S Q I d	H R X W 5 Q I d	H R X d	S )zCall Together model to get predictions based on the prompt.

        Args:
            prompt: The prompt to pass into the model.

        Returns:
            The string generated by the model.
        r7   r8   r9   r;   r   r<   c                 S   s   i | ]\}}|d k	r||qS r=   r   r>   r   r   r    rB      s       z#Together._acall.<locals>.<dictcomp>N)rX   rC   rF   rG   rH   rI   rJ   rK   rL   rM   rN   rO   rP   )r   rQ   rR   r2   rS   r   rT   r   rM   rV   rW   r,   rX   rY   r-   )r(   r3   r4   r5   r6   rC   rZ   r[   sessionr]   Zresponse_jsonr^   r*   r   r   r    _acall   sD      




zTogether._acall)NN)NN)"r   r   r   __doc__r   str__annotations__r   r   r   floatr   r   intr   r   r   r!   r   r   r&   propertyr)   dictr-   staticmethodr0   r   r2   r   r	   r_   r   ra   r   r   r   r    r      sL   
	  
7  
r   )rb   loggingtypingr   r   r   r   Zaiohttpr   Zlangchain_core._api.deprecationr   Zlangchain_core.callbacksr   r	   Z#langchain_core.language_models.llmsr
   Zlangchain_core.pydantic_v1r   r   Zlangchain_core.utilsr   r   Z&langchain_community.utilities.requestsr   	getLoggerr   loggerr   r   r   r   r    <module>   s    
  